2020-04-21 10:55:36 +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/.
|
|
|
|
*
|
|
|
|
* Copyright 2020, Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2020-04-21 12:42:05 +10:00
|
|
|
#include "path.hpp"
|
2020-04-21 10:55:36 +10:00
|
|
|
|
|
|
|
#include "../level.hpp"
|
|
|
|
#include "../packet.hpp"
|
2020-04-21 12:42:05 +10:00
|
|
|
#include "../../paths.hpp"
|
2021-04-12 15:53:13 +10:00
|
|
|
#include "../../debug/warn.hpp"
|
2020-04-21 10:55:36 +10:00
|
|
|
|
2020-04-21 12:42:05 +10:00
|
|
|
using cruft::log::sink::path;
|
2020-04-21 10:55:36 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2020-04-21 12:42:05 +10:00
|
|
|
static std::filesystem::path
|
|
|
|
find_path (void)
|
|
|
|
{
|
|
|
|
if (char const *log_path = getenv ("CRUFT_LOG_PATH"); log_path)
|
|
|
|
return cruft::paths::expand (log_path);
|
|
|
|
|
|
|
|
static constexpr char DEFAULT_PATH[] = PACKAGE_NAME ".log";
|
|
|
|
return std::filesystem::current_path () / DEFAULT_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
path::path (std::string name)
|
2020-04-21 10:55:36 +10:00
|
|
|
: crtp (std::move (name))
|
2020-04-21 12:42:05 +10:00
|
|
|
, m_output (find_path ())
|
2020-04-21 10:55:36 +10:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2020-04-21 12:42:05 +10:00
|
|
|
path::write (packet const &val)
|
2020-04-21 10:55:36 +10:00
|
|
|
{
|
|
|
|
if (val.level > log_level ())
|
|
|
|
return;
|
|
|
|
|
|
|
|
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
|
|
|
|
std::string time_string (time_len - 1, '\0');
|
|
|
|
time_t unix_time = time (nullptr);
|
|
|
|
|
|
|
|
if (0 == strftime (&time_string[0],
|
|
|
|
time_len,
|
|
|
|
"%Y-%m-%d %H%Mh%S",
|
|
|
|
localtime (&unix_time)))
|
|
|
|
{
|
|
|
|
warn ("failed to log time");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_output
|
|
|
|
<< time_string << " ["
|
|
|
|
<< std::setw (cruft::cast::lossless<int> (level_width ()))
|
|
|
|
<< std::left
|
|
|
|
<< val.level
|
|
|
|
<< std::setw (0)
|
|
|
|
<< "] " << val.message << std::endl;
|
|
|
|
|
|
|
|
if (needs_break (val.level))
|
|
|
|
breakpoint ();
|
|
|
|
}
|