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 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 .%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_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 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__)

#define MYLOG_FUNCALL mylogdebug("enter %s()", __FUNCTION__)

#endif // MyLogSystem_H
    /* EOF */


4.默认输出

20:24:01 .998715 [error   ] [logout.c] [main]:12 logout: error
20:24:01 .998887 [critical] [logout.c] [main]:13 logout: critical
20:24:01 .998913 [warning ] [logout.c] [main]:14 logout: warning
20:24:01 .998932 [message ] [logout.c] [main]:15 logout: message
20:24:01 .998948 [info    ] [logout.c] [main]:16 logout: info
20:24:01 .998966 [debug   ] [logout.c] [main]:17 logout: debug 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值