2 * Logging facility for debug/info messages.
3 * _EGL_FATAL messages are printed to stderr
4 * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
16 #define MAXSTRING 1000
17 #define FALLBACK_LOG_LEVEL _EGL_WARNING
23 EGLBoolean initialized
;
28 _EGL_MUTEX_INITIALIZER
,
35 static const char *level_strings
[] = {
36 /* the order is important */
46 * Set the function to be called when there is a message to log.
47 * Note that the function will be called with an internal lock held.
48 * Recursive logging is not allowed.
51 _eglSetLogProc(_EGLLogProc logger
)
53 EGLint num_messages
= 0;
55 _eglLockMutex(&logging
.mutex
);
57 if (logging
.logger
!= logger
) {
58 logging
.logger
= logger
;
60 num_messages
= logging
.num_messages
;
61 logging
.num_messages
= 0;
64 _eglUnlockMutex(&logging
.mutex
);
68 "New logger installed. "
69 "Messages before the new logger might not be available.");
74 * Set the log reporting level.
77 _eglSetLogLevel(EGLint level
)
84 _eglLockMutex(&logging
.mutex
);
85 logging
.level
= level
;
86 _eglUnlockMutex(&logging
.mutex
);
95 * The default logger. It prints the message to stderr.
98 _eglDefaultLogger(EGLint level
, const char *msg
)
100 fprintf(stderr
, "libEGL %s: %s\n", level_strings
[level
], msg
);
105 * Initialize the logging facility.
111 EGLint i
, level
= -1;
113 if (logging
.initialized
)
116 log_env
= getenv("EGL_LOG_LEVEL");
118 for (i
= 0; level_strings
[i
]; i
++) {
119 if (strcasecmp(log_env
, level_strings
[i
]) == 0) {
126 level
= FALLBACK_LOG_LEVEL
;
129 logging
.logger
= _eglDefaultLogger
;
130 logging
.level
= (level
>= 0) ? level
: FALLBACK_LOG_LEVEL
;
131 logging
.initialized
= EGL_TRUE
;
133 /* it is fine to call _eglLog now */
134 if (log_env
&& level
< 0) {
135 _eglLog(_EGL_WARNING
,
136 "Unrecognized EGL_LOG_LEVEL environment variable value. "
137 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
138 "Got \"%s\". Falling back to \"%s\".",
139 log_env
, level_strings
[FALLBACK_LOG_LEVEL
]);
145 * Log a message with message logger.
146 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
149 _eglLog(EGLint level
, const char *fmtStr
, ...)
154 /* one-time initialization; a little race here is fine */
155 if (!logging
.initialized
)
157 if (level
> logging
.level
|| level
< 0)
160 _eglLockMutex(&logging
.mutex
);
162 if (logging
.logger
) {
163 va_start(args
, fmtStr
);
164 vsnprintf(msg
, MAXSTRING
, fmtStr
, args
);
167 logging
.logger(level
, msg
);
168 logging
.num_messages
++;
171 _eglUnlockMutex(&logging
.mutex
);
173 if (level
== _EGL_FATAL
)
174 exit(1); /* or abort()? */