1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #ifndef CEPH_LOGENTRY_H
16 #define CEPH_LOGENTRY_H
18 #include <fmt/format.h>
20 #include "include/utime.h"
21 #include "msg/msg_fmt.h"
22 #include "msg/msg_types.h"
23 #include "common/entity_name.h"
24 #include "ostream_temp.h"
31 static const std::string CLOG_CHANNEL_NONE
= "none";
32 static const std::string CLOG_CHANNEL_DEFAULT
= "cluster";
33 static const std::string CLOG_CHANNEL_CLUSTER
= "cluster";
34 static const std::string CLOG_CHANNEL_AUDIT
= "audit";
36 // this is the key name used in the config options for the default, e.g.
37 // default=true foo=false bar=false
38 static const std::string CLOG_CONFIG_DEFAULT_KEY
= "default";
41 * Given a clog log_type, return the equivalent syslog priority
43 int clog_type_to_syslog_level(clog_type t
);
45 clog_type
string_to_clog_type(const std::string
& s
);
46 int string_to_syslog_level(std::string s
);
47 int string_to_syslog_facility(std::string s
);
49 std::string
clog_type_to_string(clog_type t
);
57 std::hash
<entity_name_t
> h
;
58 _hash
= seq
+ h(rank
);
67 LogEntryKey(const entity_name_t
& w
, utime_t t
, uint64_t s
)
68 : rank(w
), stamp(t
), seq(s
) {
72 uint64_t get_hash() const {
76 void dump(ceph::Formatter
*f
) const;
77 static void generate_test_instances(std::list
<LogEntryKey
*>& o
);
79 friend bool operator==(const LogEntryKey
& l
, const LogEntryKey
& r
) {
80 return l
.rank
== r
.rank
&& l
.stamp
== r
.stamp
&& l
.seq
== r
.seq
;
83 void encode(bufferlist
& bl
) const {
89 void decode(bufferlist::const_iterator
&p
) {
96 WRITE_CLASS_ENCODER(LogEntryKey
)
100 template<> struct hash
<LogEntryKey
> {
101 size_t operator()(const LogEntryKey
& r
) const {
110 entity_addrvec_t addrs
;
117 LogEntry() : seq(0), prio(CLOG_DEBUG
) {}
119 LogEntryKey
key() const { return LogEntryKey(rank
, stamp
, seq
); }
121 void log_to_syslog(std::string level
, std::string facility
) const;
123 void encode(ceph::buffer::list
& bl
, uint64_t features
) const;
124 void decode(ceph::buffer::list::const_iterator
& bl
);
125 void dump(ceph::Formatter
*f
) const;
126 static void generate_test_instances(std::list
<LogEntry
*>& o
);
127 static clog_type
str_to_level(std::string
const &str
);
129 WRITE_CLASS_ENCODER_FEATURES(LogEntry
)
134 // ---- pre-quincy ----
135 // channel -> [(seq#, entry), ...]
136 std::map
<std::string
,std::list
<std::pair
<uint64_t,LogEntry
>>> tail_by_channel
;
138 ceph::unordered_set
<LogEntryKey
> keys
;
141 LRUSet
<LogEntryKey
> recent_keys
;
142 std::map
<std::string
, std::pair
<uint64_t,uint64_t>> channel_info
; // channel -> [begin, end)
144 LogSummary() : version(0) {}
146 void build_ordered_tail_legacy(std::list
<LogEntry
> *tail
) const;
148 void add_legacy(const LogEntry
& e
) {
149 keys
.insert(e
.key());
150 tail_by_channel
[e
.channel
].push_back(std::make_pair(++seq
, e
));
152 void prune(size_t max
) {
153 for (auto& i
: tail_by_channel
) {
154 while (i
.second
.size() > max
) {
155 keys
.erase(i
.second
.front().second
.key());
156 i
.second
.pop_front();
159 recent_keys
.prune(max
);
161 bool contains(const LogEntryKey
& k
) const {
162 return keys
.count(k
) || recent_keys
.contains(k
);
165 void encode(ceph::buffer::list
& bl
, uint64_t features
) const;
166 void decode(ceph::buffer::list::const_iterator
& bl
);
167 void dump(ceph::Formatter
*f
) const;
168 static void generate_test_instances(std::list
<LogSummary
*>& o
);
170 WRITE_CLASS_ENCODER_FEATURES(LogSummary
)
172 inline std::ostream
& operator<<(std::ostream
& out
, const clog_type t
)
176 return out
<< "[DBG]";
178 return out
<< "[INF]";
180 return out
<< "[SEC]";
182 return out
<< "[WRN]";
184 return out
<< "[ERR]";
186 return out
<< "[???]";
190 inline std::ostream
& operator<<(std::ostream
& out
, const LogEntry
& e
)
192 return out
<< e
.stamp
<< " " << e
.name
<< " (" << e
.rank
<< ") "
194 << e
.channel
<< " " << e
.prio
<< " " << e
.msg
;
197 template <> struct fmt::formatter
<EntityName
> : fmt::formatter
<std::string_view
> {
198 template <typename FormatContext
>
199 auto format(const EntityName
& e
, FormatContext
& ctx
) {
200 return formatter
<std::string_view
>::format(e
.to_str(), ctx
);
204 template <> struct fmt::formatter
<LogEntry
> : fmt::formatter
<std::string_view
> {
205 template <typename FormatContext
>
206 auto format(const LogEntry
& e
, FormatContext
& ctx
) {
207 return fmt::format_to(ctx
.out(), "{} {} ({}) {} : {} {} {}",
208 e
.stamp
, e
.name
, e
.rank
, e
.seq
, e
.channel
, e
.prio
, e
.msg
);