Open-source pure C log function library ilog3 Quick Start (8. If you prefer simple log function library than log function library)

Source: Internet
Author: User
Open-source pure C log function library ilog3 Quick Start (8. If you prefer simple log function library than log function library)

Many netizens insisted that simple log functions should be used in the project, but they do not like log function libraries. I have no choice but to argue with them over and over again. I also like short and brisk log function libraries, instead of using a bloated library with too many functions, for example, I still insist on using an external shell to achieve better, such a log function library can be much smaller, it can be more brisk and stable, I have added too many features due to the "threat" of another group of functional control netizens. They like an extreme package of solutions, in addition, it is proved that other functions such as log4c are correct because they are also implemented.

Recently, I was developing a small application server platform tcpdaemon, which requires the log function, but the entire tcpdaemon code is very small, much smaller than ilog3, so I thought of the top netizens, after repeated consideration, I decided to add a little ghost spirit to my ilog3-a simple log function with a single mini-type, so that everyone can finally satisfy it.

Src/Add logc. h, logc. c. These two source files are not compiled into libilog3.so, but independently provided by friends who like simple log functions and dislike the log function library separately to their projects for direct source code inclusion. For example, tcpdaemon Project
tcpdaemon-1.1.0/    src/        tcpdaemon.h        main.c        tcpdaemon.c        worker.c        LOGC.h        LOGC.c        ...



There is a test case test_logc.c in the test directory of ilog3.
#include "../src/LOGC.h"int test_logc(){    char    buf[ 64 + 1 ] ;    long    buflen ;        SetLogFile( "%s/log/test_logc.log" , getenv("HOME") );    SetLogLevel( LOGLEVEL_INFO );        DebugLog( __FILE__ , __LINE__ , "call DebugLog" );    InfoLog( __FILE__ , __LINE__ , "call InfoLog" );    WarnLog( __FILE__ , __LINE__ , "call WarnLog" );    ErrorLog( __FILE__ , __LINE__ , "call ErrorLog" );    FatalLog( __FILE__ , __LINE__ , "call FatalLog" );        memset( buf , 0x00 , sizeof(buf) );    buflen = sizeof(buf) - 1 ;    DebugHexLog( __FILE__ , __LINE__ , buf , buflen , "call DebugHexLog" );    InfoHexLog( __FILE__ , __LINE__ , buf , buflen , "call InfoHexLog" );    WarnHexLog( __FILE__ , __LINE__ , buf , buflen , "call WarnHexLog" );    ErrorHexLog( __FILE__ , __LINE__ , buf , buflen , "call ErrorHexLog" );    FatalHexLog( __FILE__ , __LINE__ , buf , buflen , "call FatalHexLog" );        return 0;}int main(){    return -test_logc();}



During compilation, you only need to compile the logc. C link together.
$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c test_logc.c$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -I. -c ../src/LOGC.c$ gcc -g -fPIC -O2 -Wall -Werror -fno-strict-aliasing -o test_logc test_logc.o LOGC.o -lpthread$ ./test_logc$ cat $HOME/log/test_logc.log2014-07-20 22:35:12.890000 | INFO  | 1556:3284:test_logc.c:12 | call InfoLog2014-07-20 22:35:12.906000 | WARN  | 1556:3284:test_logc.c:13 | call WarnLog2014-07-20 22:35:12.906000 | ERROR | 1556:3284:test_logc.c:14 | call ErrorLog2014-07-20 22:35:12.906000 | FATAL | 1556:3284:test_logc.c:15 | call FatalLog2014-07-20 22:35:12.906000 | INFO  | 1556:3284:test_logc.c:20 | call InfoHexLog             0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    0123456789ABCDEF0x00000000   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000010   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000030   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................2014-07-20 22:35:12.906000 | WARN  | 1556:3284:test_logc.c:21 | call WarnHexLog             0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    0123456789ABCDEF0x00000000   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000010   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000030   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................2014-07-20 22:35:12.906000 | ERROR | 1556:3284:test_logc.c:22 | call ErrorHexLog             0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    0123456789ABCDEF0x00000000   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000010   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000030   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................2014-07-20 22:35:12.906000 | FATAL | 1556:3284:test_logc.c:23 | call FatalHexLog             0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F    0123456789ABCDEF0x00000000   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000010   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000020   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................0x00000030   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................



Is it easy? Download and play.
Home Portal: [url] http://git.oschina.net/calvinwilliam ams/ilog3#/url]
The source code package doc directory contains the user guide and reference manual, which provides more detailed instructions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.