1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010 LunarG, Inc.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
28 **************************************************************************/
32 * Logging facility for debug/info messages.
33 * _EGL_FATAL messages are printed to stderr
34 * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
43 #include "c11/threads.h"
47 #ifdef HAVE_ANDROID_PLATFORM
48 #define LOG_TAG "EGL-MAIN"
49 #include <cutils/log.h>
51 /* support versions < JellyBean */
62 #endif /* HAVE_ANDROID_PLATFORM */
64 #define MAXSTRING 1000
65 #define FALLBACK_LOG_LEVEL _EGL_WARNING
71 EGLBoolean initialized
;
83 static const char *level_strings
[] = {
84 /* the order is important */
94 * Set the function to be called when there is a message to log.
95 * Note that the function will be called with an internal lock held.
96 * Recursive logging is not allowed.
99 _eglSetLogProc(_EGLLogProc logger
)
101 EGLint num_messages
= 0;
103 mtx_lock(&logging
.mutex
);
105 if (logging
.logger
!= logger
) {
106 logging
.logger
= logger
;
108 num_messages
= logging
.num_messages
;
109 logging
.num_messages
= 0;
112 mtx_unlock(&logging
.mutex
);
116 "New logger installed. "
117 "Messages before the new logger might not be available.");
122 * The default logger. It prints the message to stderr.
125 _eglDefaultLogger(EGLint level
, const char *msg
)
127 #ifdef HAVE_ANDROID_PLATFORM
139 LOG_FATAL("%s", msg
);
145 fprintf(stderr
, "libEGL %s: %s\n", level_strings
[level
], msg
);
146 #endif /* HAVE_ANDROID_PLATFORM */
151 * Initialize the logging facility.
157 EGLint i
, level
= -1;
159 if (logging
.initialized
)
162 log_env
= getenv("EGL_LOG_LEVEL");
164 for (i
= 0; level_strings
[i
]; i
++) {
165 if (strcasecmp(log_env
, level_strings
[i
]) == 0) {
172 level
= FALLBACK_LOG_LEVEL
;
175 logging
.logger
= _eglDefaultLogger
;
176 logging
.level
= (level
>= 0) ? level
: FALLBACK_LOG_LEVEL
;
177 logging
.initialized
= EGL_TRUE
;
179 /* it is fine to call _eglLog now */
180 if (log_env
&& level
< 0) {
181 _eglLog(_EGL_WARNING
,
182 "Unrecognized EGL_LOG_LEVEL environment variable value. "
183 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
184 "Got \"%s\". Falling back to \"%s\".",
185 log_env
, level_strings
[FALLBACK_LOG_LEVEL
]);
191 * Log a message with message logger.
192 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
195 _eglLog(EGLint level
, const char *fmtStr
, ...)
201 /* one-time initialization; a little race here is fine */
202 if (!logging
.initialized
)
204 if (level
> logging
.level
|| level
< 0)
207 mtx_lock(&logging
.mutex
);
209 if (logging
.logger
) {
210 va_start(args
, fmtStr
);
211 ret
= vsnprintf(msg
, MAXSTRING
, fmtStr
, args
);
212 if (ret
< 0 || ret
>= MAXSTRING
)
213 strcpy(msg
, "<message truncated>");
216 logging
.logger(level
, msg
);
217 logging
.num_messages
++;
220 mtx_unlock(&logging
.mutex
);
222 if (level
== _EGL_FATAL
)
223 exit(1); /* or abort()? */