Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pymumu/tinylog
A lightweight C, C++ logging library developed for Linux, It is designed with high performance, asynchronized, thread-safe and process-safe; tinylog是一个专为UNIX设计的轻量级的C/C++日志模块,其提供了高性能,异步,线程安全,进程安全的日志功能。
https://github.com/pymumu/tinylog
asynchronous c cpp library lightweight log logging logging-library multithreading tinylog
Last synced: 6 days ago
JSON representation
A lightweight C, C++ logging library developed for Linux, It is designed with high performance, asynchronized, thread-safe and process-safe; tinylog是一个专为UNIX设计的轻量级的C/C++日志模块,其提供了高性能,异步,线程安全,进程安全的日志功能。
- Host: GitHub
- URL: https://github.com/pymumu/tinylog
- Owner: pymumu
- License: other
- Created: 2018-03-18T16:08:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-01-26T02:04:50.000Z (11 months ago)
- Last Synced: 2024-12-13T10:28:45.531Z (13 days ago)
- Topics: asynchronous, c, cpp, library, lightweight, log, logging, logging-library, multithreading, tinylog
- Language: C
- Homepage:
- Size: 286 KB
- Stars: 216
- Watchers: 8
- Forks: 65
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TinyLog
[中文REAMDME请看这里](README_zh-CN.md)
Tinylog is a lightweight C-language high performance log component for UNIX environment, It is high performance, asynchronized, thread-safe and process-safe log library for C/C++.
It support log archiving, asynchronized, multithreading writing, multiprocessing writing, non-blocking mode.
output example:
```C
[2018-04-03 21:52:13,485][INFO][ example.c:7 ] This is a log message.
```archive example:
```C
root@raspberrypi:/home/pi/code/tiny-log/test # ls log/ -l
total 11564
-rw-r----- 1 root root 8754060 Apr 20 21:48 test.log
-rw-r----- 1 root root 1543852 Apr 20 21:48 test.log-20180420-214824.gz
-rw-r----- 1 root root 1539119 Apr 20 21:48 test.log-20180420-214830.gz
```## Features
1. Log to compressed archive file.
2. Log level output.
3. log format customization.
4. asynchronized log output.
5. Multiple log file support.
6. printf, vprintf similar interface, easy to expand.
7. non-blocking log mode.
8. Multithreading concurrent write.
9. Multiprcessing concurrent write.
10. c++ std::cout sytle log function.
11. Output log with color.## Usage
1. Include the log header file `tlog.h` in the C code.
2. Call `tlog_init` to initialize the log module.
3. Call `tlog` or `tlog_[debug|info|notice|warn|error|fatal]` function output log.
4. Call `tlog_[debug|info|notice|warn|error|fatal]<<` output c++ log.
5. Call `tlog_exit` to exit the shutdown log component.## Example
1. Output log
```C
#include
#include "tlog.h"int main(int argc, char *argv[])
{
tlog_init("example.log", 1024 * 1024, 8, 0, 0);
tlog(TLOG_INFO, "This is a log message.\n");
tlog_exit();
return 0;
}
```1. Output log with color.
```c
#include
#include "tlog.h"int main(int argc, char *argv[])
{
tlog_init("example.log", 1024 * 1024, 8, 0, TLOG_SCREEN_COLOR);
tlog_debug("This is a debug message.");
tlog_info("This is a log message.");
tlog_notice("This is a notice message.");
tlog_warn("This is a warn message.");
tlog_error("This is a error message.");
tlog_fatal("This is a fatal message.");
tlog_exit();
return 0;
}
```1. Output log for c++ cout style
```cpp
#include
#include "tlog.h"int main(int argc, char *argv[])
{
tlog_init("example.log", 1024 * 1024, 8, 0, 0);
tlog_info << "This is a c++ cout style log message.\n";
tlog_exit();
return 0;
}
```1. Independent log stream
c printf style
```C
#include
#include "tlog.h"int main(int argc, char *argv[])
{
tlog_log *log = NULL;
tlog_init("example.log", 1024 * 1024, 8, 0, 0);
log = tlog_open("another.log", 1024 * 1024, 8, 0, TLOG_SEGMENT);
tlog_printf(log, "This is a separate log stream\n");
tlog_close(log);
tlog_exit();
return 0;
}
```cpp std::out like style
```Cpp
#include
#include "tlog.h"int main(int argc, char *argv[])
{
tlog_log *log = NULL;
tlog_init("example.log", 1024 * 1024, 8, 0, 0);
log = tlog_open("another.log", 1024 * 1024, 8, 0, TLOG_SEGMENT);
tlog_out(log) << "This is a separate log stream\n";
tlog_close(log);
tlog_exit();
return 0;
}
```## API description
1. int tlog_init(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag);
`Function`:Initialize log module
`logfile`: log file
`maxlogsize`: The maximum size of a single log file.
`maxlogcount`: Number of archived logs.
`buffsize`: Buffer size
`flag`: log output flag: List of flags are as follows
* `TLOG_MULTI_WRITE`: Enable multi-process write single log mode. (Note: When using this mode, the maxlogsize parameter of all processes must be the same)
* `TLOG_NOCOMPRESS`: The archive log is not compressed.
* `TLOG_SEGMENT`: Log segmentation, used to register the callback function, returns a complete log for subsequent processing.
* `TLOG_NONBLOCK`: Do not block when buffer is insufficient.
* `TLOG_SCREEN`: Output logs to the screen.
* `TLOG_SUPPORT_FORK`: Support fork process.
* `TLOG_SCREEN_COLOR`: Output logs to the screen with color.1. tlog(level, format, ...)
`Function`:Print log
`level`: Current log Levels
`format`: Log formats1. tlog_debug, tlog_info, tlog_notice, tlog_warn, tlog_error, tlog_fatal
`Function`:Print log, for c++ use `<<` output log.
`format`: Log formats.1. tlog_exit()
`Function`:Log component exits
1. tlog_reg_format_func(tlog_format_func func)
`Function`:Registers a custom Format function, and the callback function is defined as:tlog_format_func
1. tlog_reg_log_output_func(tlog_log_output_func output, void *private)
`Function`: Register the custom log output function. The callback function is defined as: tlog_log_output_func. The TLOG_SEGMENT flag can be set during log initialization to return the callback to an independent full log.1. tlog_setlevel(tlog_level level)
`Function`:Set log level,valid level are :TLOG_DEBUG, TLOG_INFO, TLOG_NOTICE, TLOG_WARN, TLOG_ERROR, TLOG_FATAL, TLOG_OFF
1. tlog_getlevel()
`Function`:Get log level.
1. tlog_set_logfile(const char *logfile)
`Function`:Update log file.
`logfile`: log file1. tlog_setlogscreen(int enable)
`Function`:set whether the log is output to screen.
1. tlog_open(const char *logfile, int maxlogsize, int maxlogcount, int buffsize, unsigned int flag)
`Function`: Initializes a new log stream. When finished, it is closed with tlog_cloese.
`logfile`: log file.
`maxlogsize`: The maximum size of a single log file.
`maxlogcount`: The number of archived logs.
`buffsize`: The size of the buffer.
`flag`: log output flag: List of flags are as follows
* `TLOG_MULTI_WRITE`: Enable multi-process write single log mode. (Note: When using this mode, the maxlogsize parameter of all processes must be the same)
* `TLOG_NOCOMPRESS`: The archive log is not compressed.
* `TLOG_SEGMENT`: Log segmentation, used to register the callback function, returns a complete log for subsequent processing.
* `TLOG_NONBLOCK`: Do not block when buffer is insufficient.
* `TLOG_SCREEN`: Output logs to the screen.
`return value`: log stream handle.1. tlog_close(tlog_log *log)
`Function`: Turn off the log stream
`log`: The log stream handle.1. tlog_printf(tlog_log *log, const char *format, ...)
`Function`: Print the log to the specified log stream
`log`: The log stream handle.
`format`: The log format.1. tlog_out(tlog_log *log)
`Function`: Print the log to the specified log stream, use `<<` output log like std::out
`log`: The log stream handle.1. tlog_vprintf(tlog_log *log, const char *format, va_list ap)
`Function`: Print the log to the specified log stream
`log`: The log stream handle.
`format`: The log format.
`ap`: List of parameters.1. tlog_logscreen(tlog_log *log, int enable)
`Function`: Set whether the log stream is output to the screen
`log`: The log stream handle.
`enable`: Whether to enable.1. tlog_localtime(struct tlog_time *tm)
`Function`: Get local time.
`tm`: Local time output.1. tlog_reg_output_func(tlog_log *log, tlog_output_func output)
`Function`: Register the log stream output callback function. After the specified, the built-in write local file interface will be invalid. The TLOG_SEGMENT flag can be set when the log stream is initialized to return the callback to an independent complete log.
1. tlog_set_private(tlog_log *log, void *private)
`Function`: Set private parameters for retrieval in the callback function.
`log`: The log stream handle.
`private`: private parameter.1. tlog_get_private(tlog_log *log)
`Function`: Get the private parameter, which is obtained from the callback function.
`log`: The log stream handle.
`return value`: private parameter.1. tlog_rename_logfile(tlog_log *log, const char *logfile)
`Function`: Rename log file.
`log`: The log stream handle.
`logfile`: log file.## License
MIT License