-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
56 lines (46 loc) · 1.16 KB
/
logger.cpp
File metadata and controls
56 lines (46 loc) · 1.16 KB
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
#include "logger.hpp"
#include <exception>
using namespace std;
using namespace pas;
Logger::Logger(string path, LogLevel l)
{
log.open(path);
if (!log.is_open())
throw std::runtime_error("failed to open log file");
level = l;
}
Logger::~Logger()
{
if (log.is_open())
log.close();
}
void Logger::Sync()
{
if (log.is_open())
log.flush();
}
LoggedException Logger::Add(const char * file, const char * function, const int line, const string message, LogLevel ll)
{
return Add(file, function, line, message.c_str(), ll);
}
LoggedException Logger::Add(const char * file, const char * function, const int line, const char * message, LogLevel ll)
{
// Reject rediculous messages if we're not on that setting.
// Reject verbose messages if the log is closed.
if ((ll == REDICULOUS && ll != level) || (ll == VERBOSE && !log.is_open())) {
return LoggedException("");
}
stringstream ss;
string s;
ss << setw(24) << left << file << " " << setw(24) << function << " " << setw(6) << line;
if (message != nullptr)
ss << " " << message;
s = ss.str();
if (ll <= level && log.is_open())
{
m.lock();
log << s << endl;
m.unlock();
}
return LoggedException(s, ll);
}