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.
14 #include "eglstring.h"
17 #define MAXSTRING 1000
18 #define FALLBACK_LOG_LEVEL _EGL_WARNING
24 EGLBoolean initialized
;
29 _EGL_MUTEX_INITIALIZER
,
36 static const char *level_strings
[] = {
37 /* the order is important */
47 * Set the function to be called when there is a message to log.
48 * Note that the function will be called with an internal lock held.
49 * Recursive logging is not allowed.
52 _eglSetLogProc(_EGLLogProc logger
)
54 EGLint num_messages
= 0;
56 _eglLockMutex(&logging
.mutex
);
58 if (logging
.logger
!= logger
) {
59 logging
.logger
= logger
;
61 num_messages
= logging
.num_messages
;
62 logging
.num_messages
= 0;
65 _eglUnlockMutex(&logging
.mutex
);
69 "New logger installed. "
70 "Messages before the new logger might not be available.");
75 * Set the log reporting level.
78 _eglSetLogLevel(EGLint level
)
85 _eglLockMutex(&logging
.mutex
);
86 logging
.level
= level
;
87 _eglUnlockMutex(&logging
.mutex
);
96 * The default logger. It prints the message to stderr.
99 _eglDefaultLogger(EGLint level
, const char *msg
)
101 fprintf(stderr
, "libEGL %s: %s\n", level_strings
[level
], msg
);
106 * Initialize the logging facility.
112 EGLint i
, level
= -1;
114 if (logging
.initialized
)
117 log_env
= getenv("EGL_LOG_LEVEL");
119 for (i
= 0; level_strings
[i
]; i
++) {
120 if (_eglstrcasecmp(log_env
, level_strings
[i
]) == 0) {
127 level
= FALLBACK_LOG_LEVEL
;
130 logging
.logger
= _eglDefaultLogger
;
131 logging
.level
= (level
>= 0) ? level
: FALLBACK_LOG_LEVEL
;
132 logging
.initialized
= EGL_TRUE
;
134 /* it is fine to call _eglLog now */
135 if (log_env
&& level
< 0) {
136 _eglLog(_EGL_WARNING
,
137 "Unrecognized EGL_LOG_LEVEL environment variable value. "
138 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
139 "Got \"%s\". Falling back to \"%s\".",
140 log_env
, level_strings
[FALLBACK_LOG_LEVEL
]);
146 * Log a message with message logger.
147 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
150 _eglLog(EGLint level
, const char *fmtStr
, ...)
156 /* one-time initialization; a little race here is fine */
157 if (!logging
.initialized
)
159 if (level
> logging
.level
|| level
< 0)
162 _eglLockMutex(&logging
.mutex
);
164 if (logging
.logger
) {
165 va_start(args
, fmtStr
);
166 ret
= vsnprintf(msg
, MAXSTRING
, fmtStr
, args
);
167 if (ret
< 0 || ret
>= MAXSTRING
)
168 strcpy(msg
, "<message truncated>");
171 logging
.logger(level
, msg
);
172 logging
.num_messages
++;
175 _eglUnlockMutex(&logging
.mutex
);
177 if (level
== _EGL_FATAL
)
178 exit(1); /* or abort()? */