4#include <qohosbigdataeventlogging.h>
9#include <qarkui/qarkuiutils.h>
10#include <qohosutils.h>
13namespace ch =
std::chrono;
21constexpr auto eventsDomainName =
"qtapplication";
23constexpr auto eventTimePropertyName =
"eventTime";
25std::shared_ptr<::ParamListNode> makeParamList()
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);
34std::string strftimeToString(
const char *timeFormat,
const struct tm *tm)
36 constexpr std::size_t defaultBufferSize = 64;
37 constexpr std::size_t maxBufferOverFormatSize = 1024 * 1024;
39 const auto maxBufferSize = std::strlen(timeFormat) + maxBufferOverFormatSize;
41 std::string buffer(defaultBufferSize,
'\0');
44 auto strftimeResult = std::strftime(&buffer[0], buffer.size(), timeFormat, tm);
45 if (strftimeResult != 0) {
46 buffer.resize(strftimeResult);
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());
61std::string strftimeToString(
const char *timeFormat, ch::system_clock::time_point timePoint)
63 const auto time = ch::system_clock::to_time_t(timePoint);
64 return strftimeToString(timeFormat,
std::localtime(&time));
67std::string formatTimestampString(ch::time_point<ch::system_clock> timepoint)
69 const int subSecondMilliseconds = ch::duration_cast<ch::milliseconds>(timepoint.time_since_epoch()).count() % 1000;
71 return QtOhos::printfToString(
73 strftimeToString(
"%Y-%m-%dT%H:%M:%S", timepoint).c_str(), subSecondMilliseconds,
74 strftimeToString(
"%z", timepoint).c_str());
80 BigEventLoggingEventImpl(
81 const std::string &eventName,
82 ::EventType eventType,
83 std::shared_ptr<::ParamListNode> eventParams);
84 virtual ~BigEventLoggingEventImpl();
86 bool trySend()
const override;
89 std::string m_eventName;
90 ::EventType m_eventType;
91 std::shared_ptr<::ParamListNode> m_eventParams;
97 BigEventLoggingEventBuilderImpl(
98 const std::string &eventName,
99 ::EventType eventType,
100 std::chrono::time_point<std::chrono::system_clock> eventTime);
101 virtual ~BigEventLoggingEventBuilderImpl();
103 void addParam(
const std::string ¶mName,
const std::string ¶mValue)
override;
104 void addParam(
const std::string ¶mName,
std::int64_t paramValue)
override;
109 std::string m_eventName;
110 ::EventType m_eventType;
111 std::vector<std::function<::ParamList(::ParamList)>> m_paramListNodeBuilders;
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)
123BigEventLoggingEventImpl::~BigEventLoggingEventImpl() =
default;
125bool BigEventLoggingEventImpl::trySend()
const
127 int result =
QArkUi::callArkUi(
128 Q_OHOS_NAMED_FUNC(::OH_HiAppEvent_Write),
131 m_eventType, m_eventParams.get());
134 qOhosPrintfError(
"%s: OH_HiAppEvent_Write failed: %d", Q_FUNC_INFO, result);
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)
147 addParam(eventTimePropertyName, formatTimestampString(eventTime));
150BigEventLoggingEventBuilderImpl::~BigEventLoggingEventBuilderImpl() =
default;
152void BigEventLoggingEventBuilderImpl::addParam(
const std::string ¶mName,
const std::string ¶mValue)
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()));
163void BigEventLoggingEventBuilderImpl::addParam(
const std::string ¶mName,
std::int64_t paramValue)
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);
175 auto paramListNodeSharedPtr = makeParamList();
176 auto *paramList = paramListNodeSharedPtr.get();
178 for (
const auto ¶mListNodeBuilder : m_paramListNodeBuilders) {
179 auto *modifiedParamList = paramListNodeBuilder(paramList);
180 paramList = modifiedParamList;
183 return std::make_shared<BigEventLoggingEventImpl>(m_eventName, m_eventType, paramListNodeSharedPtr);
197 const std::string &eventName, ::EventType eventType,
198 std::chrono::time_point<std::chrono::system_clock> eventTime)
200 return std::make_shared<BigEventLoggingEventBuilderImpl>(eventName, eventType, eventTime);
CZString(const char *value)
virtual ~BigEventLoggingEventBuilder()
BigEventLoggingEventBuilder()
virtual ~BigEventLoggingEvent()
std::shared_ptr< BigEventLoggingEventBuilder > makeBigEventLoggingEventBuilder(const std::string &eventName, ::EventType eventType, std::chrono::time_point< std::chrono::system_clock > eventTime)