-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrlog.cpp
More file actions
161 lines (135 loc) · 2.85 KB
/
rlog.cpp
File metadata and controls
161 lines (135 loc) · 2.85 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// rlog Library (C++ 11)
//
// Version 0.1 (Last Updated: 8/5/2017)
//
// Description:
// This class implements a Logging utility for use within
// any type of program.
//
// Author:
// Richard "Alex" Riedel
//
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <queue>
#include <string>
#include <thread>
#include <utility>
#include "rlog.hpp"
rlog::rlog() : print_time(true), writer_thread(&rlog::__run, this)
{
writer_thread.detach();
const time_t temp = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::string tempStr = std::string("Log ") + ctime(&temp);
tempStr.pop_back();
tempStr.append(".txt");
std::replace(tempStr.begin(), tempStr.end(), ' ', '_');
filename = tempStr;
out.open(filename, std::ofstream::out | std::ofstream::app);
}
rlog::rlog(std::string filename, bool currentTime) : print_time(currentTime), writer_thread(&rlog::__run, this)
{
writer_thread.detach();
this->filename = filename;
out.open(filename, std::ofstream::out | std::ofstream::app);
}
rlog::rlog(const rlog& logger) : print_time(logger.print_time),
outputQueue(std::move(logger.outputQueue))
{
filename = logger.filename;
FLUSH = logger.FLUSH;
out.open(filename, std::ofstream::out | std::ofstream::app);
}
rlog::~rlog()
{
__flush();
out.close();
}
void rlog::log(std::string message, std::string header)
{
add(header.append(message));
}
void rlog::debug_log(std::string message, std::string header)
{
add(header.append(message));
}
void rlog::warning_log(std::string message, std::string header)
{
add(header.append(message));
}
void rlog::error_log(std::string message, std::string header)
{
add(header.append(message));
}
void rlog::fatal_log(std::string message, std::string header)
{
add(header.append(message));
}
void rlog::flush()
{
FLUSH = true;
}
std::queue<std::string>::size_type rlog::size()
{
return outputQueue.size();
}
void rlog::write(std::string& message)
{
out << message << std::endl;
}
void rlog::add(std::string& message)
{
lock();
outputQueue.push(message);
unlock();
}
void rlog::lock()
{
queue_io_mutex.lock();
}
void rlog::unlock()
{
queue_io_mutex.unlock();
}
void rlog::__flush()
{
lock();
while(!outputQueue.empty())
{
write(outputQueue.front());
outputQueue.pop();
}
unlock();
}
void rlog::__run()
{
// TODO: Fix this with less busy waiting.
while(true)
{
if(FLUSH)
{
__flush();
FLUSH = false;
}
/*queueNotEmpty.wait(queue_out_lock, [this]() -> bool
{
bool ret;
lock();
ret = !outputQueue.empty();
unlock();
return ret;
});
*/
lock();
if(!outputQueue.empty())
{
write(outputQueue.front());
outputQueue.pop();
}
unlock();
std::this_thread::sleep_for(std::chrono::duration<int>(RLOG_WRITER_THREAD_SLEEP_TIME));
}
}