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