Vita
log.cc
Go to the documentation of this file.
1
13#include <fstream>
14#include <iomanip>
15#include <iostream>
16
17#include "kernel/log.h"
18
19#define HAS_UNCAUGHT_EXCEPTIONS 1
20#include "third_party/date/date.h"
21
22namespace vita
23{
24
26std::unique_ptr<std::ostream> log::stream = nullptr;
27
31log::log() : os(), level_(lOUTPUT) {}
32
48std::ostringstream &log::get(level l)
49{
50 level_ = std::min(lFATAL, l);
51 return os;
52}
53
54log::~log()
55{
56 static const std::string tags[] =
57 {
58 "ALL", "DEBUG", "INFO", "", "WARNING", "ERROR", "FATAL", ""
59 };
60
61 if (stream) // `stream`, if available, gets all the messages
62 {
63 const auto tp(std::chrono::system_clock::now());
64 const date::year_month_day d(date::floor<date::days>(tp));
65
66 *stream << date::format("%T", d) << '\t' << tags[level_] << '\t'
67 << os.str() << std::endl;
68 }
69
70 if (level_ >= reporting_level) // `cout` is selective
71 {
72 std::string tag;
73 if (level_ != lOUTPUT)
74 tag = "[" + tags[level_] + "] ";
75
76 std::cout << tag << os.str() << std::endl;
77 }
78}
79
89void log::setup_stream(const std::string &base)
90{
91 const auto tp(std::chrono::system_clock::now());
92 const date::year_month_day d(date::floor<date::days>(tp));
93
94 std::ostringstream fn;
95 fn << base << date::format("_%j_%H_%M_%S", d) << ".log";
96
97 stream = std::make_unique<std::ofstream>(fn.str());
98}
99
100} // namespace vita
static std::unique_ptr< std::ostream > stream
Optional log stream.
Definition: log.h:53
std::ostringstream & get(level=lOUTPUT)
Sets the logging level of a message.
Definition: log.cc:48
log()
Creates a log object.
Definition: log.cc:31
static void setup_stream(const std::string &base)
Sets the log::stream variable with a convenient value.
Definition: log.cc:89
level
The log level.
Definition: log.h:47
static level reporting_level
Messages with a lower level aren't logged / printed.
Definition: log.h:50
base_t base(const value_t &v)
A simple shortcut for casting an value_t to base_t.
Definition: real.h:49
The main namespace for the project.