-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.hpp
More file actions
255 lines (185 loc) · 4.63 KB
/
Copy pathsystem.hpp
File metadata and controls
255 lines (185 loc) · 4.63 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// === (C) 2020/2021 === parallel_f / system (tasks, queues, lists in parallel
// threads) Written by Denis Oliver Kropp <Leichenbegatter@outlook.com>
#pragma once
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include <string.h>
#include <stdarg.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
// parallel_f :: system == implementation
namespace parallel_f {
#ifdef _WIN32
class sysclock {
private:
LARGE_INTEGER frequency;
LARGE_INTEGER last;
public:
sysclock() {
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&last);
}
float reset() {
LARGE_INTEGER current;
QueryPerformanceCounter(¤t);
float ret = (current.QuadPart - last.QuadPart) / (float)frequency.QuadPart;
last = current;
return ret;
}
float get() {
LARGE_INTEGER current;
QueryPerformanceCounter(¤t);
float ret = (current.QuadPart - last.QuadPart) / (float)frequency.QuadPart;
// last = current;
return ret;
}
};
#else
class sysclock {
private:
long long last_us;
static long long get_us() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000LL + (long long)tv.tv_usec;
}
public:
sysclock() { last_us = get_us(); }
float reset() {
long long current = get_us();
float ret = (current - last_us) / 1000000.0f;
last_us = current;
return ret;
}
float get() {
long long current = get_us();
float ret = (current - last_us) / 1000000.0f;
// last_us = current;
return ret;
}
};
#endif
class system {
public:
static system &instance() {
static system system_instance;
return system_instance;
}
enum class AutoFlush { Never, Always, EndOfLine };
public:
/* returns local time of day in ms (0-86399999) */
static unsigned int localtime_ms() {
#ifdef _WIN32
SYSTEMTIME ST;
GetLocalTime(&ST);
return ST.wHour * 60 * 60 * 1000 + ST.wMinute * 60 * 1000 +
ST.wSecond * 1000 + ST.wMilliseconds;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
#endif
}
private:
sysclock clock;
int debug_level;
std::map<std::string, int> debug_levels;
std::stringstream slog;
std::mutex llog;
AutoFlush flog;
public:
static float get_time() { return instance().clock.get(); }
public:
system() : debug_level(0), flog(AutoFlush::Never), flush_thread_stop(false) {}
~system() {
stop_flush_thread();
flush();
}
int get_debug_level() { return debug_level; }
int get_debug_level(std::string str) {
for (auto it : debug_levels) {
if (str.find(it.first) != str.npos)
return it.second;
}
return 0;
}
void set_debug_level(int level) { debug_level = level; }
void set_debug_level(std::string str, int level) {
debug_levels[str] = level;
}
public:
void log(const char *fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt,
args); // FIXME: handle return value (overflow etc)
va_end(args);
std::unique_lock<std::mutex> lock(llog);
slog << buf;
switch (flog) {
case AutoFlush::EndOfLine:
if (buf[strlen(buf) - 1] != '\n')
return;
/* fall through */
case AutoFlush::Always:
lock.unlock();
flush();
break;
case AutoFlush::Never:
break;
}
}
void flush() {
std::unique_lock<std::mutex> lock(llog);
std::cerr << slog.str();
slog = std::stringstream();
}
void set_auto_flush(AutoFlush auto_flush = AutoFlush::EndOfLine) {
flog = auto_flush;
}
private:
bool flush_thread_stop;
std::unique_ptr<std::thread> flush_thread;
public:
void start_flush_thread(unsigned int ms) {
if (!flush_thread) {
flush_thread = std::make_unique<std::thread>([this, ms]() {
while (!flush_thread_stop) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
flush();
}
flush();
});
}
}
void stop_flush_thread() {
if (flush_thread) {
flush_thread_stop = true;
flush_thread->join();
flush_thread.reset();
}
}
};
static inline int get_debug_level() {
return system::instance().get_debug_level();
}
static inline int get_debug_level(std::string str) {
return system::instance().get_debug_level(str);
}
static inline void set_debug_level(int level) {
system::instance().set_debug_level(level);
}
static inline void set_debug_level(std::string str, int level) {
system::instance().set_debug_level(str, level);
}
} // namespace parallel_f