2012-04-24 17:38:35 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2012-04-24 17:38:35 +10:00
|
|
|
*
|
2016-01-20 15:37:52 +11:00
|
|
|
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
|
2012-04-24 17:38:35 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "log.hpp"
|
|
|
|
|
2019-10-10 15:10:41 +11:00
|
|
|
#include "../cast.hpp"
|
|
|
|
#include "../string.hpp"
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2019-10-10 15:52:37 +11:00
|
|
|
#include "sink/console.hpp"
|
|
|
|
|
2012-04-26 18:22:05 +10:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2016-03-21 14:21:14 +11:00
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2019-10-10 15:52:37 +11:00
|
|
|
bool
|
|
|
|
cruft::log::needs_break (level_t level)
|
2016-01-20 15:36:32 +11:00
|
|
|
{
|
2019-10-10 15:52:37 +11:00
|
|
|
static level_t break_level;
|
2017-01-31 20:30:38 +11:00
|
|
|
|
2016-01-21 13:55:23 +11:00
|
|
|
static bool has_level = [&] (void) {
|
|
|
|
const char *env = getenv ("BREAK_LEVEL");
|
|
|
|
if (!env)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2019-10-10 15:10:41 +11:00
|
|
|
break_level = cruft::log::to_level (env);
|
2016-01-21 13:55:23 +11:00
|
|
|
return true;
|
|
|
|
} catch (...) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} ();
|
|
|
|
|
|
|
|
return has_level && level <= break_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-10 15:52:37 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
cruft::log::sink::base&
|
|
|
|
cruft::log::default_sink ()
|
2016-03-21 14:21:14 +11:00
|
|
|
{
|
2019-10-10 15:52:37 +11:00
|
|
|
static sink::console s_default_sink (PACKAGE_NAME);
|
|
|
|
return s_default_sink;
|
2016-03-21 14:21:14 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2019-10-10 12:32:03 +11:00
|
|
|
cruft::log::write (level_t level, const std::string &msg)
|
2019-10-10 15:10:41 +11:00
|
|
|
{
|
|
|
|
return write (level, std::string_view (msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
cruft::log::write (level_t level, char const *msg)
|
|
|
|
{
|
|
|
|
return write (level, std::string_view (msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
cruft::log::write (level_t level, std::string_view msg)
|
2016-01-20 15:36:32 +11:00
|
|
|
{
|
2019-10-10 15:52:37 +11:00
|
|
|
return default_sink ().write (
|
|
|
|
packet (level, msg)
|
2019-10-10 12:32:03 +11:00
|
|
|
);
|
2015-07-23 17:37:43 +10:00
|
|
|
}
|