Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions SilKit/include/silkit/detail/impl/services/logging/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include "silkit/capi/Logger.h"

#include "silkit/services/logging/ILogger.hpp"


Expand All @@ -24,18 +23,26 @@ class Logger : public SilKit::Services::Logging::ILogger
inline ~Logger() override = default;

inline void Log(SilKit::Services::Logging::Level level, const std::string& msg) override;
inline void Log(SilKit::Services::Logging::Level level, SilKit::Services::Logging::Topic topic,
const std::string& msg) override;

inline void Trace(const std::string& msg) override;
inline void Trace(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline void Debug(const std::string& msg) override;
inline void Debug(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline void Info(const std::string& msg) override;
inline void Info(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline void Warn(const std::string& msg) override;
inline void Warn(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline void Error(const std::string& msg) override;
inline void Error(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline void Critical(const std::string& msg) override;
inline void Critical(const SilKit::Services::Logging::Topic topic, const std::string& msg) override;

inline auto GetLogLevel() const -> SilKit::Services::Logging::Level override;

Expand Down Expand Up @@ -75,10 +82,20 @@ Logger::Logger(SilKit_Vendor_Vector_SilKitRegistry* silKitRegistry)
}

void Logger::Log(SilKit::Services::Logging::Level level, const std::string& msg)
{
Log(level, SilKit::Services::Logging::Topic::None, msg);
}

void Logger::Log(SilKit::Services::Logging::Level level, SilKit::Services::Logging::Topic topic,
const std::string& msg)
{
const auto loggingLevel = static_cast<SilKit_LoggingLevel>(level);
std::string formatted;

const auto returnCode = SilKit_Logger_Log(_logger, loggingLevel, msg.c_str());
// formatted = "[" + topic + "] " + msg; // todo logger topic
formatted = msg;

const auto returnCode = SilKit_Logger_Log(_logger, loggingLevel, formatted.c_str());
ThrowOnError(returnCode);
}

Expand All @@ -88,36 +105,66 @@ void Logger::Trace(const std::string& msg)
ThrowOnError(returnCode);
}

void Logger::Trace(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Trace, topic, msg);
}

void Logger::Debug(const std::string& msg)
{
const auto returnCode = SilKit_Logger_Log(_logger, SilKit_LoggingLevel_Debug, msg.c_str());
ThrowOnError(returnCode);
}

void Logger::Debug(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Debug, topic, msg);
}

void Logger::Info(const std::string& msg)
{
const auto returnCode = SilKit_Logger_Log(_logger, SilKit_LoggingLevel_Info, msg.c_str());
ThrowOnError(returnCode);
}

void Logger::Info(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Info, topic, msg);
}

void Logger::Warn(const std::string& msg)
{
const auto returnCode = SilKit_Logger_Log(_logger, SilKit_LoggingLevel_Warn, msg.c_str());
ThrowOnError(returnCode);
}

void Logger::Warn(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Warn, topic, msg);
}

void Logger::Error(const std::string& msg)
{
const auto returnCode = SilKit_Logger_Log(_logger, SilKit_LoggingLevel_Error, msg.c_str());
ThrowOnError(returnCode);
}

void Logger::Error(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Error, topic, msg);
}

void Logger::Critical(const std::string& msg)
{
const auto returnCode = SilKit_Logger_Log(_logger, SilKit_LoggingLevel_Critical, msg.c_str());
ThrowOnError(returnCode);
}

void Logger::Critical(SilKit::Services::Logging::Topic topic, const std::string& msg)
{
Log(SilKit::Services::Logging::Level::Critical, topic, msg);
}

