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\n", "buffer may have been truncated");

    fprintf(stdout, "%s\n", 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\n", "buffer may have been truncated");

    fprintf(stderr, "%s\n", 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\n", 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

猜你喜欢

转载自www.cnblogs.com/muahao/p/10254177.html