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" <- set via CMake
5
6#include <iostream>
7#include <stdexcept>
8#include <LogIt.hpp>
10
11// Example enumeration
23
24std::ostream& operator<<(std::ostream& os, COLORS c) {
25 switch (c) {
26 case COLORS::RED: os << "RED"; break;
27 case COLORS::GREEN: os << "GREEN"; break;
28 case COLORS::BLUE: os << "BLUE"; break;
29 default: os << "UNKNOWN_COLOR"; break;
30 }
31 return os;
32}
33
34namespace logit {
35
36 template<>
37 std::string enum_to_string(COLORS value) {
38 switch (value) {
42 default: return "UNKNOWN_COLOR";
43 };
44 }
45
46};
47
48// Custom error category for demonstration
49class CustomErrorCategory : public std::error_category {
50public:
51 const char* name() const noexcept override {
52 return u8"CustomErrorCategory";
53 }
54
55 std::string message(int ev) const override {
56 switch (ev) {
57 case 1: return u8"Custom error: Invalid operation";
58 case 2: return u8"Custom error: Resource not found";
59 default: return u8"Custom error: Unknown error";
60 }
61 }
62};
63
64// Singleton instance of custom error category
65const std::error_category& custom_error_category() {
66 static CustomErrorCategory instance;
67 return instance;
68}
69
70int main() {
71 std::cout << "Starting logging example..." << std::endl;
72
73 // Add three logging backends: console, default file logger, and unique file logger
79
81
83
84 bool auth_success = true;
85 std::string email = "user@example.com";
86 LOGIT_TRACE(auth_success, email);
87
88 // Log various levels of messages
89 float someFloat = 123.456f;
90 int someInt = 789;
91 COLORS color = COLORS::RED;
92
93 LOGIT_INFO(color);
94 LOGIT_FORMAT_INFO("%s", color);
95 LOGIT_INFO("This is an informational message", someFloat, someInt);
96 LOGIT_DEBUG_IF(true, "This debug message is conditionally logged.");
97 LOGIT_WARN("Warning: Something might go wrong here!");
98 LOGIT_PRINT_ERROR("An error has occurred during processing with color: ", color);
99 LOGIT_FATAL("Fatal error! Immediate attention required!");
100
101 // Demonstrating formatted logging for homogeneous variables
102 LOGIT_FORMAT_INFO("%.2f", someFloat, 654.321f); // Two float values
103 LOGIT_FORMAT_INFO("%.4d", someInt, 999); // Two int values
104 LOGIT_FORMAT_INFO("%.2f", 747.000L); // Long double value
105
106 // Stream-based logging
107 LOGIT_STREAM_INFO() << "Stream logging: float=" << someFloat << ", int=" << someInt << ", color=" << color;
108
109 // Writing to the unique file logger using the second logger (index 2)
110 LOGIT_STREAM_TRACE_TO(2) << "Logging to unique file logger with a trace message. Color: " << color;
111 LOGIT_PRINT_INFO("Unique log was written to file: ", LOGIT_GET_LAST_FILE_NAME(2));
112
113 // Wait for all logs to flush
114 LOGIT_WAIT();
115
116 // Logging exceptions
117 try {
118 // Simulate an exception
119 throw std::runtime_error("An example runtime error");
120 } catch (const std::exception& ex) {
121 // Log the exception using the logger
122 LOGIT_FATAL(ex);
123 }
124
125 // Logging std::error_code
126 std::error_code ec(1, custom_error_category());
127 LOGIT_ERROR(ec);
128 LOGIT_FORMAT_ERROR("error_code: (%s, %d)", ec);
129
130# if __cplusplus >= 201703L
131 // Logging std::filesystem::path
132 std::filesystem::path log_path = "/var/log/example.log";
133 LOGIT_INFO(log_path);
134 LOGIT_PRINT_INFO("The log file path is: ", log_path);
135 LOGIT_FORMAT_INFO("%s", log_path);
136# endif
137
138 // Logging std::chrono::duration
139 std::chrono::seconds seconds(120);
140 std::chrono::milliseconds milliseconds(500);
141 std::chrono::microseconds microseconds(123456);
142 std::chrono::minutes minutes(5);
143 std::chrono::hours hours(2);
144
145 LOGIT_PRINT_TRACE("Seconds example: ", seconds);
146 LOGIT_TRACE(milliseconds, minutes, hours);
147 LOGIT_FORMAT_TRACE("%s", microseconds);
148
149 // Logging std::chrono::time_point
150 auto now = std::chrono::system_clock::now();
151 LOGIT_PRINT_INFO("TimePoint example: ", now);
152 LOGIT_INFO(now);
153
154 // Logging void*
155 void* ptr = &now;
156 LOGIT_PRINT_TRACE("Pointer example: ", ptr);
157 LOGIT_INFO(ptr);
158
159 // Logging smart pointers
160 auto shared = std::make_shared<int>(42);
161 LOGIT_PRINT_TRACE("Shared pointer example: ", shared);
162
163# if __cplusplus >= 201402L
164 auto unique = std::make_unique<int>(84);
165# else
166 std::unique_ptr<int> unique(new int(84));
167# endif
168 LOGIT_PRINT_TRACE("Unique pointer example: ", unique);
169
170# if __cplusplus >= 201703L
171 // Logging std::variant
172 std::variant<int, std::string> variant = "Hello, variant!";
173 LOGIT_PRINT_INFO("Variant example: ", variant);
174 variant = 123;
175 LOGIT_TRACE(variant);
176
177 // Logging std::optional
178 std::optional<std::string> optional = "Hello, optional!";
179 LOGIT_PRINT_INFO("Optional example: ", optional);
180 std::optional<std::string> optional_null;
181 LOGIT_ERROR(optional_null);
182# endif
183
184 // Ensure all loggers are flushed and cleaned up before exiting
186
187 std::cout << "Logging example completed." << std::endl;
188 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
189 return 0;
190}
Main header file for the LogIt++ library.
#define LOGIT_ADD_CONSOLE_DEFAULT()
Macro for adding the default console logger.
#define LOGIT_TRACE0()
#define LOGIT_WAIT()
Macro for waiting for all asynchronous loggers to finish processing.
#define LOGIT_ERROR(...)
#define LOGIT_INFO(...)
#define LOGIT_SHUTDOWN()
Macro for shutting down logger system.
#define LOGIT_WARN(...)
#define LOGIT_FATAL(...)
#define LOGIT_ADD_FILE_LOGGER_DEFAULT()
Macro for adding the default file logger.
#define LOGIT_PRINT_TRACE(...)
#define LOGIT_SET_QUEUE_POLICY(mode)
Sets the behavior when the queue is full.
#define LOGIT_FORMAT_INFO(fmt,...)
#define LOGIT_SET_MAX_QUEUE(size)
Sets the maximum number of queued tasks.
#define LOGIT_DEBUG_IF(condition,...)
#define LOGIT_QUEUE_DROP
Queue policy for dropping tasks when the queue is full.
#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.
#define LOGIT_GET_LAST_FILE_NAME(logger_index)
Retrieves last log file name from a specific logger.
#define LOGIT_STREAM_INFO()
Definition LogMacros.hpp:66
#define LOGIT_TRACE(...)
#define LOGIT_STREAM_TRACE_TO(index)
Definition LogMacros.hpp:71
#define LOGIT_PRINT_INFO(...)
#define LOGIT_PRINT_ERROR(...)
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_ENUM_TO_STR_CASE(value)
Expands to a case statement returning the stringified enum value.
Definition LogMacros.hpp:23
The primary namespace for the LogIt++ library.
std::string enum_to_string(EnumType value)
Helper function to convert an enumeration to a string.
void test_log_depth_3()
Logs test messages to verify path shortening at depth level 3.
Test file for verifying log path shortening at depth level 3.