added -DLOG_DONT_CODE
This commit is contained in:
parent
f9ea34994b
commit
b9039f161c
4
.clang-format
Normal file
4
.clang-format
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
IndentWidth: 2
|
||||||
|
ColumnLimit: 80
|
BIN
.screenshot.png
Normal file
BIN
.screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
1
LICENSE
1
LICENSE
@ -1,4 +1,5 @@
|
|||||||
Copyright (c) 2020 rxi
|
Copyright (c) 2020 rxi
|
||||||
|
Copyright (c) 2024 iiiypuk
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
13
README.md
13
README.md
@ -1,8 +1,7 @@
|
|||||||
# log.c
|
# log.c
|
||||||
A simple logging library implemented in C99
|
A simple logging library implemented in C99
|
||||||
|
|
||||||
![screenshot](https://cloud.githubusercontent.com/assets/3920290/23831970/a2415e96-0723-11e7-9886-f8f5d2de60fe.png)
|
![Screenshot](./.screenshot.png)
|
||||||
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
**[log.c](src/log.c?raw=1)** and **[log.h](src/log.h?raw=1)** should be dropped
|
**[log.c](src/log.c?raw=1)** and **[log.h](src/log.h?raw=1)** should be dropped
|
||||||
@ -30,19 +29,16 @@ Resulting in a line with the given format printed to stderr:
|
|||||||
20:18:26 TRACE src/main.c:11: Hello world
|
20:18:26 TRACE src/main.c:11: Hello world
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### log_set_quiet(bool enable)
|
#### log_set_quiet(bool enable)
|
||||||
Quiet-mode can be enabled by passing `true` to the `log_set_quiet()` function.
|
Quiet-mode can be enabled by passing `true` to the `log_set_quiet()` function.
|
||||||
While this mode is enabled the library will not output anything to `stderr`, but
|
While this mode is enabled the library will not output anything to `stderr`, but
|
||||||
will continue to write to files and callbacks if any are set.
|
will continue to write to files and callbacks if any are set.
|
||||||
|
|
||||||
|
|
||||||
#### log_set_level(int level)
|
#### log_set_level(int level)
|
||||||
The current logging level can be set by using the `log_set_level()` function.
|
The current logging level can be set by using the `log_set_level()` function.
|
||||||
All logs below the given level will not be written to `stderr`. By default the
|
All logs below the given level will not be written to `stderr`. By default the
|
||||||
level is `LOG_TRACE`, such that nothing is ignored.
|
level is `LOG_TRACE`, such that nothing is ignored.
|
||||||
|
|
||||||
|
|
||||||
#### log_add_fp(FILE *fp, int level)
|
#### log_add_fp(FILE *fp, int level)
|
||||||
One or more file pointers where the log will be written can be provided to the
|
One or more file pointers where the log will be written can be provided to the
|
||||||
library by using the `log_add_fp()` function. The data written to the file
|
library by using the `log_add_fp()` function. The data written to the file
|
||||||
@ -55,28 +51,27 @@ output is of the following format:
|
|||||||
Any messages below the given `level` are ignored. If the library failed to add a
|
Any messages below the given `level` are ignored. If the library failed to add a
|
||||||
file pointer a value less-than-zero is returned.
|
file pointer a value less-than-zero is returned.
|
||||||
|
|
||||||
|
|
||||||
#### log_add_callback(log_LogFn fn, void *udata, int level)
|
#### log_add_callback(log_LogFn fn, void *udata, int level)
|
||||||
One or more callback functions which are called with the log data can be
|
One or more callback functions which are called with the log data can be
|
||||||
provided to the library by using the `log_add_callback()` function. A callback
|
provided to the library by using the `log_add_callback()` function. A callback
|
||||||
function is passed a `log_Event` structure containing the `line` number,
|
function is passed a `log_Event` structure containing the `line` number,
|
||||||
`filename`, `fmt` string, `va` printf va\_list, `level` and the given `udata`.
|
`filename`, `fmt` string, `va` printf va\_list, `level` and the given `udata`.
|
||||||
|
|
||||||
|
|
||||||
#### log_set_lock(log_LockFn fn, void *udata)
|
#### log_set_lock(log_LockFn fn, void *udata)
|
||||||
If the log will be written to from multiple threads a lock function can be set.
|
If the log will be written to from multiple threads a lock function can be set.
|
||||||
The function is passed the boolean `true` if the lock should be acquired or
|
The function is passed the boolean `true` if the lock should be acquired or
|
||||||
`false` if the lock should be released and the given `udata` value.
|
`false` if the lock should be released and the given `udata` value.
|
||||||
|
|
||||||
|
|
||||||
#### const char* log_level_string(int level)
|
#### const char* log_level_string(int level)
|
||||||
Returns the name of the given log level as a string.
|
Returns the name of the given log level as a string.
|
||||||
|
|
||||||
|
|
||||||
#### LOG_USE_COLOR
|
#### LOG_USE_COLOR
|
||||||
If the library is compiled with `-DLOG_USE_COLOR` ANSI color escape codes will
|
If the library is compiled with `-DLOG_USE_COLOR` ANSI color escape codes will
|
||||||
be used when printing.
|
be used when printing.
|
||||||
|
|
||||||
|
#### LOG_DONT_CODE
|
||||||
|
If the library is compiled with `-DLOG_DONT_CODE`, then the source code file name
|
||||||
|
and line number will be removed from the output.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
This library is free software; you can redistribute it and/or modify it under
|
This library is free software; you can redistribute it and/or modify it under
|
||||||
|
73
src/log.c
73
src/log.c
@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2020 rxi
|
* Copyright (c) 2020 rxi
|
||||||
|
* Copyright (c) 2024 iiiypuk
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to
|
* of this software and associated documentation files (the "Software"), to
|
||||||
@ -38,7 +39,7 @@ static struct {
|
|||||||
Callback callbacks[MAX_CALLBACKS];
|
Callback callbacks[MAX_CALLBACKS];
|
||||||
} L;
|
} L;
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
static const char *level_strings[] = {
|
static const char *level_strings[] = {
|
||||||
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
|
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
|
||||||
};
|
};
|
||||||
@ -48,69 +49,76 @@ static const char *level_colors[] = {
|
|||||||
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
|
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
#ifndef LOG_DONT_CODE
|
||||||
|
|
||||||
static void stdout_callback(log_Event *ev) {
|
static void stdout_callback(log_Event *ev) {
|
||||||
char buf[16];
|
char buf[16];
|
||||||
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
||||||
#ifdef LOG_USE_COLOR
|
#ifdef LOG_USE_COLOR
|
||||||
fprintf(
|
fprintf(ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf,
|
||||||
ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
|
level_colors[ev->level], level_strings[ev->level], ev->file,
|
||||||
buf, level_colors[ev->level], level_strings[ev->level],
|
ev->line);
|
||||||
ev->file, ev->line);
|
#else // LOG_USE_COLOR
|
||||||
#else
|
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file,
|
||||||
fprintf(
|
ev->line);
|
||||||
ev->udata, "%s %-5s %s:%d: ",
|
#endif // LOG_USE_COLOR
|
||||||
buf, level_strings[ev->level], ev->file, ev->line);
|
|
||||||
#endif
|
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
fflush(ev->udata);
|
fflush(ev->udata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else // LOG_DONT_CODE
|
||||||
|
|
||||||
|
static void stdout_callback(log_Event *ev) {
|
||||||
|
char buf[16];
|
||||||
|
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
||||||
|
#ifdef LOG_USE_COLOR
|
||||||
|
fprintf(ev->udata, "%s %s%-5s\x1b[0m ", buf, level_colors[ev->level],
|
||||||
|
level_strings[ev->level]);
|
||||||
|
#else // LOG_USE_COLOR
|
||||||
|
fprintf(ev->udata, "%s %-5s ", buf, level_strings[ev->level]);
|
||||||
|
#endif // LOG_USE_COLOR
|
||||||
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
|
fprintf(ev->udata, "\n");
|
||||||
|
fflush(ev->udata);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // LOG_DONT_CODE
|
||||||
|
|
||||||
static void file_callback(log_Event *ev) {
|
static void file_callback(log_Event *ev) {
|
||||||
char buf[64];
|
char buf[64];
|
||||||
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
||||||
fprintf(
|
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file,
|
||||||
ev->udata, "%s %-5s %s:%d: ",
|
ev->line);
|
||||||
buf, level_strings[ev->level], ev->file, ev->line);
|
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
fflush(ev->udata);
|
fflush(ev->udata);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void lock(void) {
|
static void lock(void) {
|
||||||
if (L.lock) { L.lock(true, L.udata); }
|
if (L.lock) {
|
||||||
|
L.lock(true, L.udata);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void unlock(void) {
|
static void unlock(void) {
|
||||||
if (L.lock) { L.lock(false, L.udata); }
|
if (L.lock) {
|
||||||
}
|
L.lock(false, L.udata);
|
||||||
|
}
|
||||||
|
|
||||||
const char* log_level_string(int level) {
|
|
||||||
return level_strings[level];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *log_level_string(int level) { return level_strings[level]; }
|
||||||
|
|
||||||
void log_set_lock(log_LockFn fn, void *udata) {
|
void log_set_lock(log_LockFn fn, void *udata) {
|
||||||
L.lock = fn;
|
L.lock = fn;
|
||||||
L.udata = udata;
|
L.udata = udata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void log_set_level(int level) { L.level = level; }
|
||||||
|
|
||||||
void log_set_level(int level) {
|
void log_set_quiet(bool enable) { L.quiet = enable; }
|
||||||
L.level = level;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void log_set_quiet(bool enable) {
|
|
||||||
L.quiet = enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int log_add_callback(log_LogFn fn, void *udata, int level) {
|
int log_add_callback(log_LogFn fn, void *udata, int level) {
|
||||||
for (int i = 0; i < MAX_CALLBACKS; i++) {
|
for (int i = 0; i < MAX_CALLBACKS; i++) {
|
||||||
@ -122,12 +130,10 @@ int log_add_callback(log_LogFn fn, void *udata, int level) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int log_add_fp(FILE *fp, int level) {
|
int log_add_fp(FILE *fp, int level) {
|
||||||
return log_add_callback(file_callback, fp, level);
|
return log_add_callback(file_callback, fp, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void init_event(log_Event *ev, void *udata) {
|
static void init_event(log_Event *ev, void *udata) {
|
||||||
if (!ev->time) {
|
if (!ev->time) {
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
@ -136,7 +142,6 @@ static void init_event(log_Event *ev, void *udata) {
|
|||||||
ev->udata = udata;
|
ev->udata = udata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void log_log(int level, const char *file, int line, const char *fmt, ...) {
|
void log_log(int level, const char *file, int line, const char *fmt, ...) {
|
||||||
log_Event ev = {
|
log_Event ev = {
|
||||||
.fmt = fmt,
|
.fmt = fmt,
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright (c) 2020 rxi
|
* Copyright (c) 2020 rxi
|
||||||
|
* Copyright (c) 2024 iiiypuk
|
||||||
*
|
*
|
||||||
* This library is free software; you can redistribute it and/or modify it
|
* This library is free software; you can redistribute it and/or modify it
|
||||||
* under the terms of the MIT license. See `log.c` for details.
|
* under the terms of the MIT license. See `log.c` for details.
|
||||||
@ -8,9 +9,9 @@
|
|||||||
#ifndef LOG_H
|
#ifndef LOG_H
|
||||||
#define LOG_H
|
#define LOG_H
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define LOG_VERSION "0.1.0"
|
#define LOG_VERSION "0.1.0"
|
||||||
@ -30,12 +31,14 @@ typedef void (*log_LockFn)(bool lock, void *udata);
|
|||||||
|
|
||||||
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
|
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
|
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
const char *log_level_string(int level);
|
const char *log_level_string(int level);
|
||||||
void log_set_lock(log_LockFn fn, void *udata);
|
void log_set_lock(log_LockFn fn, void *udata);
|
||||||
|
Loading…
Reference in New Issue
Block a user