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-13 23:16:10 -08:00
committed by Benjamin Doherty
parent c81ece5c3c
commit 9fa3cbfcde
5 changed files with 75 additions and 7 deletions

View File

@@ -24,9 +24,12 @@
#include <utils/PrivateImplementation-impl.h>
#include <algorithm>
#include <mutex>
#include <string>
#include <string_view>
#include <utility>
#include <stdlib.h>
#include <stdarg.h>
template class utils::PrivateImplementation<utils::io::ostream_>;
@@ -35,7 +38,31 @@ namespace utils::io {
ostream::~ostream() = default;
void ostream::setConsumer(ConsumerCallback consumer, void* user) noexcept {
auto* const pImpl = mImpl;
std::lock_guard const lock(pImpl->mLock);
pImpl->mConsumer = { consumer, user };
}
ostream& flush(ostream& s) noexcept {
auto* const pImpl = s.mImpl;
pImpl->mLock.lock();
auto const callback = pImpl->mConsumer;
if (UTILS_UNLIKELY(callback.first)) {
auto& buf = s.getBuffer();
char const* const data = buf.get();
if (UTILS_LIKELY(data)) {
char* const str = strdup(data);
buf.reset();
pImpl->mLock.unlock();
// call ConsumerCallback without lock held
callback.first(callback.second, str);
::free(str);
return s;
}
}
pImpl->mLock.unlock();
return s.flush();
}