aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2023-01-02 14:23:23 -0800
committerChris Robinson <[email protected]>2023-01-02 19:10:13 -0800
commit8054ccd8f91fee5aacb35e01decbecb67a8da8ce (patch)
tree5484bd8e99af4f0a26582565d48e19f04d9d6d0a
parent52224f8bb87b724eb3c97605c37283f032937c9e (diff)
Combine multiple al_printf definitions
And separate the log level prefix from the message.
-rw-r--r--core/logging.cpp82
-rw-r--r--core/logging.h25
2 files changed, 49 insertions, 58 deletions
diff --git a/core/logging.cpp b/core/logging.cpp
index 8efa6a70..69cb92b5 100644
--- a/core/logging.cpp
+++ b/core/logging.cpp
@@ -7,30 +7,57 @@
#include <cstdio>
#include <string>
+#include "alspan.h"
#include "strutils.h"
#include "vector.h"
-#ifdef _WIN32
-
+#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, ...)
{
+#if (defined(_WIN32) && defined(NDEBUG)) || !defined(__ANDROID__)
+ if(gLogLevel < level) [[likely]]
+ return;
+#endif
+ /* 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;
- char stcmsg[256];
- char *str{stcmsg};
+ std::array<char,256> stcmsg{};
+
+ char *str{stcmsg.data()};
+ auto prefend1 = std::copy_n(prefix.begin(), prefix.size(), stcmsg.begin());
+ al::span<char> msg{prefend1, stcmsg.end()};
std::va_list args, args2;
va_start(args, fmt);
va_copy(args2, args);
- const int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
- if(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)) [[unlikely]]
+ const int msglen{std::vsnprintf(msg.data(), msg.size(), fmt, args)};
+ if(msglen >= 0 && static_cast<size_t>(msglen) >= msg.size()) [[unlikely]]
{
- dynmsg.resize(static_cast<size_t>(msglen) + 1u);
+ dynmsg.resize(static_cast<size_t>(msglen)+prefix.size() + 1u);
+
str = dynmsg.data();
- std::vsnprintf(str, dynmsg.size(), fmt, args2);
+ auto prefend2 = std::copy_n(prefix.begin(), prefix.size(), dynmsg.begin());
+ msg = {prefend2, dynmsg.end()};
+
+ std::vsnprintf(msg.data(), msg.size(), fmt, args2);
}
va_end(args2);
va_end(args);
@@ -40,47 +67,14 @@ void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
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.
*/
-#ifndef NDEBUG
std::wstring wstr{utf8_to_wstr(str)};
OutputDebugStringW(wstr.c_str());
-#endif
-}
-
-#else
-
-#ifdef __ANDROID__
-#include <android/log.h>
-#endif
-
-void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
-{
- al::vector<char> dynmsg;
- char stcmsg[256];
- char *str{stcmsg};
-
- std::va_list args, args2;
- va_start(args, fmt);
- va_copy(args2, args);
- const int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
- if(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)) [[unlikely]]
- {
- dynmsg.resize(static_cast<size_t>(msglen) + 1u);
- str = dynmsg.data();
- std::vsnprintf(str, dynmsg.size(), fmt, args2);
- }
- va_end(args2);
- va_end(args);
-
- if(gLogLevel >= level)
- {
- std::fputs(str, logfile);
- std::fflush(logfile);
- }
-#ifdef __ANDROID__
+#elif defined(__ANDROID__)
auto android_severity = [](LogLevel l) noexcept
{
switch(l)
@@ -97,5 +91,3 @@ void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
__android_log_print(android_severity(level), "openal", "%s", str);
#endif
}
-
-#endif
diff --git a/core/logging.h b/core/logging.h
index e90de661..2853afbd 100644
--- a/core/logging.h
+++ b/core/logging.h
@@ -16,37 +16,36 @@ extern LogLevel gLogLevel;
extern FILE *gLogFile;
+#ifdef __USE_MINGW_ANSI_STDIO
+[[gnu::format(gnu_printf,3,4)]]
+#else
+[[gnu::format(printf,3,4)]]
+#endif
+void al_print(LogLevel level, FILE *logfile, const char *fmt, ...);
#if !defined(_WIN32) && !defined(__ANDROID__)
#define TRACE(...) do { \
if(gLogLevel >= LogLevel::Trace) [[unlikely]] \
- fprintf(gLogFile, "[ALSOFT] (II) " __VA_ARGS__); \
+ al_print(LogLevel::Trace, gLogFile, __VA_ARGS__); \
} while(0)
#define WARN(...) do { \
if(gLogLevel >= LogLevel::Warning) [[unlikely]] \
- fprintf(gLogFile, "[ALSOFT] (WW) " __VA_ARGS__); \
+ al_print(LogLevel::Warning, gLogFile, __VA_ARGS__); \
} while(0)
#define ERR(...) do { \
if(gLogLevel >= LogLevel::Error) [[unlikely]] \
- fprintf(gLogFile, "[ALSOFT] (EE) " __VA_ARGS__); \
+ al_print(LogLevel::Error, gLogFile, __VA_ARGS__); \
} while(0)
#else
-#ifdef __USE_MINGW_ANSI_STDIO
-[[gnu::format(gnu_printf,3,4)]]
-#else
-[[gnu::format(printf,3,4)]]
-#endif
-void al_print(LogLevel level, FILE *logfile, const char *fmt, ...);
-
-#define TRACE(...) al_print(LogLevel::Trace, gLogFile, "[ALSOFT] (II) " __VA_ARGS__)
+#define TRACE(...) al_print(LogLevel::Trace, gLogFile, __VA_ARGS__)
-#define WARN(...) al_print(LogLevel::Warning, gLogFile, "[ALSOFT] (WW) " __VA_ARGS__)
+#define WARN(...) al_print(LogLevel::Warning, gLogFile, __VA_ARGS__)
-#define ERR(...) al_print(LogLevel::Error, gLogFile, "[ALSOFT] (EE) " __VA_ARGS__)
+#define ERR(...) al_print(LogLevel::Error, gLogFile, __VA_ARGS__)
#endif
#endif /* CORE_LOGGING_H */