Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
basic_usage.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, ๐Ÿ€โ˜€๐ŸŒ•๐ŸŒฅ ๐ŸŒŠ
3// See the LICENSE file in the project root for full license information.
4
20#include <kcenon/common/interfaces/logger_interface.h>
21namespace ci = kcenon::common::interfaces;
22using log_level_type = ci::log_level;
23#include <thread>
24#include <vector>
25#include <iostream>
26#include <chrono>
27
28using namespace kcenon::logger;
30
32 std::cout << "\n=== Basic Logging Example ===\n" << std::endl;
33
34 // Create logger instance
35 auto logger_instance = std::make_shared<logger>();
36
37 // Add console writer
38 logger_instance->add_writer(std::make_unique<console_writer>());
39
40 // Start logger (for async mode)
41 logger_instance->start();
42
43 // Log messages at different levels
44 logger_instance->log(log_level_type::trace, std::string("This is a trace message"));
45 logger_instance->log(log_level_type::debug, std::string("Debug information here"));
46 logger_instance->log(log_level_type::info, std::string("Application started successfully"));
47 logger_instance->log(log_level_type::warning, std::string("This is a warning"));
48 logger_instance->log(log_level_type::error, std::string("An error occurred!"));
49 logger_instance->log(log_level_type::critical, std::string("Critical system failure!"));
50
51 // Log with simple message (source_location auto-captured internally)
52 logger_instance->log(log_level_type::info, std::string("Message with auto-captured location"));
53
54 // Stop and flush
55 logger_instance->stop();
56}
57
59 std::cout << "\n=== Multithreaded Logging Example ===\n" << std::endl;
60
61 // Create async logger
62 auto logger_instance = std::make_shared<logger>(true, 16384); // Larger buffer for MT
63 logger_instance->add_writer(std::make_unique<console_writer>());
64 logger_instance->start();
65
66 // Launch multiple threads
67 std::vector<std::thread> threads;
68
69 for (int i = 0; i < 4; ++i) {
70 threads.emplace_back([logger_instance, i]() {
71 for (int j = 0; j < 10; ++j) {
72 logger_instance->log(log_level_type::info,
73 "Thread " + std::to_string(i) + " - Message " + std::to_string(j));
74 std::this_thread::sleep_for(std::chrono::milliseconds(10));
75 }
76 });
77 }
78
79 // Wait for all threads
80 for (auto& t : threads) {
81 t.join();
82 }
83
84 logger_instance->stop();
85}
86
88 std::cout << "\n=== Log Level Filtering Example ===\n" << std::endl;
89
90 auto logger_instance = std::make_shared<logger>();
91 logger_instance->add_writer(std::make_unique<console_writer>());
92 logger_instance->start();
93
94 // Set minimum level to INFO
95 logger_instance->set_level(log_level_type::info);
96 std::cout << "Minimum level set to INFO\n" << std::endl;
97
98 // These won't be logged
99 logger_instance->log(log_level_type::trace, std::string("This trace won't show"));
100 logger_instance->log(log_level_type::debug, std::string("This debug won't show"));
101
102 // These will be logged
103 logger_instance->log(log_level_type::info, std::string("This info will show"));
104 logger_instance->log(log_level_type::warning, std::string("This warning will show"));
105
106 logger_instance->stop();
107}
108
110 std::cout << "\n=== Sync vs Async Logging Example ===\n" << std::endl;
111
112 // Synchronous logger
113 std::cout << "Synchronous logging:" << std::endl;
114 auto sync_logger = std::make_shared<logger>(false); // sync mode
115 sync_logger->add_writer(std::make_unique<console_writer>());
116
117 auto start = std::chrono::high_resolution_clock::now();
118 for (int i = 0; i < 100; ++i) {
119 sync_logger->log(log_level_type::info, "Sync log " + std::to_string(i));
120 }
121 auto sync_time = std::chrono::high_resolution_clock::now() - start;
122
123 // Asynchronous logger
124 std::cout << "\nAsynchronous logging:" << std::endl;
125 auto async_logger = std::make_shared<logger>(true); // async mode
126 async_logger->add_writer(std::make_unique<console_writer>());
127 async_logger->start();
128
129 start = std::chrono::high_resolution_clock::now();
130 for (int i = 0; i < 100; ++i) {
131 async_logger->log(log_level_type::info, "Async log " + std::to_string(i));
132 }
133 auto async_time = std::chrono::high_resolution_clock::now() - start;
134
135 async_logger->stop();
136
137 std::cout << "\nSync time: "
138 << std::chrono::duration_cast<std::chrono::microseconds>(sync_time).count()
139 << " ยตs" << std::endl;
140 std::cout << "Async time: "
141 << std::chrono::duration_cast<std::chrono::microseconds>(async_time).count()
142 << " ยตs" << std::endl;
143}
144
145int main() {
146 try {
151
152 std::cout << "\n=== All examples completed successfully! ===" << std::endl;
153
154 } catch (const std::exception& e) {
155 std::cerr << "Error: " << e.what() << std::endl;
156 return 1;
157 }
158
159 return 0;
160}
void basic_logging_example()
ci::log_level log_level_type
void log_level_filtering_example()
void multithreaded_logging_example()
void sync_vs_async_example()
int main()
Console writer for logging to stdout/stderr.
High-performance, thread-safe logging system with asynchronous capabilities.