]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/LogClient.h
update sources to v12.1.0
[ceph.git] / ceph / src / common / LogClient.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_LOGCLIENT_H
16 #define CEPH_LOGCLIENT_H
17
18 #include <atomic>
19 #include "common/LogEntry.h"
20 #include "common/Mutex.h"
21
22 class LogClient;
23 class MLog;
24 class MLogAck;
25 class Messenger;
26 class MonMap;
27 class Message;
28 struct uuid_d;
29 struct Connection;
30
31 class LogChannel;
32
33 namespace ceph {
34 namespace logging {
35 class Graylog;
36 }
37 }
38
39 int parse_log_client_options(CephContext *cct,
40 map<string,string> &log_to_monitors,
41 map<string,string> &log_to_syslog,
42 map<string,string> &log_channels,
43 map<string,string> &log_prios,
44 map<string,string> &log_to_graylog,
45 map<string,string> &log_to_graylog_host,
46 map<string,string> &log_to_graylog_port,
47 uuid_d &fsid,
48 string &host);
49
50 class LogClientTemp
51 {
52 public:
53 LogClientTemp(clog_type type_, LogChannel &parent_);
54 LogClientTemp(const LogClientTemp &rhs);
55 ~LogClientTemp();
56
57 template<typename T>
58 std::ostream& operator<<(const T& rhs)
59 {
60 return ss << rhs;
61 }
62
63 private:
64 clog_type type;
65 LogChannel &parent;
66 stringstream ss;
67 };
68
69 /** Manage where we output to and at which priority
70 *
71 * Not to be confused with the LogClient, which is the almighty coordinator
72 * of channels. We just deal with the boring part of the logging: send to
73 * syslog, send to file, generate LogEntry and queue it for the LogClient.
74 *
75 * Past queueing the LogEntry, the LogChannel is done with the whole thing.
76 * LogClient will deal with sending and handling of LogEntries.
77 */
78 class LogChannel
79 {
80 public:
81
82 LogChannel(CephContext *cct, LogClient *lc, const std::string &channel);
83 LogChannel(CephContext *cct, LogClient *lc,
84 const std::string &channel,
85 const std::string &facility,
86 const std::string &prio);
87
88 LogClientTemp debug() {
89 return LogClientTemp(CLOG_DEBUG, *this);
90 }
91 void debug(std::stringstream &s) {
92 do_log(CLOG_DEBUG, s);
93 }
94 LogClientTemp info() {
95 return LogClientTemp(CLOG_INFO, *this);
96 }
97 void info(std::stringstream &s) {
98 do_log(CLOG_INFO, s);
99 }
100 LogClientTemp warn() {
101 return LogClientTemp(CLOG_WARN, *this);
102 }
103 void warn(std::stringstream &s) {
104 do_log(CLOG_WARN, s);
105 }
106 LogClientTemp error() {
107 return LogClientTemp(CLOG_ERROR, *this);
108 }
109 void error(std::stringstream &s) {
110 do_log(CLOG_ERROR, s);
111 }
112 LogClientTemp sec() {
113 return LogClientTemp(CLOG_SEC, *this);
114 }
115 void sec(std::stringstream &s) {
116 do_log(CLOG_SEC, s);
117 }
118
119 void set_log_to_monitors(bool v) {
120 log_to_monitors = v;
121 }
122 void set_log_to_syslog(bool v) {
123 log_to_syslog = v;
124 }
125 void set_log_channel(const std::string& v) {
126 log_channel = v;
127 }
128 void set_log_prio(const std::string& v) {
129 log_prio = v;
130 }
131 void set_syslog_facility(const std::string& v) {
132 syslog_facility = v;
133 }
134 std::string get_log_prio() { return log_prio; }
135 std::string get_log_channel() { return log_channel; }
136 std::string get_syslog_facility() { return syslog_facility; }
137 bool must_log_to_syslog() { return log_to_syslog; }
138 /**
139 * Do we want to log to syslog?
140 *
141 * @return true if log_to_syslog is true and both channel and prio
142 * are not empty; false otherwise.
143 */
144 bool do_log_to_syslog() {
145 return must_log_to_syslog() &&
146 !log_prio.empty() && !log_channel.empty();
147 }
148 bool must_log_to_monitors() { return log_to_monitors; }
149
150 bool do_log_to_graylog() {
151 return (graylog != nullptr);
152 }
153
154 typedef shared_ptr<LogChannel> Ref;
155
156 /**
157 * update config values from parsed k/v map for each config option
158 *
159 * Pick out the relevant value based on our channel.
160 */
161 void update_config(map<string,string> &log_to_monitors,
162 map<string,string> &log_to_syslog,
163 map<string,string> &log_channels,
164 map<string,string> &log_prios,
165 map<string,string> &log_to_graylog,
166 map<string,string> &log_to_graylog_host,
167 map<string,string> &log_to_graylog_port,
168 uuid_d &fsid,
169 string &host);
170
171 void do_log(clog_type prio, std::stringstream& ss);
172 void do_log(clog_type prio, const std::string& s);
173
174 private:
175 CephContext *cct;
176 LogClient *parent;
177 Mutex channel_lock;
178 std::string log_channel;
179 std::string log_prio;
180 std::string syslog_facility;
181 bool log_to_syslog;
182 bool log_to_monitors;
183 shared_ptr<ceph::logging::Graylog> graylog;
184
185
186 friend class LogClientTemp;
187 };
188
189 typedef LogChannel::Ref LogChannelRef;
190
191 class LogClient
192 {
193 public:
194 enum logclient_flag_t {
195 NO_FLAGS = 0,
196 FLAG_MON = 0x1,
197 };
198
199 LogClient(CephContext *cct, Messenger *m, MonMap *mm,
200 enum logclient_flag_t flags);
201 virtual ~LogClient() {
202 channels.clear();
203 }
204
205 bool handle_log_ack(MLogAck *m);
206 Message *get_mon_log_message(bool flush);
207 bool are_pending();
208
209 LogChannelRef create_channel() {
210 return create_channel(CLOG_CHANNEL_DEFAULT);
211 }
212
213 LogChannelRef create_channel(const std::string& name) {
214 LogChannelRef c;
215 if (channels.count(name))
216 c = channels[name];
217 else {
218 c = std::make_shared<LogChannel>(cct, this, name);
219 channels[name] = c;
220 }
221 return c;
222 }
223
224 void destroy_channel(const std::string& name) {
225 if (channels.count(name))
226 channels.erase(name);
227 }
228
229 void shutdown() {
230 channels.clear();
231 }
232
233 uint64_t get_next_seq();
234 const entity_inst_t& get_myinst();
235 const EntityName& get_myname();
236 version_t queue(LogEntry &entry);
237
238 private:
239 Message *_get_mon_log_message();
240 void _send_to_mon();
241
242 CephContext *cct;
243 Messenger *messenger;
244 MonMap *monmap;
245 bool is_mon;
246 Mutex log_lock;
247 version_t last_log_sent;
248 std::atomic<uint64_t> last_log;
249 std::deque<LogEntry> log_queue;
250
251 std::map<std::string, LogChannelRef> channels;
252
253 };
254 #endif