Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ess-dmsc/graylog-logger
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/ConsoleInterface.cpp
  • Loading branch information
SkyToGround committed Jan 12, 2022
2 parents 768f5d2 + d7136b7 commit 2a92afe
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.7)
project("graylog-logger"
VERSION 2.0.0
VERSION 2.1.1
DESCRIPTION "A simple logging library."
LANGUAGES CXX
)
Expand Down
4 changes: 2 additions & 2 deletions console_logger/ConsoleLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ void PrintAlternatives() {
std::cout << "To prevent the application from doing this, use the -f and -a "
"flags but do not\n";
std::cout << "provide a file name or address. The level paramater is a value "
"between 0 and 7\n";
std::cout << "with 0 being \"Emergency\" and 7 indicating a debug message. "
"between 0 and 8\n";
std::cout << "with 0 being \"Emergency\" and 8 indicating a trace message. "
"The default level\n";
std::cout << "is 7 (debug). The default file name is \"messages.log\". The "
"default address\n";
Expand Down
17 changes: 16 additions & 1 deletion documentation/changes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
## Changes

### Version 2.1.1
* Fixed threading issues.

### Version 2.1.0
* Added the `Trace` severity level.
* Added code for converting `char*` to `std::string` when using `Log::FmtMsg`. This is done in order to prevent use after stack return bugs.
* Minor unit test fixes.

### Version 2.0.4
* Conan package updates.
* Build system (CI) changes.

### Version 2.0.3
* CMake fixes.

### Version 2.0.2
* Made important CMkae changes required for properly making Conan packages. (ed-alertedh)
* Made important CMake changes required for properly making Conan packages. (ed-alertedh)
* Made minor changes to the code to prevent issues when using a `using namespace std;` statement.
* Minor documentation changes.

Expand Down
1 change: 1 addition & 0 deletions include/graylog_logger/ConsoleInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Log {
class ConsoleInterface : public BaseLogHandler {
public:
explicit ConsoleInterface();
virtual ~ConsoleInterface() = default;
void addMessage(const LogMessage &Message) override;
/// \brief Waits for all messages created before the call to flush to be
/// printed and then flushes the output stream.
Expand Down
10 changes: 9 additions & 1 deletion include/graylog_logger/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include "graylog_logger/Logger.hpp"
namespace Log {

template <typename T> struct convert { using type = T; };

template <> struct convert<char const *> { using type = std::string; };

template <> struct convert<char *> { using type = std::string; };

template <typename T> using charStarToString = typename convert<T>::type;

/// \brief Submit a formatted message to the logging library.
///
/// The following fields will be added to the message by the function:
Expand All @@ -36,7 +44,7 @@ namespace Log {
/// \param[in] args The variables to be inserted into the format string.
template <typename... Args>
void FmtMsg(const Severity Level, const std::string Format, Args... args) {
Logger::Inst().fmt_log(Level, Format, args...);
Logger::Inst().fmt_log<charStarToString<Args>...>(Level, Format, args...);
}
} // namespace Log
#endif
Expand Down
1 change: 1 addition & 0 deletions include/graylog_logger/LogUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum class Severity : int {
Informational = 6,
Info = 6,
Debug = 7,
Trace = 8,
};

/// \brief Used to store multiple different types for the extra fields provided
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(Graylog_INC
../include/graylog_logger/ConsoleInterface.hpp
../include/graylog_logger/FileInterface.hpp
GraylogConnection.hpp
Semaphore.hpp
../include/graylog_logger/GraylogInterface.hpp
../include/graylog_logger/Log.hpp
../include/graylog_logger/Logger.hpp
Expand Down
6 changes: 4 additions & 2 deletions src/ConsoleInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
namespace Log {

std::string ConsoleStringCreator(const LogMessage &Message) {
std::array<std::string, 8> sevToStr = {{"EMERGENCY", "ALERT", "CRITICAL",
std::array<std::string, 9> sevToStr = {{"EMERGENCY", "ALERT", "CRITICAL",
"ERROR", "WARNING", "Notice", "Info",
"Debug"}};
"Debug", "Trace"}};
return sevToStr.at(int(Message.SeverityLevel)) + std::string(": ") +
Message.MessageString;
}

ConsoleInterface::ConsoleInterface() {
BaseLogHandler::setMessageStringCreatorFunction(ConsoleStringCreator);
}

using std::string_literals::operator""s;
void ConsoleInterface::addMessage(const LogMessage &Message) {
Executor.SendWork([=]() {
printf(
Expand Down
4 changes: 2 additions & 2 deletions src/GraylogConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ GraylogConnection::Impl::~Impl() {

GraylogConnection::Impl::Status
GraylogConnection::Impl::getConnectionStatus() const {
return ConnectionState;
return ConnectionState.load(std::memory_order_relaxed);
}

void GraylogConnection::Impl::threadFunction() { Service.run(); }

void GraylogConnection::Impl::setState(
GraylogConnection::Impl::Status NewState) {
ConnectionState = NewState;
ConnectionState.store(NewState, std::memory_order_relaxed);
}

bool GraylogConnection::Impl::flush(
Expand Down
4 changes: 2 additions & 2 deletions src/LogUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ std::string BaseLogHandler::messageToString(const LogMessage &Message) {
size_t BytesWritten =
std::strftime(static_cast<char *>(TimeBuffer.data()), TimeBufferSize,
"%F %T", std::localtime(&cTime));
std::array<std::string, 8> sevToStr = {{"EMERGENCY", "ALERT", "CRITICAL",
std::array<std::string, 9> sevToStr = {{"EMERGENCY", "ALERT", "CRITICAL",
"ERROR", "WARNING", "Notice", "Info",
"Debug"}};
"Debug", "Trace"}};
return std::string(static_cast<char *>(TimeBuffer.data()), BytesWritten) +
std::string(" (") + Message.Host + std::string(") ") +
sevToStr.at(int(Message.SeverityLevel)) + std::string(": ") +
Expand Down
15 changes: 13 additions & 2 deletions src/LoggingBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//

#include "graylog_logger/LoggingBase.hpp"
#include "Semaphore.hpp"
#include <chrono>
#include <ciso646>
#include <sys/types.h>
Expand Down Expand Up @@ -110,11 +111,21 @@ LoggingBase::LoggingBase() {
LoggingBase::~LoggingBase() { LoggingBase::removeAllHandlers(); }

void LoggingBase::addLogHandler(const LogHandler_P &Handler) {
Executor.SendWork([=]() { Handlers.push_back(Handler); });
Semaphore Check;
Executor.SendWork([=, &Check]() {
Handlers.push_back(Handler);
Check.notify();
});
Check.wait();
}

void LoggingBase::removeAllHandlers() {
Executor.SendWork([=]() { Handlers.clear(); });
Semaphore Check;
Executor.SendWork([=, &Check]() {
Handlers.clear();
Check.notify();
});
Check.wait();
}

std::vector<LogHandler_P> LoggingBase::getHandlers() { return Handlers; }
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ set(UnitTest_SRC
set(UnitTest_INC
BaseLogHandlerStandIn.hpp
LogTestServer.hpp
Semaphore.hpp)
)

add_executable(unit_tests EXCLUDE_FROM_ALL ${UnitTest_SRC} ${UnitTest_INC})
set(unit_test_libs PUBLIC
Expand Down
23 changes: 23 additions & 0 deletions unit_tests/ConsoleInterfaceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "graylog_logger/ConsoleInterface.hpp"
#include "Semaphore.hpp"
#include "graylog_logger/LoggingBase.hpp"
#include <atomic>
#include <ciso646>
#include <gtest/gtest.h>

Expand Down Expand Up @@ -52,9 +54,30 @@ TEST(ConsoleInterface, OnInitialisationQueueEmpty) {

class ConsoleInterfaceStandIn : public ConsoleInterface {
public:
void addMessage(LogMessage const &) override { GotMsg = true; }
std::atomic_bool GotMsg{false};
using ConsoleInterface::Executor;
};

class TempLoggingBase : public LoggingBase {
public:
using LoggingBase::Executor;
};

TEST(LoggingBase, AddConsoleHandlerTest) {
TempLoggingBase log;
auto standIn = std::make_shared<ConsoleInterfaceStandIn>();
ASSERT_EQ(standIn.use_count(), 1);
log.addLogHandler(standIn);
ASSERT_EQ(standIn.use_count(), 2);
ASSERT_FALSE(standIn->GotMsg);
log.log({}, "Msg");
Semaphore Signal;
log.Executor.SendWork([&]() { Signal.notify(); });
Signal.wait();
ASSERT_TRUE(standIn->GotMsg);
}

TEST(ConsoleInterface, QueueSizeOneIsNotEmpty) {
ConsoleInterfaceStandIn cInter;
Semaphore Signal1, Signal2;
Expand Down
8 changes: 4 additions & 4 deletions unit_tests/LoggingBaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ TEST(LoggingBase, ClearHandlersTest) {

TEST(LoggingBase, LogSeveritiesTest) {
LoggingBase log;
log.setMinSeverity(Severity::Debug);
log.setMinSeverity(Severity::Trace);
auto standIn = std::make_shared<BaseLogHandlerStandIn>();
log.addLogHandler(standIn);
log.flush(10s);
std::vector<Severity> testSeverities = {
Severity::Alert, Severity::Critical, Severity::Debug,
Severity::Emergency, Severity::Error, Severity::Informational,
Severity::Notice, Severity::Warning};
Severity::Notice, Severity::Warning, Severity::Trace};
for (auto sev : testSeverities) {
log.log(sev, "");
log.flush(10s);
Expand All @@ -68,13 +68,13 @@ TEST(LoggingBase, LogSeveritiesTest) {

TEST(LoggingBase, LogIntSeveritiesTest) {
LoggingBase log;
log.setMinSeverity(Severity::Debug);
log.setMinSeverity(Severity::Trace);
auto standIn = std::make_shared<BaseLogHandlerStandIn>();
log.addLogHandler(standIn);
std::vector<Severity> testSeverities = {
Severity::Alert, Severity::Critical, Severity::Debug,
Severity::Emergency, Severity::Error, Severity::Informational,
Severity::Notice, Severity::Warning};
Severity::Notice, Severity::Warning, Severity::Trace};
for (auto sev : testSeverities) {
log.log(Severity(int(sev)), "");
log.flush(10s);
Expand Down
6 changes: 3 additions & 3 deletions unit_tests/QueueLengthTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

using namespace Log;

class ConsoleInterfaceStandIn : public ConsoleInterface {
class ConsoleInterfaceStandIn2 : public ConsoleInterface {
public:
ConsoleInterfaceStandIn() : ConsoleInterface(){};
ConsoleInterfaceStandIn2() : ConsoleInterface(){};
};

class FileInterfaceStandIn : public FileInterface {
Expand Down Expand Up @@ -56,7 +56,7 @@ TEST_F(QueueLength, ConsoleInterfaceTest) {
int TestLimit{50};
testing::internal::CaptureStdout();
{
ConsoleInterfaceStandIn CLogger;
ConsoleInterfaceStandIn2 CLogger;
CLogger.setMessageStringCreatorFunction([&MsgCounter](auto Msg) {
MsgCounter++;
return "";
Expand Down

0 comments on commit 2a92afe

Please sign in to comment.