1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "app_event.h"
16
17#include "ffrt.h"
18#include "hiappevent_base.h"
19#include "hiappevent_verify.h"
20#include "hiappevent_write.h"
21
22namespace OHOS {
23namespace HiviewDFX {
24namespace HiAppEvent {
25Event::Event(const std::string& domain, const std::string& name, EventType type)
26{
27    eventPack_ = std::make_shared<AppEventPack>(domain, name, type);
28}
29
30void Event::AddParam(const std::string& key, bool value)
31{
32    eventPack_->AddParam(key, value);
33}
34
35void Event::AddParam(const std::string& key, int32_t value)
36{
37    eventPack_->AddParam(key, value);
38}
39
40void Event::AddParam(const std::string& key, int64_t value)
41{
42    eventPack_->AddParam(key, value);
43}
44
45void Event::AddParam(const std::string& key, double value)
46{
47    eventPack_->AddParam(key, value);
48}
49
50void Event::AddParam(const std::string& key, const std::string& value)
51{
52    eventPack_->AddParam(key, value);
53}
54
55void Event::AddParam(const std::string& key, const std::vector<bool>& value)
56{
57    eventPack_->AddParam(key, value);
58}
59
60void Event::AddParam(const std::string& key, const std::vector<int32_t>& value)
61{
62    eventPack_->AddParam(key, value);
63}
64
65void Event::AddParam(const std::string& key, const std::vector<int64_t>& value)
66{
67    eventPack_->AddParam(key, value);
68}
69
70void Event::AddParam(const std::string& key, const std::vector<double>& value)
71{
72    eventPack_->AddParam(key, value);
73}
74
75void Event::AddParam(const std::string& key, const std::vector<std::string>& value)
76{
77    eventPack_->AddParam(key, value);
78}
79
80int Write(const Event& event)
81{
82    if (!IsApp()) {
83        return ErrorCode::ERROR_NOT_APP;
84    }
85    int ret = VerifyAppEvent(event.eventPack_);
86    if (ret >= 0) {
87        auto appEventPack = event.eventPack_;
88        ffrt::submit([appEventPack]() {
89            WriteEvent(appEventPack);
90            }, {}, {}, ffrt::task_attr().name("app_event"));
91    }
92    return ret;
93}
94} // namespace HiAppEvent
95} // namespace HiviewDFX
96} // namespace OHOS
97