[*] updated logging

This commit is contained in:
Reece Wilson 2023-03-29 14:14:16 +01:00
parent 5c53621598
commit da3693feaf
3 changed files with 28 additions and 56 deletions

View File

@ -161,7 +161,6 @@
#endif // ALSOFT_EAX
FILE *gLogFile{stderr};
#ifdef _DEBUG
LogLevel gLogLevel{LogLevel::Warning};
#else

View File

@ -11,28 +11,13 @@
#include "strutils.h"
#include "vector.h"
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#elif defined(__ANDROID__)
#include <android/log.h>
#endif
void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
void al_print(LogLevel level, const char *fmt, ...)
{
/* Kind of ugly since string literals are const char arrays with a size
* that includes the null terminator, which we want to exclude from the
* span.
*/
auto prefix = al::as_span("[ALSOFT] (--) ").first<14>();
switch(level)
{
case LogLevel::Disable: break;
case LogLevel::Error: prefix = al::as_span("[ALSOFT] (EE) ").first<14>(); break;
case LogLevel::Warning: prefix = al::as_span("[ALSOFT] (WW) ").first<14>(); break;
case LogLevel::Trace: prefix = al::as_span("[ALSOFT] (II) ").first<14>(); break;
}
al::vector<char> dynmsg;
std::array<char,256> stcmsg{};
@ -60,30 +45,20 @@ void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
if(gLogLevel >= level)
{
fputs(str, logfile);
fflush(logfile);
}
#if defined(_WIN32) && !defined(NDEBUG)
/* OutputDebugStringW has no 'level' property to distinguish between
* informational, warning, or error debug messages. So only print them for
* non-Release builds.
*/
std::wstring wstr{utf8_to_wstr(str)};
OutputDebugStringW(wstr.c_str());
#elif defined(__ANDROID__)
auto android_severity = [](LogLevel l) noexcept
{
switch(l)
switch (level)
{
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::Disable:
case LogLevel::Error:
AuLogError("{}", str);
break;
case LogLevel::Warning:
AuLogWarn("{}", str);
break;
case LogLevel::Trace:
AuLogVerbose("{}", str);
break;
default:
AuLogGame("{}", str);
}
return ANDROID_LOG_ERROR;
};
__android_log_print(android_severity(level), "openal", "%s", str);
#endif
}
}

View File

@ -14,34 +14,32 @@ enum class LogLevel {
};
extern LogLevel gLogLevel;
extern FILE *gLogFile;
void al_print(LogLevel level, FILE *logfile, const char *fmt, ...);
void al_print(LogLevel level, const char *fmt, ...);
#if (!defined(_WIN32) || defined(NDEBUG)) && !defined(__ANDROID__)
#define TRACE(...) do { \
if(gLogLevel >= LogLevel::Trace) UNLIKELY \
al_print(LogLevel::Trace, gLogFile, __VA_ARGS__); \
#define TRACE(...) do { \
if(gLogLevel >= LogLevel::Trace) UNLIKELY \
al_print(LogLevel::Trace, __VA_ARGS__); \
} while(0)
#define WARN(...) do { \
if(gLogLevel >= LogLevel::Warning) UNLIKELY \
al_print(LogLevel::Warning, gLogFile, __VA_ARGS__); \
} while(0)
#define WARN(...) do { \
if(gLogLevel >= LogLevel::Warning) UNLIKELY \
al_print(LogLevel::Warning, __VA_ARGS__); \
} while(0)
#define ERR(...) do { \
if(gLogLevel >= LogLevel::Error) UNLIKELY \
al_print(LogLevel::Error, gLogFile, __VA_ARGS__); \
#define ERR(...) do { \
if(gLogLevel >= LogLevel::Error) UNLIKELY \
al_print(LogLevel::Error, __VA_ARGS__); \
} while(0)
#else
#define TRACE(...) al_print(LogLevel::Trace, gLogFile, __VA_ARGS__)
#define TRACE(...) al_print(LogLevel::Trace, __VA_ARGS__)
#define WARN(...) al_print(LogLevel::Warning, gLogFile, __VA_ARGS__)
#define WARN(...) al_print(LogLevel::Warning, __VA_ARGS__)
#define ERR(...) al_print(LogLevel::Error, gLogFile, __VA_ARGS__)
#define ERR(...) al_print(LogLevel::Error, __VA_ARGS__)
#endif
#endif /* CORE_LOGGING_H */