Simple logging library developed in C.
Example output:
[08-04-2023 15:14:22] | [ INFO ] | [App (19683)] | Initializing...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19685)] | P1 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19684)] | P0 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19686)] | P2 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19689)] | P5 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19687)] | P3 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19688)] | P4 -- Init...
[08-04-2023 15:14:22] | [ DEBUG ] | [Sub (19690)] | P6 -- Init...
[08-04-2023 15:14:22] | [ TRACE ] | [App (19683)] | Waiting...
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19688)] | P4 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19687)] | P3 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19686)] | P2 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19689)] | P5 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19685)] | P1 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19690)] | P6 -- End of work.
[08-04-2023 15:14:25] | [ DEBUG ] | [Sub (19684)] | P0 -- End of work.
[08-04-2023 15:14:25] | [ INFO ] | [App (19683)] | Done.
Format:
[08-04-2023 15:14:22] | [ INFO ] | [App (19683)] | Initializing...
^datetime ^level ^owner(^PID) ^Formatted message
/* --- LOG WRITERS */
// Write `level` log with `owner` and message as formatted output
void logs_log(log_level_t level, const char* owner, const char* fmt, ...);
// Write TRACE log with `owner` and message as formatted output
void logs_log_trace(const char* owner, const char* fmt, ...);
// Write DEBUG log with `owner` and message as formatted output
void logs_log_debug(const char* owner, const char* fmt, ...);
// Write INFO log with `owner` and message as formatted output
void logs_log_info(const char* owner, const char* fmt, ...);
// Write WARNING log with `owner` and message as formatted output
void logs_log_warn(const char* owner, const char* fmt, ...);
// Write ERROR log with `owner` and message as formatted output
void logs_log_error(const char* owner, const char* fmt, ...);
// Write FATAL log with `owner` and message as formatted output
void logs_log_fatal(const char* owner, const char* fmt, ...);
// Log levels enumeration
enum __log_level_t{
LOG_LEVEL_TRACE, // --> 0
LOG_LEVEL_DEBUG, // --> 1
LOG_LEVEL_INFO, // --> 2
LOG_LEVEL_WARNING, // --> 3
LOG_LEVEL_ERROR, // --> 4
LOG_LEVEL_FATAL}; // --> 5
typedef enum __log_level_t log_level_t;
/* --- OPTIONS */
// Creating semaphore with name as `sem_name` to sync threads printing.
// `Attention`: Remember to call `logs_threads_safety_disable()` to remove created semaphore!
// Returns: `0` on success; `1` - sem_open error
int logs_threads_safety_enable(const char* sem_name);
// Removing semaphore with name as `sem_name` to sync threads printing.
// Returns: `0` on success; `1` - sem_unlink error; `2` - sem_close error
int logs_threads_safety_disable(const char* sem_name);
// Set minimal log level to `level` (default: LOG_LEVEL_TRACE)
void logs_set_minimal_log_level(log_level_t level);
// Set log output stream to `stream` (default: `stderr`)
void logs_set_output_stream(FILE* stream);
// Set log output stream to `stderr`
void logs_set_output_stream_default();
1.1. Download repository:
git clone https://github.com/Baro-coder/C_LibLogs
1.2. Change directory:
cd ./C_LibLogs/
sudo ./install.sh
1.1. Download repository:
git clone https://github.com/Baro-coder/C_LibLogs
1.2. Change directory:
cd ./C_LibLogs/
2.1. Compile source:
gcc -c ./logs/logs.c -o ./logs/logs.o
2.2. Make static library:
ar crs ./logs/liblogs.a ./logs/logs.o
3.1. Copy header file to include path:
sudo ./logs/logs.h /usr/include/
3.2. Copy library file to lib path:
sudo ./logs/liblogs.a /usr/lib/
fork.c :
#include <logs.h>
#include <sys/wait.h>
void* thread_body(int id) {
logs_log_debug("Sub", "P%d -- Init...", id);
sleep(1);
logs_log(LOG_LEVEL_DEBUG, "Sub", "P%d -- End of work.", id);
return NULL;
}
int main(void) {
int p_count = 10;
logs_log_info("App", "Initializing...");
logs_threads_safety_enable("/semTest");
for(int i = 0; i < p_count; i++) {
if(fork() == 0) {
thread_body(i);
exit(0);
}
}
logs_log_trace("App", "Waiting...");
for(int i = 0; i < p_count; i++) {
wait(NULL);
}
logs_threads_safety_disable("/semTest");
logs_log_info("App", "Done.");
return 0;
}
You have to link library in compilation process:
Example:
gcc fork.c -o fork.out -llogs
./fork.out
[08-04-2023 15:29:06] | [ INFO ] | [App (20756)] | Initializing...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20757)] | P0 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20759)] | P2 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20758)] | P1 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20760)] | P3 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20764)] | P7 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20761)] | P4 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20762)] | P5 -- Init...
[08-04-2023 15:29:06] | [ TRACE ] | [App (20756)] | Waiting...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20763)] | P6 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20765)] | P8 -- Init...
[08-04-2023 15:29:06] | [ DEBUG ] | [Sub (20766)] | P9 -- Init...
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20757)] | P0 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20759)] | P2 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20758)] | P1 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20760)] | P3 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20764)] | P7 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20761)] | P4 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20762)] | P5 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20763)] | P6 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20765)] | P8 -- End of work.
[08-04-2023 15:29:07] | [ DEBUG ] | [Sub (20766)] | P9 -- End of work.
[08-04-2023 15:29:07] | [ INFO ] | [App (20756)] | Done.
Library is full open-source software. You can redistribute it and/or modify it under the terms of the GNU General Public License v3.
See LICENSE for details.