Skip to content

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++日志模块,其提供了高性能,异步,线程安全,进程安全的日志功能。

License

pymumu/tinylog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TinyLog

中文REAMDME请看这里

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

  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

    #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;
    }
  2. Output log with color.

    #include <stdio.h>
    #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;
    }
  3. 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;
    }
  4. 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

  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.
  2. tlog(level, format, ...)

    Function:Print log
    level: Current log Levels
    format: Log formats

  3. tlog_debug, tlog_info, tlog_notice, tlog_warn, tlog_error, tlog_fatal

    Function:Print log, for c++ use << output log.
    format: Log formats.

  4. tlog_exit()

    Function:Log component exits

  5. tlog_reg_format_func(tlog_format_func func)

    Function:Registers a custom Format function, and the callback function is defined as:tlog_format_func

  6. 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.

  7. 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

  8. tlog_getlevel()

    Function:Get log level.

  9. tlog_set_logfile(const char *logfile)

    Function:Update log file. logfile: log file

  10. tlog_setlogscreen(int enable)

    Function:set whether the log is output to screen.

  11. 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.
  12. tlog_close(tlog_log *log)

    Function: Turn off the log stream
    log: The log stream handle.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. tlog_localtime(struct tlog_time *tm)

    Function: Get local time.
    tm: Local time output.

  18. 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.

  19. 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.

  20. 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.

  21. tlog_rename_logfile(tlog_log *log, const char *logfile)

    Function: Rename log file.
    log: The log stream handle.
    logfile: log file.

License

MIT License

About

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++日志模块,其提供了高性能,异步,线程安全,进程安全的日志功能。

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published