1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#include "XSyslogFieldsImp.h"
const std::string XEnumSyslogFieldsEventSourceTypeMapping[] = {
"other",
"router",
"switch",
"host",
"card",
"port",
"slotThreshold",
"portThreshold",
"virtualMachine",
"virtualNetworkFunction"
};
const std::string XEnumSyslogFieldsSyslogSevMapping[] = {
"Alert",
"Critical",
"Debug",
"Emergency",
"Error",
"Info",
"Notice",
"Warning"
};
const std::string XEnumSyslogFieldsSyslogFieldsVersionMapping[] = {
"4.0"
};
XSyslogFieldsImp::XSyslogFieldsImp(std::shared_ptr<XCommonEventHeader> header,
XEnumSyslogFieldsEventSourceType eventSourceType,
const XString& syslogTag,
const XString& syslogMsg
):
header_(header),
eventSourceType_(eventSourceType),
syslogFieldsVersion_(XEnumSyslogFieldsSyslogFieldsVersion4_0),
syslogMsg_(syslogMsg),
syslogTag_(syslogTag)
{
}
std::string XSyslogFieldsImp::toString()
{
try
{
auto js = toJson();
return js.dump();
}
catch (json::exception& e)
{
SPDLOG_ERROR("Fail to dump XSyslogFields to json string:{}.", e.what());
return "";
}
}
void XSyslogFieldsImp::setSyslogProcId(XNumber syslogProcId)
{
comVals_["syslogProcId"] = syslogProcId;
}
void XSyslogFieldsImp::setSyslogFacility(XInteger syslogFacility)
{
comVals_["syslogFacility"] = syslogFacility;
}
void XSyslogFieldsImp::setSyslogPri(XInteger syslogPri)
{
comVals_["syslogPri"] = syslogPri;
}
void XSyslogFieldsImp::setEventSourceType(XEnumSyslogFieldsEventSourceType eventSourceType)
{
auto val = XEnumSyslogFieldsEventSourceTypeMapping[eventSourceType];
comVals_["eventSourceType"] = val;
}
void XSyslogFieldsImp::setSyslogMsgHost(const XString& syslogMsgHost)
{
comVals_["syslogMsgHost"] = syslogMsgHost;
}
void XSyslogFieldsImp::setSyslogSev(XEnumSyslogFieldsSyslogSev syslogSev)
{
auto val = XEnumSyslogFieldsSyslogSevMapping[syslogSev];
comVals_["syslogSev"] = val;
}
void XSyslogFieldsImp::setSyslogSData(const XString& syslogSData)
{
comVals_["syslogSData"] = syslogSData;
}
void XSyslogFieldsImp::setSyslogMsg(const XString& syslogMsg)
{
comVals_["syslogMsg"] = syslogMsg;
}
void XSyslogFieldsImp::setSyslogProc(const XString& syslogProc)
{
comVals_["syslogProc"] = syslogProc;
}
void XSyslogFieldsImp::setEventSourceHost(const XString& eventSourceHost)
{
comVals_["eventSourceHost"] = eventSourceHost;
}
void XSyslogFieldsImp::setSyslogTs(const XString& syslogTs)
{
comVals_["syslogTs"] = syslogTs;
}
void XSyslogFieldsImp::setSyslogTag(const XString& syslogTag)
{
comVals_["syslogTag"] = syslogTag;
}
void XSyslogFieldsImp::setSyslogVer(XNumber syslogVer)
{
comVals_["syslogVer"] = syslogVer;
}
void XSyslogFieldsImp::setAdditionalFields(const XHashMap& additionalFields)
{
additionalFields_ = additionalFields;
}
void XSyslogFieldsImp::setSyslogSdId(const XString& syslogSdId)
{
comVals_["syslogSdId"] = syslogSdId;
}
json XSyslogFieldsImp::toJson()
{
try
{
json field = {
{ "eventSourceType", XEnumSyslogFieldsEventSourceTypeMapping[eventSourceType_] },
{ "syslogMsg", syslogMsg_ },
{ "syslogFieldsVersion", XEnumSyslogFieldsSyslogFieldsVersionMapping[syslogFieldsVersion_] },
{ "syslogTag", syslogTag_ }
};
//optional val
if (!additionalFields_.empty())
{
field["additionalFields"] = additionalFields_;
}
field = mergeCommonValues(field, comVals_);
json jsEvent;
header_->setDomain(XEnumCommonEventHeaderDomainSyslog);
jsEvent["event"]["commonEventHeader"] = dynamic_pointer_cast<XJsonable>(header_)->toJson();
jsEvent["event"]["syslogFields"] = field;
return jsEvent;
}
catch (json::exception& e)
{
SPDLOG_ERROR("Fail to build XSyslogFields to json object:{}.", e.what());
return json();
}
}
std::shared_ptr<XCommonEventHeader> XSyslogFieldsImp::getCommonHeader()
{
return header_;
}
|