LogIt++
Loading...
Searching...
No Matches
example_logit_basic.cpp
Go to the documentation of this file.
1
3
4#define LOGIT_BASE_PATH "E:\\_repoz\\log-it-cpp"
5
6#include <iostream>
7#include <stdexcept>
8#include <log-it/LogIt.hpp>
9
10// Example enumeration
11enum class COLORS {
12 NC = -1,
13 BLACK,
14 RED,
15 GREEN,
16 YELLOW,
17 BLUE,
18 MAGENTA,
19 CYAN,
20 WHITE,
21};
22
23std::ostream& operator<<(std::ostream& os, COLORS c) {
24 switch (c) {
25 case COLORS::RED: os << "RED"; break;
26 case COLORS::GREEN: os << "GREEN"; break;
27 case COLORS::BLUE: os << "BLUE"; break;
28 default: os << "UNKNOWN_COLOR"; break;
29 }
30 return os;
31}
32
33namespace logit {
34
35 template<>
36 std::string enum_to_string(COLORS value) {
37 switch (value) {
41 default: return "UNKNOWN_COLOR";
42 };
43 }
44
45};
46
47// Custom error category for demonstration
48class CustomErrorCategory : public std::error_category {
49public:
50 const char* name() const noexcept override {
51 return u8"CustomErrorCategory";
52 }
53
54 std::string message(int ev) const override {
55 switch (ev) {
56 case 1: return u8"Custom error: Invalid operation";
57 case 2: return u8"Custom error: Resource not found";
58 default: return u8"Custom error: Unknown error";
59 }
60 }
61};
62
63// Singleton instance of custom error category
64const std::error_category& custom_error_category() {
65 static CustomErrorCategory instance;
66 return instance;
67}
68
69int main() {
70 std::cout << "Starting logging example..." << std::endl;
71
72 // Add three logging backends: console, default file logger, and unique file logger
76
78
79 // Log various levels of messages
80 float someFloat = 123.456f;
81 int someInt = 789;
82 COLORS color = COLORS::RED;
83
84 LOGIT_INFO(color);
85 LOGIT_FORMAT_INFO("%s", color);
86 LOGIT_INFO("This is an informational message", someFloat, someInt);
87 LOGIT_DEBUG_IF(true, "This debug message is conditionally logged.");
88 LOGIT_WARN("Warning: Something might go wrong here!");
89 LOGIT_PRINT_ERROR("An error has occurred during processing with color: ", color);
90 LOGIT_FATAL("Fatal error! Immediate attention required!");
91
92 // Demonstrating formatted logging for homogeneous variables
93 LOGIT_FORMAT_INFO("%.2f", someFloat, 654.321f); // Two float values
94 LOGIT_FORMAT_INFO("%.4d", someInt, 999); // Two int values
95 LOGIT_FORMAT_INFO("%.2f", 747.000L); // Long double value
96
97 // Stream-based logging
98 LOGIT_STREAM_INFO() << "Stream logging: float=" << someFloat << ", int=" << someInt << ", color=" << color;
99
100 // Writing to the unique file logger using the second logger (index 2)
101 LOGIT_STREAM_TRACE_TO(2) << "Logging to unique file logger with a trace message. Color: " << color;
102 LOGIT_PRINT_INFO("Unique log was written to file: ", LOGIT_GET_LAST_FILE_NAME(2));
103
104 // Logging exceptions
105 try {
106 // Simulate an exception
107 throw std::runtime_error("An example runtime error");
108 } catch (const std::exception& ex) {
109 // Log the exception using the logger
110 LOGIT_FATAL(ex);
111 }
112
113 // Logging std::error_code
114 std::error_code ec(1, custom_error_category());
115 LOGIT_ERROR(ec);
116 LOGIT_FORMAT_ERROR("error_code: (%s, %d)", ec);
117
118# if __cplusplus >= 201703L
119 // Logging std::filesystem::path
120 std::filesystem::path log_path = "/var/log/example.log";
121 LOGIT_INFO(log_path);
122 LOGIT_PRINT_INFO("The log file path is: ", log_path);
123 LOGIT_FORMAT_INFO("%s", log_path);
124# endif
125
126 // Logging std::chrono::duration
127 std::chrono::seconds seconds(120);
128 std::chrono::milliseconds milliseconds(500);
129 std::chrono::microseconds microseconds(123456);
130 std::chrono::minutes minutes(5);
131 std::chrono::hours hours(2);
132
133 LOGIT_PRINT_TRACE("Seconds example: ", seconds);
134 LOGIT_TRACE(milliseconds, minutes, hours);
135 LOGIT_FORMAT_TRACE("%s", microseconds);
136
137 // Logging std::chrono::time_point
138 auto now = std::chrono::system_clock::now();
139 LOGIT_PRINT_INFO("TimePoint example: ", now);
140 LOGIT_INFO(now);
141
142 // Logging void*
143 void* ptr = &now;
144 LOGIT_PRINT_TRACE("Pointer example: ", ptr);
145 LOGIT_INFO(ptr);
146
147 // Logging smart pointers
148 auto shared = std::make_shared<int>(42);
149 LOGIT_PRINT_TRACE("Shared pointer example: ", shared);
150
151# if __cplusplus >= 201402L
152 auto unique = std::make_unique<int>(84);
153# else
154 std::unique_ptr<int> unique(new int(84));
155# endif
156 LOGIT_PRINT_TRACE("Unique pointer example: ", unique);
157
158# if __cplusplus >= 201703L
159 // Logging std::variant
160 std::variant<int, std::string> variant = "Hello, variant!";
161 LOGIT_PRINT_INFO("Variant example: ", variant);
162 variant = 123;
163 LOGIT_TRACE(variant);
164
165 // Logging std::optional
166 std::optional<std::string> optional = "Hello, optional!";
167 LOGIT_PRINT_INFO("Optional example: ", optional);
168 std::optional<std::string> optional_null;
169 LOGIT_ERROR(optional_null);
170# endif
171
172 // Ensure all loggers are flushed and cleaned up before exiting
173 LOGIT_WAIT();
174
175 std::cout << "Logging example completed." << std::endl;
176 return 0;
177}
Main header file for the LogIt++ library.
const char * name() const noexcept override
std::string message(int ev) const override
const std::error_category & custom_error_category()
std::ostream & operator<<(std::ostream &os, COLORS c)
int main()
#define LOGIT_ADD_CONSOLE_DEFAULT()
Macro for adding the default console logger. This logger uses the default format pattern and asynchro...
#define LOGIT_TRACE0()
#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_FILE_LOGGER_DEFAULT()
Macro for adding the default file logger. This logger writes logs to the default file path and delete...
#define LOGIT_PRINT_TRACE(...)
#define LOGIT_FORMAT_INFO(fmt,...)
#define LOGIT_ENUM_TO_STR_CASE(value)
Definition LogMacros.hpp:53
#define LOGIT_DEBUG_IF(condition,...)
#define LOGIT_FORMAT_ERROR(fmt,...)
#define LOGIT_FORMAT_TRACE(fmt,...)
#define LOGIT_ADD_UNIQUE_FILE_LOGGER_DEFAULT_SINGLE_MODE()
Macro for adding the default unique file logger in single_mode. This macro adds a UniqueFileLogger wi...
#define LOGIT_GET_LAST_FILE_NAME(logger_index)
Macro for retrieving the last log file name from a specific logger.
#define LOGIT_STREAM_INFO()
Definition LogMacros.hpp:72
#define LOGIT_TRACE(...)
#define LOGIT_STREAM_TRACE_TO(index)
Definition LogMacros.hpp:77
#define LOGIT_PRINT_INFO(...)
#define LOGIT_PRINT_ERROR(...)
The primary namespace for the LogIt++ library.
std::string enum_to_string(EnumType value)
Helper function to convert an enumeration to a string.