TinyLog
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:
[2018-04-03 21:52:13,485][INFO][ example.c:7 ] This is a log message.
archive example:
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
- Log to compressed archive file.
- Log level output.
- log format customization.
- asynchronized log output.
- Multiple log file support.
- printf, vprintf similar interface, easy to expand.
- non-blocking log mode.
- Multithreading concurrent write.
- Multiprcessing concurrent write.
- c++ std::cout sytle log function.
Usage
- Include the log header file
tlog.h
in the C code. - Call
tlog_init
to initialize the log module. - Call
tlog
ortlog_[debug|info|notice|warn|error|fatal]
function output log. - Call
tlog_[debug|info|notice|warn|error|fatal]<<
output c++ log. - Call
tlog_exit
to exit the shutdown log component.
Example
-
Output log
#include <stdio.h> #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; }
-
Output log for c++ cout style
#include <stdio.h> #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; }
-
Independent log stream
c printf style
#include <stdio.h> #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
#include <stdio.h> #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
-
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 followsTLOG_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(level, format, ...)
Function
:Print log
level
: Current log Levels
format
: Log formats -
tlog_debug, tlog_info, tlog_notice, tlog_warn, tlog_error, tlog_fatal
Function
:Print log, for c++ use<<
output log.
format
: Log formats. -
tlog_exit()
Function
:Log component exits -
tlog_reg_format_func(tlog_format_func func)
Function
:Registers a custom Format function, and the callback function is defined as:tlog_format_func -
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. -
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 -
tlog_getlevel()
Function
:Get log level. -
tlog_set_logfile(const char *logfile)
Function
:Update log file.logfile
: log file -
tlog_setlogscreen(int enable)
Function
:set whether the log is output to screen. -
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 followsTLOG_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.
-
tlog_close(tlog_log *log)
Function
: Turn off the log stream
log
: The log stream handle. -
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. -
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. -
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. -
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. -
tlog_localtime(struct tlog_time *tm)
Function
: Get local time.
tm
: Local time output. -
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. -
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. -
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. -
tlog_rename_logfile(tlog_log *log, const char *logfile)
Function
: Rename log file.
log
: The log stream handle.
logfile
: log file.
License
MIT License