log/sink: implement ostream sink
This commit is contained in:
parent
5cc0688b35
commit
c481a7cf38
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "ostream.hpp"
|
||||||
|
|
||||||
|
#include "../level.hpp"
|
||||||
|
#include "../packet.hpp"
|
||||||
|
|
||||||
|
using cruft::log::sink::ostream;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
ostream::ostream (std::string name, std::filesystem::path const &dst)
|
||||||
|
: crtp (std::move (name))
|
||||||
|
, m_output (dst)
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
void
|
||||||
|
ostream::write (packet const &val)
|
||||||
|
{
|
||||||
|
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 ();
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "base.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
|
||||||
|
namespace cruft::log::sink {
|
||||||
|
class ostream : public crtp<ostream> {
|
||||||
|
public:
|
||||||
|
ostream (
|
||||||
|
std::string name,
|
||||||
|
std::filesystem::path const &path
|
||||||
|
);
|
||||||
|
|
||||||
|
void write (packet const&) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ofstream m_output;
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user