2
0

Sinks report success now & fallback sink triggers on failure.

We now trigger the fallback sink (currently, mpw_log_sink_file) if no
sinks successfully handled the log message.
This commit is contained in:
Maarten Billemont 2020-04-14 19:12:28 -04:00
parent 62e1563fa6
commit 11d6660e5a
2 changed files with 7 additions and 5 deletions

View File

@ -97,14 +97,15 @@ void mpw_log_ssink(LogLevel level, const char *file, int line, const char *funct
.message = message,
};
bool sunk = false;
for (unsigned int s = 0; s < sinks_count; ++s) {
MPLogSink *sink = sinks[s];
if (sink)
sink( &record );
sunk |= sink( &record );
}
if (!sinks_count)
mpw_log_sink_file( &record );
if (!sunk)
sunk = mpw_log_sink_file( &record );
if (record.level <= LogLevelError) {
/* error breakpoint */;
@ -113,7 +114,7 @@ void mpw_log_ssink(LogLevel level, const char *file, int line, const char *funct
abort();
}
void mpw_log_sink_file(const MPLogEvent *record) {
bool mpw_log_sink_file(const MPLogEvent *record) {
if (!mpw_log_sink_file_target)
mpw_log_sink_file_target = stderr;
@ -145,6 +146,7 @@ void mpw_log_sink_file(const MPLogEvent *record) {
}
fprintf( mpw_log_sink_file_target, "%s\n", record->message );
return true;
}
void mpw_uint16(const uint16_t number, uint8_t buf[2]) {

View File

@ -54,7 +54,7 @@ typedef struct {
} MPLogEvent;
/** A log sink describes a function that can receive log events. */
typedef void (MPLogSink)(const MPLogEvent *event);
typedef bool (MPLogSink)(const MPLogEvent *event);
/** To receive events, sinks need to be registered. If no sinks are registered, log events are sent to the mpw_log_sink_file sink. */
bool mpw_log_sink_register(MPLogSink *sink);