1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "Backend.h"
#include <cstdio>
#include <cstdarg>
#include <set>
namespace {
std::set<log_sink_ptr>& log_formatter_getSinks() {
static std::set<log_sink_ptr> sinks;
return sinks;
}
std::set<log_cleanup_ptr>& log_formatter_getCleanupFuncs() {
static std::set<log_cleanup_ptr> cleanupFuncs;
return cleanupFuncs;
}
}
#ifdef __cplusplus
extern "C" {
#endif
extern char* log_formatter_format(const char* section, int level, const char* fmt, va_list arguments);
void log_backend_registerSink(log_sink_ptr sink) { log_formatter_getSinks().insert(sink); }
void log_backend_unregisterSink(log_sink_ptr sink) { log_formatter_getSinks().erase(sink); }
void log_backend_registerCleanup(log_cleanup_ptr cleanupFunc) { log_formatter_getCleanupFuncs().insert(cleanupFunc); }
void log_backend_unregisterCleanup(log_cleanup_ptr cleanupFunc) { log_formatter_getCleanupFuncs().erase(cleanupFunc); }
/**
* @name logging_backend
* ILog.h backend implementation.
*/
///@{
/// Eventually formats and routes the record to all sinks
void log_backend_record(const char* section, int level, const char* fmt, va_list arguments)
{
const std::set<log_sink_ptr>& sinks = log_formatter_getSinks();
if (sinks.empty()) {
static bool warned = false;
if (!warned) {
warned = true;
fprintf(stderr,
"\nWARNING: A log message was recorded, but no sink is registered."
"\n (there will be no further warnings)\n\n");
}
} else {
// format the record
const char* record = log_formatter_format(section, level, fmt, arguments);
// sink the record into each registered sink
for (auto si = sinks.begin(); si != sinks.end(); ++si) {
(*si)(section, level, record);
}
delete[] record;
}
}
/// Passes on a cleanup request to all sinks
void log_backend_cleanup() {
const std::set<log_cleanup_ptr>& cleanupFuncs = log_formatter_getCleanupFuncs();
for (auto si = cleanupFuncs.begin(); si != cleanupFuncs.end(); ++si) {
(*si)();
}
}
///@}
#ifdef __cplusplus
} // extern "C"
#endif
|