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
    
    
  • 相关阅读:
    shell 指定范围产生随机数
    shell脚本比较两个数大小
    Shell 脚本实现随机抽取班级学生
    linux通过挂载系统光盘搭建本地yum仓库的方法
    kuberenetes 上使用helm部署rancher如何卸载干净
    Windows 下 左Ctrl和Caps交换
    C#笔记 -- 协变、逆变
    Python 读取window下UTF-8-BOM 文件
    生成命令行程序使用脚本
    ffmpeg 命令小记
  • 原文地址:https://www.cnblogs.com/muahao/p/10254177.html
Copyright © 2011-2022 走看看