log: allow setting the default_sink

This commit is contained in:
Danny Robson 2019-10-10 16:02:10 +11:00
parent 67ea686753
commit 7a5fa7fa2d
3 changed files with 30 additions and 8 deletions

View File

@ -12,6 +12,10 @@
#include <string> #include <string>
// Windows.h or one of its friends defines a macro 'ERROR'. Screw Microsoft.
#ifdef ERROR
#undef ERROR
#endif
namespace cruft::log { namespace cruft::log {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////

View File

@ -14,6 +14,7 @@
#include "sink/console.hpp" #include "sink/console.hpp"
#include <mutex>
#include <map> #include <map>
#include <string> #include <string>
@ -42,11 +43,30 @@ cruft::log::needs_break (level_t level)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static std::once_flag s_sink_init;
static std::unique_ptr<cruft::log::sink::base> s_default_sink;
//-----------------------------------------------------------------------------
cruft::log::sink::base&
cruft::log::default_sink (std::unique_ptr<sink::base> value)
{
s_default_sink = std::move (value);
return *s_default_sink;
}
//-----------------------------------------------------------------------------
cruft::log::sink::base& cruft::log::sink::base&
cruft::log::default_sink () cruft::log::default_sink ()
{ {
static sink::console s_default_sink (PACKAGE_NAME); std::call_once (s_sink_init, [&] () {
return s_default_sink; default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
});
return *s_default_sink;
} }

View File

@ -13,19 +13,17 @@
#include "sink/base.hpp" #include "sink/base.hpp"
#include "../format.hpp" #include "../format.hpp"
#include <memory>
#include <iosfwd>
#include <string> #include <string>
// Windows.h or one of its friends defines a macro 'ERROR'. Screw Microsoft.
#ifdef ERROR
#undef ERROR
#endif
namespace cruft::log { namespace cruft::log {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
sink::base& default_sink (void); sink::base& default_sink (void);
sink::base& default_sink (std::unique_ptr<sink::base>);
//-------------------------------------------------------------------------
void write (level_t, std::string const &msg); void write (level_t, std::string const &msg);
void write (level_t, char const *msg); void write (level_t, char const *msg);
void write (level_t, std::string_view msg); void write (level_t, std::string_view msg);