{"id":25244832,"url":"https://github.com/ziadasem/thread_safe_logger","last_synced_at":"2025-04-05T21:15:48.131Z","repository":{"id":270900868,"uuid":"911791450","full_name":"ziadasem/thread_safe_logger","owner":"ziadasem","description":"A thread-safe logger that is flexible enough to support console, file, and other logging strategies. developed in C++","archived":false,"fork":false,"pushed_at":"2025-01-03T21:42:45.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T01:41:24.496Z","etag":null,"topics":["modern-cpp","multithreading","oop","oop-cpp","strategy-pattern","threads"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ziadasem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-03T21:28:26.000Z","updated_at":"2025-01-04T12:35:03.000Z","dependencies_parsed_at":"2025-01-03T22:38:30.712Z","dependency_job_id":null,"html_url":"https://github.com/ziadasem/thread_safe_logger","commit_stats":null,"previous_names":["ziadasem/thread_safe_logger"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fthread_safe_logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fthread_safe_logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fthread_safe_logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ziadasem%2Fthread_safe_logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ziadasem","download_url":"https://codeload.github.com/ziadasem/thread_safe_logger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399887,"owners_count":20932880,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["modern-cpp","multithreading","oop","oop-cpp","strategy-pattern","threads"],"created_at":"2025-02-12T01:37:02.671Z","updated_at":"2025-04-05T21:15:48.109Z","avatar_url":"https://github.com/ziadasem.png","language":"C++","readme":"# ThreadSafeLogger\n\n\nA thread-safe logging system with support for different logging strategies, including logging to console and file. The logger ensures that log entries are handled asynchronously and safely in a multi-threaded environment.\n\n## Features\n\n- **Thread-Safety**: Ensures that log messages are processed safely across multiple threads.\n- **Logging Strategies**: Supports multiple logging strategies including:\n  - **Console Logging**: Logs messages to the console with customizable colors.\n  - **File Logging**: Logs messages to a specified file, with an optional console output.\n- **Log Levels**: Supports different log types, including:\n  - `INFO_DEBUG`, `WARNING_DEBUG`, `ERROR_DEBUG`\n  - `INFO_RELEASE`, `WARNING_RELEASE`, `ERROR_RELEASE`\n- **Singleton Pattern**: Ensures only one instance of the logger is created and shared across the application.\n\n## Files\n\n### `ThreadSafeLogger.hpp`\n\nDefines the main `ThreadSafeLogger` class responsible for managing log messages. It includes methods for logging messages, setting the log output, and managing the logging thread. It follows the singleton pattern to ensure only one instance exists.\n\n- `log`: Logs a message with a specific log type.\n- `startLoggingThread`: Starts the logging thread to process log messages.\n- `setLoggingOutput`: Configures which types of logs to display.\n\n### `LoggingStrategyBase.hpp`\n\nDefines the interface for logging strategies. Any logging strategy (such as console or file logging) must implement this interface and define the `log` method to handle logging.\n\n### `LogModel.hpp`\n\nDefines the `LogModel` class, which contains the structure of a log message, including the message content, file name, line number, and log type.\n\n### `FileLoggingStrategy.hpp`\n\nImplements the `LogginStrategyBase` interface for logging to a file. The log messages are written to a specified file, with an optional console output.\n\n- Constructor: Initializes the file logging strategy with a file name and optional console output flag.\n- Destructor: Ensures the file is properly closed when the logging strategy is no longer needed.\n\n### `ConsoleLoggingStrategy.hpp`\n\nImplements the `LogginStrategyBase` interface for logging to the console. Supports ANSI color codes to colorize log messages based on log type.\n\n- `enableANSIColors`: Enables ANSI color codes for console output (works on systems that support it).\n\n## Installation\n\n1. Clone or download the repository.\n2. Include the header files in your project.\n3. Link to any necessary libraries (if needed for console or file logging).\n\n## Usage\n\n### 1. Setting up the Logger\n\nTo use the logger, you need to create an instance of `ThreadSafeLogger`. You can specify the logging strategy (e.g., `ConsoleLoggingStrategy`, `FileLoggingStrategy`) during initialization.\n\n```cpp\n#include \"thread_safe_logger.hpp\"\n#include \"console_logging_strategy.hpp\"\n#include \"file_logging_strategy.hpp\"\n\nint main() {\n    // Example: Using Console Logging\n    auto consoleStrategy = std::make_unique\u003cConsoleLoggingStrategy\u003e();\n    ThreadSafeLogger\u0026 logger = ThreadSafeLogger::getInstance(std::move(consoleStrategy));\n\n    // Log messages\n    logger.log(\"This is an info message\", LogType::INFO_DEBUG, __FILE__, __LINE__);\n\n    // Example: Using File Logging\n    auto fileStrategy = std::make_unique\u003cFileLoggingStrategy\u003e(\"log.txt\", true);\n    ThreadSafeLogger\u0026 loggerFile = ThreadSafeLogger::getInstance(std::move(fileStrategy));\n\n    // Log messages\n    loggerFile.log(\"This is an error message\", LogType::ERROR_DEBUG, __FILE__, __LINE__);\n\n    return 0;\n}\n```\n\n### 2. Log Types\nThe logger supports different log types, defined in LogType:\n\n* INFO_DEBUG\n* WARNING_DEBUG\n* ERROR_DEBUG\n* INFO_RELEASE\n* WARNING_RELEASE\n* ERROR_RELEASE\n\nEach log type can be configured to display in the desired logging output (console or file).\n\n### 3. Customizing the Logger\nYou can customize which types of logs are displayed in both debug and release modes.\n\n```cpp\nlogger.setLoggingOutput(true, true, true, true, true, true);\n```\n\n### 4. Multi-threaded Logging\n\nThe logger is thread-safe, meaning it can be used in multi-threaded applications where different threads log messages concurrently.\n\n```cpp\n#include \"headers/thread_safe_logger.hpp\"\n#include \"headers/file_logging_strategy.hpp\"\n#include \u003cformat\u003e\n#include \u003cthread\u003e\n#include \u003cvector\u003e\n#include \u003cutility\u003e\n\nusing namespace std;\n\nvoid logSomeMessages(int id, ThreadSafeLogger\u0026 logger)\n{\n    for (int i{ 0 }; i \u003c 10; ++i) {\n        std::string message = \"Hello from thread: \" + std::to_string(id) + \" and iteration # \" + std::to_string(i);\n        logger.log(message, \n                   i % 3 == 0 ? LogType::ERROR_DEBUG :\n                   i % 2 == 0 ? LogType::WARNING_DEBUG : \n                   LogType::INFO_DEBUG, \n                   __FILE__, __LINE__);\n        this_thread::sleep_for(50ms);\n    }\n}\n\nint main()\n{\n    // Create the FileLoggingStrategy and pass it to the logger\n    std::unique_ptr\u003cFileLoggingStrategy\u003e strategy = std::make_unique\u003cFileLoggingStrategy\u003e(\"log_file.txt\");\n    auto\u0026 logger = ThreadSafeLogger::getInstance(std::move(strategy));\n\n    // Start the logging thread to process the logs\n    logger.startLoggingThread();\n\n    vector\u003cthread\u003e threads;\n    // Create a few threads all working with the same Logger instance.\n    for (int i{ 0 }; i \u003c 10; ++i) {\n        threads.emplace_back(logSomeMessages, i, ref(logger));\n    }\n\n    // Wait for all threads to finish logging\n    for (int i{ 0 }; i \u003c 10; ++i) {\n        threads[i].join();\n    }\n\n    return 0;\n}\n```\n\n\n## Acknowledgements\nThis project uses the Singleton design pattern to ensure a single logger instance.\nColor support for the console is achieved using ANSI escape sequences.\n\n\nDISCLAIMER: The README was generated by AI. It is temporary until I can write a complete documentation.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziadasem%2Fthread_safe_logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fziadasem%2Fthread_safe_logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fziadasem%2Fthread_safe_logger/lists"}