glib的log系统重新定义

0.声明转载 https://github.com/shadow/shadow-plugin-tor/blob/master/src/torctl/torctl-main.c

1.编译gcc -g logout.c logout.h -o logout $(pkg-config --cflags --libs glib-2.0 gobject-2.0 gtk+-2.0)

2.代码: 

/**** (C) COPYRIGHT 2019 name ****
* File Name       :  logout.h
* Author          :  name
* Version         :  V1.0
* Date            :  21/03/2019
* Description     :
***********************************/

#ifndef MyLogSystem_H
#define MyLogSystem_H

#include <glib.h>

void _torctlmain_log(GLogLevelFlags level, const gchar* fileName,
    const gchar* functionName, gint line, const gchar* format, ...);

#define mylogerror(...) _torctlmain_log(G_LOG_LEVEL_ERROR, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define mylogcritical(...) _torctlmain_log(G_LOG_LEVEL_CRITICAL, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define mylogwarning(...) _torctlmain_log(G_LOG_LEVEL_WARNING, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define mylogmessage(...) _torctlmain_log(G_LOG_LEVEL_MESSAGE, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define myloginfo(...) _torctlmain_log(G_LOG_LEVEL_INFO, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
#define mylogdebug(...) _torctlmain_log(G_LOG_LEVEL_DEBUG, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)

#endif // MyLogSystem_H
/* EOF */

/**** (C) COPYRIGHT 2019 your name ****
* File Name       :  test.c
* Author          :  your name
* Version         :  V1.0
* Date            :  16/03/2019
* Description     :
***********************************/
#include "logout.h"

typedef enum {
    NOTIME = 0, // display no time
    HourTime,   // display hour and so
    ALLTime     // all time include year
} LogTimeLength;

GLogLevelFlags torctlLogFilterLevel = G_LOG_LEVEL_DEBUG;
LogTimeLength defaultLogTime = HourTime;

static const gchar* _torctlmain_logLevelToString(GLogLevelFlags logLevel)
{
    switch (logLevel) {
    case G_LOG_LEVEL_ERROR:
        return "error   ";
    case G_LOG_LEVEL_CRITICAL:
        return "critical";
    case G_LOG_LEVEL_WARNING:
        return "warning ";
    case G_LOG_LEVEL_MESSAGE:
        return "message ";
    case G_LOG_LEVEL_INFO:
        return "info    ";
    case G_LOG_LEVEL_DEBUG:
        return "debug   ";
    default:
        return "default";
    }
}

void _torctlmain_log(GLogLevelFlags level, const gchar* fileName, const gchar* functionName, gint line, const gchar* format, ...)
{
    if (level > torctlLogFilterLevel) {
        return;
    }

    va_list vargs;
    va_start(vargs, format);

    GDateTime* dt = g_date_time_new_now_local();
    GString* newformat = g_string_new(NULL);

    switch (defaultLogTime) {
    case NOTIME:
        g_string_append_printf(newformat, "[%s] [%s:%d] [%s] %s",
            _torctlmain_logLevelToString(level), fileName, line, functionName, format);
        break;
    case ALLTime:
        g_string_append_printf(newformat, "%04i-%02i-%02i %02i:%02i:%02i %" G_GINT64_FORMAT ".%06i [%s] [%s:%d] [%s] %s",
            g_date_time_get_year(dt), g_date_time_get_month(dt), g_date_time_get_day_of_month(dt),
            g_date_time_get_hour(dt), g_date_time_get_minute(dt), g_date_time_get_second(dt),
            g_date_time_to_unix(dt), g_date_time_get_microsecond(dt),
            _torctlmain_logLevelToString(level), fileName, line, functionName, format);
        break;
    case HourTime:
    default:
        g_string_append_printf(newformat, "%02i:%02i:%02i %" G_GINT64_FORMAT ".%06i [%s] [%s:%d] [%s] %s",
            g_date_time_get_hour(dt), g_date_time_get_minute(dt), g_date_time_get_second(dt),
            g_date_time_to_unix(dt), g_date_time_get_microsecond(dt),
            _torctlmain_logLevelToString(level), fileName, line, functionName, format);
        break;
    }

    gchar* message = g_strdup_vprintf(newformat->str, vargs);
    g_print("%s\n", message);
    g_free(message);

    g_string_free(newformat, TRUE);
    g_date_time_unref(dt);

    va_end(vargs);
}

// gcc -g logout.c logout.h -o logout $(pkg-config --cflags --libs glib-2.0 gobject-2.0 gtk+-2.0)

/* EOF */


/**** (C) COPYRIGHT 2019 your name ****
* File Name       :  test.c
* Author          :  your name
* Version         :  V1.0
* Date            :  16/03/2019
* Description     :
***********************************/
#include "logout.h"

int main(int argc, char** argv)
{
    mylogerror("logout: error");
    mylogcritical("logout: critical");
    mylogwarning("logout: warning");
    mylogmessage("logout: message");
    myloginfo("logout: info");
    mylogdebug("logout: debug");

    return 0;
}

4.默认输出

20:24:01 1553171041.998715 [error   ] [logout.c] [main]:12 logout: error
20:24:01 1553171041.998887 [critical] [logout.c] [main]:13 logout: critical
20:24:01 1553171041.998913 [warning ] [logout.c] [main]:14 logout: warning
20:24:01 1553171041.998932 [message ] [logout.c] [main]:15 logout: message
20:24:01 1553171041.998948 [info    ] [logout.c] [main]:16 logout: info
20:24:01 1553171041.998966 [debug   ] [logout.c] [main]:17 logout: debug 

猜你喜欢

转载自blog.csdn.net/Hu_yilang/article/details/88724847