summaryrefslogtreecommitdiffstats
path: root/src/common/LogEntry.cc
blob: dfa1ab2fa1dfd569638f49cca535be0d02672133 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
//
#include <syslog.h>
#include <boost/algorithm/string/predicate.hpp>

#include "LogEntry.h"
#include "Formatter.h"
#include "include/stringify.h"

using std::list;
using std::map;
using std::make_pair;
using std::pair;
using std::string;

using ceph::bufferlist;
using ceph::decode;
using ceph::encode;
using ceph::Formatter;

// ----
// LogEntryKey

void LogEntryKey::dump(Formatter *f) const
{
  f->dump_stream("rank") << rank;
  f->dump_stream("stamp") << stamp;
  f->dump_unsigned("seq", seq);
}

void LogEntryKey::generate_test_instances(list<LogEntryKey*>& o)
{
  o.push_back(new LogEntryKey);
  o.push_back(new LogEntryKey(entity_name_t::CLIENT(1234), utime_t(1,2), 34));
}

clog_type LogEntry::str_to_level(std::string const &str)
{
  std::string level_str = str;
  std::transform(level_str.begin(), level_str.end(), level_str.begin(),
      [](char c) {return std::tolower(c);});

  if (level_str == "debug") {
    return CLOG_DEBUG;
  } else if (level_str == "info") {
    return CLOG_INFO;
  } else if (level_str == "sec") {
    return CLOG_SEC;
  } else if (level_str == "warn" || level_str == "warning") {
    return CLOG_WARN;
  } else if (level_str == "error" || level_str == "err") {
    return CLOG_ERROR;
  } else {
    return CLOG_UNKNOWN;
  }
}

// ----

int clog_type_to_syslog_level(clog_type t)
{
  switch (t) {
    case CLOG_DEBUG:
      return LOG_DEBUG;
    case CLOG_INFO:
      return LOG_INFO;
    case CLOG_WARN:
      return LOG_WARNING;
    case CLOG_ERROR:
      return LOG_ERR;
    case CLOG_SEC:
      return LOG_CRIT;
    default:
      ceph_abort();
      return 0;
  }
}

clog_type string_to_clog_type(const string& s)
{
  if (boost::iequals(s, "debug") ||
      boost::iequals(s, "dbg"))
    return CLOG_DEBUG;
  if (boost::iequals(s, "info") ||
      boost::iequals(s, "inf"))
    return CLOG_INFO;
  if (boost::iequals(s, "warning") ||
      boost::iequals(s, "warn") ||
      boost::iequals(s, "wrn"))
    return CLOG_WARN;
  if (boost::iequals(s, "error") ||
      boost::iequals(s, "err"))
    return CLOG_ERROR;
  if (boost::iequals(s, "security") ||
      boost::iequals(s, "sec"))
    return CLOG_SEC;

  return CLOG_UNKNOWN;
}

int string_to_syslog_level(string s)
{
  if (boost::iequals(s, "debug"))
    return LOG_DEBUG;
  if (boost::iequals(s, "info") ||
      boost::iequals(s, "notice"))
    return LOG_INFO;
  if (boost::iequals(s, "warning") ||
      boost::iequals(s, "warn"))
    return LOG_WARNING;
  if (boost::iequals(s, "error") ||
      boost::iequals(s, "err"))
    return LOG_ERR;
  if (boost::iequals(s, "crit") ||
      boost::iequals(s, "critical") ||
      boost::iequals(s, "emerg"))
    return LOG_CRIT;

  // err on the side of noise!
  return LOG_DEBUG;
}

int string_to_syslog_facility(string s)
{
  if (boost::iequals(s, "auth"))
    return LOG_AUTH;
  if (boost::iequals(s, "authpriv"))
    return LOG_AUTHPRIV;
  if (boost::iequals(s, "cron"))
    return LOG_CRON;
  if (boost::iequals(s, "daemon"))
    return LOG_DAEMON;
  if (boost::iequals(s, "ftp"))
    return LOG_FTP;
  if (boost::iequals(s, "kern"))
    return LOG_KERN;
  if (boost::iequals(s, "local0"))
    return LOG_LOCAL0;
  if (boost::iequals(s, "local1"))
    return LOG_LOCAL1;
  if (boost::iequals(s, "local2"))
    return LOG_LOCAL2;
  if (boost::iequals(s, "local3"))
    return LOG_LOCAL3;
  if (boost::iequals(s, "local4"))
    return LOG_LOCAL4;
  if (boost::iequals(s, "local5"))
    return LOG_LOCAL5;
  if (boost::iequals(s, "local6"))
    return LOG_LOCAL6;
  if (boost::iequals(s, "local7"))
    return LOG_LOCAL7;
  if (boost::iequals(s, "lpr"))
    return LOG_LPR;
  if (boost::iequals(s, "mail"))
    return LOG_MAIL;
  if (boost::iequals(s, "news"))
    return LOG_NEWS;
  if (boost::iequals(s, "syslog"))
    return LOG_SYSLOG;
  if (boost::iequals(s, "user"))
    return LOG_USER;
  if (boost::iequals(s, "uucp"))
    return LOG_UUCP;

  // default to USER
  return LOG_USER;
}

string clog_type_to_string(clog_type t)
{
  switch (t) {
    case CLOG_DEBUG:
      return "debug";
    case CLOG_INFO:
      return "info";
    case CLOG_WARN:
      return "warn";
    case CLOG_ERROR:
      return "err";
    case CLOG_SEC:
      return "crit";
    default:
      ceph_abort();
      return 0;
  }
}

