{"id":18426686,"url":"https://github.com/Baro-00/C_LibLogs","last_synced_at":"2025-10-31T04:30:54.592Z","repository":{"id":162965661,"uuid":"624024094","full_name":"Baro-00/C_LibLogs","owner":"Baro-00","description":"C logging library","archived":false,"fork":false,"pushed_at":"2025-02-18T08:35:27.000Z","size":75,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T04:42:49.787Z","etag":null,"topics":["c","c-library","logging-library"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Baro-00.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2023-04-05T15:26:27.000Z","updated_at":"2025-02-02T22:39:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"4a4fb9a2-9335-494f-a82d-b47681daecfe","html_url":"https://github.com/Baro-00/C_LibLogs","commit_stats":null,"previous_names":["baro-00/c_liblogs"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Baro-00/C_LibLogs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baro-00%2FC_LibLogs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baro-00%2FC_LibLogs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baro-00%2FC_LibLogs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baro-00%2FC_LibLogs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Baro-00","download_url":"https://codeload.github.com/Baro-00/C_LibLogs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Baro-00%2FC_LibLogs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281930854,"owners_count":26585787,"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","status":"online","status_checked_at":"2025-10-31T02:00:07.401Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["c","c-library","logging-library"],"created_at":"2024-11-06T05:08:33.786Z","updated_at":"2025-10-31T04:30:54.286Z","avatar_url":"https://github.com/Baro-00.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ***C LibLogs***\r\n\r\n## **Content:**\r\n\r\n1. [Overview](#overview)\r\n2. [Installation](#installation)\r\n3. [Usage](#usage)\r\n4. [Example](#example)\r\n5. [License](#license)\r\n\r\n## **Overview**\r\n\r\n### **Description**\r\n\r\nSimple logging library developed in C, providing structured log messages with timestamps, log levels, process IDs, and formatted messages. The library supports multi-threaded applications by implementing semaphore-based synchronization.\r\n\r\n*Example output:*\r\n\r\n``` text\r\n[08-04-2023 15:14:22] | [ INFO  ] | [App (19683)] | Initializing...\r\n[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19685)] | P1 -- Init...\r\n[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19684)] | P0 -- Init...\r\n[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19686)] | P2 -- Init...\r\n[08-04-2023 15:14:22] | [ TRACE ] | [App (19683)] | Waiting...\r\n[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19686)] | P2 -- End of work.\r\n[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19685)] | P1 -- End of work.\r\n[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19684)] | P0 -- End of work.\r\n[08-04-2023 15:14:25] | [ INFO  ] | [App (19683)] | Done.\r\n```\r\n\r\n*Format:*\r\n\r\n``` text\r\n[datetime] | [level] | [owner(PID)] | message\r\n```\r\n\r\n### **Interface**\r\n\r\n``` c\r\n/* --- LOG WRITERS */\r\nvoid logs_log(log_level_t level, const char* owner, const char* fmt, ...);\r\nvoid logs_log_trace(const char* owner, const char* fmt, ...);\r\nvoid logs_log_debug(const char* owner, const char* fmt, ...);\r\nvoid logs_log_info(const char* owner, const char* fmt, ...);\r\nvoid logs_log_warn(const char* owner, const char* fmt, ...);\r\nvoid logs_log_error(const char* owner, const char* fmt, ...);\r\nvoid logs_log_fatal(const char* owner, const char* fmt, ...);\r\n```\r\n\r\n### **Log Levels**\r\n\r\n``` c\r\nenum __log_level_t{\r\n    LOG_LEVEL_TRACE,    // --\u003e 0\r\n    LOG_LEVEL_DEBUG,    // --\u003e 1\r\n    LOG_LEVEL_INFO,     // --\u003e 2\r\n    LOG_LEVEL_WARNING,  // --\u003e 3\r\n    LOG_LEVEL_ERROR,    // --\u003e 4\r\n    LOG_LEVEL_FATAL};   // --\u003e 5\r\n\r\ntypedef enum __log_level_t log_level_t;\r\n```\r\n\r\n### **Other functions**\r\n\r\n``` c\r\n/* --- OPTIONS */\r\nint logs_threads_safety_enable(const char* sem_name);\r\nint logs_threads_safety_disable(const char* sem_name);\r\n\r\nvoid logs_set_minimal_log_level(log_level_t level);\r\n\r\nvoid logs_set_output_stream(FILE* stream);\r\nvoid logs_set_output_stream_default();\r\n```\r\n\r\n#### **Threads safety mode**\r\n\r\nWhen using the library in multi-threaded or multi-process applications, it is necessary to synchronize access to shared logging resources. This can be done by enabling thread safety mode with a named semaphore:\r\n\r\n``` c\r\n// Enable thread safety mode using a named semaphore\r\nlogs_threads_safety_enable(\"/semTest\");\r\n```\r\n\r\nAfter enabling thread safety, all log operations will be synchronized, ensuring that multiple threads or processes do not write logs simultaneously in a conflicting manner.\r\n\r\nOnce logging is no longer needed, disable thread safety to release system resources:\r\n\r\n``` c\r\n// Disable thread safety mode\r\nlogs_threads_safety_disable(\"/semTest\");\r\n```\r\n\r\nFailing to disable thread safety after usage may lead to unnecessary resource consumption or locking issues.\r\n\r\n#### **Set minimal log level**\r\n\r\nYou can control the verbosity of logs using `logs_set_minimal_log_level()`. For example:\r\n\r\n``` c\r\n// Only log messages with level INFO and above\r\nlogs_set_minimal_log_level(LOG_LEVEL_INFO);\r\n```\r\n\r\n#### **Logging to a File**\r\n\r\nBy default, logs are written to `stderr`. You can redirect logs to a file:\r\n\r\n``` c\r\nFILE* logfile = fopen(\"logs.txt\", \"w\");\r\nif (logfile) {\r\n    logs_set_output_stream(logfile);\r\n}\r\n\r\n// -- Program code begin --\r\n// ...\r\n// -- Program code end --\r\n\r\nif (logfile) {\r\n    fclose(logfile);\r\n}\r\n```\r\n\r\n---\r\n\r\n## **Installation**\r\n\r\n### **Linux Installation**\r\n\r\n#### **Using CMake**\r\n\r\n1. Install CMake Build System Tools with:\r\n\r\n``` console\r\nsudo apt install -y cmake\r\n```\r\n\r\n2. Open a terminal in your project directory and run:\r\n\r\n``` console\r\nmkdir build \u0026\u0026 cd build\r\ncmake ..\r\nmake\r\n```\r\n\r\n***Additionally***, if you want the library to be available **globally** (not only in an actual project), you can eventually execute the following command:\r\n\r\n``` console\r\nsudo make install\r\n```\r\n\r\nIn this way, the library will be installed in `/usr/local/lib` and `/usr/local/include/`. It can be necessary to update `LD_LIBRARY_PATH` with:\r\n\r\n``` sh\r\nexport LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH\r\n```\r\n\r\n***Alternatively***, include the library in your CMake project with:\r\n\r\n``` cmake\r\nadd_subdirectory(c-liblogs)\r\ntarget_link_libraries(your_project PRIVATE c-liblogs)\r\n```\r\n\r\n#### **Manual Installation**\r\n\r\n1. Clone repository:\r\n\r\n``` console\r\ngit clone https://github.com/Baro-coder/C_LibLogs\r\ncd C_LibLogs\r\n```\r\n\r\n2. Build:\r\n\r\n``` console\r\ngcc -c ./src/logs.c -o ./src/log.o -Iinclude\r\nar crs ./src/liblogs.a ./src/log.o\r\n```\r\n\r\n3. Install:\r\n\r\n``` console\r\nsudo cp ./include/logs.h /usr/local/include/\r\nsudo cp ./src/liblogs.a /usr/local/lib/\r\n```\r\n\r\nTo compile your program using the library, use:\r\n\r\n``` console\r\ngcc your_program.c -o your_program -llogs\r\n```\r\n\r\n### **Windows Installation**\r\n\r\n#### **Using CMake**\r\n\r\n1. Install [CMake](https://cmake.org/download/) and [MinGW-w64](https://www.mingw-w64.org/) if not already installed.\r\n\r\n2. Open a terminal in the project directory and run:\r\n\r\n``` console\r\nmkdir build \u0026\u0026 cd build\r\ncmake .. -G \"MinGW Makefiles\"\r\nmingw32-make\r\n```\r\n\r\n***Alternatively***, include the library in your CMake project with:\r\n\r\n``` cmake\r\nadd_subdirectory(c-liblogs)\r\ntarget_link_libraries(your_project PRIVATE c-liblogs)\r\n```\r\n\r\n#### **Manual Installation**\r\n\r\n1. Clone repository:\r\n\r\n``` console\r\ngit clone https://github.com/Baro-coder/C_LibLogs\r\ncd C_LibLogs\r\n```\r\n\r\n2. Build:\r\n\r\n``` console\r\ngcc -c ./src/logs.c -o ./src/log.o -Iinclude\r\nar crs ./src/liblogs.a ./src/log.o\r\n```\r\n\r\n3. Copy necessary files to your project directory:\r\n\r\n``` console\r\ncopy include\\logs.h C:\\your_project\\include\r\ncopy src\\liblogs.a C:\\your_project\\lib\\\r\n```\r\n\r\n***Alternatively***, if you want the library to be available **globally** (not only in an actual project), you can copy the files to MinGW resources with:\r\n\r\n``` console\r\ncopy include\\logs.h C:\\MinGW\\include\r\ncopy src\\liblogs.a C:\\MinGW\\lib\\\r\n```\r\n\r\nTo compile your program using the library, use:\r\n\r\n``` console\r\ngcc your_program.c -o your_program.exe -Lyour_project/lib -llogs -Iyour_project/include\r\n```\r\n\r\n---\r\n\r\n## **Usage**\r\n\r\n### **Including in project**\r\n\r\n``` c\r\n#include \u003clogs.h\u003e\r\n```\r\n\r\n### **Compiling with the library**\r\n\r\n``` terminal\r\ngcc your_program.c -o your_program -llogs\r\n```\r\n\r\n---\r\n\r\n## **Example**\r\n\r\n### **fork.c**\r\n\r\n``` c\r\n#include \u003clogs.h\u003e\r\n\r\n#ifdef _WIN32\r\n    #include \u003cwindows.h\u003e\r\n#elif defined(__linux__)\r\n    #include \u003csys/wait.h\u003e\r\n    #include \u003csys/types.h\u003e\r\n    #include \u003cpthread.h\u003e\r\n#elif defined(__APPLE__)\r\n    #include \u003cTargetConditionals.h\u003e\r\n    #if TARGET_OS_MAC\r\n        #include \u003csys/wait.h\u003e\r\n    #endif\r\n#else\r\n    #error \"Unsupported OS\"\r\n#endif\r\n\r\n#define P_COUNT 20\r\n\r\nvoid* thread_body(void* arg) {\r\n    int id = *((int*)arg);\r\n    logs_log_debug(\"Sub\", \"P%d -- Init...\", id);\r\n    logs_log(LOG_LEVEL_DEBUG, \"Sub\", \"P%d -- End of work.\", id);\r\n    return NULL;\r\n}\r\n\r\n\r\nint main(void) {\r\n    logs_log_info(\"App\", \"Initializing...\");\r\n    logs_threads_safety_enable(\"/semTest\");\r\n    \r\n#ifdef _WIN32\r\n    HANDLE threads[P_COUNT];\r\n#else\r\n    pthread_t threads[P_COUNT];\r\n#endif\r\n\r\n    for(int i = 0; i \u003c P_COUNT; i++) {\r\n        int* id = malloc(sizeof(int));\r\n        *id = i;\r\n\r\n#ifdef _WIN32\r\n        threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread_body, id, 0, NULL);\r\n        if (threads[i] == NULL) {\r\n            logs_log_error(\"App\", \"Thread creation failed.\");\r\n            return 1;\r\n        }\r\n#else\r\n        if(pthread_create(\u0026threads[i], NULL, thread_body, id) != 0) {\r\n            logs_log_error(\"App\", \"Thread creation failed.\");\r\n            return 1;\r\n        }\r\n#endif\r\n    }\r\n\r\n    logs_log_trace(\"App\", \"Waiting...\");\r\n\r\n    for(int i = 0; i \u003c P_COUNT; i++) {\r\n#ifdef _WIN32\r\n        WaitForSingleObject(threads[i], INFINITE);\r\n        CloseHandle(threads[i]);\r\n#else\r\n        pthread_join(threads[i], NULL);\r\n#endif\r\n    }\r\n\r\n    logs_threads_safety_disable(\"/semTest\");\r\n    logs_log_info(\"App\", \"Done.\");\r\n\r\n    return 0;\r\n}\r\n```\r\n\r\n### **Running the example**\r\n\r\n``` console\r\ngcc fork.c -o fork.exe -llogs\r\n./fork.exe\r\n```\r\n\r\n---\r\n\r\n## **License**\r\n\r\nLibrary is full **open-source software**. You can redistribute it and/or modify it under the terms of the **GNU General Public License v3**.\r\n\r\nSee [*LICENSE*](https://github.com/Baro-coder/C_LibLogs/blob/master/LICENSE) for details.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBaro-00%2FC_LibLogs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBaro-00%2FC_LibLogs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBaro-00%2FC_LibLogs/lists"}