Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / mesa / src / egl / main / egllog.c
blob7a99854ec0c308ebc83595699c9e5720d730d284
1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010 LunarG, Inc.
6 * All Rights Reserved.
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
18 * of the Software.
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 **************************************************************************/
31 /**
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.
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
43 #include "egllog.h"
44 #include "eglstring.h"
45 #include "eglmutex.h"
47 #define MAXSTRING 1000
48 #define FALLBACK_LOG_LEVEL _EGL_WARNING
51 static struct {
52 _EGLMutex mutex;
54 EGLBoolean initialized;
55 EGLint level;
56 _EGLLogProc logger;
57 EGLint num_messages;
58 } logging = {
59 #if !defined(_EGL_OS_AROS)
60 _EGL_MUTEX_INITIALIZER,
61 EGL_FALSE,
62 FALLBACK_LOG_LEVEL,
63 NULL,
65 #else
66 .initialized = EGL_FALSE,
67 .level = FALLBACK_LOG_LEVEL,
68 .logger = NULL,
69 .num_messages = 0
70 #endif
73 static const char *level_strings[] = {
74 /* the order is important */
75 "fatal",
76 "warning",
77 "info",
78 "debug",
79 NULL
83 /**
84 * Set the function to be called when there is a message to log.
85 * Note that the function will be called with an internal lock held.
86 * Recursive logging is not allowed.
88 void
89 _eglSetLogProc(_EGLLogProc logger)
91 EGLint num_messages = 0;
93 _eglLockMutex(&logging.mutex);
95 if (logging.logger != logger) {
96 logging.logger = logger;
98 num_messages = logging.num_messages;
99 logging.num_messages = 0;
102 _eglUnlockMutex(&logging.mutex);
104 if (num_messages)
105 _eglLog(_EGL_DEBUG,
106 "New logger installed. "
107 "Messages before the new logger might not be available.");
112 * Set the log reporting level.
114 void
115 _eglSetLogLevel(EGLint level)
117 switch (level) {
118 case _EGL_FATAL:
119 case _EGL_WARNING:
120 case _EGL_INFO:
121 case _EGL_DEBUG:
122 _eglLockMutex(&logging.mutex);
123 logging.level = level;
124 _eglUnlockMutex(&logging.mutex);
125 break;
126 default:
127 break;
132 #if defined(_EGL_OS_AROS)
133 #include <aros/debug.h>
134 #endif
136 * The default logger. It prints the message to stderr.
138 static void
139 _eglDefaultLogger(EGLint level, const char *msg)
141 #if !defined(_EGL_OS_AROS)
142 fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
143 #else
144 bug("[EGL]: %s: %s\n", level_strings[level], msg);
145 #endif
150 * Initialize the logging facility.
152 static void
153 _eglInitLogger(void)
155 const char *log_env;
156 EGLint i, level = -1;
158 if (logging.initialized)
159 return;
161 log_env = getenv("EGL_LOG_LEVEL");
162 if (log_env) {
163 for (i = 0; level_strings[i]; i++) {
164 if (_eglstrcasecmp(log_env, level_strings[i]) == 0) {
165 level = i;
166 break;
170 else {
171 level = FALLBACK_LOG_LEVEL;
174 logging.logger = _eglDefaultLogger;
175 logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;
176 logging.initialized = EGL_TRUE;
177 #if defined(_EGL_OS_AROS)
178 _eglInitMutex(&logging.mutex);
179 #endif
181 /* it is fine to call _eglLog now */
182 if (log_env && level < 0) {
183 _eglLog(_EGL_WARNING,
184 "Unrecognized EGL_LOG_LEVEL environment variable value. "
185 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
186 "Got \"%s\". Falling back to \"%s\".",
187 log_env, level_strings[FALLBACK_LOG_LEVEL]);
193 * Log a message with message logger.
194 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
196 void
197 _eglLog(EGLint level, const char *fmtStr, ...)
199 va_list args;
200 char msg[MAXSTRING];
201 int ret;
203 /* one-time initialization; a little race here is fine */
204 if (!logging.initialized)
205 _eglInitLogger();
206 if (level > logging.level || level < 0)
207 return;
209 _eglLockMutex(&logging.mutex);
211 if (logging.logger) {
212 va_start(args, fmtStr);
213 ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
214 if (ret < 0 || ret >= MAXSTRING)
215 strcpy(msg, "<message truncated>");
216 va_end(args);
218 logging.logger(level, msg);
219 logging.num_messages++;
222 _eglUnlockMutex(&logging.mutex);
224 if (level == _EGL_FATAL)
225 exit(1); /* or abort()? */