50 std::function<void(
LogLevel level, std::string_view category, std::string_view message)>;
59 std::shared_ptr<const LogSink>
sink;
70 static thread_local bool flag =
false;
76 if (!registry.active.load(std::memory_order_relaxed))
82 std::shared_ptr<const LogSink> sink;
84 std::lock_guard<std::mutex> lock(registry.mutex);
92 (*sink)(level, category, message);
101 const char* prefix =
"";
109#if defined(__APPLE__)
110 os_log_type_t type = OS_LOG_TYPE_DEFAULT;
123 static os_log_t pulp_log = os_log_create(
"dev.pulp.runtime",
"runtime");
124 os_log_with_type(pulp_log, type,
"%{public}s%{public}s",
125 prefix, std::string(message).c_str());
127 auto full = std::string(prefix) + std::string(message) +
"\n";
128 OutputDebugStringA(full.c_str());
131 std::fprintf(stderr,
"%s%.*s\n", prefix,
132 static_cast<int>(message.size()), message.data());
149 std::shared_ptr<const LogSink> incoming;
151 incoming = std::make_shared<const LogSink>(std::move(sink));
153 std::shared_ptr<const LogSink> previous;
155 std::lock_guard<std::mutex> lock(registry.mutex);
156 previous = std::move(registry.sink);
157 registry.sink = std::move(incoming);
158 registry.active.store(registry.sink !=
nullptr, std::memory_order_relaxed);
160 return previous ? *previous :
LogSink{};
164template <
typename... Args>
167 auto msg = std::format(fmt, std::forward<Args>(args)...);
171template<
typename... Args>
172void log(
LogLevel level, std::format_string<Args...> fmt, Args&&... args) {
173 auto msg = std::format(fmt, std::forward<Args>(args)...);
177template<
typename... Args>
178void log_debug(std::format_string<Args...> fmt, Args&&... args) {
184template<
typename... Args>
185void log_info(std::format_string<Args...> fmt, Args&&... args) {
189template<
typename... Args>
190void log_warn(std::format_string<Args...> fmt, Args&&... args) {
194template<
typename... Args>
195void log_error(std::format_string<Args...> fmt, Args&&... args) {
Portable try/catch macros for source that must also compile with C++ exceptions disabled (-fno-except...
#define PULP_TRY
Definition exceptions.hpp:42
#define PULP_CATCH_ALL
Definition exceptions.hpp:43
bool & in_log_sink_dispatch()
Definition log.hpp:69
void log_impl(LogLevel level, std::string_view category, std::string_view message)
Definition log.hpp:100
LogSinkRegistry & log_sink_registry()
Definition log.hpp:64
void dispatch_log_sink(LogLevel level, std::string_view category, std::string_view message)
Definition log.hpp:74
Definition generated_event_source.hpp:33
LogLevel
Definition log.hpp:42
@ Warning
Definition log.hpp:42
@ Info
Definition log.hpp:42
@ Error
Definition log.hpp:42
@ Debug
Definition log.hpp:42
void log_with_category(std::string_view category, LogLevel level, std::format_string< Args... > fmt, Args &&... args)
Log with an explicit category (a subsystem tag such as "audio" or "net").
Definition log.hpp:165
constexpr std::string_view kDefaultLogCategory
Category used by log_info and the other level helpers.
Definition log.hpp:53
std::function< void(LogLevel level, std::string_view category, std::string_view message)> LogSink
Receives every record after the platform writers.
Definition log.hpp:49
void log_warn(std::format_string< Args... > fmt, Args &&... args)
Definition log.hpp:190
LogSink set_log_sink(LogSink sink)
Install sink as the process-wide log sink and return the previous one (empty if none).
Definition log.hpp:147
void log_error(std::format_string< Args... > fmt, Args &&... args)
Definition log.hpp:195
void log(LogLevel level, std::format_string< Args... > fmt, Args &&... args)
Definition log.hpp:172
void log_info(std::format_string< Args... > fmt, Args &&... args)
Definition log.hpp:185
void log_debug(std::format_string< Args... > fmt, Args &&... args)
Definition log.hpp:178
std::mutex mutex
Definition log.hpp:58
std::atomic< bool > active
Definition log.hpp:60
std::shared_ptr< const LogSink > sink
Definition log.hpp:59