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
    
    
  • 相关阅读:
    第一阶段
    堆栈的内容------全局变量(实例变量)---静态变量等等
    this和引用变量的地址值是同一个---------new后面的是构造方法
    引用数据类型的传递,那个值先输出,后面的也同样是同一个值
    基本数据类型的传递,参数传递的是具体的值
    构造方法和构造代码块
    装饰器初识
    Bootstrap框架
    Django ORM那些相关操作
    jQuery
  • 原文地址:https://www.cnblogs.com/muahao/p/10254177.html
Copyright © 2011-2022 走看看