void LogEntry::log_to_syslog(string level, string facility)
{
  int min = string_to_syslog_level(level);
  int l = clog_type_to_syslog_level(prio);
  if (l <= min) {
    int f = string_to_syslog_facility(facility);
    syslog(l | f, "%s %s %llu : %s",
	   name.to_cstr(),
	   stringify(rank).c_str(),
	   (long long unsigned)seq,
	   msg.c_str());
  }
}

void LogEntry::encode(bufferlist& bl, uint64_t features) const
{
  if (!HAVE_FEATURE(features, SERVER_NAUTILUS)) {
    ENCODE_START(4, 2, bl);
    __u16 t = prio;
    entity_inst_t who;
    who.name = rank;
    who.addr = addrs.as_legacy_addr();
    encode(who, bl, features);
    encode(stamp, bl);
    encode(seq, bl);
    encode(t, bl);
    encode(msg, bl);
    encode(channel, bl);
    encode(name, bl);
    ENCODE_FINISH(bl);
    return;
  }
  ENCODE_START(5, 5, bl);
  __u16 t = prio;
  encode(name, bl);
  encode(rank, bl);
  encode(addrs, bl, features);
  encode(stamp, bl);
  encode(seq, bl);
  encode(t, bl);
  encode(msg, bl);
  encode(channel, bl);
  ENCODE_FINISH(bl);
}

void LogEntry::decode(bufferlist::const_iterator& bl)
{
  DECODE_START_LEGACY_COMPAT_LEN(5, 2, 2, bl);
  if (struct_v < 5) {
    __u16 t;
    entity_inst_t who;
    decode(who, bl);
    rank = who.name;
    addrs.v.clear();
    addrs.v.push_back(who.addr);
    decode(stamp, bl);
    decode(seq, bl);
    decode(t, bl);
    prio = (clog_type)t;
    decode(msg, bl);
    if (struct_v >= 3) {
      decode(channel, bl);
    } else {
      // prior to having logging channels we only had a cluster log.
      // Ensure we keep that appearance when the other party has no
      // clue of what a 'channel' is.
      channel = CLOG_CHANNEL_CLUSTER;
    }
    if (struct_v >= 4) {
      decode(name, bl);
    }
  } else {
    __u16 t;
    decode(name, bl);
    decode(rank, bl);
    decode(addrs, bl);
    decode(stamp, bl);
    decode(seq, bl);
    decode(t, bl);
    prio = (clog_type)t;
    decode(msg, bl);
    decode(channel, bl);
  }
  DECODE_FINISH(bl);
}

void LogEntry::dump(Formatter *f) const
{
  f->dump_stream("name") << name;
  f->dump_stream("rank") << rank;
  f->dump_object("addrs", addrs);
  f->dump_stream("stamp") << stamp;
  f->dump_unsigned("seq", seq);
  f->dump_string("channel", channel);
  f->dump_stream("priority") << prio;
  f->dump_string("message", msg);
}

void LogEntry::generate_test_instances(list<LogEntry*>& o)
{
  o.push_back(new LogEntry);
}


// -----

void LogSummary::build_ordered_tail(list<LogEntry> *tail) const
{
  tail->clear();
  // channel -> (begin, end)
  map<string,pair<list<pair<uint64_t,LogEntry>>::const_iterator,
		  list<pair<uint64_t,LogEntry>>::const_iterator>> pos;
  for (auto& i : tail_by_channel) {
    pos.emplace(i.first, make_pair(i.second.begin(), i.second.end()));
  }
  while (true) {
    uint64_t min_seq = 0;
    list<pair<uint64_t,LogEntry>>::const_iterator *minp = 0;
    for (auto& i : pos) {
      if (i.second.first == i.second.second) {
	continue;
      }
      if (min_seq == 0 || i.second.first->first < min_seq) {
	min_seq = i.second.first->first;
	minp = &i.second.first;
      }
    }
    if (min_seq == 0) {
      break; // done
    }
    tail->push_back((*minp)->second);
    ++(*minp);
  }
}

void LogSummary::encode(bufferlist& bl, uint64_t features) const
{
  if (!HAVE_FEATURE(features, SERVER_MIMIC)) {
    ENCODE_START(2, 2, bl);
    encode(version, bl);
    list<LogEntry> tail;
    build_ordered_tail(&tail);
    encode(tail, bl, features);
    ENCODE_FINISH(bl);
    return;
  }
  ENCODE_START(3, 3, bl);
  encode(version, bl);
  encode(seq, bl);
  encode(tail_by_channel, bl, features);
  ENCODE_FINISH(bl);
}

void LogSummary::decode(bufferlist::const_iterator& bl)
{
  DECODE_START_LEGACY_COMPAT_LEN(3, 2, 2, bl);
  decode(version, bl);
  if (struct_v < 3) {
    list<LogEntry> tail;
    decode(tail, bl);
    for (auto& i : tail) {
      add(i);
    }
  } else {
    decode(seq, bl);
    decode(tail_by_channel, bl);
  }
  DECODE_FINISH(bl);
  keys.clear();
  for (auto& i : tail_by_channel) {
    for (auto& e : i.second) {
      keys.insert(e.second.key());
    }
  }
}

void LogSummary::dump(Formatter *f) const
{
  f->dump_unsigned("version", version);
  f->open_object_section("tail_by_channel");
  for (auto& i : tail_by_channel) {
    f->open_object_section(i.first.c_str());
    for (auto& j : i.second) {
      string s = stringify(j.first);
      f->dump_object(s.c_str(), j.second);
    }
    f->close_section();
  }
  f->close_section();
}

void LogSummary::generate_test_instances(list<LogSummary*>& o)
{
  o.push_back(new LogSummary);
  // more!
}