forked from frankyeh/TIPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog.hpp
More file actions
348 lines (328 loc) · 10.1 KB
/
prog.hpp
File metadata and controls
348 lines (328 loc) · 10.1 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef TIPL_PROG_HPP
#define TIPL_PROG_HPP
#include <memory>
#include <ctime>
#include <iostream>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
#include <QProgressDialog>
#include <QApplication>
#endif
#include "mt.hpp"
#include "po.hpp"
namespace tipl{
inline bool prog_aborted = false;
inline bool show_prog = false;
inline std::vector<std::chrono::high_resolution_clock::time_point> process_time;
inline std::chrono::high_resolution_clock::time_point next_update_time = std::chrono::high_resolution_clock::now();
inline std::vector<std::string> status_list,at_list;
inline std::atomic_int status_count = 0;
inline std::mutex print_mutex,msg_mutex;
inline std::string last_msg;
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
inline std::shared_ptr<QProgressDialog> progressDialog;
#endif
// can only called in main thread
inline std::string get_prog_status(void)
{
std::string result;
for(size_t i = 1;i < status_list.size();++i)
{
if(status_list[i].empty())
continue;
result += tipl::split(status_list[i],'\n').front();
if(i < at_list.size())
result += " " + at_list[i];
if(result.back() != '\n')
result += "\n";
}
std::scoped_lock<std::mutex> lock2(msg_mutex);
if(last_msg.empty() && !result.empty())
result.pop_back();
return result+last_msg;
}
inline void create_prog(void)
{
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(!show_prog || !tipl::is_main_thread())
return;
progressDialog.reset(new QProgressDialog(get_prog_status().c_str(),"Cancel",0,100));
progressDialog->setAttribute(Qt::WA_ShowWithoutActivating);
progressDialog->activateWindow();
progressDialog->show();
progressDialog->raise();
QApplication::processEvents();
#endif
}
inline void close_prog(void)
{
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(!show_prog || !tipl::is_main_thread() || status_count > 1)
return;
if(progressDialog.get())
{
progressDialog->close();
progressDialog.reset();
QApplication::processEvents();
}
#endif
}
class progress{
private:
void begin_prog(const std::string& status,bool show_now = false)
{
if(!tipl::is_main_thread())
return;
prog_aborted = false;
status_list.push_back(status);
++status_count;
process_time.resize(status_list.size());
process_time.back() = std::chrono::high_resolution_clock::now();
last_msg.clear();
if(show_now)
create_prog();
}
static bool check_prog(unsigned int now,unsigned int total)
{
if(!show_prog || !tipl::is_main_thread() || !status_count)
{
if(prog_aborted)
return false;
return now < total;
}
if(now >= total || prog_aborted)
{
if(at_list.size() == status_list.size())
at_list.back().clear();
return false;
}
if(std::chrono::high_resolution_clock::now() < next_update_time)
return true;
next_update_time = std::chrono::high_resolution_clock::now()+std::chrono::milliseconds(500);
at_list.resize(status_list.size());
std::ostringstream outstr;
outstr << "(" << now << "/" << total << ")";
{
int exp_min = (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() -
process_time.back()).count()*(total-now)/(now+1)/1000/60);
if(exp_min)
outstr << " " << exp_min << " min";
}
at_list.back() = outstr.str();
#if defined(TIPL_USE_QT) && !defined(__CUDACC__)
if(!progressDialog.get())
create_prog();
progressDialog->setLabelText(get_prog_status().c_str());
progressDialog->adjustSize();
progressDialog->setRange(0, int(total));
progressDialog->setValue(int(now));
if(progressDialog->wasCanceled())
{
prog_aborted = true;
progress::print("operation aborted",false,false,1);
}
QApplication::processEvents();
#endif
return !prog_aborted;
}
static std::string get_head(bool head_node, bool tail_node)
{
std::string head;
for(size_t i = 1;i < status_count;++i)
head += "│ ";
if(!tipl::is_main_thread())
{
head += "│ ";
return head;
}
if(status_count)
{
if(head_node)
head += "├──┬──";
else
if(tail_node)
head += "│ ";
else
head += "├──";
}
else
if(head_node)
head = "┌──";
if(tail_node)
head += "└──";
return head;
}
static std::string get_color_line(std::string line,bool head_node,int error_code)
{
std::string color_end = "\033[0m";
std::string color31 = "\033[1;31m";
std::string color32 = "\033[0;32m";
std::string color33 = "\033[0;33m";
std::string color34 = "\033[1;34m";
std::string color35 = "\033[1;35m";
if(error_code == 1) // ❌
return color31 + "❌" + line + color_end;
if(error_code == 2) // ❗
return color31 + "❗" + line + color_end;
if(tipl::begins_with(line,"sav"))
return color35 + "💾" + line + color_end;
if(tipl::begins_with(line,"open"))
return color35 + "📂" + line + color_end;
if(head_node)
return color34 + std::string("📟") + line + color_end;
auto eq_pos = line.find('=');
if(eq_pos != std::string::npos)
return color32 + line.substr(0,eq_pos) + color_end + line.substr(eq_pos);
auto info_pos = line.find(": ");
if(info_pos != std::string::npos)
return color33 + line.substr(0,info_pos) + color_end + line.substr(info_pos);
return line;
}
public:
static void print(const std::string& status,bool head_node, bool tail_node,int error_code = 0)
{
std::scoped_lock<std::mutex> lock(print_mutex);
std::istringstream in(status);
std::string line;
while(std::getline(in,line))
{
if(line.empty())
continue;
auto new_line = get_color_line(line,head_node,error_code);
if(!tipl::is_main_thread())
{
std::ostringstream out;
out << "[thread " << std::this_thread::get_id() << "]" << new_line;
new_line = out.str();
}
std::cout << get_head(head_node,tail_node) + new_line << std::endl;
head_node = false;
}
}
public:
bool temporary = false;
progress(void):temporary(true){}
progress(const std::string& status,bool show_now = false)
{
print(status,true,false);
begin_prog(status,show_now);
}
progress(const std::string& status1,const std::string& status2,bool show_now = false)
{
std::string s(status1);
s += status2;
print(s.c_str(),true,false);
begin_prog(s.c_str(),show_now);
}
static bool is_running(void) {return status_count > 1;}
static bool aborted(void) { return prog_aborted;}
template<typename value_type1,typename value_type2>
bool operator()(value_type1 now,value_type2 total)
{
return check_prog(uint32_t(now),uint32_t(total));
}
~progress(void)
{
if(!tipl::is_main_thread() || temporary)
return;
std::ostringstream out;
{
std::string unit("ms");
float count = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - process_time.back()).count();
if(count > 1000.0f)
{
count /= 1000.0f;
unit = "s";
if(count > 60.0f)
{
count /= 60.0;
unit = "m";
if(count > 60.0f)
{
count /= 60.0f;
unit = "h";
}
}
}
out << "⏱" << count << unit;
}
status_list.pop_back();
--status_count;
print(out.str().c_str(),false,true);
process_time.pop_back();
if(status_list.empty())
at_list.clear();
{
std::scoped_lock<std::mutex> lock2(msg_mutex);
last_msg.clear();
}
close_prog();
}
};
template<typename fun_type>
bool run(const std::string& msg,fun_type fun)
{
if(!show_prog)
{
fun();
return true;
}
progress prog(msg);
std::atomic_bool ended = false;
tipl::par_for(2,[&](int i)
{
if(!i)
{
fun();
ended = true;
}
else
{
size_t i = 0;
while(!ended)
{
prog(i,i+1);
if(prog.aborted())
return;
++i;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
},2);
return !prog.aborted();
}
template<int code>
class output{
std::ostringstream s;
public:
~output()
{
auto out = s.str();
if(out.empty())
return;
if(out.back() == '\n')
out.pop_back();
progress::print(out,false,false,code);
std::scoped_lock<std::mutex> lock2(msg_mutex);
std::getline(std::istringstream(out),last_msg);
}
output& operator<<(std::ostream& (*var)(std::ostream&))
{
s << var;
return *this;
}
template<typename type>
output& operator<<(const type& v)
{
s << v;
return *this;
}
};
using out = output<0>;
using error = output<1>; //❌
using warning = output<2>; //❗
}
#endif//TIPL_PROG_HPP