1 /*
2  * Copyright (c) 2022 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 "logger_event.h"
16 
17 #include <memory>
18 
19 #include "hiview_event_common.h"
20 #include "json/json.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 namespace {
25 using namespace BaseEventSpace;
26 using ParamValueAdder = void (*)(Json::Value &root, const std::string &name, const ParamValue& value);
27 
AddUint8Value(Json::Value & root,const std::string & name,const ParamValue & value)28 void AddUint8Value(Json::Value &root, const std::string &name, const ParamValue& value)
29 {
30     root[name] = value.GetUint8();
31 }
32 
AddUint16Value(Json::Value & root,const std::string & name,const ParamValue & value)33 void AddUint16Value(Json::Value &root, const std::string &name, const ParamValue& value)
34 {
35     root[name] = value.GetUint16();
36 }
37 
AddUint32Value(Json::Value & root,const std::string & name,const ParamValue & value)38 void AddUint32Value(Json::Value &root, const std::string &name, const ParamValue& value)
39 {
40     root[name] = value.GetUint32();
41 }
42 
AddUint64Value(Json::Value & root,const std::string & name,const ParamValue & value)43 void AddUint64Value(Json::Value &root, const std::string &name, const ParamValue& value)
44 {
45     root[name] = value.GetUint64();
46 }
47 
AddStringValue(Json::Value & root,const std::string & name,const ParamValue & value)48 void AddStringValue(Json::Value &root, const std::string &name, const ParamValue& value)
49 {
50     root[name] = value.GetString();
51 }
52 
AddUint32VecValue(Json::Value & root,const std::string & name,const ParamValue & value)53 void AddUint32VecValue(Json::Value &root, const std::string &name, const ParamValue& value)
54 {
55     auto vec = value.GetUint32Vec();
56     for (auto num : vec) {
57         root[name].append(num);
58     }
59 }
60 
AddStringVecValue(Json::Value & root,const std::string & name,const ParamValue & value)61 void AddStringVecValue(Json::Value &root, const std::string &name, const ParamValue& value)
62 {
63     auto vec = value.GetStringVec();
64     for (auto str : vec) {
65         root[name].append(str);
66     }
67 }
68 
69 const ParamValueAdder ADDER_FUNCS[] = {
70     &AddUint8Value,
71     &AddUint16Value,
72     &AddUint32Value,
73     &AddUint64Value,
74     &AddStringValue,
75     &AddUint32VecValue,
76     &AddStringVecValue
77 };
78 }
79 
GetValue(const std::string & name)80 ParamValue LoggerEvent::GetValue(const std::string& name)
81 {
82     return paramMap_[name];
83 }
84 
ToJsonString()85 std::string LoggerEvent::ToJsonString()
86 {
87     Json::Value root;
88     root[KEY_OF_DOMAIN] = HiSysEvent::Domain::HIVIEWDFX;
89     root[KEY_OF_NAME] = this->eventName_;
90     root[KEY_OF_TYPE] = (int)this->eventType_;
91 
92     for (auto &param : paramMap_) {
93         size_t typeIndex = param.second.GetType();
94         if (typeIndex < (sizeof(ADDER_FUNCS) / sizeof(ADDER_FUNCS[0]))) {
95             ADDER_FUNCS[typeIndex](root, param.first, param.second);
96         }
97     }
98 
99     Json::StreamWriterBuilder builder;
100     builder["indentation"] = "";
101     std::unique_ptr<Json::StreamWriter> jsonWriter(builder.newStreamWriter());
102     std::ostringstream os;
103     jsonWriter->write(root, &os);
104     return os.str();
105 }
106 
InnerUpdate(const std::string & name,const ParamValue & value)107 void LoggerEvent::InnerUpdate(const std::string &name, const ParamValue& value)
108 {
109     if ((paramMap_.find(name) != paramMap_.end()) && (paramMap_[name].GetType() == value.GetType())) {
110         paramMap_[name] = value;
111     }
112 }
113 } // namespace HiviewDFX
114 } // namespace OHOS
115