/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* Copyright 2009-2010 Chia-I Wu <[email protected]>4* Copyright 2010 LunarG, Inc.5* All Rights Reserved.6*7* Permission is hereby granted, free of charge, to any person obtaining a8* copy of this software and associated documentation files (the9* "Software"), to deal in the Software without restriction, including10* without limitation the rights to use, copy, modify, merge, publish,11* distribute, sub license, and/or sell copies of the Software, and to12* permit persons to whom the Software is furnished to do so, subject to13* the following conditions:14*15* The above copyright notice and this permission notice (including the16* next paragraph) shall be included in all copies or substantial portions17* of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR20* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,21* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL22* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER23* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING24* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER25* DEALINGS IN THE SOFTWARE.26*27**************************************************************************/282930/**31* Logging facility for debug/info messages.32* _EGL_FATAL messages are printed to stderr33* The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.34*/353637#include <stdarg.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <strings.h>42#include "c11/threads.h"43#include "util/macros.h"44#include "util/u_string.h"4546#include "egllog.h"4748#ifdef HAVE_ANDROID_PLATFORM49#define LOG_TAG "EGL-MAIN"50#if ANDROID_API_LEVEL >= 2651#include <log/log.h>52#else53#include <cutils/log.h>54#endif /* use log/log.h start from android 8 major version */5556#endif /* HAVE_ANDROID_PLATFORM */5758#define MAXSTRING 100059#define FALLBACK_LOG_LEVEL _EGL_WARNING606162static struct {63mtx_t mutex;6465EGLBoolean initialized;66EGLint level;67} logging = {68.mutex = _MTX_INITIALIZER_NP,69.initialized = EGL_FALSE,70.level = FALLBACK_LOG_LEVEL,71};7273static const char *level_strings[] = {74[_EGL_FATAL] = "fatal",75[_EGL_WARNING] = "warning",76[_EGL_INFO] = "info",77[_EGL_DEBUG] = "debug",78};798081/**82* The default logger. It prints the message to stderr.83*/84static void85_eglDefaultLogger(EGLint level, const char *msg)86{87#ifdef HAVE_ANDROID_PLATFORM88static const int egl2alog[] = {89[_EGL_FATAL] = ANDROID_LOG_ERROR,90[_EGL_WARNING] = ANDROID_LOG_WARN,91[_EGL_INFO] = ANDROID_LOG_INFO,92[_EGL_DEBUG] = ANDROID_LOG_DEBUG,93};94LOG_PRI(egl2alog[level], LOG_TAG, "%s", msg);95#else96fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);97#endif /* HAVE_ANDROID_PLATFORM */98}99100101/**102* Initialize the logging facility.103*/104static void105_eglInitLogger(void)106{107const char *log_env;108EGLint i, level = -1;109110if (logging.initialized)111return;112113log_env = getenv("EGL_LOG_LEVEL");114if (log_env) {115for (i = 0; i < ARRAY_SIZE(level_strings); i++) {116if (strcasecmp(log_env, level_strings[i]) == 0) {117level = i;118break;119}120}121}122123logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;124logging.initialized = EGL_TRUE;125126/* it is fine to call _eglLog now */127if (log_env && level < 0) {128_eglLog(_EGL_WARNING,129"Unrecognized EGL_LOG_LEVEL environment variable value. "130"Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "131"Got \"%s\". Falling back to \"%s\".",132log_env, level_strings[FALLBACK_LOG_LEVEL]);133}134}135136137/**138* Log a message with message logger.139* \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.140*/141void142_eglLog(EGLint level, const char *fmtStr, ...)143{144va_list args;145char msg[MAXSTRING];146int ret;147148/* one-time initialization; a little race here is fine */149if (!logging.initialized)150_eglInitLogger();151if (level > logging.level || level < 0)152return;153154mtx_lock(&logging.mutex);155156va_start(args, fmtStr);157ret = vsnprintf(msg, MAXSTRING, fmtStr, args);158if (ret < 0 || ret >= MAXSTRING)159strcpy(msg, "<message truncated>");160va_end(args);161162_eglDefaultLogger(level, msg);163164mtx_unlock(&logging.mutex);165166if (level == _EGL_FATAL)167exit(1); /* or abort()? */168}169170171