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

This example demonstrates logger metrics and diagnostics:

// BSD 3-Clause License
// Copyright (c) 2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <kcenon/common/interfaces/logger_interface.h>
#include <thread>
#include <iostream>
#include <random>
using namespace kcenon::logger;
namespace ci = kcenon::common::interfaces;
void generate_logs(logger* log, int thread_id, int count) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> level_dist(0, 5);
std::uniform_int_distribution<> message_size_dist(10, 200);
// Map integer to log level using ILogger interface
static constexpr ci::log_level levels[] = {
ci::log_level::trace,
ci::log_level::debug,
ci::log_level::info,
ci::log_level::warning,
ci::log_level::error,
ci::log_level::critical
};
for (int i = 0; i < count; ++i) {
auto level = levels[level_dist(gen)];
// Generate message of random size
std::string message = "Thread " + std::to_string(thread_id) + " - Message " + std::to_string(i);
message.append(message_size_dist(gen), 'x');
log->log(level, message);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void print_metrics(const logger_metrics& metrics) {
std::cout << "\n=== Logger Performance Metrics ===" << std::endl;
std::cout << "Messages logged: " << metrics.messages_logged.load() << std::endl;
std::cout << "Messages dropped: " << metrics.messages_dropped.load() << std::endl;
std::cout << "Total log time: " << metrics.total_log_time_ns.load() / 1000000.0 << " ms" << std::endl;
std::cout << "Throughput: " << metrics.get_messages_per_second() << " msg/s" << std::endl;
std::cout << "Queue utilization: " << metrics.get_queue_utilization_percent() << "%" << std::endl;
std::cout << "Avg enqueue time: " << metrics.get_avg_enqueue_time_ns() << " ns" << std::endl;
std::cout << "Writer errors: " << metrics.writer_errors.load() << std::endl;
std::cout << "Flush operations: " << metrics.flush_operations.load() << std::endl;
}
// Note: Structured logging functionality has been simplified in this version
std::cout << "\n=== Testing Basic Logging ===" << std::endl;
auto base_logger = std::make_shared<logger>(false); // Sync mode for testing
base_logger->add_writer(std::make_unique<console_writer>());
base_logger->start();
// Test basic logging at different levels
base_logger->log(ci::log_level::info, std::string("User logged in - user_id: 12345, ip: 192.168.1.100"));
base_logger->log(ci::log_level::error, std::string("Database connection failed - host: db.example.com, port: 5432"));
base_logger->log(ci::log_level::debug, std::string("Retry attempt 3 of 5"));
base_logger->stop();
}
int main() {
// Create logger with metrics enabled
auto logger = std::make_unique<logger_module::logger>(true, 1024); // Small buffer to test dropping
// Add console writer
logger->add_writer(std::make_unique<console_writer>());
// Enable metrics collection
auto enable_result = logger->enable_metrics_collection(true);
if (enable_result.is_err()) {
std::cerr << "Failed to enable metrics collection" << std::endl;
}
// Start logger
logger->start();
std::cout << "Starting logger metrics demo..." << std::endl;
std::cout << "Generating logs from multiple threads..." << std::endl;
// Launch multiple threads to generate logs
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i) {
threads.emplace_back(generate_logs, logger.get(), i, 100);
}
// Wait for threads to complete
for (auto& t : threads) {
t.join();
}
// Wait a bit for processing
std::this_thread::sleep_for(std::chrono::seconds(1));
// Get and display metrics
auto metrics_result = logger->get_current_metrics();
if (metrics_result.has_value()) {
print_metrics(metrics_result.value());
} else {
std::cerr << "Failed to get metrics" << std::endl;
}
// Test basic logging functionality
// Stop logger
logger->stop();
std::cout << "\nDemo completed!" << std::endl;
return 0;
}
int main()
common::VoidResult log(common::interfaces::log_level level, const std::string &message) override
Log a message with specified level (ILogger interface)
Definition logger.cpp:378
Console writer for logging to stdout/stderr.
High-performance, thread-safe logging system with asynchronous capabilities.
Common types and enumerations for logger system.
void print_metrics(const logger_metrics &metrics)
void generate_logs(logger *log, int thread_id, int count)
void test_structured_logging()
Performance statistics for logger operations.
std::atomic< uint64_t > flush_operations
Number of flush operations.
double get_queue_utilization_percent() const
Get queue utilization percentage.
uint64_t get_avg_enqueue_time_ns() const
Get average enqueue time in nanoseconds.
std::atomic< uint64_t > total_log_time_ns
Total time spent logging (nanoseconds)
std::atomic< uint64_t > writer_errors
Number of writer errors.
double get_messages_per_second() const
Get messages per second.
std::atomic< uint64_t > messages_dropped
Messages dropped due to queue full.
std::atomic< uint64_t > messages_logged
Total messages logged.