Skip to content

Commit

Permalink
[glibutil] Added gutil_log_tid option
Browse files Browse the repository at this point in the history
Allows to add thread id prefix to the log messages. That can be useful
for debugging race conditions and other multi-threaded issues.
  • Loading branch information
monich committed Feb 24, 2021
1 parent dfcf238 commit e21366d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/gutil_log.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2019 Jolla Ltd.
* Copyright (C) 2014-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2014-2021 Jolla Ltd.
* Copyright (C) 2014-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -167,6 +167,7 @@ GLOG_MODULE_DECL(gutil_log_default)
extern GLogProc gutil_log_func;
extern GLogProc2 gutil_log_func2;
extern gboolean gutil_log_timestamp; /* Only affects stdout and stderr */
extern gboolean gutil_log_tid; /* Since 1.0.51 */

/* Log module (optional) */
#define GLOG_MODULE_DEFINE_(var,name) \
Expand Down
40 changes: 36 additions & 4 deletions src/gutil_log.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2014-2019 Jolla Ltd.
* Copyright (C) 2014-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2014-2021 Jolla Ltd.
* Copyright (C) 2014-2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -32,8 +32,13 @@

#include "gutil_log.h"

#if defined(DEBUG) && defined(_WIN32)
#ifdef unix
# include <unistd.h>
# include <sys/syscall.h>
# define gettid() ((int)syscall(SYS_gettid))
#elif defined(_WIN32)
# include <windows.h>
# define gettid() ((int)GetCurrentThreadId())
#endif

#ifndef GLOG_SYSLOG
Expand All @@ -51,6 +56,9 @@
/* Allows timestamps in stdout log */
gboolean gutil_log_timestamp = TRUE;

/* Adds thread id prefix */
gboolean gutil_log_tid = FALSE; /* Since 1.0.51 */

/* Log configuration */
static GUTIL_DEFINE_LOG_FN2(gutil_log_default_proc);
GLogProc gutil_log_func = gutil_log_stdout;
Expand Down Expand Up @@ -191,8 +199,18 @@ gutil_log_stdio(
}
#endif
if (name) {
#ifdef gettid
if (gutil_log_tid)
fprintf(out, "[%d] %s[%s] %s%s\n", gettid(), t, name, prefix, msg);
else
#endif
fprintf(out, "%s[%s] %s%s\n", t, name, prefix, msg);
} else {
#ifdef gettid
if (gutil_log_tid)
fprintf(out, "[%d] %s%s%s\n", gettid(), t, prefix, msg);
else
#endif
fprintf(out, "%s%s%s\n", t, prefix, msg);
}
if (msg != buf) g_free(msg);
Expand Down Expand Up @@ -280,13 +298,27 @@ gutil_log_syslog(
}
}

if (name || prefix) {
if (name || prefix
#ifdef gettid
|| gutil_log_tid
#endif
) {
char buf[512];
char* msg = gutil_log_format(buf, sizeof(buf), format, va);
if (!prefix) prefix = "";
if (name) {
#ifdef gettid
if (gutil_log_tid)
syslog(priority, "[%d] [%s] %s%s", gettid(), name, prefix, msg);
else
#endif
syslog(priority, "[%s] %s%s", name, prefix, msg);
} else {
#ifdef gettid
if (gutil_log_tid)
syslog(priority, "[%d] %s%s", gettid(), prefix, msg);
else
#endif
syslog(priority, "%s%s", prefix, msg);
}
if (msg != buf) g_free(msg);
Expand Down

0 comments on commit e21366d

Please sign in to comment.