San7o/micro-log.h
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
micro-log.h =========== Header-only, configurable, thread safe logging framework in C99. ``` 2025-09-21 22:32:36 INFO | Logger initialized 2025-09-21 22:32:36 INFO | I’d just like to interject for a moment... 2025-09-21 22:32:36 INFO | Closing logger ``` Author: Giovanni Santini Mail: giovanni.santini@proton.me License: MIT Features -------- - Multiple logging levels - Log to stdout, file, UNIX sockets, and network sockets - Configurable metadata (level, date, time, pid, tid, etc.) - JSON serialization support - Thread-safe logging - read settings from file (see the file `settings`) - optional colored output - compile time settings Initial implementation based on Oak: https://github.com/San7o/oak Example ------- #define MICRO_LOG_IMPLEMENTATION #include "micro-log.h" int main(void) { micro_log_init(); micro_log_set_flags(MICRO_LOG_FLAG_LEVEL | MICRO_LOG_FLAG_DATE | MICRO_LOG_FLAG_TIME); micro_log_info("I’d just like to interject for a moment..."); micro_log_close(); } Usage ----- Do this: #define MICRO_LOG_IMPLEMENTATION before you include this file in *one* C or C++ file to create the implementation. i.e. it should look like this: #include ... #include ... #include ... #define MICRO_LOG_IMPLEMENTATION #include "micro-log.h" You can tune the library by #defining certain values. See the "Config" comments under the "Configuration" section in the header. !!IMPORTANT: READ THIS FOR MULTITHREADING !! Multithreading support is optional as it adds a small performance setback since printing must be regulated via a mutex. If you want to support logging from multiple threads, then define MICRO_LOG_MULTITHREADED before including the header file. (Almost) All log function have two versions: one that interacts with a global logger, and another that uses a logger instance you supply. This is wanted because most of the time you just want a global logger without the need to pass references to loggers, but you may also need different loggers for different parts of your application, hence both options are provided. We will see some examples with the global logger. To use a local one, you just need to use the version '2' of the functions and pass the logger as the first parameter. For example `micro_log_info(...)` becomes `micro_log_info2(MicroLog logger, ...)`. After including the library, you can initialize a logger with `micro_log_init`. ``` micro_log_init(); ``` If using `micro_log_init2`, you can specify a pointer to a logger. Remember to close the logger after you are done with `micro_log_close()`. You can set additional settings like the log level with `micro_log_set_level` or a file with `micro_log_set_file`, check out the function declarations. To log something, use the macro `micro_log_` with the level you want to use, like: ``` micro_log_info("I’d like to interject for a moment. %s", text); ``` You can format the logs like printf(3). Check out more examples at the end of the header. You can also read some settings from a file. Check out the file `settings` for additional information. Code ---- The official git repository of micro-log.h is hosted at: https://github.com/San7o/micro-log.h This is part of a bigger collection of header-only C99 libraries called "micro-headers", contributions are welcome: https://github.com/San7o/micro-headers TODO ---- - windows support