]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/LogEntry.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / common / LogEntry.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15#ifndef CEPH_LOGENTRY_H
16#define CEPH_LOGENTRY_H
17
7c673cae 18#include "include/utime.h"
11fdf7f2 19#include "msg/msg_types.h"
31f18b77 20#include "common/entity_name.h"
7c673cae
FG
21
22namespace ceph {
23 class Formatter;
24}
25
26typedef enum {
27 CLOG_DEBUG = 0,
28 CLOG_INFO = 1,
29 CLOG_SEC = 2,
30 CLOG_WARN = 3,
31 CLOG_ERROR = 4,
32 CLOG_UNKNOWN = -1,
33} clog_type;
34
35static const std::string CLOG_CHANNEL_NONE = "none";
36static const std::string CLOG_CHANNEL_DEFAULT = "cluster";
37static const std::string CLOG_CHANNEL_CLUSTER = "cluster";
38static const std::string CLOG_CHANNEL_AUDIT = "audit";
39
40// this is the key name used in the config options for the default, e.g.
41// default=true foo=false bar=false
42static const std::string CLOG_CONFIG_DEFAULT_KEY = "default";
43
44/*
45 * Given a clog log_type, return the equivalent syslog priority
46 */
47int clog_type_to_syslog_level(clog_type t);
48
49clog_type string_to_clog_type(const string& s);
50int string_to_syslog_level(string s);
51int string_to_syslog_facility(string s);
52
53string clog_type_to_string(clog_type t);
54
55
56struct LogEntryKey {
31f18b77
FG
57private:
58 uint64_t _hash = 0;
59
60 void _calc_hash() {
11fdf7f2
TL
61 hash<entity_name_t> h;
62 _hash = seq + h(rank);
31f18b77
FG
63 }
64
11fdf7f2 65 entity_name_t rank;
7c673cae 66 utime_t stamp;
31f18b77 67 uint64_t seq = 0;
7c673cae 68
31f18b77
FG
69public:
70 LogEntryKey() {}
11fdf7f2
TL
71 LogEntryKey(const entity_name_t& w, utime_t t, uint64_t s)
72 : rank(w), stamp(t), seq(s) {
31f18b77
FG
73 _calc_hash();
74 }
75
76 uint64_t get_hash() const {
77 return _hash;
78 }
7c673cae 79
7c673cae
FG
80 void dump(Formatter *f) const;
81 static void generate_test_instances(list<LogEntryKey*>& o);
31f18b77
FG
82
83 friend bool operator==(const LogEntryKey& l, const LogEntryKey& r) {
11fdf7f2 84 return l.rank == r.rank && l.stamp == r.stamp && l.seq == r.seq;
31f18b77 85 }
7c673cae 86};
7c673cae 87
31f18b77
FG
88namespace std {
89 template<> struct hash<LogEntryKey> {
90 size_t operator()(const LogEntryKey& r) const {
91 return r.get_hash();
92 }
93 };
94} // namespace std
7c673cae
FG
95
96struct LogEntry {
31f18b77 97 EntityName name;
11fdf7f2
TL
98 entity_name_t rank;
99 entity_addrvec_t addrs;
7c673cae
FG
100 utime_t stamp;
101 uint64_t seq;
102 clog_type prio;
103 string msg;
104 string channel;
105
106 LogEntry() : seq(0), prio(CLOG_DEBUG) {}
107
11fdf7f2 108 LogEntryKey key() const { return LogEntryKey(rank, stamp, seq); }
7c673cae
FG
109
110 void log_to_syslog(string level, string facility);
111
112 void encode(bufferlist& bl, uint64_t features) const;
11fdf7f2 113 void decode(bufferlist::const_iterator& bl);
7c673cae
FG
114 void dump(Formatter *f) const;
115 static void generate_test_instances(list<LogEntry*>& o);
224ce89b 116 static clog_type str_to_level(std::string const &str);
7c673cae
FG
117};
118WRITE_CLASS_ENCODER_FEATURES(LogEntry)
119
120struct LogSummary {
121 version_t version;
11fdf7f2
TL
122 // channel -> [(seq#, entry), ...]
123 map<string,list<pair<uint64_t,LogEntry>>> tail_by_channel;
124 uint64_t seq = 0;
31f18b77 125 ceph::unordered_set<LogEntryKey> keys;
7c673cae
FG
126
127 LogSummary() : version(0) {}
128
11fdf7f2
TL
129 void build_ordered_tail(list<LogEntry> *tail) const;
130
7c673cae 131 void add(const LogEntry& e) {
11fdf7f2
TL
132 keys.insert(e.key());
133 tail_by_channel[e.channel].push_back(make_pair(++seq, e));
31f18b77
FG
134 }
135 void prune(size_t max) {
11fdf7f2
TL
136 for (auto& i : tail_by_channel) {
137 while (i.second.size() > max) {
138 keys.erase(i.second.front().second.key());
139 i.second.pop_front();
140 }
31f18b77 141 }
7c673cae
FG
142 }
143 bool contains(const LogEntryKey& k) const {
31f18b77 144 return keys.count(k);
7c673cae
FG
145 }
146
147 void encode(bufferlist& bl, uint64_t features) const;
11fdf7f2 148 void decode(bufferlist::const_iterator& bl);
7c673cae
FG
149 void dump(Formatter *f) const;
150 static void generate_test_instances(list<LogSummary*>& o);
151};
152WRITE_CLASS_ENCODER_FEATURES(LogSummary)
153
31f18b77 154inline ostream& operator<<(ostream& out, const clog_type t)
7c673cae
FG
155{
156 switch (t) {
157 case CLOG_DEBUG:
158 return out << "[DBG]";
159 case CLOG_INFO:
160 return out << "[INF]";
161 case CLOG_SEC:
162 return out << "[SEC]";
163 case CLOG_WARN:
164 return out << "[WRN]";
165 case CLOG_ERROR:
166 return out << "[ERR]";
167 default:
168 return out << "[???]";
169 }
170}
171
172inline ostream& operator<<(ostream& out, const LogEntry& e)
173{
11fdf7f2
TL
174 return out << e.stamp << " " << e.name << " (" << e.rank << ") "
175 << e.seq << " : "
7c673cae
FG
176 << e.channel << " " << e.prio << " " << e.msg;
177}
178
179#endif