egl: fix android logger compilation
[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 #endif /* HAVE_ANDROID_PLATFORM */
53
54 #define MAXSTRING 1000
55 #define FALLBACK_LOG_LEVEL _EGL_WARNING
56
57
58 static struct {
59 mtx_t mutex;
60
61 EGLBoolean initialized;
62 EGLint level;
63 } logging = {
64 .mutex = _MTX_INITIALIZER_NP,
65 .initialized = EGL_FALSE,
66 .level = FALLBACK_LOG_LEVEL,
67 };
68
69 static const char *level_strings[] = {
70 [_EGL_FATAL] = "fatal",
71 [_EGL_WARNING] = "warning",
72 [_EGL_INFO] = "info",
73 [_EGL_DEBUG] = "debug",
74 };
75
76
77 /**
78 * The default logger. It prints the message to stderr.
79 */
80 static void
81 _eglDefaultLogger(EGLint level, const char *msg)
82 {
83 #ifdef HAVE_ANDROID_PLATFORM
84 static const int egl2alog[] = {
85 [_EGL_FATAL] = ANDROID_LOG_ERROR,
86 [_EGL_WARNING] = ANDROID_LOG_WARN,
87 [_EGL_INFO] = ANDROID_LOG_INFO,
88 [_EGL_DEBUG] = ANDROID_LOG_DEBUG,
89 };
90 LOG_PRI(egl2alog[level], LOG_TAG, "%s", msg);
91 #else
92 fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
93 #endif /* HAVE_ANDROID_PLATFORM */
94 }
95
96
97 /**
98 * Initialize the logging facility.
99 */
100 static void
101 _eglInitLogger(void)
102 {
103 const char *log_env;
104 EGLint i, level = -1;
105
106 if (logging.initialized)
107 return;
108
109 log_env = getenv("EGL_LOG_LEVEL");
110 if (log_env) {
111 for (i = 0; i < ARRAY_SIZE(level_strings); i++) {
112 if (strcasecmp(log_env, level_strings[i]) == 0) {
113 level = i;
114 break;
115 }
116 }
117 }
118
119 logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;
120 logging.initialized = EGL_TRUE;
121
122 /* it is fine to call _eglLog now */
123 if (log_env && level < 0) {
124 _eglLog(_EGL_WARNING,
125 "Unrecognized EGL_LOG_LEVEL environment variable value. "
126 "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
127 "Got \"%s\". Falling back to \"%s\".",
128 log_env, level_strings[FALLBACK_LOG_LEVEL]);
129 }
130 }
131
132
133 /**
134 * Log a message with message logger.
135 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
136 */
137 void
138 _eglLog(EGLint level, const char *fmtStr, ...)
139 {
140 va_list args;
141 char msg[MAXSTRING];
142 int ret;
143
144 /* one-time initialization; a little race here is fine */
145 if (!logging.initialized)
146 _eglInitLogger();
147 if (level > logging.level || level < 0)
148 return;
149
150 mtx_lock(&logging.mutex);
151
152 va_start(args, fmtStr);
153 ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
154 if (ret < 0 || ret >= MAXSTRING)
155 strcpy(msg, "<message truncated>");
156 va_end(args);
157
158 _eglDefaultLogger(level, msg);
159
160 mtx_unlock(&logging.mutex);
161
162 if (level == _EGL_FATAL)
163 exit(1); /* or abort()? */
164 }