summaryrefslogtreecommitdiff
path: root/src/egl/main/egllog.c
blob: 00340e940cd46b0a9102d89db74ab0ef9c447ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**************************************************************************
 *
 * Copyright 2008 VMware, Inc.
 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
 * Copyright 2010 LunarG, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/


/**
 * Logging facility for debug/info messages.
 * _EGL_FATAL messages are printed to stderr
 * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
 */


#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "c11/threads.h"
#include "util/macros.h"

#include "egllog.h"
#ifdef HAVE_TIZEN_PLATFORM
#define LOG_TAG "MESA"
#include <dlog.h>
#endif

#ifdef HAVE_ANDROID_PLATFORM
#define LOG_TAG "EGL-MAIN"
#if ANDROID_API_LEVEL >= 26
#include <log/log.h>
#else
#include <cutils/log.h>
#endif /* use log/log.h start from android 8 major version */

#endif /* HAVE_ANDROID_PLATFORM */

#define MAXSTRING 1000
#define FALLBACK_LOG_LEVEL _EGL_FATAL


static struct {
   mtx_t mutex;

   EGLBoolean initialized;
   EGLint level;
} logging = {
   .mutex = _MTX_INITIALIZER_NP,
   .initialized = EGL_FALSE,
   .level = FALLBACK_LOG_LEVEL,
};

static const char *level_strings[] = {
   [_EGL_FATAL] = "fatal",
   [_EGL_WARNING]  = "warning",
   [_EGL_INFO] = "info",
   [_EGL_DEBUG] = "debug",
};


#ifndef HAVE_TIZEN_PLATFORM
/**
 * The default logger.  It prints the message to stderr.
 */
static void
_eglDefaultLogger(EGLint level, const char *msg)
{
#ifdef HAVE_ANDROID_PLATFORM
   static const int egl2alog[] = {
      [_EGL_FATAL] = ANDROID_LOG_ERROR,
      [_EGL_WARNING]  = ANDROID_LOG_WARN,
      [_EGL_INFO] = ANDROID_LOG_INFO,
      [_EGL_DEBUG] = ANDROID_LOG_DEBUG,
   };
   LOG_PRI(egl2alog[level], LOG_TAG, "%s", msg);
#else
   fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
#endif /* HAVE_ANDROID_PLATFORM */
}
#endif


/**
 * Initialize the logging facility.
 */
static void
_eglInitLogger(void)
{
   const char *log_env;
   EGLint i, level = -1;

   if (logging.initialized)
      return;

   log_env = getenv("EGL_LOG_LEVEL");
   if (log_env) {
      for (i = 0; i < ARRAY_SIZE(level_strings); i++) {
         if (strcasecmp(log_env, level_strings[i]) == 0) {
            level = i;
            break;
         }
      }
   }

   logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;
   logging.initialized = EGL_TRUE;

   /* it is fine to call _eglLog now */
   if (log_env && level < 0) {
      _eglLog(_EGL_WARNING,
              "Unrecognized EGL_LOG_LEVEL environment variable value. "
              "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
              "Got \"%s\". Falling back to \"%s\".",
              log_env, level_strings[FALLBACK_LOG_LEVEL]);
   }
}


/**
 * Log a message with message logger.
 * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
 */
void
_eglLog(EGLint level, const char *fmtStr, ...)
{
   va_list args;
#ifndef HAVE_TIZEN_PLATFORM
   char msg[MAXSTRING];
   int ret;
#endif

   /* one-time initialization; a little race here is fine */
   if (!logging.initialized)
      _eglInitLogger();
   if (level > logging.level || level < 0)
      return;

   mtx_lock(&logging.mutex);

#ifdef HAVE_TIZEN_PLATFORM
   log_priority dlog_prio;

   switch (level) {
   case _EGL_FATAL:
      dlog_prio = DLOG_ERROR;
      break;
   case _EGL_WARNING:
      dlog_prio = DLOG_WARN;
      break;
   case _EGL_INFO:
      dlog_prio = DLOG_INFO;
      break;
   case _EGL_DEBUG:
      dlog_prio = DLOG_DEBUG;
      break;
   default:
      mtx_unlock(&logging.mutex);
      return;
   }
#endif

   va_start(args, fmtStr);
#ifdef HAVE_TIZEN_PLATFORM
   __dlog_vprint(LOG_ID_SYSTEM, dlog_prio, LOG_TAG, fmtStr, args);
#else
   ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
   if (ret < 0 || ret >= MAXSTRING)
      strcpy(msg, "<message truncated>");
#endif
   va_end(args);

#ifndef HAVE_TIZEN_PLATFORM
   _eglDefaultLogger(level, msg);
#endif

   mtx_unlock(&logging.mutex);

   if (level == _EGL_FATAL)
      exit(1); /* or abort()? */
}