13464ebd5Sriastradh/************************************************************************** 23464ebd5Sriastradh * 3af69d88dSmrg * Copyright 2008 VMware, Inc. 43464ebd5Sriastradh * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com> 53464ebd5Sriastradh * Copyright 2010 LunarG, Inc. 63464ebd5Sriastradh * All Rights Reserved. 73464ebd5Sriastradh * 83464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a 93464ebd5Sriastradh * copy of this software and associated documentation files (the 103464ebd5Sriastradh * "Software"), to deal in the Software without restriction, including 113464ebd5Sriastradh * without limitation the rights to use, copy, modify, merge, publish, 123464ebd5Sriastradh * distribute, sub license, and/or sell copies of the Software, and to 133464ebd5Sriastradh * permit persons to whom the Software is furnished to do so, subject to 143464ebd5Sriastradh * the following conditions: 153464ebd5Sriastradh * 163464ebd5Sriastradh * The above copyright notice and this permission notice (including the 173464ebd5Sriastradh * next paragraph) shall be included in all copies or substantial portions 183464ebd5Sriastradh * of the Software. 193464ebd5Sriastradh * 203464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 213464ebd5Sriastradh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 223464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 233464ebd5Sriastradh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 243464ebd5Sriastradh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 253464ebd5Sriastradh * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 263464ebd5Sriastradh * DEALINGS IN THE SOFTWARE. 273464ebd5Sriastradh * 283464ebd5Sriastradh **************************************************************************/ 293464ebd5Sriastradh 303464ebd5Sriastradh 314a49301eSmrg/** 324a49301eSmrg * Logging facility for debug/info messages. 334a49301eSmrg * _EGL_FATAL messages are printed to stderr 344a49301eSmrg * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs. 354a49301eSmrg */ 364a49301eSmrg 374a49301eSmrg 384a49301eSmrg#include <stdarg.h> 394a49301eSmrg#include <stdio.h> 404a49301eSmrg#include <stdlib.h> 4101e04c3fSmrg#include <string.h> 4201e04c3fSmrg#include "c11/threads.h" 4301e04c3fSmrg#include "util/macros.h" 447ec681f3Smrg#include "util/u_string.h" 454a49301eSmrg 464a49301eSmrg#include "egllog.h" 4701e04c3fSmrg 4801e04c3fSmrg#ifdef HAVE_ANDROID_PLATFORM 4901e04c3fSmrg#define LOG_TAG "EGL-MAIN" 5001e04c3fSmrg#if ANDROID_API_LEVEL >= 26 5101e04c3fSmrg#include <log/log.h> 5201e04c3fSmrg#else 5301e04c3fSmrg#include <cutils/log.h> 5401e04c3fSmrg#endif /* use log/log.h start from android 8 major version */ 5501e04c3fSmrg 5601e04c3fSmrg#endif /* HAVE_ANDROID_PLATFORM */ 574a49301eSmrg 584a49301eSmrg#define MAXSTRING 1000 594a49301eSmrg#define FALLBACK_LOG_LEVEL _EGL_WARNING 604a49301eSmrg 614a49301eSmrg 624a49301eSmrgstatic struct { 6301e04c3fSmrg mtx_t mutex; 644a49301eSmrg 654a49301eSmrg EGLBoolean initialized; 664a49301eSmrg EGLint level; 674a49301eSmrg} logging = { 6801e04c3fSmrg .mutex = _MTX_INITIALIZER_NP, 6901e04c3fSmrg .initialized = EGL_FALSE, 7001e04c3fSmrg .level = FALLBACK_LOG_LEVEL, 714a49301eSmrg}; 724a49301eSmrg 734a49301eSmrgstatic const char *level_strings[] = { 7401e04c3fSmrg [_EGL_FATAL] = "fatal", 7501e04c3fSmrg [_EGL_WARNING] = "warning", 7601e04c3fSmrg [_EGL_INFO] = "info", 7701e04c3fSmrg [_EGL_DEBUG] = "debug", 784a49301eSmrg}; 794a49301eSmrg 804a49301eSmrg 814a49301eSmrg/** 824a49301eSmrg * The default logger. It prints the message to stderr. 834a49301eSmrg */ 844a49301eSmrgstatic void 854a49301eSmrg_eglDefaultLogger(EGLint level, const char *msg) 864a49301eSmrg{ 8701e04c3fSmrg#ifdef HAVE_ANDROID_PLATFORM 8801e04c3fSmrg static const int egl2alog[] = { 8901e04c3fSmrg [_EGL_FATAL] = ANDROID_LOG_ERROR, 9001e04c3fSmrg [_EGL_WARNING] = ANDROID_LOG_WARN, 9101e04c3fSmrg [_EGL_INFO] = ANDROID_LOG_INFO, 9201e04c3fSmrg [_EGL_DEBUG] = ANDROID_LOG_DEBUG, 9301e04c3fSmrg }; 9401e04c3fSmrg LOG_PRI(egl2alog[level], LOG_TAG, "%s", msg); 9501e04c3fSmrg#else 964a49301eSmrg fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg); 9701e04c3fSmrg#endif /* HAVE_ANDROID_PLATFORM */ 984a49301eSmrg} 994a49301eSmrg 1004a49301eSmrg 1014a49301eSmrg/** 1024a49301eSmrg * Initialize the logging facility. 1034a49301eSmrg */ 1044a49301eSmrgstatic void 1054a49301eSmrg_eglInitLogger(void) 1064a49301eSmrg{ 1074a49301eSmrg const char *log_env; 1084a49301eSmrg EGLint i, level = -1; 1094a49301eSmrg 1104a49301eSmrg if (logging.initialized) 1114a49301eSmrg return; 1124a49301eSmrg 1134a49301eSmrg log_env = getenv("EGL_LOG_LEVEL"); 1144a49301eSmrg if (log_env) { 11501e04c3fSmrg for (i = 0; i < ARRAY_SIZE(level_strings); i++) { 11601e04c3fSmrg if (strcasecmp(log_env, level_strings[i]) == 0) { 1174a49301eSmrg level = i; 1184a49301eSmrg break; 1194a49301eSmrg } 1204a49301eSmrg } 1214a49301eSmrg } 1224a49301eSmrg 1234a49301eSmrg logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL; 1244a49301eSmrg logging.initialized = EGL_TRUE; 1254a49301eSmrg 1264a49301eSmrg /* it is fine to call _eglLog now */ 1274a49301eSmrg if (log_env && level < 0) { 1284a49301eSmrg _eglLog(_EGL_WARNING, 1294a49301eSmrg "Unrecognized EGL_LOG_LEVEL environment variable value. " 1304a49301eSmrg "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". " 1314a49301eSmrg "Got \"%s\". Falling back to \"%s\".", 1324a49301eSmrg log_env, level_strings[FALLBACK_LOG_LEVEL]); 1334a49301eSmrg } 1344a49301eSmrg} 1354a49301eSmrg 1364a49301eSmrg 1374a49301eSmrg/** 1384a49301eSmrg * Log a message with message logger. 1394a49301eSmrg * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG. 1404a49301eSmrg */ 1414a49301eSmrgvoid 1424a49301eSmrg_eglLog(EGLint level, const char *fmtStr, ...) 1434a49301eSmrg{ 1444a49301eSmrg va_list args; 1454a49301eSmrg char msg[MAXSTRING]; 1463464ebd5Sriastradh int ret; 1474a49301eSmrg 1484a49301eSmrg /* one-time initialization; a little race here is fine */ 1494a49301eSmrg if (!logging.initialized) 1504a49301eSmrg _eglInitLogger(); 1514a49301eSmrg if (level > logging.level || level < 0) 1524a49301eSmrg return; 1534a49301eSmrg 15401e04c3fSmrg mtx_lock(&logging.mutex); 1554a49301eSmrg 15601e04c3fSmrg va_start(args, fmtStr); 15701e04c3fSmrg ret = vsnprintf(msg, MAXSTRING, fmtStr, args); 15801e04c3fSmrg if (ret < 0 || ret >= MAXSTRING) 15901e04c3fSmrg strcpy(msg, "<message truncated>"); 16001e04c3fSmrg va_end(args); 1614a49301eSmrg 16201e04c3fSmrg _eglDefaultLogger(level, msg); 1634a49301eSmrg 16401e04c3fSmrg mtx_unlock(&logging.mutex); 1654a49301eSmrg 1664a49301eSmrg if (level == _EGL_FATAL) 1674a49301eSmrg exit(1); /* or abort()? */ 1684a49301eSmrg} 169