added -DLOG_DONT_CODE

This commit is contained in:
Alexander Popov 2024-05-30 22:58:07 +03:00
parent f9ea34994b
commit b9039f161c
6 changed files with 59 additions and 51 deletions

4
.clang-format Normal file
View File

@ -0,0 +1,4 @@
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 2
ColumnLimit: 80

BIN
.screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -1,4 +1,5 @@
Copyright (c) 2020 rxi
Copyright (c) 2024 iiiypuk
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

View File

@ -1,8 +1,7 @@
# log.c
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
**[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
```
#### log_set_quiet(bool enable)
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
will continue to write to files and callbacks if any are set.
#### log_set_level(int level)
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
level is `LOG_TRACE`, such that nothing is ignored.
#### log_add_fp(FILE *fp, int level)
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
@ -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
file pointer a value less-than-zero is returned.
#### log_add_callback(log_LogFn fn, void *udata, int level)
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
function is passed a `log_Event` structure containing the `line` number,
`filename`, `fmt` string, `va` printf va\_list, `level` and the given `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.
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.
#### const char* log_level_string(int level)
Returns the name of the given log level as a string.
#### LOG_USE_COLOR
If the library is compiled with `-DLOG_USE_COLOR` ANSI color escape codes will
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
This library is free software; you can redistribute it and/or modify it under

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020 rxi
* Copyright (c) 2024 iiiypuk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
@ -38,7 +39,7 @@ static struct {
Callback callbacks[MAX_CALLBACKS];
} L;
// clang-format off
static const char *level_strings[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};
@ -48,86 +49,91 @@ static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
#endif
// clang-format on
#ifndef 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 \x1b[90m%s:%d:\x1b[0m ",
buf, level_colors[ev->level], level_strings[ev->level],
ev->file, ev->line);
#else
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
#endif
fprintf(ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf,
level_colors[ev->level], level_strings[ev->level], ev->file,
ev->line);
#else // LOG_USE_COLOR
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file,
ev->line);
#endif // LOG_USE_COLOR
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
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) {
char buf[64];
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
fprintf(
ev->udata, "%s %-5s %s:%d: ",
buf, level_strings[ev->level], ev->file, ev->line);
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file,
ev->line);
vfprintf(ev->udata, ev->fmt, ev->ap);
fprintf(ev->udata, "\n");
fflush(ev->udata);
}
static void lock(void) {
if (L.lock) { L.lock(true, L.udata); }
static void lock(void) {
if (L.lock) {
L.lock(true, L.udata);
}
}
static void unlock(void) {
if (L.lock) { L.lock(false, L.udata); }
}
const char* log_level_string(int level) {
return level_strings[level];
if (L.lock) {
L.lock(false, L.udata);
}
}
const char *log_level_string(int level) { return level_strings[level]; }
void log_set_lock(log_LockFn fn, void *udata) {
L.lock = fn;
L.udata = udata;
}
void log_set_level(int level) { L.level = level; }
void log_set_level(int level) {
L.level = level;
}
void log_set_quiet(bool enable) {
L.quiet = enable;
}
void log_set_quiet(bool enable) { L.quiet = enable; }
int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) {
if (!L.callbacks[i].fn) {
L.callbacks[i] = (Callback) { fn, udata, level };
L.callbacks[i] = (Callback){fn, udata, level};
return 0;
}
}
return -1;
}
int log_add_fp(FILE *fp, int level) {
return log_add_callback(file_callback, fp, level);
}
static void init_event(log_Event *ev, void *udata) {
if (!ev->time) {
time_t t = time(NULL);
@ -136,13 +142,12 @@ static void init_event(log_Event *ev, void *udata) {
ev->udata = udata;
}
void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = {
.fmt = fmt,
.file = file,
.line = line,
.level = level,
.fmt = fmt,
.file = file,
.line = line,
.level = level,
};
lock();

View File

@ -1,5 +1,6 @@
/**
* Copyright (c) 2020 rxi
* Copyright (c) 2024 iiiypuk
*
* 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.
@ -8,9 +9,9 @@
#ifndef LOG_H
#define LOG_H
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#define LOG_VERSION "0.1.0"
@ -30,14 +31,16 @@ typedef void (*log_LockFn)(bool lock, void *udata);
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_debug(...) log_log(LOG_DEBUG, __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_error(...) log_log(LOG_ERROR, __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_level(int level);
void log_set_quiet(bool enable);