Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosbigdataeventlogging.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qohosbigdataeventlogging.h>
5
6#include <cstring>
7#include <ctime>
8#include <functional>
9#include <qarkui/qarkuiutils.h>
10#include <qohosutils.h>
11#include <vector>
12
13namespace ch = std::chrono;
14
15QT_BEGIN_NAMESPACE
16
17namespace QtOhos {
18
19namespace {
20
21constexpr auto eventsDomainName = "qtapplication";
22
23constexpr auto eventTimePropertyName = "eventTime";
24
25std::shared_ptr<::ParamListNode> makeParamList()
26{
27 return std::shared_ptr<::ParamListNode>(
28 QArkUi::callArkUiOrFailOnNullResult(Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_CreateParamList)),
29 [](auto *paramListNode) {
30 QArkUi::callArkUi(Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_DestroyParamList), paramListNode);
31 });
32}
33
34std::string strftimeToString(const char *timeFormat, const struct tm *tm)
35{
36 constexpr std::size_t defaultBufferSize = 64;
37 constexpr std::size_t maxBufferOverFormatSize = 1024 * 1024;
38
39 const auto maxBufferSize = std::strlen(timeFormat) + maxBufferOverFormatSize;
40
41 std::string buffer(defaultBufferSize, '\0');
42
43 while (true) {
44 auto strftimeResult = std::strftime(&buffer[0], buffer.size(), timeFormat, tm);
45 if (strftimeResult != 0) {
46 buffer.resize(strftimeResult);
47 break;
48 }
49
50 buffer.resize(buffer.size() * 2);
51 if (buffer.size() > maxBufferSize) {
52 qOhosReportFatalErrorAndAbort(
53 "%s: exceeded buffer size for strftime, format: %zu, buffer: %zu",
54 Q_FUNC_INFO, std::strlen(timeFormat), buffer.size());
55 }
56 }
57
58 return buffer;
59}
60
61std::string strftimeToString(const char *timeFormat, ch::system_clock::time_point timePoint)
62{
63 const auto time = ch::system_clock::to_time_t(timePoint);
64 return strftimeToString(timeFormat, std::localtime(&time));
65}
66
67std::string formatTimestampString(ch::time_point<ch::system_clock> timepoint)
68{
69 const int subSecondMilliseconds = ch::duration_cast<ch::milliseconds>(timepoint.time_since_epoch()).count() % 1000;
70
71 return QtOhos::printfToString(
72 "%s.%03d%s",
73 strftimeToString("%Y-%m-%dT%H:%M:%S", timepoint).c_str(), subSecondMilliseconds,
74 strftimeToString("%z", timepoint).c_str());
75}
76
77class BigEventLoggingEventImpl : public BigEventLoggingEvent
78{
79public:
80 BigEventLoggingEventImpl(
81 const std::string &eventName,
82 ::EventType eventType,
83 std::shared_ptr<::ParamListNode> eventParams);
84 virtual ~BigEventLoggingEventImpl();
85
86 bool trySend() const override;
87
88private:
89 std::string m_eventName;
90 ::EventType m_eventType;
91 std::shared_ptr<::ParamListNode> m_eventParams;
92};
93
94class BigEventLoggingEventBuilderImpl final : public BigEventLoggingEventBuilder
95{
96public:
97 BigEventLoggingEventBuilderImpl(
98 const std::string &eventName,
99 ::EventType eventType,
100 std::chrono::time_point<std::chrono::system_clock> eventTime);
101 virtual ~BigEventLoggingEventBuilderImpl();
102
103 void addParam(const std::string &paramName, const std::string &paramValue) override;
104 void addParam(const std::string &paramName, std::int64_t paramValue) override;
105
106 std::shared_ptr<BigEventLoggingEvent> buildEvent() const override;
107
108private:
109 std::string m_eventName;
110 ::EventType m_eventType;
111 std::vector<std::function<::ParamList(::ParamList)>> m_paramListNodeBuilders;
112};
113
114BigEventLoggingEventImpl::BigEventLoggingEventImpl(
115 const std::string &eventName, ::EventType eventType,
116 std::shared_ptr<::ParamListNode> eventParams)
117 : m_eventName(eventName)
118 , m_eventType(eventType)
119 , m_eventParams(eventParams)
120{
121}
122
123BigEventLoggingEventImpl::~BigEventLoggingEventImpl() = default;
124
125bool BigEventLoggingEventImpl::trySend() const
126{
127 int result = QArkUi::callArkUi(
128 Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_Write),
129 QArkUi::CZString(eventsDomainName),
130 QArkUi::CZString(m_eventName.c_str()),
131 m_eventType, m_eventParams.get());
132
133 if (result != 0) {
134 qOhosPrintfError("%s: OH_HiAppEvent_Write failed: %d", Q_FUNC_INFO, result);
135 return false;
136 }
137
138 return true;
139}
140
141BigEventLoggingEventBuilderImpl::BigEventLoggingEventBuilderImpl(
142 const std::string &eventName, ::EventType eventType,
143 std::chrono::time_point<std::chrono::system_clock> eventTime)
144 : m_eventName(eventName)
145 , m_eventType(eventType)
146{
147 addParam(eventTimePropertyName, formatTimestampString(eventTime));
148}
149
150BigEventLoggingEventBuilderImpl::~BigEventLoggingEventBuilderImpl() = default;
151
152void BigEventLoggingEventBuilderImpl::addParam(const std::string &paramName, const std::string &paramValue)
153{
154 m_paramListNodeBuilders.push_back(
155 [paramName, paramValue](auto *paramListNode) {
156 return QArkUi::callArkUiOrFailOnNullResult(
157 Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_AddStringParam),
158 paramListNode, QArkUi::CZString(paramName.c_str()),
159 QArkUi::CZString(paramValue.c_str()));
160 });
161}
162
163void BigEventLoggingEventBuilderImpl::addParam(const std::string &paramName, std::int64_t paramValue)
164{
165 m_paramListNodeBuilders.push_back(
166 [paramName, paramValue](auto *paramListNode) {
167 return QArkUi::callArkUiOrFailOnNullResult(
168 Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_AddInt64Param),
169 paramListNode, QArkUi::CZString(paramName.c_str()), paramValue);
170 });
171}
172
173std::shared_ptr<BigEventLoggingEvent> BigEventLoggingEventBuilderImpl::buildEvent() const
174{
175 auto paramListNodeSharedPtr = makeParamList();
176 auto *paramList = paramListNodeSharedPtr.get();
177
178 for (const auto &paramListNodeBuilder : m_paramListNodeBuilders) {
179 auto *modifiedParamList = paramListNodeBuilder(paramList);
180 paramList = modifiedParamList;
181 }
182
183 return std::make_shared<BigEventLoggingEventImpl>(m_eventName, m_eventType, paramListNodeSharedPtr);
184}
185
186}
187
189
191
193
195
197 const std::string &eventName, ::EventType eventType,
198 std::chrono::time_point<std::chrono::system_clock> eventTime)
199{
200 return std::make_shared<BigEventLoggingEventBuilderImpl>(eventName, eventType, eventTime);
201}
202
203}
204
205QT_END_NAMESPACE
CZString(const char *value)
std::shared_ptr< BigEventLoggingEventBuilder > makeBigEventLoggingEventBuilder(const std::string &eventName, ::EventType eventType, std::chrono::time_point< std::chrono::system_clock > eventTime)