-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.cpp
89 lines (71 loc) · 2.21 KB
/
error.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "error.hpp"
using namespace rogue;
//-----------------------------------------------------------------------------
LogSinkFile rogue::g_logSinkFile;
LogLevel rogue::g_logLevel = LogLevel::None;
//-----------------------------------------------------------------------------
void LogSink::Log(LogLevel level, const vector<pair<string, string> >& msg)
{
}
//-----------------------------------------------------------------------------
LogSinkFile::LogSinkFile()
: _log(nullptr)
{
}
//-----------------------------------------------------------------------------
LogSinkFile::~LogSinkFile()
{
if (_log)
fclose(_log);
}
//-----------------------------------------------------------------------------
bool LogSinkFile::Open(const char* filename)
{
_log = fopen(filename, "at");
return !!_log;
}
//-----------------------------------------------------------------------------
void LogSinkFile::Log(LogLevel level, const vector<pair<string, string> >& msg)
{
static char levelPrefix[] = { '-', 'D', 'I', 'W', 'E' };
if (!_log)
return;
ptime now = microsec_clock::local_time();
fprintf(_log, "[%c] %s - ", levelPrefix[(int)level], boost::posix_time::to_iso_extended_string(now).c_str());
for (size_t i = 0; i < msg.size(); ++i)
{
auto& p = msg[i];
bool last = i == msg.size() - 1;
fprintf(_log, "%s=%s%c", p.first.c_str(), p.second.c_str(), last ? '\n' : '|');
}
fflush(_log);
}
//-----------------------------------------------------------------------------
LogStream::LogStream(LogSink* sink, LogLevel level)
: _sink(sink)
, _level(level)
{
}
//-----------------------------------------------------------------------------
LogStream::~LogStream()
{
if (_level < g_logLevel)
return;
if (_sink)
_sink->Log(_level, _output);
}
//-----------------------------------------------------------------------------
void LogStream::Append(const string& key, const string& value)
{
if (_level < g_logLevel)
return;
_output.push_back(make_pair(key, value));
}
//-----------------------------------------------------------------------------
LogStream& rogue::operator<<(LogStream& s, const char* desc)
{
std::ostringstream str;
str << desc;
s.Append("desc", desc);
return s;
}