auto Logger::GetLogLevel() const -> SilKit::Services::Logging::Level
{
SilKit_LoggingLevel loggingLevel{SilKit_LoggingLevel_Info};
Expand Down
26 changes: 26 additions & 0 deletions SilKit/include/silkit/services/logging/ILogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,50 @@ class ILogger
*/
virtual void Log(Level level, const std::string& msg) = 0;

/*! \brief Log a message with a specified level and topic/category
*
* \param level The log level for the message
* \param topic The topic or category for the message (UTF-8).
* \param msg The message which shall be logged (UTF-8).
*/
virtual void Log(Level level, Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level trace.
virtual void Trace(const std::string& msg) = 0;

//! \brief Log a message with log level trace and a topic/category.
virtual void Trace(Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level debug.
virtual void Debug(const std::string& msg) = 0;

//! \brief Log a message with log level debug and a topic/category.
virtual void Debug(Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level info.
virtual void Info(const std::string& msg) = 0;

//! \brief Log a message with log level info and a topic/category.
virtual void Info(Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level warn.
virtual void Warn(const std::string& msg) = 0;

//! \brief Log a message with log level warn and a topic/category.
virtual void Warn(Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level error.
virtual void Error(const std::string& msg) = 0;

//! \brief Log a message with log level error and a topic/category.
virtual void Error(Topic topic, const std::string& msg) = 0;

//! \brief Log a message with log level critical.
virtual void Critical(const std::string& msg) = 0;

//! \brief Log a message with log level critical and a topic/category.
virtual void Critical(Topic topic, const std::string& msg) = 0;

//! \brief Get the lowest configured log level of all sinks
virtual Level GetLogLevel() const = 0;
};
Expand Down
25 changes: 25 additions & 0 deletions SilKit/include/silkit/services/logging/LoggingDatatypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ enum class Level : uint32_t
Off = 0xffffffff //!< Logging is disabled
};

/*! \brief Topic of a log message
*/
enum class Topic : uint32_t
{
None = 0, //!< Log message without topic
User = 1, //!< Log message of the SIL Kit user
LifeCycle = 2, //!< Log message of the lifecycle
SystemState = 3, //!< Log message of the system state
MessageTracing = 4, //!< Log message of the message tracing
ServiceDiscovery = 5, //!< Log message of the service discovery
Asio = 6, //!< Log message of the asio
TimeSync = 7, //!< Log message of the time sync service
Participant = 8, //!< Log message of the participant
TimeConfig = 9,
RequestReply = 10,
SystemMonitor = 11,
Controller = 12,
Tracing = 13,
Metrics = 14,
Dashboard = 15,
NetSim = 16,
Invalid = 0xffffffff //!< Invalid log message topic
};


} // namespace Logging
} // namespace Services
} // namespace SilKit
120 changes: 119 additions & 1 deletion SilKit/include/silkit/services/logging/string_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ inline std::string to_string(const Level& level);
inline Level from_string(const std::string& levelStr);
inline std::ostream& operator<<(std::ostream& out, const Level& level);

// ================================================================================
inline std::string to_string(const Topic& topic);
inline Topic from_topic_string(const std::string& topicStr);
inline std::ostream& operator<<(std::ostream& out, const Topic& topic);
//inline bool operator== (const Topic& lhs, const Topic& rhs);
// ================================================================================
// Inline Implementations
// ================================================================================

Expand Down Expand Up @@ -84,6 +88,120 @@ inline Level from_string(const std::string& levelStr)
return Level::Off;
}


std::string to_string(const Topic& topic)
{
std::stringstream outStream;
outStream << topic;
return outStream.str();
}

std::ostream& operator<<(std::ostream& outStream, const Topic& topic)
{
switch (topic)
{
case Topic::User:
outStream << "user";
break;
case Topic::TimeSync:
outStream << "timesync";
break;
case Topic::LifeCycle:
outStream << "lifecycle";
break;
case Topic::SystemState:
outStream << "systemstate";
break;
case Topic::MessageTracing:
outStream << "messagetracing";
break;
case Topic::ServiceDiscovery:
outStream << "servicediscovery";
break;
case Topic::Participant:
outStream << "participant";
break;
case Topic::Asio:
outStream << "asio";
break;
case Topic::TimeConfig:
outStream << "timeconfig";
break;
case Topic::RequestReply:
outStream << "requestreply";
break;
case Topic::SystemMonitor:
outStream << "systemmonitor";
break;
case Topic::Controller:
outStream << "controller";
break;
case Topic::Tracing:
outStream << "tracing";
break;
case Topic::Metrics:
outStream << "metrics";
break;
case Topic::Dashboard:
outStream << "dashboard";
break;
case Topic::NetSim:
outStream << "netsim";
break;
case Topic::None:
outStream << "none";
break;
default:
outStream << "invalid";
}
return outStream;
}

