]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/LogEntry.cc
update sources to v12.1.1
[ceph.git] / ceph / src / common / LogEntry.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 //
4 #include <syslog.h>
5 #include <boost/algorithm/string/predicate.hpp>
6
7 #include "LogEntry.h"
8 #include "Formatter.h"
9 #include "include/stringify.h"
10
11 // ----
12 // LogEntryKey
13
14 void LogEntryKey::encode(bufferlist& bl, uint64_t features) const
15 {
16 ::encode(who, bl, features);
17 ::encode(stamp, bl);
18 ::encode(seq, bl);
19 }
20
21 void LogEntryKey::decode(bufferlist::iterator& bl)
22 {
23 ::decode(who, bl);
24 ::decode(stamp, bl);
25 ::decode(seq, bl);
26 _calc_hash();
27 }
28
29 void LogEntryKey::dump(Formatter *f) const
30 {
31 f->dump_stream("who") << who;
32 f->dump_stream("stamp") << stamp;
33 f->dump_unsigned("seq", seq);
34 }
35
36 void LogEntryKey::generate_test_instances(list<LogEntryKey*>& o)
37 {
38 o.push_back(new LogEntryKey);
39 o.push_back(new LogEntryKey(entity_inst_t(), utime_t(1,2), 34));
40 }
41
42 clog_type LogEntry::str_to_level(std::string const &str)
43 {
44 std::string level_str = str;
45 std::transform(level_str.begin(), level_str.end(), level_str.begin(),
46 [](char c) {return std::tolower(c);});
47
48 if (level_str == "debug") {
49 return CLOG_DEBUG;
50 } else if (level_str == "info") {
51 return CLOG_INFO;
52 } else if (level_str == "sec") {
53 return CLOG_SEC;
54 } else if (level_str == "warn" || level_str == "warning") {
55 return CLOG_WARN;
56 } else if (level_str == "error" || level_str == "err") {
57 return CLOG_ERROR;
58 } else {
59 return CLOG_UNKNOWN;
60 }
61 }
62
63 // ----
64
65 int clog_type_to_syslog_level(clog_type t)
66 {
67 switch (t) {
68 case CLOG_DEBUG:
69 return LOG_DEBUG;
70 case CLOG_INFO:
71 return LOG_INFO;
72 case CLOG_WARN:
73 return LOG_WARNING;
74 case CLOG_ERROR:
75 return LOG_ERR;
76 case CLOG_SEC:
77 return LOG_CRIT;
78 default:
79 ceph_abort();
80 return 0;
81 }
82 }
83
84 clog_type string_to_clog_type(const string& s)
85 {
86 if (boost::iequals(s, "debug") ||
87 boost::iequals(s, "dbg"))
88 return CLOG_DEBUG;
89 if (boost::iequals(s, "info") ||
90 boost::iequals(s, "inf"))
91 return CLOG_INFO;
92 if (boost::iequals(s, "warning") ||
93 boost::iequals(s, "warn") ||
94 boost::iequals(s, "wrn"))
95 return CLOG_WARN;
96 if (boost::iequals(s, "error") ||
97 boost::iequals(s, "err"))
98 return CLOG_ERROR;
99 if (boost::iequals(s, "security") ||
100 boost::iequals(s, "sec"))
101 return CLOG_SEC;
102
103 return CLOG_UNKNOWN;
104 }
105
106 int string_to_syslog_level(string s)
107 {
108 if (boost::iequals(s, "debug"))
109 return LOG_DEBUG;
110 if (boost::iequals(s, "info") ||
111 boost::iequals(s, "notice"))
112 return LOG_INFO;
113 if (boost::iequals(s, "warning") ||
114 boost::iequals(s, "warn"))
115 return LOG_WARNING;
116 if (boost::iequals(s, "error") ||
117 boost::iequals(s, "err"))
118 return LOG_ERR;
119 if (boost::iequals(s, "crit") ||
120 boost::iequals(s, "critical") ||
121 boost::iequals(s, "emerg"))
122 return LOG_CRIT;
123
124 // err on the side of noise!
125 return LOG_DEBUG;
126 }
127
128 int string_to_syslog_facility(string s)
129 {
130 if (boost::iequals(s, "auth"))
131 return LOG_AUTH;
132 if (boost::iequals(s, "authpriv"))
133 return LOG_AUTHPRIV;
134 if (boost::iequals(s, "cron"))
135 return LOG_CRON;
136 if (boost::iequals(s, "daemon"))
137 return LOG_DAEMON;
138 if (boost::iequals(s, "ftp"))
139 return LOG_FTP;
140 if (boost::iequals(s, "kern"))
141 return LOG_KERN;
142 if (boost::iequals(s, "local0"))
143 return LOG_LOCAL0;
144 if (boost::iequals(s, "local1"))
145 return LOG_LOCAL1;
146 if (boost::iequals(s, "local2"))
147 return LOG_LOCAL2;
148 if (boost::iequals(s, "local3"))
149 return LOG_LOCAL3;
150 if (boost::iequals(s, "local4"))
151 return LOG_LOCAL4;
152 if (boost::iequals(s, "local5"))
153 return LOG_LOCAL5;
154 if (boost::iequals(s, "local6"))
155 return LOG_LOCAL6;
156 if (boost::iequals(s, "local7"))
157 return LOG_LOCAL7;
158 if (boost::iequals(s, "lpr"))
159 return LOG_LPR;
160 if (boost::iequals(s, "mail"))
161 return LOG_MAIL;
162 if (boost::iequals(s, "news"))
163 return LOG_NEWS;
164 if (boost::iequals(s, "syslog"))
165 return LOG_SYSLOG;
166 if (boost::iequals(s, "user"))
167 return LOG_USER;
168 if (boost::iequals(s, "uucp"))
169 return LOG_UUCP;
170
171 // default to USER
172 return LOG_USER;
173 }
174
175 string clog_type_to_string(clog_type t)
176 {
177 switch (t) {
178 case CLOG_DEBUG:
179 return "debug";
180 case CLOG_INFO:
181 return "info";
182 case CLOG_WARN:
183 return "warn";
184 case CLOG_ERROR:
185 return "err";
186 case CLOG_SEC:
187 return "crit";
188 default:
189 ceph_abort();
190 return 0;
191 }
192 }
193
194 void LogEntry::log_to_syslog(string level, string facility)
195 {
196 int min = string_to_syslog_level(level);
197 int l = clog_type_to_syslog_level(prio);
198 if (l <= min) {
199 int f = string_to_syslog_facility(facility);
200 syslog(l | f, "%s %llu : %s",
201 stringify(who).c_str(),
202 (long long unsigned)seq,
203 msg.c_str());
204 }
205 }
206
207 void LogEntry::encode(bufferlist& bl, uint64_t features) const
208 {
209 ENCODE_START(4, 2, bl);
210 __u16 t = prio;
211 ::encode(who, bl, features);
212 ::encode(stamp, bl);
213 ::encode(seq, bl);
214 ::encode(t, bl);
215 ::encode(msg, bl);
216 ::encode(channel, bl);
217 ::encode(name, bl);
218 ENCODE_FINISH(bl);
219 }
220
221 void LogEntry::decode(bufferlist::iterator& bl)
222 {
223 DECODE_START_LEGACY_COMPAT_LEN(4, 2, 2, bl);
224 __u16 t;
225 ::decode(who, bl);
226 ::decode(stamp, bl);
227 ::decode(seq, bl);
228 ::decode(t, bl);
229 prio = (clog_type)t;
230 ::decode(msg, bl);
231 if (struct_v >= 3) {
232 ::decode(channel, bl);
233 } else {
234 // prior to having logging channels we only had a cluster log.
235 // Ensure we keep that appearance when the other party has no
236 // clue of what a 'channel' is.
237 channel = CLOG_CHANNEL_CLUSTER;
238 }
239 if (struct_v >= 4) {
240 ::decode(name, bl);
241 }
242 DECODE_FINISH(bl);
243 }
244
245 void LogEntry::dump(Formatter *f) const
246 {
247 f->dump_stream("who") << who;
248 f->dump_stream("name") << name;
249 f->dump_stream("stamp") << stamp;
250 f->dump_unsigned("seq", seq);
251 f->dump_string("channel", channel);
252 f->dump_stream("priority") << prio;
253 f->dump_string("message", msg);
254 }
255
256 void LogEntry::generate_test_instances(list<LogEntry*>& o)
257 {
258 o.push_back(new LogEntry);
259 }
260
261
262 // -----
263
264 void LogSummary::encode(bufferlist& bl, uint64_t features) const
265 {
266 ENCODE_START(2, 2, bl);
267 ::encode(version, bl);
268 ::encode(tail, bl, features);
269 ENCODE_FINISH(bl);
270 }
271
272 void LogSummary::decode(bufferlist::iterator& bl)
273 {
274 DECODE_START_LEGACY_COMPAT_LEN(2, 2, 2, bl);
275 ::decode(version, bl);
276 ::decode(tail, bl);
277 DECODE_FINISH(bl);
278 keys.clear();
279 for (auto& p : tail) {
280 keys.insert(p.key());
281 }
282 }
283
284 void LogSummary::dump(Formatter *f) const
285 {
286 f->dump_unsigned("version", version);
287 f->open_array_section("tail");
288 for (list<LogEntry>::const_iterator p = tail.begin(); p != tail.end(); ++p) {
289 f->open_object_section("entry");
290 p->dump(f);
291 f->close_section();
292 }
293 f->close_section();
294 }
295
296 void LogSummary::generate_test_instances(list<LogSummary*>& o)
297 {
298 o.push_back(new LogSummary);
299 // more!
300 }
301