diff options
author | Chris Robinson <[email protected]> | 2020-09-28 11:34:24 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2020-09-28 11:34:24 -0700 |
commit | f02bc1354d406707619c3f4704e06c9d15b24f63 (patch) | |
tree | ebd59b653f71c3b00a0df2e7217cd3a17cdcd519 | |
parent | da29489ead3ecfc269a220a22ee117d93bed23a6 (diff) |
Rework logging a little
Use OutputDebugStringW on Windows in addition to the log file
Avoid duplicate formatter parsing with Android
-rw-r--r-- | alc/helpers.cpp | 58 | ||||
-rw-r--r-- | alc/logging.h | 40 |
2 files changed, 67 insertions, 31 deletions
diff --git a/alc/helpers.cpp b/alc/helpers.cpp index 2b262b65..369ab9f1 100644 --- a/alc/helpers.cpp +++ b/alc/helpers.cpp @@ -80,7 +80,7 @@ const PathNamePair &GetProcBinary() } -void al_print(FILE *logfile, const char *fmt, ...) +void al_print(LogLevel level, FILE *logfile, const char *fmt, ...) { al::vector<char> dynmsg; char stcmsg[256]; @@ -100,8 +100,12 @@ void al_print(FILE *logfile, const char *fmt, ...) va_end(args); std::wstring wstr{utf8_to_wstr(str)}; - fputws(wstr.c_str(), logfile); - fflush(logfile); + if(gLogLevel >= level) + { + fputws(wstr.c_str(), logfile); + fflush(logfile); + } + OutputDebugStringW(wstr.c_str()); } @@ -221,6 +225,9 @@ void SetRTPriority(void) #ifdef __HAIKU__ #include <FindDirectory.h> #endif +#ifdef __ANDROID__ +#include <android/log.h> +#endif #ifdef HAVE_PROC_PIDPATH #include <libproc.h> #endif @@ -316,14 +323,47 @@ const PathNamePair &GetProcBinary() } -void al_print(FILE *logfile, const char *fmt, ...) +void al_print(LogLevel level, FILE *logfile, const char *fmt, ...) { - std::va_list ap; - va_start(ap, fmt); - vfprintf(logfile, fmt, ap); - va_end(ap); + al::vector<char> dynmsg; + char stcmsg[256]; + char *str{stcmsg}; - fflush(logfile); + va_list args, args2; + va_start(args, fmt); + va_copy(args2, args); + int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)}; + if UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)) + { + dynmsg.resize(static_cast<size_t>(msglen) + 1u); + str = dynmsg.data(); + msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2); + } + va_end(args2); + va_end(args); + + if(gLogLevel >= level) + { + fputs(str, logfile); + fflush(logfile); + } +#ifdef __ANDROID__ + auto android_severity = [](LogLevel l) noexcept + { + switch(l) + { + case LogLevel::Trace: return ANDROID_LOG_DEBUG; + case LogLevel::Warning: return ANDROID_LOG_WARN; + case LogLevel::Error: return ANDROID_LOG_ERROR; + /* Should not happen. */ + case LogLevel::Ref: + case LogLevel::Disable: + break; + } + return ANDROID_LOG_ERROR; + }; + __android_log_print(android_severity(level), "openal", "%s", str); +#endif } diff --git a/alc/logging.h b/alc/logging.h index 77593779..4b015814 100644 --- a/alc/logging.h +++ b/alc/logging.h @@ -6,22 +6,6 @@ #include "opthelpers.h" -extern FILE *gLogFile; - -[[gnu::format(printf,2,3)]] void al_print(FILE *logfile, const char *fmt, ...); -#if !defined(_WIN32) -#define AL_PRINT fprintf -#else -#define AL_PRINT al_print -#endif - -#ifdef __ANDROID__ -#include <android/log.h> -#define LOG_ANDROID(T, ...) __android_log_print(T, "openal", "AL lib: " __VA_ARGS__) -#else -#define LOG_ANDROID(T, ...) ((void)0) -#endif - enum class LogLevel { Disable, Error, @@ -31,22 +15,34 @@ enum class LogLevel { }; extern LogLevel gLogLevel; +extern FILE *gLogFile; + + +#if !defined(_WIN32) && !defined(__ANDROID__) #define TRACE(...) do { \ if UNLIKELY(gLogLevel >= LogLevel::Trace) \ - AL_PRINT(gLogFile, "AL lib: (II) " __VA_ARGS__); \ - LOG_ANDROID(ANDROID_LOG_DEBUG, __VA_ARGS__); \ + fprintf(gLogFile, "[ALSOFT] (II) " __VA_ARGS__); \ } while(0) #define WARN(...) do { \ if UNLIKELY(gLogLevel >= LogLevel::Warning) \ - AL_PRINT(gLogFile, "AL lib: (WW) " __VA_ARGS__); \ - LOG_ANDROID(ANDROID_LOG_WARN, __VA_ARGS__); \ + fprintf(gLogFile, "[ALSOFT] (WW) " __VA_ARGS__); \ } while(0) #define ERR(...) do { \ if UNLIKELY(gLogLevel >= LogLevel::Error) \ - AL_PRINT(gLogFile, "AL lib: (EE) " __VA_ARGS__); \ - LOG_ANDROID(ANDROID_LOG_ERROR, __VA_ARGS__); \ + fprintf(gLogFile, "[ALSOFT] (EE) " __VA_ARGS__); \ } while(0) +#else + +[[gnu::format(printf,3,4)]] void al_print(LogLevel level, FILE *logfile, const char *fmt, ...); + +#define TRACE(...) al_print(LogLevel::Trace, gLogFile, "[ALSOFT] (II) " __VA_ARGS__) + +#define WARN(...) al_print(LogLevel::Warning, gLogFile, "[ALSOFT] (WW) " __VA_ARGS__) + +#define ERR(...) al_print(LogLevel::Error, gLogFile, "[ALSOFT] (EE) " __VA_ARGS__) +#endif + #endif /* LOGGING_H */ |