-
Notifications
You must be signed in to change notification settings - Fork 6
/
tasktimer.h
144 lines (115 loc) · 3.62 KB
/
tasktimer.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
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
#pragma once
#include "timer.h"
#include <stdarg.h>
#if defined(__cplusplus) && !defined(__CUDACC__)
#include <ostream>
#endif
#include <boost/format.hpp>
/**
The TaskTimer class should log how long time it takes to execute a scope while
distinguishing nested scopes and different threads.
A canonical example
-------------------
{
TaskTimer tt("Doing this slow thing");
doSlowThing();
}
Example output:
12:49:36.241581 Doing this slow thing... done in 100 ms.
Where "12:49:36.241581" is the time when creating TaskTimer and
"done in 100 ms." will be sent to cout when doSlowThing has
returned, and 'TaskTimer tt' is going out of scope.
Nested and formatted logging
----------------------------
{
int N = 2;
TaskTimer tt("Doing these %d slow things", N);
for (int i=0; i<N; ++i) {
TaskTimer tt(boost::format("Thing %d") % i);
doSlowThing();
}
}
Example output:
12:49:36.200000 Doing these 2 slow things
12:49:36.200000 - Thing 0... done in 100 ms.
12:49:36.300000 - Thing 1... done in 100 ms.
12:49:36.400000 done in 200 ms.
Running the previous example in two threads
-------------------------------------------
Example output:
12:49:36.200000 Doing these 2 slow things
12:49:36.200000 - Thing 0... done in 100 ms.
12:49:36.250000 1 Doing these 2 slow things
12:49:36.250000 1 - Thing 0... done in 100 ms.
12:49:36.300000 - Thing 1... done in 100 ms.
12:49:36.350000 1 - Thing 1... done in 100 ms.
12:49:36.400000 done in 200 ms.
12:49:36.450000 1 done in 200 ms.
Showing progress
----------------
{
TaskTimer tt("Doing this slow thing");
initialize();
for (int i=0; i<9; ++i) {
tt.partlyDone();
doSlowThing();
}
}
Where tt.partlyDone() will output an extra dot "." for each call.
Use TaskInfo to omit "done in 100 ms."
*/
class TaskTimer {
public:
enum LogLevel {
LogVerbose = 0,
LogDetailed = 1,
LogSimple = 2
};
TaskTimer(LogLevel logLevel, const char* task, ...);
TaskTimer(bool, LogLevel logLevel, const char* task, va_list args);
TaskTimer(const char* task, ...);
TaskTimer(bool, const char* task, va_list args);
TaskTimer(const boost::format& fmt);
TaskTimer();
TaskTimer(const TaskTimer&) = delete;
TaskTimer& operator=(const TaskTimer&) = delete;
~TaskTimer();
static void this_thread_quit();
void info(const char* taskInfo, ...);
void partlyDone();
void suppressTiming();
double elapsedTime();
#if defined(__cplusplus) && !defined(__CUDACC__)
static void setLogLevelStream( LogLevel logLevel, std::ostream* str );
static bool isEnabled(LogLevel logLevel);
#endif
static bool enabled();
static void setEnabled( bool );
static std::string timeToString( double T );
private:
Timer timer_{false};
unsigned numPartlyDone;
bool is_unwinding;
bool suppressTimingInfo;
LogLevel logLevel;
TaskTimer* upperLevel; // obsolete
//TaskTimer& getCurrentTimer();
void init(LogLevel logLevel, const char* task, va_list args);
void initEllipsis(LogLevel logLevel, const char* f, ...);
void vinfo(const char* taskInfo, va_list args);
void logprint(const char* txt);
bool printIndentation();
};
class TaskInfo {
public:
TaskInfo(const char* task, ...);
TaskInfo(const boost::format&);
TaskInfo(const TaskInfo&) = delete;
TaskInfo& operator=(const TaskInfo&) = delete;
~TaskInfo();
TaskTimer& tt() { return *tt_; }
private:
TaskTimer* tt_;
};
#define TaskLogIfFalse(X) if (false == (X)) TaskInfo("! Not true: %s", #X)
#define TIME(x) do { TaskTimer tt("%s", #x); x; } while(false)