zoukankan      html  css  js  c++  java
  • A useful logger function in C project.

    #cat log.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <errno.h>
    #include <limits.h>
    #include <stdarg.h>
    
    #include "log.h"
    
    /* Log */
    #define IDLE_MEM_CFG_PRINT_LOG 1
    
    void ilog(const char *func, int line, char *fmt, ... )
    {
        char buf[LINE_MAX] = {0};
        time_t timep;
        va_list va;
        int pos = 0;
    
        /* Bail if we don't have logging functionality enabled */
        if (!IDLE_MEM_CFG_PRINT_LOG)
            return;
    
        /* Header, including the timestamp */
        time(&timep);
        pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d] ", ctime(&timep), func, line);
    
        /* Variable arguments */
        va_start(va, fmt);
        pos = vsnprintf(buf + pos, LINE_MAX - pos, fmt, va);
        va_end(va);
    
        /* Raise a warning if the buffer is overran */
        if (pos >= (LINE_MAX - 1))
            fprintf(stderr, "%s
    ", "buffer may have been truncated");
    
        fprintf(stdout, "%s
    ", buf);
    }
    
    void error_log(char *err, const char *func, int line, int ret, char *fmt, ... )
    {
        char buf[LINE_MAX] = {0};
        time_t timep;
        va_list va;
        int pos;
    
        /* Bail if we don't have logging functionality enabled */
        if (!IDLE_MEM_CFG_PRINT_LOG)
            return;
    
        /* Header, including the timestamp */
        time (&timep);
        if(err) {
            pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d]: Retval %d " %s " ",
                       ctime(&timep), func, line, ret, err);
        } else {
            pos = snprintf(buf, LINE_MAX, "<%s>[%s:%d]: Retval %d ",
                       ctime(&timep), func, line, ret);
        }
    
        /* Variable arguments */
        va_start(va, fmt);
        pos = vsnprintf(buf + pos, LINE_MAX - pos, fmt, va);
        va_end(va);
    
        /* Raise a warning if the buffer is overran */
        if (pos >= (LINE_MAX - 1))
            fprintf(stderr, "%s
    ", "buffer may have been truncated");
    
        fprintf(stderr, "%s
    ", buf);
    }
    
    
    #cat log.h
    /* Log */
    void error_log(char *err, const char *func, int line, int ret, char *fmt, ... );
    void ilog(const char *func, int line, char *fmt, ... );
    #define ELOG(libc, ret, fmt, args... )                              
        do {                                            
            if (libc)                                   
                error_log(strerror(errno), __func__, __LINE__, ret, fmt, ##args);   
            else                                        
                error_log(NULL, __func__, __LINE__, ret, fmt, ##args);          
        } while(0)
    
    #define ILOG(fmt, args...)                                  
        do {                                            
            ilog(__func__, __LINE__, fmt, ##args);                      
        } while(0)
    
    #cat main.c
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include "log.h"
    
    void main(void)
    {
        char *name1 = "jack";
        char *name2 = "tom";
    	int ret;
    
    	ret = 0;
        ILOG("hello %s, my name is %s
    ", name1, name2);
    	ELOG(0, ret, "waiting you");
    	ELOG(1, ret, "waiting you");
    }
    
    gcc -o main log.c log.h main.c
    
    #./main
    <Fri Jan 11 11:19:04 2019
    >[main:13] hello jack, my name is tom
    
    <Fri Jan 11 11:19:04 2019
    >[main:14]: Retval 0 waiting you
    <Fri Jan 11 11:19:04 2019
    >[main:15]: Retval 0 " Success " waiting you
    
    
  • 相关阅读:
    通过json动态创建控制器
    记一次bug解决!改变思路解决问题的同时,还需要弄明白是什么原因。
    __proto__,prototype,constructor
    事件:compositionstart & compositionend,解决oninput获取到拼音的问题。
    事件绑定----阻止冒泡失效
    预装的win10系统如何恢复
    rem.js
    vscode 使用 github仓库
    nginx使用
    伸缩盒
  • 原文地址:https://www.cnblogs.com/muahao/p/10254177.html
Copyright © 2011-2022 走看看