Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
basic_usage.cpp File Reference

Basic logger usage examples covering core logging functionality. More...

#include <kcenon/logger/core/logger.h>
#include <kcenon/logger/writers/console_writer.h>
#include <kcenon/common/interfaces/logger_interface.h>
#include <thread>
#include <vector>
#include <iostream>
#include <chrono>
Include dependency graph for basic_usage.cpp:

Go to the source code of this file.

Typedefs

using log_level_type = ci::log_level
 

Functions

void basic_logging_example ()
 
void multithreaded_logging_example ()
 
void log_level_filtering_example ()
 
void sync_vs_async_example ()
 
int main ()
 

Detailed Description

Basic logger usage examples covering core logging functionality.

Definition in file basic_usage.cpp.

Typedef Documentation

◆ log_level_type

using log_level_type = ci::log_level

Definition at line 22 of file basic_usage.cpp.

Function Documentation

◆ basic_logging_example()

void basic_logging_example ( )
Examples
basic_usage.cpp.

Definition at line 31 of file basic_usage.cpp.

31 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ log_level_filtering_example()

void log_level_filtering_example ( )
Examples
basic_usage.cpp.

Definition at line 87 of file basic_usage.cpp.

87 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 145 of file basic_usage.cpp.

145 {
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()
void log_level_filtering_example()
void multithreaded_logging_example()
void sync_vs_async_example()

References basic_logging_example(), log_level_filtering_example(), multithreaded_logging_example(), and sync_vs_async_example().

Here is the call graph for this function:

◆ multithreaded_logging_example()

void multithreaded_logging_example ( )
Examples
basic_usage.cpp.

Definition at line 58 of file basic_usage.cpp.

58 {
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}

Referenced by main().

Here is the caller graph for this function:

◆ sync_vs_async_example()

void sync_vs_async_example ( )
Examples
basic_usage.cpp.

Definition at line 109 of file basic_usage.cpp.

109 {
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}

Referenced by main().

Here is the caller graph for this function: