37 using namespace UTILSLIB;
43 static const char* defaultTracerFileName(
"default_MNETracer_file.json");
44 bool MNETracer::ms_bIsEnabled(
false);
45 std::ofstream MNETracer::ms_OutputFileStream;
46 bool MNETracer::ms_bIsFirstEvent(
true);
47 std::mutex MNETracer::ms_outFileMutex;
48 long long MNETracer::ms_iZeroTime(0);
55 : m_bIsInitialized(false)
56 , m_bPrintToTerminal(false)
58 , m_sFunctionName(function)
59 , m_iLineNumber(lineNumber)
63 , m_dDurationMilis(0.)
76 if (ms_bIsEnabled && m_bIsInitialized)
80 if (m_bPrintToTerminal)
83 printDurationMiliSec();
92 ms_OutputFileStream.open(jsonFileName);
95 if (ms_OutputFileStream.is_open())
105 enable(defaultTracerFileName);
115 ms_OutputFileStream.flush();
116 ms_OutputFileStream.close();
117 ms_bIsEnabled =
false;
146 long long timeNow = getTimeNow() - ms_iZeroTime;
148 s.append(
"{\"name\":\"").append(name).append(
"\",\"ph\":\"C\",\"ts\":");
149 s.append(std::to_string(timeNow)).append(
",\"pid\":1,\"tid\":1");
150 s.append(
",\"args\":{\"").append(name).append(
"\":").append(std::to_string(val)).append(
"}}\n");
156 void MNETracer::initialize()
158 registerConstructionTime();
161 m_bIsInitialized =
true;
166 void MNETracer::setZeroTime()
168 ms_iZeroTime = getTimeNow();
173 void MNETracer::registerConstructionTime()
175 m_iBeginTime = getTimeNow() - ms_iZeroTime;
180 void MNETracer::registerFinalTime()
182 m_iEndTime = getTimeNow() - ms_iZeroTime;
187 long long MNETracer::getTimeNow()
189 auto timeNow = std::chrono::high_resolution_clock::now();
190 return std::chrono::time_point_cast<std::chrono::microseconds>(timeNow).time_since_epoch().count();
195 void MNETracer::registerThreadId()
197 auto longId = std::hash<std::thread::id>{}(std::this_thread::get_id());
198 m_iThreadId = std::to_string(longId).substr(0, 5);
203 void MNETracer::formatFunctionName()
205 const char* pattern(
" __cdecl");
206 constexpr
int patternLenght(8);
207 size_t pos = m_sFunctionName.find(pattern);
208 if (pos != std::string::npos) {
209 m_sFunctionName.replace(pos, patternLenght,
"");
215 void MNETracer::formatFileName()
217 const char* patternIn(
"\\");
218 const char* patternOut(
"\\\\");
219 constexpr
int patternOutLength(4);
220 size_t start_pos = 0;
221 while ((start_pos = m_sFileName.find(patternIn, start_pos)) != std::string::npos)
223 m_sFileName.replace(start_pos, 1, patternOut);
224 start_pos += patternOutLength;
230 void MNETracer::calculateDuration()
232 m_dDurationMilis = (m_iEndTime - m_iBeginTime) * 0.001;
237 void MNETracer::printDurationMiliSec()
239 std::cout <<
"Scope: " << m_sFileName <<
" - " << m_sFunctionName <<
" DurationMs: " << m_dDurationMilis <<
"ms.\n";
244 void MNETracer::writeHeader()
246 writeToFile(
"{\"displayTimeUnit\": \"ms\",\"traceEvents\":[\n");
251 void MNETracer::writeFooter()
258 void MNETracer::writeToFile(
const std::string& str)
260 ms_outFileMutex.lock();
261 if(ms_OutputFileStream.is_open()) {
262 ms_OutputFileStream << str;
264 ms_outFileMutex.unlock();
269 void MNETracer::writeBeginEvent()
272 if (!ms_bIsFirstEvent)
275 s.append(
"{\"name\":\"").append(m_sFunctionName).append(
"\",\"cat\":\"bst\",");
276 s.append(
"\"ph\":\"B\",\"ts\":").append(std::to_string(m_iBeginTime)).append(
",\"pid\":1,\"tid\":");
277 s.append(m_iThreadId).append(
",\"args\":{\"file path\":\"").append(m_sFileName).append(
"\",\"line number\":");
278 s.append(std::to_string(m_iLineNumber)).append(
"}}\n");
280 ms_bIsFirstEvent =
false;
285 void MNETracer::writeEndEvent()
288 s.append(
",{\"name\":\"").append(m_sFunctionName).append(
"\",\"cat\":\"bst\",");
289 s.append(
"\"ph\":\"E\",\"ts\":").append(std::to_string(m_iEndTime)).append(
",\"pid\":1,\"tid\":");
290 s.append(m_iThreadId).append(
",\"args\":{\"file path\":\"").append(m_sFileName).append(
"\",\"line number\":");
291 s.append(std::to_string(m_iLineNumber)).append(
"}}\n");
299 return m_bPrintToTerminal;
306 m_bPrintToTerminal = s;