2005-11-23 01:37:30 +00:00
|
|
|
/**
|
|
|
|
* Logging facility for debug/info messages.
|
2008-06-06 22:10:22 +01:00
|
|
|
* _EGL_FATAL messages are printed to stderr
|
|
|
|
* The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
|
2005-11-23 01:37:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-02-20 15:11:11 +00:00
|
|
|
#include <string.h>
|
2009-09-29 09:11:06 +01:00
|
|
|
|
2005-11-23 01:37:30 +00:00
|
|
|
#include "egllog.h"
|
2010-05-31 04:47:58 +01:00
|
|
|
#include "eglstring.h"
|
2009-09-29 09:11:06 +01:00
|
|
|
#include "eglmutex.h"
|
2005-11-23 01:37:30 +00:00
|
|
|
|
|
|
|
#define MAXSTRING 1000
|
2009-09-29 09:11:06 +01:00
|
|
|
#define FALLBACK_LOG_LEVEL _EGL_WARNING
|
2005-11-23 01:37:30 +00:00
|
|
|
|
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
static struct {
|
|
|
|
_EGLMutex mutex;
|
2007-02-20 15:11:11 +00:00
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
EGLBoolean initialized;
|
|
|
|
EGLint level;
|
|
|
|
_EGLLogProc logger;
|
|
|
|
EGLint num_messages;
|
|
|
|
} logging = {
|
|
|
|
_EGL_MUTEX_INITIALIZER,
|
|
|
|
EGL_FALSE,
|
|
|
|
FALLBACK_LOG_LEVEL,
|
|
|
|
NULL,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *level_strings[] = {
|
|
|
|
/* the order is important */
|
|
|
|
"fatal",
|
|
|
|
"warning",
|
|
|
|
"info",
|
|
|
|
"debug",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the function to be called when there is a message to log.
|
|
|
|
* Note that the function will be called with an internal lock held.
|
|
|
|
* Recursive logging is not allowed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_eglSetLogProc(_EGLLogProc logger)
|
2007-02-20 15:11:11 +00:00
|
|
|
{
|
2009-09-29 09:11:06 +01:00
|
|
|
EGLint num_messages = 0;
|
2007-02-20 15:11:11 +00:00
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
_eglLockMutex(&logging.mutex);
|
|
|
|
|
|
|
|
if (logging.logger != logger) {
|
|
|
|
logging.logger = logger;
|
|
|
|
|
|
|
|
num_messages = logging.num_messages;
|
|
|
|
logging.num_messages = 0;
|
2007-02-20 15:11:11 +00:00
|
|
|
}
|
2009-09-29 09:11:06 +01:00
|
|
|
|
|
|
|
_eglUnlockMutex(&logging.mutex);
|
|
|
|
|
|
|
|
if (num_messages)
|
|
|
|
_eglLog(_EGL_DEBUG,
|
|
|
|
"New logger installed. "
|
|
|
|
"Messages before the new logger might not be available.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the log reporting level.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_eglSetLogLevel(EGLint level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
case _EGL_FATAL:
|
|
|
|
case _EGL_WARNING:
|
|
|
|
case _EGL_INFO:
|
|
|
|
case _EGL_DEBUG:
|
|
|
|
_eglLockMutex(&logging.mutex);
|
|
|
|
logging.level = level;
|
|
|
|
_eglUnlockMutex(&logging.mutex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2007-02-20 15:11:11 +00:00
|
|
|
}
|
2009-09-29 09:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default logger. It prints the message to stderr.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
_eglDefaultLogger(EGLint level, const char *msg)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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; level_strings[i]; i++) {
|
2010-05-31 04:47:58 +01:00
|
|
|
if (_eglstrcasecmp(log_env, level_strings[i]) == 0) {
|
2009-09-29 09:11:06 +01:00
|
|
|
level = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-02-20 15:11:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-09-29 09:11:06 +01:00
|
|
|
level = FALLBACK_LOG_LEVEL;
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.logger = _eglDefaultLogger;
|
|
|
|
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. "
|
2008-06-06 22:10:22 +01:00
|
|
|
"Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
|
2009-09-29 09:11:06 +01:00
|
|
|
"Got \"%s\". Falling back to \"%s\".",
|
|
|
|
log_env, level_strings[FALLBACK_LOG_LEVEL]);
|
2007-02-20 15:11:11 +00:00
|
|
|
}
|
|
|
|
}
|
2005-11-23 01:37:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-09-29 09:11:06 +01:00
|
|
|
* Log a message with message logger.
|
2005-11-23 01:37:30 +00:00
|
|
|
* \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
_eglLog(EGLint level, const char *fmtStr, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char msg[MAXSTRING];
|
2010-11-01 17:23:13 +00:00
|
|
|
int ret;
|
2007-02-20 15:11:11 +00:00
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
/* one-time initialization; a little race here is fine */
|
|
|
|
if (!logging.initialized)
|
|
|
|
_eglInitLogger();
|
|
|
|
if (level > logging.level || level < 0)
|
|
|
|
return;
|
2005-11-23 01:37:30 +00:00
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
_eglLockMutex(&logging.mutex);
|
2005-11-23 01:37:30 +00:00
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
if (logging.logger) {
|
2005-11-23 01:37:30 +00:00
|
|
|
va_start(args, fmtStr);
|
2010-11-01 17:23:13 +00:00
|
|
|
ret = vsnprintf(msg, MAXSTRING, fmtStr, args);
|
|
|
|
if (ret < 0 || ret >= MAXSTRING)
|
|
|
|
strcpy(msg, "<message truncated>");
|
2005-11-23 01:37:30 +00:00
|
|
|
va_end(args);
|
|
|
|
|
2009-09-29 09:11:06 +01:00
|
|
|
logging.logger(level, msg);
|
|
|
|
logging.num_messages++;
|
2005-11-23 01:37:30 +00:00
|
|
|
}
|
2009-09-29 09:11:06 +01:00
|
|
|
|
|
|
|
_eglUnlockMutex(&logging.mutex);
|
|
|
|
|
|
|
|
if (level == _EGL_FATAL)
|
|
|
|
exit(1); /* or abort()? */
|
2005-11-23 01:37:30 +00:00
|
|
|
}
|