lok: register view callback also for nested sm view
[LibreOffice.git] / comphelper / source / misc / traceevent.cxx
blobfb07e1caa771b71420a9d44d38768a7cb072d5d9
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
12 #include <atomic>
13 #include <mutex>
14 #include <iostream>
16 #include <comphelper/profilezone.hxx>
17 #include <comphelper/sequence.hxx>
18 #include <comphelper/traceevent.hxx>
20 namespace comphelper
22 #ifdef DBG_UTIL
23 std::atomic<bool> TraceEvent::s_bRecording = (getenv("TRACE_EVENT_RECORDING") != nullptr);
24 #else
25 std::atomic<bool> TraceEvent::s_bRecording = false;
26 #endif
28 std::size_t TraceEvent::s_nBufferSize = 0;
29 void (*TraceEvent::s_pBufferFullCallback)() = nullptr;
31 int AsyncEvent::s_nIdCounter = 0;
33 static thread_local int nProfileZoneNesting = 0; // Level of Nested Profile Zones
35 namespace
37 std::vector<OUString> g_aRecording; // recorded data
38 std::mutex g_aMutex;
41 void TraceEvent::addRecording(const OUString& sObject)
43 std::lock_guard aGuard(g_aMutex);
45 g_aRecording.emplace_back(sObject);
47 if (s_nBufferSize > 0 && g_aRecording.size() >= s_nBufferSize)
49 if (s_pBufferFullCallback != nullptr)
50 (*s_pBufferFullCallback)();
54 void TraceEvent::addInstantEvent(const char* sName, const std::map<OUString, OUString>& args)
56 long long nNow = getNow();
58 int nPid = 0;
59 oslProcessInfo aProcessInfo;
60 aProcessInfo.Size = sizeof(oslProcessInfo);
61 if (osl_getProcessInfo(nullptr, osl_Process_IDENTIFIER, &aProcessInfo) == osl_Process_E_None)
62 nPid = aProcessInfo.Ident;
64 addRecording("{"
65 "\"name:\""
66 + OUString(sName, strlen(sName), RTL_TEXTENCODING_UTF8)
67 + "\","
68 "\"ph\":\"i\""
69 + createArgsString(args) + ",\"ts\":" + OUString::number(nNow)
70 + ","
71 "\"pid\":"
72 + OUString::number(nPid)
73 + ","
74 "\"tid\":"
75 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
78 void TraceEvent::startRecording()
80 std::lock_guard aGuard(g_aMutex);
81 s_bRecording = true;
84 void TraceEvent::stopRecording() { s_bRecording = false; }
86 void TraceEvent::setBufferSizeAndCallback(std::size_t bufferSize, void (*bufferFullCallback)())
88 s_nBufferSize = bufferSize;
89 s_pBufferFullCallback = bufferFullCallback;
92 std::vector<OUString> TraceEvent::getEventVectorAndClear()
94 bool bRecording;
95 std::vector<OUString> aRecording;
97 std::lock_guard aGuard(g_aMutex);
98 bRecording = s_bRecording;
99 stopRecording();
100 aRecording.swap(g_aRecording);
102 // reset start time and nesting level
103 if (bRecording)
104 startRecording();
105 return aRecording;
108 css::uno::Sequence<OUString> TraceEvent::getRecordingAndClear()
110 return comphelper::containerToSequence(getEventVectorAndClear());
113 void ProfileZone::addRecording()
115 assert(s_bRecording);
117 long long nNow = getNow();
119 // Generate a single "Complete Event" (type X)
120 TraceEvent::addRecording("{"
121 "\"name\":\""
122 + OUString(m_sName, strlen(m_sName), RTL_TEXTENCODING_UTF8)
123 + "\","
124 "\"ph\":\"X\","
125 "\"ts\":"
126 + OUString::number(m_nCreateTime)
127 + ","
128 "\"dur\":"
129 + OUString::number(nNow - m_nCreateTime) + m_sArgs
130 + ","
131 "\"pid\":"
132 + OUString::number(m_nPid)
133 + ","
134 "\"tid\":"
135 + OUString::number(osl_getThreadIdentifier(nullptr)) + "},");
138 int ProfileZone::getNestingLevel() { return nProfileZoneNesting; }
140 void ProfileZone::setNestingLevel(int nNestingLevel) { nProfileZoneNesting = nNestingLevel; }
142 } // namespace comphelper
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */