Skip to content

Commit

Permalink
[glibutil] Initialize default log settings from the environment
Browse files Browse the repository at this point in the history
The following environment variables are checked on Linux:

  GUTIL_LOG_DEFAULT_LEVEL - configures default log level
  GUTIL_LOG_TIMESTAMP     - configures gutil_log_timestamp option
  GUTIL_LOG_TID           - configures gutil_log_tid option

This setup is done once at program's startup and then the program
can override these settings the way it wants, based on its command
line options, configuration files or whatever.

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 e21366d commit c707a15
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/gutil_log.c
Expand Up @@ -31,6 +31,9 @@
*/

#include "gutil_log.h"
#include "gutil_misc.h"

#include <stdlib.h>

#ifdef unix
# include <unistd.h>
Expand Down Expand Up @@ -639,6 +642,33 @@ gutil_log_get_type()
GLOG_TYPE_CUSTOM;
}

/* Initialize defaults from the environment */
#ifndef _WIN32
__attribute__((constructor))
static
void
gutil_log_init()
{
int val = 0;

if (gutil_parse_int(getenv("GUTIL_LOG_DEFAULT_LEVEL"), 0, &val) &&
val >= GLOG_LEVEL_INHERIT && val <= GLOG_LEVEL_VERBOSE) {
gutil_log_default.level = val;
GDEBUG("Default log level %d", val);
}

if (gutil_parse_int(getenv("GUTIL_LOG_TIMESTAMP"), 0, &val) && val > 0) {
gutil_log_timestamp = (val > 0);
GDEBUG("Timestamps %s", (val > 0) ? "enabled" : "disabled");
}

if (gutil_parse_int(getenv("GUTIL_LOG_TID"), 0, &val) && val > 0) {
gutil_log_tid = (val > 0);
GDEBUG("Thread id prefix %s", (val > 0) ? "enabled" : "disabled");
}
}
#endif

/*
* Local Variables:
* mode: C
Expand Down

0 comments on commit c707a15

Please sign in to comment.