#include "test_log.h" #include "log.h" #include "test_utils.h" #include #include #include #include // Helper to redirect stderr temporarily static int stderr_pipe[2]; static void capture_stderr_start() { fflush(stderr); EXPECT_EQ_INT(pipe(stderr_pipe), 0); EXPECT_EQ_INT(dup2(stderr_pipe[1], STDERR_FILENO), STDERR_FILENO); close(stderr_pipe[1]); } static void capture_stderr_end() { fflush(stderr); close(stderr_pipe[0]); } static void test_log_message_debug() { set_log_level(LOG_LEVEL_DEBUG); // Should output at DEBUG level log_message(LOG_LEVEL_DEBUG, "Debug message test: %d", 42); log_message(LOG_LEVEL_INFO, "Info message test"); log_message(LOG_LEVEL_WARNING, "Warning message test"); log_message(LOG_LEVEL_ERROR, "Error message test"); // If we get here without crashing, the test passes EXPECT_TRUE(true); } static void test_log_message_level_filtering() { set_log_level(LOG_LEVEL_WARNING); capture_stderr_start(); log_message(LOG_LEVEL_DEBUG, "Should NOT appear"); log_message(LOG_LEVEL_INFO, "Should NOT appear"); log_message(LOG_LEVEL_WARNING, "Should appear"); log_message(LOG_LEVEL_ERROR, "Should appear"); capture_stderr_end(); // We can't easily check the content, but we verified it doesn't crash EXPECT_TRUE(true); } static void test_log_message_error() { set_log_level(LOG_LEVEL_ERROR); log_message(LOG_LEVEL_ERROR, "Error only: %s", "critical"); EXPECT_TRUE(true); } static void test_set_log_level() { set_log_level(LOG_LEVEL_DEBUG); log_message(LOG_LEVEL_DEBUG, "debug visible"); set_log_level(LOG_LEVEL_WARNING); log_message(LOG_LEVEL_DEBUG, "debug hidden (no crash)"); log_message(LOG_LEVEL_WARNING, "warning visible"); set_log_level(LOG_LEVEL_INFO); log_message(LOG_LEVEL_INFO, "info visible"); EXPECT_TRUE(true); } static void test_log_message_various_args() { set_log_level(LOG_LEVEL_DEBUG); log_message(LOG_LEVEL_INFO, "String: %s, Int: %d, Hex: %x", "test", 123, 0xFF); log_message(LOG_LEVEL_WARNING, "Warning with number %d", 42); log_message(LOG_LEVEL_DEBUG, "Debug with pointer %p", (void*)0x1234); EXPECT_TRUE(true); } void test_log() { test_log_message_debug(); test_log_message_level_filtering(); test_log_message_error(); test_set_log_level(); test_log_message_various_args(); }