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"
2
3#include <iostream>
4#include <sstream>
5#include <log-it/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 return "";
34 }
35
36 int64_t get_int_param(const logit::LoggerParam& param) const override {
37 // Returning a default value, can be customized
38 return 0;
39 }
40
41 double get_float_param(const logit::LoggerParam& param) const override {
42 // Returning a default value, can be customized
43 return 0.0;
44 }
45
46 void wait() override {
47 // No asynchronous logging here, but method is required
48 }
49
50 ~CustomLogger() override = default;
51};
53
54int main() {
55 std::cout << "Starting logging example with custom backend..." << std::endl;
56
57 // Adding the custom logger using LOGIT_ADD_LOGGER macro
58 LOGIT_ADD_LOGGER(CustomLogger, (), logit::SimpleLogFormatter, ("%v"));
59
60 // Logging some test messages with the custom logger
61 float someFloat = 99.99f;
62 int someInt = 42;
63
64 LOGIT_INFO("Custom backend info log", someFloat, someInt);
65 LOGIT_WARN("This is a warning message from the custom logger");
66 LOGIT_ERROR("Custom logger error message occurred");
67
68 // Simulate exception logging with custom backend
69 try {
70 throw std::runtime_error("Example runtime error");
71 } catch (const std::exception& ex) {
72 LOGIT_FATAL(ex);
73 }
74
75 // Ensure all logs are flushed before exiting
76 LOGIT_WAIT();
77
78 std::cout << "Logging example with custom backend completed." << std::endl;
79 return 0;
80}
Main header file for the LogIt++ library.
#define LOGIT_WAIT()
Macro for waiting for all asynchronous loggers to finish processing.
#define LOGIT_ERROR(...)
#define LOGIT_INFO(...)
#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:13
virtual double get_float_param(const LoggerParam &param) const =0
Retrieves a floating-point parameter from the logger. Derived classes should implement this to return...
virtual int64_t get_int_param(const LoggerParam &param) const =0
Retrieves an integer parameter from the logger. Derived classes should implement this to return speci...
virtual void log(const LogRecord &, const std::string &)=0
Logs a message.
virtual std::string get_string_param(const LoggerParam &param) const =0
Retrieves a string parameter from the logger. Derived classes should implement this to return specifi...
virtual void wait()=0
Waits for all asynchronous logging operations to complete.
A simple log formatter that formats log messages based on a user-defined pattern.
std::string to_string(const LogLevel &level, const int &mode=0)
Convert LogLevel to a std::string representation.
Definition Enums.hpp:89
LoggerParam
Enumeration for different logger parameters that can be retrieved.
Definition Enums.hpp:46
Stores log metadata and content.
Definition LogRecord.hpp:15
const LogLevel log_level
Log level (severity).
Definition LogRecord.hpp:16
const int64_t timestamp_ms
Timestamp in milliseconds.
Definition LogRecord.hpp:17