65 std::cout <<
"[LOG] Logging task " << task_id <<
" starting" << std::endl;
67 for (
int i = 0; i < 5; ++i) {
68 std::string message =
"Task " + std::to_string(task_id) +
69 " - Log entry " + std::to_string(i);
71 logger_instance->log(ci::log_level::info, message);
74 std::this_thread::sleep_for(std::chrono::milliseconds(50));
77 std::cout <<
"[OK] Logging task " << task_id <<
" completed" << std::endl;
81 std::cout <<
"[LOG] Heavy logging task " << task_id <<
" starting" << std::endl;
83 for (
int i = 0; i < 20; ++i) {
84 std::string large_message =
"Heavy Task " + std::to_string(task_id) +
85 " - Large log entry " + std::to_string(i) +
86 " with lots of data: " + std::string(100,
'X');
88 logger_instance->log(ci::log_level::debug, large_message);
92 logger_instance->log(ci::log_level::warning,
93 "Checkpoint " + std::to_string(i) +
" for task " + std::to_string(task_id));
96 std::this_thread::sleep_for(std::chrono::milliseconds(25));
99 std::cout <<
"[OK] Heavy logging task " << task_id <<
" completed" << std::endl;
103 std::cout <<
"[WARN] Risky logging task " << task_id <<
" starting" << std::endl;
105 std::random_device rd;
106 std::mt19937 gen(rd());
107 std::uniform_int_distribution<> crash_dist(1, 10);
109 for (
int i = 0; i < 10; ++i) {
110 std::string message =
"Risky Task " + std::to_string(task_id) +
111 " - Entry " + std::to_string(i);
113 logger_instance->log(ci::log_level::info, message);
117 int outcome = crash_dist(gen);
120 std::this_thread::sleep_for(std::chrono::milliseconds(30));
121 }
else if (outcome == 8) {
123 logger_crash_safety::instance().emergency_log(
"CRITICAL",
124 "Emergency condition detected in task " + std::to_string(task_id));
126 }
else if (outcome == 9) {
137 std::cout <<
"[WARN] Risky logging task " + std::to_string(task_id) +
" finished" << std::endl;
173 std::cout <<
"=== Logger System Crash Protection Demo ===" << std::endl;
174 std::cout <<
"This demo shows comprehensive logging crash protection mechanisms\n" << std::endl;
177 system(
"mkdir -p ./logs");
180 std::cout <<
"--- Step 1: Initialize Logger Crash Protection ---" << std::endl;
182 auto& logger_safety = logger_crash_safety::instance();
183 logger_safety.initialize(logger_crash_safety_level::standard,
184 "./logs/emergency.log", 2000);
186 logger_safety.set_auto_backup(
true, 3000);
187 logger_safety.set_max_emergency_entries(500);
189 std::cout <<
"[OK] Logger crash protection initialized" << std::endl;
192 std::cout <<
"\n--- Step 2: Create Logger with Crash Protection ---" << std::endl;
194 auto main_logger = std::make_shared<logger>(
true);
195 main_logger->add_writer(std::make_unique<console_writer>());
196 main_logger->add_writer(std::make_unique<file_writer>(
"./logs/application.log"));
197 main_logger->set_level(ci::log_level::debug);
198 main_logger->start();
202 scoped_logger_crash_protection logger_protection(
"MainLogger",
206 std::cout <<
"[OK] Logger created and protected" << std::endl;
209 std::cout <<
"\n--- Step 3: Normal Logging Operations ---" << std::endl;
211 main_logger->log(ci::log_level::info, std::string(
"Logger crash protection demo started"));
212 main_logger->log(ci::log_level::debug, std::string(
"Debug information available"));
213 main_logger->log(ci::log_level::warning, std::string(
"This is a warning message"));
216 std::cout <<
"\n--- Step 4: Multi-threaded Logging Stress Test ---" << std::endl;
218 std::vector<std::thread> logging_threads;
221 for (
int i = 0; i < 3; ++i) {
226 for (
int i = 3; i < 5; ++i) {
231 for (
auto& t : logging_threads) {
234 logging_threads.clear();
236 std::cout <<
"[OK] Multi-threaded stress test completed" << std::endl;
239 std::cout <<
"\n--- Step 5: File Recovery Test ---" << std::endl;
245 if (log_file_recovery::is_corrupted(
"./logs/corrupted.log")) {
246 std::cout <<
"[DETECT] Corruption detected in test file" << std::endl;
249 if (log_file_recovery::recover_file(
"./logs/corrupted.log",
250 "./logs/recovered.log")) {
251 std::cout <<
"[OK] File recovery successful" << std::endl;
253 std::cout <<
"[FAIL] File recovery failed" << std::endl;
258 log_file_recovery::create_backup_with_checksum(
"./logs/application.log",
259 "./logs/application_checksum_backup.log");
262 if (log_file_recovery::verify_integrity(
"./logs/application_checksum_backup.log",
263 "./logs/application_checksum_backup.log.checksum")) {
264 std::cout <<
"[OK] Backup integrity verified" << std::endl;
268 std::cout <<
"\n--- Step 6: Emergency Logging Test ---" << std::endl;
270 logger_safety.emergency_log(
"INFO",
"Testing emergency logging system");
271 logger_safety.emergency_log(
"WARNING",
"Emergency logging is signal-safe");
272 logger_safety.emergency_log(
"ERROR",
"This log survives crashes");
276 std::cout <<
"\n--- Step 7: Risky Operations Test ---" << std::endl;
277 std::cout <<
"[WARN] Some operations may trigger crash protection" << std::endl;
280 for (
int i = 10; i < 15; ++i) {
285 for (
auto& t : logging_threads) {
290 std::cout <<
"\n--- Step 8: Async Logger Crash Safety ---" << std::endl;
292 async_logger_crash_safety::configure_async_safety(
"MainLogger", 2000,
true);
293 async_logger_crash_safety::set_overflow_handler(
"MainLogger",
295 std::cout <<
"[WARN] Buffer overflow: " << dropped <<
" messages dropped" << std::endl;
299 for (
int i = 0; i < 1000; ++i) {
300 main_logger->log(ci::log_level::debug,
"Burst log " + std::to_string(i));
304 logger_safety.force_flush_all();
305 logger_safety.force_backup_all();
310 std::cout <<
"\n--- Step 9: Crash Protection Statistics ---" << std::endl;
312 auto stats = logger_safety.get_stats();
313 std::cout <<
"Emergency Log Statistics:" << std::endl;
314 std::cout <<
" Total emergency logs: " << stats.total_emergency_logs << std::endl;
315 std::cout <<
" Successful flushes: " << stats.successful_flushes << std::endl;
316 std::cout <<
" Failed flushes: " << stats.failed_flushes << std::endl;
317 std::cout <<
" Backup count: " << stats.backup_count << std::endl;
319 std::cout <<
"\nApplication Statistics:" << std::endl;
320 std::cout <<
" Total logs written: " <<
logs_written.load() << std::endl;
321 std::cout <<
" Emergency logs: " <<
emergency_logs.load() << std::endl;
322 std::cout <<
" Logging active: " << (
logging_active.load() ?
"Yes" :
"No") << std::endl;
325 std::cout <<
"\n--- Step 10: Recovery Test ---" << std::endl;
327 if (logger_safety.check_and_recover()) {
328 std::cout <<
"[OK] Recovery actions were taken" << std::endl;
330 std::cout <<
"[INFO] No recovery needed" << std::endl;
334 std::cout <<
"\n--- Step 11: Graceful Shutdown ---" << std::endl;
336 main_logger->log(ci::log_level::info, std::string(
"Shutting down logger crash protection demo"));
339 std::cout <<
"\n=== Demo Completed Successfully ===" << std::endl;
340 std::cout <<
"Key features demonstrated:" << std::endl;
341 std::cout <<
"[OK] Emergency logging (signal-safe)" << std::endl;
342 std::cout <<
"[OK] Automatic log flushing on crash" << std::endl;
343 std::cout <<
"[OK] Log file corruption detection and recovery" << std::endl;
344 std::cout <<
"[OK] Backup creation with integrity verification" << std::endl;
345 std::cout <<
"[OK] Async logger crash safety" << std::endl;
346 std::cout <<
"[OK] Multi-threaded logging protection" << std::endl;
347 std::cout <<
"[OK] RAII-based crash protection registration" << std::endl;
348 std::cout <<
"[OK] Buffer overflow handling" << std::endl;
std::atomic< bool > logging_active
void simulate_file_corruption()
void simulate_logger_crash()
std::atomic< int > logs_written
void heavy_logging_task(int task_id, std::shared_ptr< logger > logger_instance)
void backup_main_logger(const std::string &backup_path)
std::atomic< int > emergency_logs
void save_emergency_state()
void on_logger_crash(const std::string &logger_name)
void simulate_disk_full()
void potentially_crashing_logging_task(int task_id, std::shared_ptr< logger > logger_instance)
void normal_logging_task(int task_id, std::shared_ptr< logger > logger_instance)