log: choose sink based on LOG_SINK

This commit is contained in:
Danny Robson 2020-04-21 10:56:04 +10:00
parent c481a7cf38
commit 8961f097cd

View File

@ -13,7 +13,9 @@
#include "../string.hpp"
#include "sink/console.hpp"
#include "sink/ostream.hpp"
#include <iostream>
#include <mutex>
#include <map>
#include <string>
@ -63,9 +65,28 @@ cruft::log::sink::base&
cruft::log::default_sink ()
{
if (unlikely (!s_sink_init)) {
default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
static char const *DEFAULT_SINK = "CONSOLE";
char const *log_sink = getenv ("CRUFT_LOG_SINK") ?: DEFAULT_SINK;
if (!strcmp (log_sink, "FILE")) {
char const *log_path = getenv ("CRUFT_LOG_PATH");
default_sink (
std::make_unique<sink::ostream> (
PACKAGE_NAME,
log_path
)
);
} else if (strcmp (log_sink, "CONSOLE")) {
default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
} else {
std::clog << "Unknown logging sink. Falling back to console.\n";
default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
}
s_sink_init = true;
}