-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog.cpp
41 lines (34 loc) · 1.01 KB
/
log.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
#include <ctime>
#include <mutex>
#include <fstream>
#include "log.h"
#include "options.h"
// std::localtime not thread safe
static std::mutex localtime_lock;
void log(const std::string& msg, const LogType prefix, const bool err)
{
if (err)
{
std::cerr << prefix << ": " << msg << std::endl;
}
if (LOGGING)
{
// std::localtime isn't thread safe
std::unique_lock<std::mutex> lck(localtime_lock);
char timestamp[50] = "";
time_t now = std::time(nullptr);
std::strftime(timestamp, 50, "%c %Z", std::localtime(&now));
std::ofstream log(LOG_FILE, std::ios_base::out|std::ios_base::app);
log << timestamp << "\t" << prefix << ": " << msg << std::endl;
}
}
std::ostream& operator<<(std::ostream& os, const LogType& t)
{
switch(t)
{
case LogType::Error: return os << "Error";
case LogType::Warning: return os << "Warning";
case LogType::Notice: return os << "Notice";
default: return os << "Unknown";
}
}