v2.0.0
Loading...
Searching...
No Matches
mnetracer.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35#include "mnetracer.h"
36
37#include <iostream>
38
39using namespace UTILSLIB;
40
41//=============================================================================================================
42// DEFINE STATIC MEMBER VARIABLES
43//=============================================================================================================
44
45static const char* defaultTracerFileName("default_MNETracer_file.json");
46bool MNETracer::ms_bIsEnabled(false);
47std::ofstream MNETracer::ms_OutputFileStream;
48bool MNETracer::ms_bIsFirstEvent(true);
49std::mutex MNETracer::ms_outFileMutex;
50long long MNETracer::ms_iZeroTime(0);
51
52//=============================================================================================================
53// DEFINE MEMBER METHODS
54//=============================================================================================================
55
56MNETracer::MNETracer(const std::string &file, const std::string &function, int lineNumber)
57: m_bIsInitialized(false)
58, m_bPrintToTerminal(false)
59, m_sFileName(file)
60, m_sFunctionName(function)
61, m_iLineNumber(lineNumber)
62, m_iThreadId("0")
63, m_iBeginTime(0)
64, m_iEndTime(0)
65, m_dDurationMilis(0.)
66{
67 if (ms_bIsEnabled)
68 {
69 initialize();
70 writeBeginEvent();
71 }
72}
73
74//=============================================================================================================
75
77{
78 if (ms_bIsEnabled && m_bIsInitialized)
79 {
80 registerFinalTime();
81 writeEndEvent();
82 if (m_bPrintToTerminal)
83 {
84 calculateDuration();
85 printDurationMiliSec();
86 }
87 }
88}
89
90//=============================================================================================================
91
92void MNETracer::enable(const std::string &jsonFileName)
93{
94 ms_OutputFileStream.open(jsonFileName);
95 writeHeader();
96 setZeroTime();
97 if (ms_OutputFileStream.is_open())
98 {
99 ms_bIsEnabled = true;
100 }
101}
102
103//=============================================================================================================
104
106{
107 enable(defaultTracerFileName);
108}
109
110//=============================================================================================================
111
113{
114 if (ms_bIsEnabled)
115 {
116 writeFooter();
117 ms_OutputFileStream.flush();
118 ms_OutputFileStream.close();
119 ms_bIsEnabled = false;
120 }
121}
122
123//=============================================================================================================
124
125void MNETracer::start(const std::string &jsonFileName)
126{
127 enable(jsonFileName);
128}
129
130//=============================================================================================================
131
133{
134 enable();
135}
136
137//=============================================================================================================
138
140{
141 disable();
142}
143
144//=============================================================================================================
145
146void MNETracer::traceQuantity(const std::string &name, long val)
147{
148 long long timeNow = getTimeNow() - ms_iZeroTime;
149 std::string s;
150 s.append("{\"name\":\"").append(name).append("\",\"ph\":\"C\",\"ts\":");
151 s.append(std::to_string(timeNow)).append(",\"pid\":1,\"tid\":1");
152 s.append(",\"args\":{\"").append(name).append("\":").append(std::to_string(val)).append("}}\n");
153 writeToFile(s);
154}
155
156//=============================================================================================================
157
158void MNETracer::initialize()
159{
160 registerConstructionTime();
161 registerThreadId();
162 formatFileName();
163 m_bIsInitialized = true;
164}
165
166//=============================================================================================================
167
168void MNETracer::setZeroTime()
169{
170 ms_iZeroTime = getTimeNow();
171}
172
173//=============================================================================================================
174
175void MNETracer::registerConstructionTime()
176{
177 m_iBeginTime = getTimeNow() - ms_iZeroTime;
178}
179
180//=============================================================================================================
181
182void MNETracer::registerFinalTime()
183{
184 m_iEndTime = getTimeNow() - ms_iZeroTime;
185}
186
187//=============================================================================================================
188
189long long MNETracer::getTimeNow()
190{
191 auto timeNow = std::chrono::high_resolution_clock::now();
192 return std::chrono::time_point_cast<std::chrono::microseconds>(timeNow).time_since_epoch().count();
193}
194
195//=============================================================================================================
196
197void MNETracer::registerThreadId()
198{
199 auto longId = std::hash<std::thread::id>{}(std::this_thread::get_id());
200 m_iThreadId = std::to_string(longId).substr(0, 5);
201}
202
203//=============================================================================================================
204
205void MNETracer::formatFunctionName()
206{
207 const char* pattern(" __cdecl");
208 constexpr int patternLenght(8);
209 size_t pos = m_sFunctionName.find(pattern);
210 if (pos != std::string::npos) {
211 m_sFunctionName.replace(pos, patternLenght, "");
212 }
213}
214
215//=============================================================================================================
216
217void MNETracer::formatFileName()
218{
219 const char* patternIn("\\");
220 const char* patternOut("\\\\");
221 constexpr int patternOutLength(4);
222 size_t start_pos = 0;
223 while ((start_pos = m_sFileName.find(patternIn, start_pos)) != std::string::npos)
224 {
225 m_sFileName.replace(start_pos, 1, patternOut);
226 start_pos += patternOutLength;
227 }
228}
229
230//=============================================================================================================
231
232void MNETracer::calculateDuration()
233{
234 m_dDurationMilis = (m_iEndTime - m_iBeginTime) * 0.001;
235}
236
237//=============================================================================================================
238
239void MNETracer::printDurationMiliSec()
240{
241 std::cout << "Scope: " << m_sFileName << " - " << m_sFunctionName << " DurationMs: " << m_dDurationMilis << "ms.\n";
242}
243
244//=============================================================================================================
245
246void MNETracer::writeHeader()
247{
248 writeToFile("{\"displayTimeUnit\": \"ms\",\"traceEvents\":[\n");
249}
250
251//=============================================================================================================
252
253void MNETracer::writeFooter()
254{
255 writeToFile("]}");
256}
257
258//=============================================================================================================
259
260void MNETracer::writeToFile(const std::string& str)
261{
262 ms_outFileMutex.lock();
263 if(ms_OutputFileStream.is_open()) {
264 ms_OutputFileStream << str;
265 }
266 ms_outFileMutex.unlock();
267}
268
269//=============================================================================================================
270
271void MNETracer::writeBeginEvent()
272{
273 std::string s;
274 if (!ms_bIsFirstEvent)
275 s.append(",");
276
277 s.append("{\"name\":\"").append(m_sFunctionName).append("\",\"cat\":\"bst\",");
278 s.append("\"ph\":\"B\",\"ts\":").append(std::to_string(m_iBeginTime)).append(",\"pid\":1,\"tid\":");
279 s.append(m_iThreadId).append(",\"args\":{\"file path\":\"").append(m_sFileName).append("\",\"line number\":");
280 s.append(std::to_string(m_iLineNumber)).append("}}\n");
281 writeToFile(s);
282 ms_bIsFirstEvent = false;
283}
284
285//=============================================================================================================
286
287void MNETracer::writeEndEvent()
288{
289 std::string s;
290 s.append(",{\"name\":\"").append(m_sFunctionName).append("\",\"cat\":\"bst\",");
291 s.append("\"ph\":\"E\",\"ts\":").append(std::to_string(m_iEndTime)).append(",\"pid\":1,\"tid\":");
292 s.append(m_iThreadId).append(",\"args\":{\"file path\":\"").append(m_sFileName).append("\",\"line number\":");
293 s.append(std::to_string(m_iLineNumber)).append("}}\n");
294 writeToFile(s);
295}
296
297//=============================================================================================================
298
300{
301 return m_bPrintToTerminal;
302}
303
304//=============================================================================================================
305
307{
308 m_bPrintToTerminal = s;
309}
310
Declaration of a MNETracer object. This class allows a user to easily measure executions times of a f...
Shared utilities (I/O helpers, spectral analysis, layout management, warp algorithms).
Definition buildinfo.h:45
static void stop()
Convenience overload of the method disable.
static void enable()
static void disable()
disable If the class "is enabled" (it's static variabable ms_bIsEnabled is true), the output file has...
static void start()
Convenience overload of the method enable.
MNETracer(const std::string &file, const std::string &function, int lineNumber)
MNETracer constructor will check if the class "is enabled". If it is, it will record the creation tim...
Definition mnetracer.cpp:56
static void traceQuantity(const std::string &name, long val)
traceQuantity Allows to keep track of a specific variable in the output tracing file.
void setPrintToTerminal(bool s)