egl: remove unneeded else statement in _eglInitLogger
[mesa.git] / src / egl / main / egllog.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010 LunarG, Inc.
6 * All Rights Reserved.
7 *
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:
15 *
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.
19 *
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.
27 *
28 **************************************************************************/
29
30
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.
35 */
36
37
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <strings.h>
43 #include "c11/threads.h"
44
45 #include "egllog.h"
46
47 #ifdef HAVE_ANDROID_PLATFORM
48 #define LOG_TAG "EGL-MAIN"
49 #include <cutils/log.h>
50
51 /* support versions < JellyBean */
52 #ifndef ALOGW
53 #define ALOGW LOGW
54 #endif
55 #ifndef ALOGD
56 #define ALOGD LOGD
57 #endif
58 #ifndef ALOGI
59 #define ALOGI LOGI
60 #endif
61
62 #endif /* HAVE_ANDROID_PLATFORM */
63
64 #define MAXSTRING 1000
65 #define FALLBACK_LOG_LEVEL _EGL_WARNING
66
67
68 static struct {
69 mtx_t mutex;
70
71 EGLBoolean initialized;
72 EGLint level;
73 } logging = {
74 _MTX_INITIALIZER_NP,
75 EGL_FALSE,
76 FALLBACK_LOG_LEVEL,
77 };
78
79 static const char *level_strings[] = {
80 /* the order is important */
81 "fatal",
82 "warning",
83 "info",
84 "debug",
85 NULL
86 };
87
88
89 /**
90 * The default logger. It prints the message to stderr.
91 */
92 static void
93 _eglDefaultLogger(EGLint level, const char *msg)
94 {
95 #ifdef HAVE_ANDROID_PLATFORM
96 switch (level) {
97 case _EGL_DEBUG:
98 ALOGD("%s", msg);
99 break;
100 case _EGL_INFO:
101 ALOGI("%s", msg);
102 break;
103 case _EGL_WARNING:
104 ALOGW("%s", msg);
105 break;
106 case _EGL_FATAL:
107 LOG_FATAL("%s", msg);
108 break;
109 default:
110 break;
111 }
112 #else
113 fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
114 #endif /* HAVE_ANDROID_PLATFORM */
115 }
116
117
118 /**
119 * Initialize the logging facility.
120 */
121 static void
122 _eglInitLogger(void)
123 {
124 const char *log_env;
125 EGLint i, level = -1;
126
127 if (logging.initialized)
128 return;
129
130 log_env = getenv("EGL_LOG_LEVEL");
131 if (log_env) {
132 for (i = 0; level_strings[i]; i++) {
133 if (strcasecmp(log_env, level_strings[i]) == 0) {
134 level = i;
135 break;
136 }
137 }
138 }
139
140 logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;
141 logging.initialized = EGL_TRUE;
142
143 /* it is fine to call _eglLog now */
144 if (log_env && level < 0) {
145 _eglLog(_EGL_WARNING,
146 "Unrecognized EGL_LOG_LEVEL environment variable value. "
147 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
148 "Got \"%s\". Falling back to \"%s\".",
149 log_env, level_strings[FALLBACK_LOG_LEVEL]);
150 }
151 }
152
153
154 /**
155 * Log a message with message logger.
156 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
157 */
158 void
159 _eglLog(EGLint level, const char *fmtStr, ...)
160 {
161 va_list args;
162 char msg[MAXSTRING];
163 int ret;
164
165 /* one-time initialization; a little race here is fine */
166 if (!logging.initialized)
167 _eglInitLogger();
168 if (level > logging.level || level < 0)
169 return;
170
171 mtx_lock(&logging.mutex);
172
173 va_start(args, fmtStr);
174 ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
175 if (ret < 0 || ret >= MAXSTRING)
176 strcpy(msg, "<message truncated>");
177 va_end(args);
178
179 _eglDefaultLogger(level, msg);
180
181 mtx_unlock(&logging.mutex);
182
183 if (level == _EGL_FATAL)
184 exit(1); /* or abort()? */
185 }