Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / LogEntry.h
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
18 #include "include/utime.h"
19 #include "msg/msg_types.h" // for entity_inst_t
20 #include "common/entity_name.h"
21
22 namespace ceph {
23   class Formatter;
24 }
25
26 typedef 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
35 static const std::string CLOG_CHANNEL_NONE    = "none";
36 static const std::string CLOG_CHANNEL_DEFAULT = "cluster";
37 static const std::string CLOG_CHANNEL_CLUSTER = "cluster";
38 static 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
42 static const std::string CLOG_CONFIG_DEFAULT_KEY = "default";
43
44 /*
45  * Given a clog log_type, return the equivalent syslog priority
46  */
47 int clog_type_to_syslog_level(clog_type t);
48
49 clog_type string_to_clog_type(const string& s);
50 int string_to_syslog_level(string s);
51 int string_to_syslog_facility(string s);
52
53 string clog_type_to_string(clog_type t);
54
55
56 struct LogEntryKey {
57 private:
58   uint64_t _hash = 0;
59
60   void _calc_hash() {
61     hash<entity_inst_t> h;
62     _hash = seq + h(who);
63   }
64
65   entity_inst_t who;
66   utime_t stamp;
67   uint64_t seq = 0;
68
69 public:
70   LogEntryKey() {}
71   LogEntryKey(const entity_inst_t& w, utime_t t, uint64_t s)
72     : who(w), stamp(t), seq(s) {
73     _calc_hash();
74   }
75
76   uint64_t get_hash() const {
77     return _hash;
78   }
79
80   void encode(bufferlist& bl, uint64_t features) const;
81   void decode(bufferlist::iterator& bl);
82   void dump(Formatter *f) const;
83   static void generate_test_instances(list<LogEntryKey*>& o);
84
85   friend bool operator==(const LogEntryKey& l, const LogEntryKey& r) {
86     return l.who == r.who && l.stamp == r.stamp && l.seq == r.seq;
87   }
88 };
89 WRITE_CLASS_ENCODER_FEATURES(LogEntryKey)
90
91 namespace std {
92   template<> struct hash<LogEntryKey> {
93     size_t operator()(const LogEntryKey& r) const {
94       return r.get_hash();
95     }
96   };
97 } // namespace std
98
99 struct LogEntry {
100   entity_inst_t who;
101   EntityName name;
102   utime_t stamp;
103   uint64_t seq;
104   clog_type prio;
105   string msg;
106   string channel;
107
108   LogEntry() : seq(0), prio(CLOG_DEBUG) {}
109
110   LogEntryKey key() const { return LogEntryKey(who, stamp, seq); }
111
112   void log_to_syslog(string level, string facility);
113
114   void encode(bufferlist& bl, uint64_t features) const;
115   void decode(bufferlist::iterator& bl);
116   void dump(Formatter *f) const;
117   static void generate_test_instances(list<LogEntry*>& o);
118   static clog_type str_to_level(std::string const &str);
119 };
120 WRITE_CLASS_ENCODER_FEATURES(LogEntry)
121
122 struct LogSummary {
123   version_t version;
124   list<LogEntry> tail;
125   ceph::unordered_set<LogEntryKey> keys;
126
127   LogSummary() : version(0) {}
128
129   void add(const LogEntry& e) {
130     tail.push_back(e);
131     keys.insert(tail.back().key());
132   }
133   void prune(size_t max) {
134     while (tail.size() > max) {
135       keys.erase(tail.front().key());
136       tail.pop_front();
137     }
138   }
139   bool contains(const LogEntryKey& k) const {
140     return keys.count(k);
141   }
142
143   void encode(bufferlist& bl, uint64_t features) const;
144   void decode(bufferlist::iterator& bl);
145   void dump(Formatter *f) const;
146   static void generate_test_instances(list<LogSummary*>& o);
147 };
148 WRITE_CLASS_ENCODER_FEATURES(LogSummary)
149
150 inline ostream& operator<<(ostream& out, const clog_type t)
151 {
152   switch (t) {
153   case CLOG_DEBUG:
154     return out << "[DBG]";
155   case CLOG_INFO:
156     return out << "[INF]";
157   case CLOG_SEC:
158     return out << "[SEC]";
159   case CLOG_WARN:
160     return out << "[WRN]";
161   case CLOG_ERROR:
162     return out << "[ERR]";
163   default:
164     return out << "[???]";
165   }
166 }
167
168 inline ostream& operator<<(ostream& out, const LogEntry& e)
169 {
170   return out << e.stamp << " " << e.name << " " << e.who
171              << " " << e.seq << " : "
172              << e.channel << " " << e.prio << " " << e.msg;
173 }
174
175 #endif