Initial commit of source code
This commit is contained in:
70
src/dBus/api/log.h
Normal file
70
src/dBus/api/log.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "api.h"
|
||||
|
||||
#include <sdi_toolBox/dateTime/age.h>
|
||||
|
||||
namespace api::log
|
||||
{
|
||||
//--------------------------------------------------------------
|
||||
// Log levels
|
||||
//--------------------------------------------------------------
|
||||
enum class LogLevel : uint8_t
|
||||
{
|
||||
Message = 0,
|
||||
Debug,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
|
||||
System, // Reserved for system-level logs, not intended for user messages
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
|
||||
/* --- */
|
||||
|
||||
//--------------------------------------------------------------
|
||||
// Log message structure
|
||||
//--------------------------------------------------------------
|
||||
struct LogMessage : dBus::api::DefaultData<dBus::makeID("log.message")>
|
||||
{
|
||||
using Timestamp = std::chrono::steady_clock::time_point;
|
||||
|
||||
virtual ~LogMessage() = default;
|
||||
|
||||
Timestamp timestamp = std::chrono::steady_clock::now();
|
||||
LogLevel level = LogLevel::Message;
|
||||
std::string message;
|
||||
|
||||
[[nodiscard]] std::string toString() const override
|
||||
{
|
||||
const auto timestampFormatter = Age(std::chrono::duration_cast<std::chrono::nanoseconds>(timestamp.time_since_epoch()));
|
||||
const auto logEntry = std::format("{}{}",
|
||||
getLevelString(),
|
||||
message);
|
||||
return logEntry;
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] std::string getLevelString() const
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
using enum LogLevel;
|
||||
case Debug:
|
||||
return "[Debug] ";
|
||||
case Info:
|
||||
return "[Info] ";
|
||||
case Warning:
|
||||
return "[Warning] ";
|
||||
case Error:
|
||||
return "[Error] ";
|
||||
case System:
|
||||
return "[System] ";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
} // namespace api::log
|
||||
Reference in New Issue
Block a user