From 093143e30123297f98bbe3aa03f33eef929ad035 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 5 Aug 2012 13:35:55 +0000 Subject: [PATCH] android: simplify the logger code. --- src/log.cpp | 57 ++++++++++++++++++++++++++++++----------------------- src/log.h | 10 +++++++++- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/log.cpp b/src/log.cpp index f1782fc4..81821644 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -38,11 +38,7 @@ void Log::Debug(char const *fmt, ...) { va_list ap; va_start(ap, fmt); -#if defined __ANDROID__ - __android_log_vprint(ANDROID_LOG_DEBUG, "LOL", fmt, ap); -#else - Helper("DEBUG: ", fmt, ap); -#endif + Helper(DebugMessage, fmt, ap); va_end(ap); } @@ -50,11 +46,7 @@ void Log::Info(char const *fmt, ...) { va_list ap; va_start(ap, fmt); -#if defined __ANDROID__ - __android_log_vprint(ANDROID_LOG_INFO, "LOL", fmt, ap); -#else - Helper("INFO: ", fmt, ap); -#endif + Helper(InfoMessage, fmt, ap); va_end(ap); } @@ -62,11 +54,7 @@ void Log::Warn(char const *fmt, ...) { va_list ap; va_start(ap, fmt); -#if defined __ANDROID__ - __android_log_vprint(ANDROID_LOG_WARN, "LOL", fmt, ap); -#else - Helper("WARN: ", fmt, ap); -#endif + Helper(WarnMessage, fmt, ap); va_end(ap); } @@ -74,11 +62,7 @@ void Log::Error(char const *fmt, ...) { va_list ap; va_start(ap, fmt); -#if defined __ANDROID__ - __android_log_vprint(ANDROID_LOG_ERROR, "LOL", fmt, ap); -#else - Helper("ERROR: ", fmt, ap); -#endif + Helper(ErrorMessage, fmt, ap); va_end(ap); } @@ -86,17 +70,40 @@ void Log::Error(char const *fmt, ...) * Private helper function */ -void Log::Helper(char const *prefix, char const *fmt, va_list ap) +void Log::Helper(MessageType type, char const *fmt, va_list ap) { -#if defined _WIN32 +#if defined __ANDROID__ + int prio[] = + { + ANDROID_LOG_DEBUG, + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR + }; + + //__android_log_print(prio[type], "LOL", "thread %ld", pthread_self()); + __android_log_vprint(prio[type], "LOL", fmt, ap); + +#else + char const *prefix[] = + { + "DEBUG", + "INFO", + "WARN", + "ERROR", + }; + +# if defined _WIN32 char buf[4096]; vsnprintf(buf, 4095, fmt, ap); buf[4095] = '\0'; - OutputDebugString(prefix); + OutputDebugString(prefix[type]); + OutputDebugString(": "); OutputDebugString(buf); -#else - fprintf(stderr, "%s", prefix); +# else + fprintf(stderr, "%s: ", prefix[type]); vfprintf(stderr, fmt, ap); +# endif #endif } diff --git a/src/log.h b/src/log.h index 703c879c..ab9aacaa 100644 --- a/src/log.h +++ b/src/log.h @@ -38,7 +38,15 @@ public: #undef LOL_FMT_ATTR private: - static void Helper(char const *prefix, char const *format, va_list ap); + enum MessageType + { + DebugMessage, + InfoMessage, + WarnMessage, + ErrorMessage + }; + + static void Helper(MessageType type, char const *fmt, va_list ap); }; } /* namespace lol */