aboutsummaryrefslogtreecommitdiffstats
path: root/src/egl/main/egllog.c
blob: 23eb523eebe462e776a5cdbe03740fc1e7341228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
 * Logging facility for debug/info messages.
 * _EGL_FATAL messages are printed to stderr
 * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
 */


#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "egllog.h"

#define MAXSTRING 1000
#define FALLBACK_LOG_LEVEL      _EGL_WARNING
#define FALLBACK_LOG_LEVEL_STR  "warning"

static EGLint ReportingLevel = -1;


static void
log_level_initialize(void)
{
   char *log_env = getenv("EGL_LOG_LEVEL");

   if (log_env == NULL) {
      ReportingLevel = FALLBACK_LOG_LEVEL;
   }
   else if (strcasecmp(log_env, "fatal") == 0) {
      ReportingLevel = _EGL_FATAL;
   }
   else if (strcasecmp(log_env, "warning") == 0) {
      ReportingLevel = _EGL_WARNING;
   }
   else if (strcasecmp(log_env, "info") == 0) {
      ReportingLevel = _EGL_INFO;
   }
   else if (strcasecmp(log_env, "debug") == 0) {
      ReportingLevel = _EGL_DEBUG;
   }
   else {
      fprintf(stderr, "Unrecognized EGL_LOG_LEVEL environment variable value. "
              "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
              "Got \"%s\". Falling back to \"%s\".\n",
              log_env, FALLBACK_LOG_LEVEL_STR);
      ReportingLevel = FALLBACK_LOG_LEVEL;
   }
}


/**
 * Log a message to stderr.
 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
 */
void
_eglLog(EGLint level, const char *fmtStr, ...)
{
   va_list args;
   char msg[MAXSTRING];
   const char *levelStr;
   static int log_level_initialized = 0;

   if (!log_level_initialized) {
      log_level_initialize();
      log_level_initialized = 1;
   }

   if (level <= ReportingLevel) {
      switch (level) {
      case _EGL_FATAL:
         levelStr = "Fatal";
         break;
      case _EGL_WARNING:
         levelStr = "Warning";
         break;
      case _EGL_INFO:
         levelStr = "Info";
         break;
      case _EGL_DEBUG:
         levelStr = "Debug";
         break;
      default:
         levelStr = "";
      }

      va_start(args, fmtStr);
      vsnprintf(msg, MAXSTRING, fmtStr, args);
      va_end(args);

      fprintf(stderr, "libEGL %s: %s\n", levelStr, msg);

      if (level == _EGL_FATAL) {
         exit(1); /* or abort()? */
      }
   }
}