LogIt++
Loading...
Searching...
No Matches
example_logit_custom_backend.cpp
Go to the documentation of this file.
1// #define LOGIT_BASE_PATH "E:\\_repoz\\log-it-cpp" <- set via CMake
2
3#include <iostream>
4#include <sstream>
5#include <LogIt.hpp>
6
7
9// Custom Logger class inheriting from ILogger
10class CustomLogger : public logit::ILogger {
11public:
12
13 CustomLogger() = default;
14
18 void log(const logit::LogRecord& record, const std::string& message) override {
19 std::ostringstream oss;
20
21 // Example format: "[LEVEL] timestamp: message"
22 oss << "[" << logit::to_string(record.log_level) << "] "
23 << record.timestamp_ms << ": "
24 << message << std::endl;
25
26 // Output the log message (in this case, to the console)
27 std::cout << oss.str();
28 }
29
30 // Implementing other pure virtual methods
31 std::string get_string_param(const logit::LoggerParam& param) const override {
32 // Returning an empty string for this example, can be customized
33 (void)param;
34 return "";
35 }
36
37 int64_t get_int_param(const logit::LoggerParam& param) const override {
38 // Returning a default value, can be customized
39 (void)param;
40 return 0;
41 }
42
43 double get_float_param(const logit::LoggerParam& param) const override {
44 // Returning a default value, can be customized
45 (void)param;
46 return 0.0;
47 }
48
49 void set_log_level(logit::LogLevel level) override {
50 m_log_level = level;
51 }
52
53 logit::LogLevel get_log_level() const override {
54 return m_log_level;
55 }
56
57
58 void wait() override {
59 // No asynchronous logging here, but method is required
60 }
61
62 ~CustomLogger() override = default;
63
64private:
66};
68
69int main() {
70 std::cout << "Starting logging example with custom backend..." << std::endl;
71
72 // Adding the custom logger using LOGIT_ADD_LOGGER macro
73 LOGIT_ADD_LOGGER(CustomLogger, (), logit::SimpleLogFormatter, ("%v"));
74
75 // Logging some test messages with the custom logger
76 float someFloat = 99.99f;
77 int someInt = 42;
78
79 LOGIT_INFO("Custom backend info log", someFloat, someInt);
80 LOGIT_WARN("This is a warning message from the custom logger");
81 LOGIT_ERROR("Custom logger error message occurred");
82
83 // Simulate exception logging with custom backend
84 try {
85 throw std::runtime_error("Example runtime error");
86 } catch (const std::exception& ex) {
87 LOGIT_FATAL(ex);
88 }
89
90 // Ensure all logs are flushed before exiting
92
93 std::cout << "Logging example with custom backend completed." << std::endl;
94 return 0;
95}
Main header file for the LogIt++ library.
#define LOGIT_ERROR(...)
#define LOGIT_INFO(...)
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_WARN(...)
#define LOGIT_FATAL(...)
#define LOGIT_ADD_LOGGER(logger_type, logger_args, formatter_type, formatter_args)
Macro for adding a logger with a specific formatter.
Interface for loggers that handle log message output.
Definition ILogger.hpp:15
virtual double get_float_param(const LoggerParam &param) const =0
Retrieves a floating-point parameter from the logger.
virtual void set_log_level(LogLevel level)=0
Sets the minimal log level for this logger.
virtual int64_t get_int_param(const LoggerParam &param) const =0
Retrieves an integer parameter from the logger.
virtual void log(const LogRecord &record, const std::string &message)=0
Logs a message.
virtual std::string get_string_param(const LoggerParam &param) const =0
Retrieves a string parameter from the logger.
virtual void wait()=0
Waits for all asynchronous logging operations to complete.
virtual LogLevel get_log_level() const =0
Gets the minimal log level for this logger.
A simple log formatter that formats log messages based on a user-defined pattern.
std::string to_string(LogLevel level, int mode=0)
Convert LogLevel to a std::string representation.
Definition enums.hpp:90
LogLevel
Logging levels.
Definition enums.hpp:15
@ LOG_LVL_TRACE
Trace level logging.
Definition enums.hpp:16
LoggerParam
Enumeration for different logger parameters that can be retrieved.
Definition enums.hpp:47
const LogLevel log_level
Log level (severity).
Definition LogRecord.hpp:18
const int64_t timestamp_ms
Timestamp in milliseconds.
Definition LogRecord.hpp:19