AiCPlayer
Interface of aic vm - for rendering aspect, sensors, video records
logger.c
Go to the documentation of this file.
1 
5 #include <stdarg.h> // for va_list
6 #include <stdio.h> // for NULL
7 #include <string.h> // for memset
8 #include <time.h> // for localtime, time, time_t
9 
10 #include "logger.h"
11 
12 void _log_wrap(GLogLevelFlags level, const char* fmt, ...)
13 {
14  va_list va;
15  va_start(va, fmt);
16  g_logv("player", level, fmt, va);
17  va_end(va);
18 }
19 
20 static const gchar* log_level_to_string(GLogLevelFlags level)
21 {
22  switch (level)
23  {
24  case G_LOG_LEVEL_ERROR:
25  return "ERROR";
26  case G_LOG_LEVEL_CRITICAL:
27  return "CRITICAL";
28  case G_LOG_LEVEL_WARNING:
29  return "WARNING";
30  case G_LOG_LEVEL_MESSAGE:
31  return "MESSAGE";
32  case G_LOG_LEVEL_INFO:
33  return "INFO";
34  case G_LOG_LEVEL_DEBUG:
35  return "DEBUG";
36  default:
37  return "UNKNOWN";
38  }
39 }
40 
41 static void log_handler_cb(const gchar* log_domain, GLogLevelFlags log_level, const gchar* message,
42  gpointer user_data)
43 {
44  (void) log_domain;
45  (void) user_data;
46  const gchar* log_level_str;
47 
48  int debug_enabled = 1;
49 
50  /* Ignore debug messages if disabled. */
51  if (!debug_enabled && (log_level & G_LOG_LEVEL_DEBUG))
52  {
53  return;
54  }
55 
56  log_level_str = log_level_to_string(log_level & G_LOG_LEVEL_MASK);
57 
58  time_t raw_time;
59  time(&raw_time);
60  struct tm info;
61  localtime_r(&raw_time, &info);
62  char time[30];
63  memset(time, 0, 30);
64  strftime(time, 30, "%H:%M:%S", &info);
65  /* Use g_printerr() for warnings and g_print() otherwise. */
66  if (log_level <= G_LOG_LEVEL_WARNING)
67  {
68  g_printerr("%s %8s %s\n", time, log_level_str, message);
69  }
70  else
71  {
72  g_print("%s %8s %s\n", time, log_level_str, message);
73  }
74 }
75 
77 {
78  g_log_set_handler("player", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
79  log_handler_cb, NULL);
80 }
void init_logger()
Definition: logger.c:76
Logging macros.
void _log_wrap(GLogLevelFlags level, const char *fmt,...)
Definition: logger.c:12