v2.0.0
Loading...
Searching...
No Matches
mnetracer.h
Go to the documentation of this file.
1//=============================================================================================================
35#ifndef TRACER_H
36#define TRACER_H
37
38//=============================================================================================================
39// MACRO DEFINITIONS
40//=============================================================================================================
41
42#ifdef TRACE
43#define MNE_TRACE() UTILSLIB::MNETracer _mneTracer__LINE__(__FILE__,__func__,__LINE__);
44#define MNE_TRACER_ENABLE(FILENAME) UTILSLIB::MNETracer::enable(#FILENAME);
45#define MNE_TRACER_DISABLE UTILSLIB::MNETracer::disable();
46#define MNE_TRACE_VALUE(NAME, VALUE) UTILSLIB::MNETracer::traceQuantity(NAME, VALUE);
47#else
48#define MNE_TRACE()
49#define MNE_TRACER_ENABLE(FILENAME)
50#define MNE_TRACER_DISABLE
51#define MNE_TRACE_VALUE()
52#endif
53
54#ifdef MNE_TRACE_MEMORY
55#define MNE_TRACE_MEMORY_REPORT \
56std::cout << "Total Memory Allocated : " << 0.000001 * __totalMemAllocated << " MB\n";\
57std::cout << "Total Memory Deleted : " << 0.000001 * __totalMemDeleted << " MB\n";\
58std::cout << "Total Memory Diff : " << 0.000001 * (__totalMemAllocated - __totalMemDeleted) << " MB\n";\
59
60size_t __totalMemAllocated(0);
61size_t __totalMemDeleted(0);
62void *operator new(size_t size)
63{
64 // std::cout << "Allocating " << size << " Bytes.\n";
65 __totalMemAllocated += size;
66 return malloc(size);
67}
68
69void operator delete(void *memory, size_t size)
70{
71 // std::cout << "Deleting " << size << " Bytes.\n";
72 __totalMemDeleted += size;
73 free(memory);
74}
75#else
76#define MNE_TRACE_MEMORY_REPORT
77#endif
78
79//=============================================================================================================
80// INCLUDES
81//=============================================================================================================
82
83#include "utils_global.h"
84
85#include <iostream>
86#include <fstream>
87#include <string>
88#include <chrono>
89#include <thread>
90#include <mutex>
91
92//=============================================================================================================
93// DEFINE NAMESPACE MNESCAN
94//=============================================================================================================
95
96namespace UTILSLIB
97{
98
123{
124public:
133 MNETracer(const std::string& file, const std::string& function, int lineNumber);
134
140 ~MNETracer();
141
146 static void enable(const std::string& jsonFileName);
147
151 static void enable();
152
157 static void disable();
158
163 static void start(const std::string& jsonFileName);
164
168 static void start();
169
173 static void stop();
174
180 static void traceQuantity(const std::string& name, long val);
181
187
192 void setPrintToTerminal(bool s);
193
194private:
199 static void writeHeader();
200
205 static void writeFooter();
206
211 static void writeToFile(const std::string& str);
212
217 static void setZeroTime();
218
223 static long long getTimeNow();
224
229 void initialize();
230
234 void formatFileName();
235
239 void formatFunctionName();
240
244 void registerConstructionTime();
245
249 void registerFinalTime();
250
254 void registerThreadId();
255
259 void calculateDuration();
260
264 void printDurationMiliSec();
265
269 void writeBeginEvent();
270
274 void writeEndEvent();
275
276 static bool ms_bIsEnabled;
277 static std::ofstream ms_OutputFileStream;
278 static bool ms_bIsFirstEvent;
279 static std::mutex ms_outFileMutex;
280 static long long ms_iZeroTime;
281
282 bool m_bIsInitialized;
283 bool m_bPrintToTerminal;
284 std::string m_sFileName;
285 std::string m_sFunctionName;
286 int m_iLineNumber;
287 std::string m_iThreadId;
288 long long m_iBeginTime;
289 long long m_iEndTime;
290 double m_dDurationMilis;
291}; // MNETracer
292
293} // namespace UTILSLIB
294
295#endif //if TRACE defined
296
utils library export/import macros.
#define UTILSSHARED_EXPORT
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 disable()
disable If the class "is enabled" (it's static variabable ms_bIsEnabled is true), the output file has...
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.
static void start(const std::string &jsonFileName)
Convenience overload of the method enable.
static void enable(const std::string &jsonFileName)
Definition mnetracer.cpp:92
void setPrintToTerminal(bool s)