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