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