-
Notifications
You must be signed in to change notification settings - Fork 3
/
log.h
61 lines (51 loc) · 973 Bytes
/
log.h
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
#ifndef LOG_H
#define LOG_H
#include <string>
#include <sstream>
#include <mutex>
#include <ctime>
struct Log
{
static Log& get() {
static Log instance;
return instance;
}
static void write(const std::string& added) {
time_t start = time(NULL);
Log& got = get();
while (!got.mutex.try_lock())
if (difftime(time(NULL), start) > 2)
throw(std::runtime_error("Could not lock report mutex"));
got.contents.append(added);
got.mutex.unlock();
}
static std::string& read() {
return get().contents;
}
void clear() {
contents.clear();
}
private:
Log();
~Log();
std::mutex mutex;
std::string contents;
Log(Log const&) = delete;
void operator=(Log const&) = delete;
};
#include <iostream>
class logStream {
public:
logStream() { }
~logStream() {
Log::write(stream.str());
}
template <class T>
std::stringstream& operator<< (T input) {
stream << input;
return stream;
}
private:
std::stringstream stream;
};
#endif // LOG_H