inline Topic from_topic_string(const std::string& topicStr)
{
auto topic = SilKit::Util::LowerCase(topicStr);
if (topic == "none")
return Topic::None;
if (topic == "user")
return Topic::User;
if (topic == "timesync")
return Topic::TimeSync;
if (topic == "lifecycle")
return Topic::LifeCycle;
if (topic == "systemstate")
return Topic::SystemState;
if (topic == "messagetracing")
return Topic::MessageTracing;
if (topic == "servicediscovery")
return Topic::ServiceDiscovery;
if (topic == "participant")
return Topic::Participant;
if (topic == "timeconfig")
return Topic::TimeConfig;
if (topic == "asio")
return Topic::Asio;
if (topic == "requestreply")
return Topic::RequestReply;
if (topic == "systemmonitor")
return Topic::SystemMonitor;
if (topic == "controller")
return Topic::Controller;
if (topic == "tracing")
return Topic::Tracing;
if (topic == "metrics")
return Topic::Metrics;
if (topic == "netsim")
return Topic::NetSim;
if (topic == "dashboard")
return Topic::Dashboard;
return Topic::Invalid;
}
/*
inline bool operator==(const Topic& lhs, const Topic& rhs)
{
return static_cast<int>(lhs) == static_cast<int>(rhs);
}*/

} // namespace Logging
} // namespace Services
} // namespace SilKit
4 changes: 3 additions & 1 deletion SilKit/source/capi/CapiLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ try

auto logger = reinterpret_cast<SilKit::Services::Logging::ILogger*>(self);
auto enumLevel = static_cast<SilKit::Services::Logging::Level>(level);
auto topic = SilKit::Services::Logging::Topic::User;

std::string useString{message}; //ensure we do not trigger the FMT template overload for const char*
logger->Log(enumLevel, useString);
logger->Log(enumLevel, topic, useString);
return SilKit_ReturnCode_SUCCESS;
}
CAPI_CATCH_EXCEPTIONS
Expand Down
7 changes: 7 additions & 0 deletions SilKit/source/capi/Test_CapiLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ class MockLogger : public SilKit::Services::Logging::ILogger
{
public:
MOCK_METHOD2(Log, void(Level, const std::string&));
MOCK_METHOD(void, Log, (Level, Topic, const std::string&), ());
MOCK_METHOD1(Trace, void(const std::string&));
MOCK_METHOD(void, Trace, (Topic, const std::string&), ());
MOCK_METHOD1(Debug, void(const std::string&));
MOCK_METHOD(void, Debug, (Topic, const std::string&), ());
MOCK_METHOD1(Info, void(const std::string&));
MOCK_METHOD(void, Info, (Topic, const std::string&), ());
MOCK_METHOD1(Warn, void(const std::string&));
MOCK_METHOD(void, Warn, (Topic, const std::string&), ());
MOCK_METHOD1(Error, void(const std::string&));
MOCK_METHOD(void, Error, (Topic, const std::string&), ());
MOCK_METHOD1(Critical, void(const std::string&));
MOCK_METHOD(void, Critical, (Topic, const std::string&), ());

MOCK_CONST_METHOD0(GetLogLevel, Level());
};
Expand Down
4 changes: 3 additions & 1 deletion SilKit/source/config/Configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ struct Sink
Type type{Type::Remote};
Services::Logging::Level level{Services::Logging::Level::Info};
std::string logName;
std::vector<Services::Logging::Topic> disabledTopics{};
std::vector<Services::Logging::Topic> enabledTopics{};
};

//! \brief Logger service
//! \brief Logger serviceoo
struct Logging
{
bool logFromRemotes{false};
Expand Down
Loading