aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/logging.cpp82
-rw-r--r--core/logging.h25
2 files changed, 82 insertions, 25 deletions
diff --git a/core/logging.cpp b/core/logging.cpp
index b6248514..56ad0a0d 100644
--- a/core/logging.cpp
+++ b/core/logging.cpp
@@ -3,8 +3,12 @@
#include "logging.h"
+#include <cctype>
#include <cstdarg>
#include <cstdio>
+#include <cstring>
+#include <mutex>
+#include <optional>
#include <string>
#include <vector>
@@ -28,6 +32,49 @@ LogLevel gLogLevel{LogLevel::Error};
#endif
+namespace {
+
+enum class LogState : uint8_t {
+ FirstRun,
+ Ready,
+ Disable
+};
+
+std::mutex LogCallbackMutex;
+LogState gLogState{LogState::FirstRun};
+
+LogCallbackFunc gLogCallback{};
+void *gLogCallbackPtr{};
+
+constexpr std::optional<char> GetLevelCode(LogLevel level)
+{
+ switch(level)
+ {
+ case LogLevel::Disable: break;
+ case LogLevel::Error: return 'E';
+ case LogLevel::Warning: return 'W';
+ case LogLevel::Trace: return 'I';
+ }
+ return std::nullopt;
+}
+
+} // namespace
+
+void al_set_log_callback(LogCallbackFunc callback, void *userptr)
+{
+ auto cblock = std::lock_guard{LogCallbackMutex};
+ gLogCallback = callback;
+ gLogCallbackPtr = callback ? userptr : nullptr;
+ if(gLogState == LogState::FirstRun)
+ {
+ auto extlogopt = al::getenv("ALSOFT_DISABLE_LOG_CALLBACK");
+ if(!extlogopt || *extlogopt != "1")
+ gLogState = LogState::Ready;
+ else
+ gLogState = LogState::Disable;
+ }
+}
+
void al_print(LogLevel level, const char *fmt, ...)
{
/* Kind of ugly since string literals are const char arrays with a size
@@ -54,16 +101,22 @@ void al_print(LogLevel level, const char *fmt, ...)
va_start(args, fmt);
va_copy(args2, args);
const int msglen{std::vsnprintf(msg.data(), msg.size(), fmt, args)};
- if(msglen >= 0 && static_cast<size_t>(msglen) >= msg.size()) UNLIKELY
+ if(msglen >= 0)
{
- dynmsg.resize(static_cast<size_t>(msglen)+prefix.size() + 1u);
+ if(static_cast<size_t>(msglen) >= msg.size()) UNLIKELY
+ {
+ dynmsg.resize(static_cast<size_t>(msglen)+prefix.size() + 1u);
- str = dynmsg.data();
- auto prefend2 = std::copy_n(prefix.begin(), prefix.size(), dynmsg.begin());
- msg = {prefend2, dynmsg.end()};
+ str = dynmsg.data();
+ auto prefend2 = std::copy_n(prefix.begin(), prefix.size(), dynmsg.begin());
+ msg = {prefend2, dynmsg.end()};
- std::vsnprintf(msg.data(), msg.size(), fmt, args2);
+ std::vsnprintf(msg.data(), msg.size(), fmt, args2);
+ }
+ msg = msg.first(static_cast<size_t>(msglen));
}
+ else
+ msg = {msg.data(), std::strlen(msg.data())};
va_end(args2);
va_end(args);
@@ -96,4 +149,21 @@ void al_print(LogLevel level, const char *fmt, ...)
};
__android_log_print(android_severity(level), "openal", "%s", str);
#endif
+
+ auto cblock = std::lock_guard{LogCallbackMutex};
+ if(gLogState != LogState::Disable)
+ {
+ while(!msg.empty() && std::isspace(msg.back()))
+ {
+ msg.back() = '\0';
+ msg = msg.first(msg.size()-1);
+ }
+ if(auto logcode = GetLevelCode(level); logcode && !msg.empty())
+ {
+ if(gLogCallback)
+ gLogCallback(gLogCallbackPtr, *logcode, msg.data(), static_cast<int>(msg.size()));
+ else if(gLogState == LogState::FirstRun)
+ gLogState = LogState::Disable;
+ }
+ }
}
diff --git a/core/logging.h b/core/logging.h
index c99c4e45..06b7cdde 100644
--- a/core/logging.h
+++ b/core/logging.h
@@ -16,6 +16,12 @@ extern LogLevel gLogLevel;
extern FILE *gLogFile;
+
+using LogCallbackFunc = void(*)(void *userptr, char level, const char *message, int length) noexcept;
+
+void al_set_log_callback(LogCallbackFunc callback, void *userptr);
+
+
#ifdef __USE_MINGW_ANSI_STDIO
[[gnu::format(gnu_printf,2,3)]]
#else
@@ -23,29 +29,10 @@ extern FILE *gLogFile;
#endif
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, __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, __VA_ARGS__); \
-} while(0)
-
-#else
-
#define TRACE(...) al_print(LogLevel::Trace, __VA_ARGS__)
#define WARN(...) al_print(LogLevel::Warning, __VA_ARGS__)
#define ERR(...) al_print(LogLevel::Error, __VA_ARGS__)
-#endif
#endif /* CORE_LOGGING_H */