add a way to set a log consumer

this is a private API to capture filament's logs.
This commit is contained in:
Mathias Agopian
2023-12-19 12:09:28 -08:00
committed by Benjamin Doherty
parent c81ece5c3c
commit 9fa3cbfcde
5 changed files with 75 additions and 7 deletions
+15 -7
View File
@@ -31,6 +31,8 @@
#include <emscripten/console.h>
#endif
#include <mutex>
namespace utils {
namespace io {
@@ -52,6 +54,12 @@ private:
ostream& LogStream::flush() noexcept {
std::lock_guard const lock(mImpl->mLock);
Buffer& buf = getBuffer();
char const* const str = buf.get();
if (UTILS_UNLIKELY(!str)) {
// this can happen if the log hasn't been written ever
return *this;
}
#ifdef __ANDROID__
int prio = ANDROID_LOG_UNKNOWN;
switch (mPriority) {
@@ -61,20 +69,20 @@ ostream& LogStream::flush() noexcept {
case LOG_INFO: prio = ANDROID_LOG_INFO; break;
case LOG_VERBOSE: prio = ANDROID_LOG_VERBOSE; break;
}
__android_log_write(prio, UTILS_LOG_TAG, buf.get());
__android_log_write(prio, UTILS_LOG_TAG, str);
#elif defined(__EMSCRIPTEN__)
switch (mPriority) {
case LOG_DEBUG:
case LOG_WARNING:
case LOG_INFO:
_emscripten_out(buf.get());
_emscripten_out(str);
break;
case LOG_ERROR:
_emscripten_err(buf.get());
_emscripten_err(str);
break;
case LOG_VERBOSE:
#ifndef NFIL_DEBUG
_emscripten_out(buf.get());
_emscripten_out(str);
#endif
break;
}
@@ -83,14 +91,14 @@ ostream& LogStream::flush() noexcept {
case LOG_DEBUG:
case LOG_WARNING:
case LOG_INFO:
fprintf(stdout, "%s", buf.get());
fprintf(stdout, "%s", str);
break;
case LOG_ERROR:
fprintf(stderr, "%s", buf.get());
fprintf(stderr, "%s", str);
break;
case LOG_VERBOSE:
#ifndef NDEBUG
fprintf(stdout, "%s", buf.get());
fprintf(stdout, "%s", str);
#endif
break;
}