现在的位置: 首页 > 综合 > 正文

C/C++/Objective-C 日志记录功能模块

2013年10月07日 ⁄ 综合 ⁄ 共 1428字 ⁄ 字号 评论关闭

刚写了一个Logger Module,适用语言 C/C++/Objective-C 以及其他兼容C的语言, 欢迎大家试用 ~_~ 

初始有3种级别:

WSLOG_LEVEL_COMMON     普通打印信息

WSLOG_LEVEL_WARNING    警告信息
WSLOG_LEVEL_ERROR      错误信息
    每种类别的 log 分别记录于不同文件。

具备功能:
1. 具有打印缓冲区,减少I/O次数,缓冲区大小可调
2. 可设置单个记录文件的大小,以及某类log的文件数量,循环更新
3. 采用 enum 和 array 结合,便于扩展 log level级别
4. 通过条件编译,轻松实现 console 打印 和 日志文件记录两种方式切换

详见:

https://github.com/winlin/GTCLogModule

GTCLogModule

This log module can be used by C/C++/Objective-C projects.

Features

  • high robustness and proformace
  • process/thread safe;
  • high configurable, include buffer size, log file numbers, log file size etc;
  • three default log level and easily to expand;
  • print buffer to reduce disk I/O times;
  • easily change stdout/stderr between log file just according to a #define macro;
  • detial document comments

Usage

It's very simple, you can learn from the test.c file.

    MITLogOpen("TestApp", "./logs");

    char dir[1024];
    getcwd(dir, sizeof(dir));
    MITLog_DetPrintf(MITLOG_LEVEL_COMMON, "%s", dir);

    // 1. usage in one thread demo
    // at first in main thread call MITLogOpen()
    MITLog_DetPuts(MITLOG_LEVEL_COMMON, "just have a try and feel the speed:write 3000000 messages");
    time_t starttime = time(NULL);
    for (int i=MITLOG_INDEX_COMM_FILE; i<=MITLOG_INDEX_ERROR_FILE; ++i) {
        for (int j=0; j < 1000000; ++j) {
            MITLogWrite(MITLOG_LEVEL_COMMON, "This is for common:%d", j);
            MITLogWrite(MITLOG_LEVEL_WARNING, "This is for warning:%d", j);
            MITLogWrite(MITLOG_LEVEL_ERROR, "This is for error:%d", j);
        }
    }
    time_t closetime = time(NULL);
    MITLog_DetPrintf(MITLOG_LEVEL_COMMON, "All time:%ld\n", closetime-starttime);
    // at last you should close the log module
    MITLogClose();

Winlin

抱歉!评论已关闭.