commit a1d3848ab60c792f5cd57c69c0843e55c54ad662 Author: rxi Date: Sun Mar 12 14:35:25 2017 +0000 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7e3bf17 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2017 rxi + +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 +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5b7e88 --- /dev/null +++ b/README.md @@ -0,0 +1,70 @@ +# log.c +A simple logging library implemented in C99 + +![screenshot](https://cloud.githubusercontent.com/assets/3920290/23831970/a2415e96-0723-11e7-9886-f8f5d2de60fe.png) + + +## Usage +**[log.c](src/log.c?raw=1)** and **[log.h](src/log.h?raw=1)** should be dropped +into an existing project and compiled along with it. The library provides 6 +function-like macros for logging: + +```c +log_trace(const char *fmt, ...); +log_debug(const char *fmt, ...); +log_info(const char *fmt, ...); +log_warn(const char *fmt, ...); +log_error(const char *fmt, ...); +log_fatal(const char *fmt, ...); +``` + +Each function takes a printf format string followed by additional arguments: + +```c +log_trace("Hello %s", "world") +``` + +Resulting in a line with the given format printed to stderr: + +``` +20:18:26 TRACE src/main.c:11: Hello world +``` + + +#### log_set_quiet(int enable) +Quiet-mode can be enabled by passing `1` 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 the file if one is 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 be ignored. By default the level is +`LOG_TRACE`, such that nothing is ignored. + + +#### log_set_fp(FILE *fp) +A file pointer where the log should be written can be provided to the library by +using the `log_set_fp()` function. The data written to the file output is +of the following format: + +``` +2047-03-11 20:18:26 TRACE src/main.c:11: Hello world +``` + + +#### log_set_lock(log_LockFn fn) +If the log will be written to from multiple threads a lock function can be set. +The function is passed a `udata` value (set by `log_set_udata()`) and the +integer `1` if the lock should be acquired or `0` if the lock should be +released. + + +#### LOG_USE_COLOR +If the library is compiled with `-DLOG_USE_COLOR` ANSI color escape codes will +be used when printing. + + +## License +This library is free software; you can redistribute it and/or modify it under +the terms of the MIT license. See [LICENSE](LICENSE) for details. diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..7c74ec0 --- /dev/null +++ b/src/log.c @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2017 rxi + * + * 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 the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "log.h" + +static struct { + void *udata; + log_LockFn lock; + FILE *fp; + int level; + int quiet; +} L; + + +static const char *level_names[] = { + "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" +}; + +#ifdef LOG_USE_COLOR +static const char *level_colors[] = { + "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" +}; +#endif + + +static void lock(void) { + if (L.lock) { + L.lock(L.udata, 1); + } +} + + +static void unlock(void) { + if (L.lock) { + L.lock(L.udata, 0); + } +} + + +void log_set_udata(void *udata) { + L.udata = udata; +} + + +void log_set_lock(log_LockFn fn) { + L.lock = fn; +} + + +void log_set_fp(FILE *fp) { + L.fp = fp; +} + + +void log_set_level(int level) { + L.level = level; +} + + +void log_set_quiet(int enable) { + L.quiet = enable ? 1 : 0; +} + + +void log_log(int level, const char *file, int line, const char *fmt, ...) { + if (level < L.level) { + return; + } + + /* Acquire lock */ + lock(); + + /* Get current time */ + time_t t = time(NULL); + struct tm *lt = localtime(&t); + + /* Log to stderr */ + if (!L.quiet) { + va_list args; + char buf[16]; + buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0'; +#ifdef LOG_USE_COLOR + fprintf( + stderr, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", + buf, level_colors[level], level_names[level], file, line); +#else + fprintf(stderr, "%s %-5s %s:%d: ", buf, level_names[level], file, line); +#endif + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); + } + + /* Log to file */ + if (L.fp) { + va_list args; + char buf[32]; + buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0'; + fprintf(L.fp, "%s %-5s %s:%d: ", buf, level_names[level], file, line); + va_start(args, fmt); + vfprintf(L.fp, fmt, args); + va_end(args); + fprintf(L.fp, "\n"); + } + + /* Release lock */ + unlock(); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..956fb81 --- /dev/null +++ b/src/log.h @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2017 rxi + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See `microtar.c` for details. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include + +#define LOG_VERSION "0.1.0" + +typedef void (*log_LockFn)(void *udata, int lock); + +enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; + +#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__) + +void log_set_udata(void *udata); +void log_set_lock(log_LockFn fn); +void log_set_fp(FILE *fp); +void log_set_level(int level); +void log_set_quiet(int enable); + +void log_log(int level, const char *file, int line, const char *fmt, ...); + +#endif