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 * Logging facility for debug/info messages.
32 * _EGL_FATAL messages are printed to stderr
33 * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
34 */
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include "c11/threads.h"
41 #include "util/macros.h"
42 #include "util/simple_mtx.h"
43 #include "util/u_string.h"
44
45 #include "egllog.h"
46
47 #ifdef HAVE_ANDROID_PLATFORM
48 #define LOG_TAG "EGL-MAIN"
49 #if ANDROID_API_LEVEL >= 26
50 #include <log/log.h>
51 #else
52 #include <cutils/log.h>
53 #endif /* use log/log.h start from android 8 major version */
54
55 #endif /* HAVE_ANDROID_PLATFORM */
56
57 #define MAXSTRING 1000
58 #define FALLBACK_LOG_LEVEL _EGL_WARNING
59
60 static struct {
61 simple_mtx_t mutex;
62
63 EGLBoolean initialized;
64 EGLint level;
65 } logging = {
66 .mutex = SIMPLE_MTX_INITIALIZER,
67 .initialized = EGL_FALSE,
68 .level = FALLBACK_LOG_LEVEL,
69 };
70
71 static const char *level_strings[] = {
72 [_EGL_FATAL] = "fatal",
73 [_EGL_WARNING] = "warning",
74 [_EGL_INFO] = "info",
75 [_EGL_DEBUG] = "debug",
76 };
77
78 /**
79 * The default logger. It prints the message to stderr.
80 */
81 static void
_eglDefaultLogger(EGLint level,const char * msg)82 _eglDefaultLogger(EGLint level, const char *msg)
83 {
84 #ifdef HAVE_ANDROID_PLATFORM
85 static const int egl2alog[] = {
86 [_EGL_FATAL] = ANDROID_LOG_ERROR,
87 [_EGL_WARNING] = ANDROID_LOG_WARN,
88 [_EGL_INFO] = ANDROID_LOG_INFO,
89 [_EGL_DEBUG] = ANDROID_LOG_DEBUG,
90 };
91 LOG_PRI(egl2alog[level], LOG_TAG, "%s", msg);
92 #else
93 fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
94 #endif /* HAVE_ANDROID_PLATFORM */
95 }
96
97 /**
98 * Initialize the logging facility.
99 */
100 static void
_eglInitLogger(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 * Return the log level.
134 */
135 EGLint
_eglGetLogLevel(void)136 _eglGetLogLevel(void)
137 {
138 return logging.level;
139 }
140
141 /**
142 * Log a message with message logger.
143 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
144 */
145 void
_eglLog(EGLint level,const char * fmtStr,...)146 _eglLog(EGLint level, const char *fmtStr, ...)
147 {
148 va_list args;
149 char msg[MAXSTRING];
150 int ret;
151
152 /* one-time initialization; a little race here is fine */
153 if (!logging.initialized)
154 _eglInitLogger();
155 if (level > logging.level || level < 0)
156 return;
157
158 simple_mtx_lock(&logging.mutex);
159
160 va_start(args, fmtStr);
161 ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
162 if (ret < 0 || ret >= MAXSTRING)
163 strcpy(msg, "<message truncated>");
164 va_end(args);
165
166 _eglDefaultLogger(level, msg);
167
168 simple_mtx_unlock(&logging.mutex);
169
170 if (level == _EGL_FATAL)
171 exit(1); /* or abort()? */
172 }
173