|
| 1 | +#include "Logger.hpp" |
| 2 | +#include <gtest/gtest.h> |
| 3 | +#include <fstream> |
| 4 | +#include <sstream> |
| 5 | +#include <cstdio> // For remove() |
| 6 | +#include <thread> |
| 7 | +#include <regex> |
| 8 | + |
| 9 | +// Helper to read file contents |
| 10 | +std::string readFile(const std::string& filename) { |
| 11 | + std::ifstream in(filename); |
| 12 | + std::stringstream buffer; |
| 13 | + buffer << in.rdbuf(); |
| 14 | + return buffer.str(); |
| 15 | +} |
| 16 | + |
| 17 | +// Captures std::cout and std::cerr output |
| 18 | +class OutputCapture { |
| 19 | +public: |
| 20 | + void startCapture() { |
| 21 | + oldCout = std::cout.rdbuf(coutStream.rdbuf()); |
| 22 | + oldCerr = std::cerr.rdbuf(cerrStream.rdbuf()); |
| 23 | + } |
| 24 | + |
| 25 | + void stopCapture() { |
| 26 | + std::cout.rdbuf(oldCout); |
| 27 | + std::cerr.rdbuf(oldCerr); |
| 28 | + } |
| 29 | + |
| 30 | + std::string getCapturedStdout() const { |
| 31 | + return coutStream.str(); |
| 32 | + } |
| 33 | + |
| 34 | + std::string getCapturedStderr() const { |
| 35 | + return cerrStream.str(); |
| 36 | + } |
| 37 | + |
| 38 | +private: |
| 39 | + std::ostringstream coutStream; |
| 40 | + std::ostringstream cerrStream; |
| 41 | + std::streambuf* oldCout = nullptr; |
| 42 | + std::streambuf* oldCerr = nullptr; |
| 43 | +}; |
| 44 | + |
| 45 | +TEST(LoggerTest, LogsToFileAndConsole) { |
| 46 | + std::string testFile = "test_log.txt"; |
| 47 | + std::remove(testFile.c_str()); // Ensure clean start |
| 48 | + |
| 49 | + LoggingConfig cfg; |
| 50 | + cfg.filename = testFile; |
| 51 | + Logger::init(cfg); |
| 52 | + |
| 53 | + OutputCapture capture; |
| 54 | + capture.startCapture(); |
| 55 | + |
| 56 | + Logger::info("This is an info message."); |
| 57 | + Logger::error("This is an error message."); |
| 58 | + |
| 59 | + capture.stopCapture(); |
| 60 | + |
| 61 | + std::string fileContent = readFile(testFile); |
| 62 | + std::string stdoutContent = capture.getCapturedStdout(); |
| 63 | + std::string stderrContent = capture.getCapturedStderr(); |
| 64 | + |
| 65 | + std::regex infoPattern(R"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} INFO: This is an info message\.\n)"); |
| 66 | + std::regex errorPattern(R"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} ERROR: This is an error message\.\n)"); |
| 67 | + |
| 68 | + // Console output checks |
| 69 | + EXPECT_TRUE(std::regex_match(stdoutContent, infoPattern)); |
| 70 | + EXPECT_TRUE(std::regex_match(stderrContent, errorPattern)); |
| 71 | + |
| 72 | + // File output checks |
| 73 | + EXPECT_NE(fileContent.find("INFO: This is an info message."), std::string::npos); |
| 74 | + EXPECT_NE(fileContent.find("ERROR: This is an error message."), std::string::npos); |
| 75 | + |
| 76 | + std::remove(testFile.c_str()); // Cleanup |
| 77 | + Logger::destroy(); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(LoggerTest, LogsWithoutFile) { |
| 81 | + LoggingConfig cfg; |
| 82 | + cfg.filename = ""; // No file |
| 83 | + Logger::init(cfg); |
| 84 | + |
| 85 | + OutputCapture capture; |
| 86 | + capture.startCapture(); |
| 87 | + |
| 88 | + Logger::info("Console only info"); |
| 89 | + Logger::error("Console only error"); |
| 90 | + |
| 91 | + capture.stopCapture(); |
| 92 | + |
| 93 | + EXPECT_NE(capture.getCapturedStdout().find("INFO: Console only info"), std::string::npos); |
| 94 | + EXPECT_NE(capture.getCapturedStderr().find("ERROR: Console only error"), std::string::npos); |
| 95 | + |
| 96 | + Logger::destroy(); |
| 97 | +} |
| 98 | + |
| 99 | +TEST(LoggerTest, ThreadSafeLogging) { |
| 100 | + std::string testFile = "threaded_log.txt"; |
| 101 | + std::remove(testFile.c_str()); |
| 102 | + |
| 103 | + LoggingConfig cfg; |
| 104 | + cfg.filename = testFile; |
| 105 | + Logger::init(cfg); |
| 106 | + |
| 107 | + auto logTask = [](int threadId) { |
| 108 | + for (int i = 0; i < 10; ++i) { |
| 109 | + Logger::info("Thread " + std::to_string(threadId) + " message " + std::to_string(i)); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + std::thread t1(logTask, 1); |
| 114 | + std::thread t2(logTask, 2); |
| 115 | + |
| 116 | + t1.join(); |
| 117 | + t2.join(); |
| 118 | + |
| 119 | + std::string content = readFile(testFile); |
| 120 | + |
| 121 | + for (int i = 0; i < 10; ++i) { |
| 122 | + EXPECT_NE(content.find("Thread 1 message " + std::to_string(i)), std::string::npos); |
| 123 | + EXPECT_NE(content.find("Thread 2 message " + std::to_string(i)), std::string::npos); |
| 124 | + } |
| 125 | + |
| 126 | + std::remove(testFile.c_str()); |
| 127 | + Logger::destroy(); |
| 128 | +} |
0 commit comments