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>
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) {
39 LOGIT_ENUM_TO_STR_CASE(COLORS::RED)
40 LOGIT_ENUM_TO_STR_CASE(COLORS::GREEN)
41 LOGIT_ENUM_TO_STR_CASE(COLORS::BLUE)
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
74 LOGIT_ADD_CONSOLE_DEFAULT();
75 LOGIT_ADD_FILE_LOGGER_DEFAULT();
76 LOGIT_ADD_UNIQUE_FILE_LOGGER_DEFAULT_SINGLE_MODE();
77
79
80 LOGIT_TRACE0();
81
82 // Log various levels of messages
83 float someFloat = 123.456f;
84 int someInt = 789;
85 COLORS color = COLORS::RED;
86
87 LOGIT_INFO(color);
88 LOGIT_FORMAT_INFO("%s", color);
89 LOGIT_INFO("This is an informational message", someFloat, someInt);
90 LOGIT_DEBUG_IF(true, "This debug message is conditionally logged.");
91 LOGIT_WARN("Warning: Something might go wrong here!");
92 LOGIT_PRINT_ERROR("An error has occurred during processing with color: ", color);
93 LOGIT_FATAL("Fatal error! Immediate attention required!");
94
95 // Demonstrating formatted logging for homogeneous variables
96 LOGIT_FORMAT_INFO("%.2f", someFloat, 654.321f); // Two float values
97 LOGIT_FORMAT_INFO("%.4d", someInt, 999); // Two int values
98 LOGIT_FORMAT_INFO("%.2f", 747.000L); // Long double value
99
100 // Stream-based logging
101 LOGIT_STREAM_INFO() << "Stream logging: float=" << someFloat << ", int=" << someInt << ", color=" << color;
102
103 // Writing to the unique file logger using the second logger (index 2)
104 LOGIT_STREAM_TRACE_TO(2) << "Logging to unique file logger with a trace message. Color: " << color;
105 LOGIT_PRINT_INFO("Unique log was written to file: ", LOGIT_GET_LAST_FILE_NAME(2));
106
107 // Wait for all logs to flush
108 LOGIT_WAIT();
109
110 // Logging exceptions
111 try {
112 // Simulate an exception
113 throw std::runtime_error("An example runtime error");
114 } catch (const std::exception& ex) {
115 // Log the exception using the logger
116 LOGIT_FATAL(ex);
117 }
118
119 // Logging std::error_code
120 std::error_code ec(1, custom_error_category());
121 LOGIT_ERROR(ec);
122 LOGIT_FORMAT_ERROR("error_code: (%s, %d)", ec);
123
124# if __cplusplus >= 201703L
125 // Logging std::filesystem::path
126 std::filesystem::path log_path = "/var/log/example.log";
127 LOGIT_INFO(log_path);
128 LOGIT_PRINT_INFO("The log file path is: ", log_path);
129 LOGIT_FORMAT_INFO("%s", log_path);
130# endif
131
132 // Logging std::chrono::duration
133 std::chrono::seconds seconds(120);
134 std::chrono::milliseconds milliseconds(500);
135 std::chrono::microseconds microseconds(123456);
136 std::chrono::minutes minutes(5);
137 std::chrono::hours hours(2);
138
139 LOGIT_PRINT_TRACE("Seconds example: ", seconds);
140 LOGIT_TRACE(milliseconds, minutes, hours);
141 LOGIT_FORMAT_TRACE("%s", microseconds);
142
143 // Logging std::chrono::time_point
144 auto now = std::chrono::system_clock::now();
145 LOGIT_PRINT_INFO("TimePoint example: ", now);
146 LOGIT_INFO(now);
147
148 // Logging void*
149 void* ptr = &now;
150 LOGIT_PRINT_TRACE("Pointer example: ", ptr);
151 LOGIT_INFO(ptr);
152
153 // Logging smart pointers
154 auto shared = std::make_shared<int>(42);
155 LOGIT_PRINT_TRACE("Shared pointer example: ", shared);
156
157# if __cplusplus >= 201402L
158 auto unique = std::make_unique<int>(84);
159# else
160 std::unique_ptr<int> unique(new int(84));
161# endif
162 LOGIT_PRINT_TRACE("Unique pointer example: ", unique);
163
164# if __cplusplus >= 201703L
165 // Logging std::variant
166 std::variant<int, std::string> variant = "Hello, variant!";
167 LOGIT_PRINT_INFO("Variant example: ", variant);
168 variant = 123;
169 LOGIT_TRACE(variant);
170
171 // Logging std::optional
172 std::optional<std::string> optional = "Hello, optional!";
173 LOGIT_PRINT_INFO("Optional example: ", optional);
174 std::optional<std::string> optional_null;
175 LOGIT_ERROR(optional_null);
176# endif
177
178 // Ensure all loggers are flushed and cleaned up before exiting
179 LOGIT_SHUTDOWN();
180
181 std::cout << "Logging example completed." << std::endl;
182 return 0;
183}
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()
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.