1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef __CEPH_LOG_LOG_H
5 #define __CEPH_LOG_LOG_H
7 #include <boost/circular_buffer.hpp>
9 #include <condition_variable>
14 #include <string_view>
16 #include "common/Thread.h"
17 #include "common/likely.h"
19 #include "log/Entry.h"
27 class Log
: private Thread
29 using EntryRing
= boost::circular_buffer
<ConcreteEntry
>;
30 using EntryVector
= std::vector
<ConcreteEntry
>;
32 static const std::size_t DEFAULT_MAX_NEW
= 100;
33 static const std::size_t DEFAULT_MAX_RECENT
= 10000;
35 Log
**m_indirect_this
;
38 const SubsystemMap
*m_subs
;
40 std::mutex m_queue_mutex
;
41 std::mutex m_flush_mutex
;
42 std::condition_variable m_cond_loggers
;
43 std::condition_variable m_cond_flusher
;
45 pthread_t m_queue_mutex_holder
;
46 pthread_t m_flush_mutex_holder
;
48 EntryVector m_new
; ///< new entries
49 EntryRing m_recent
; ///< recent (less new) entries we've already written at low detail
50 EntryVector m_flush
; ///< entries to be flushed (here to optimize heap allocations)
52 std::string m_log_file
;
57 int m_fd_last_error
= 0; ///< last error we say writing to fd (if any)
59 int m_syslog_log
= -2, m_syslog_crash
= -2;
60 int m_stderr_log
= -1, m_stderr_crash
= -1;
61 int m_graylog_log
= -3, m_graylog_crash
= -3;
63 std::string m_log_stderr_prefix
;
65 std::shared_ptr
<Graylog
> m_graylog
;
67 std::vector
<char> m_log_buf
;
71 std::size_t m_max_new
= DEFAULT_MAX_NEW
;
72 std::size_t m_max_recent
= DEFAULT_MAX_RECENT
;
74 bool m_inject_segv
= false;
76 void *entry() override
;
78 void _log_safe_write(std::string_view sv
);
80 void _flush(EntryVector
& q
, bool requeue
, bool crash
);
82 void _log_message(const char *s
, bool crash
);
85 using Thread::is_started
;
87 Log(const SubsystemMap
*s
);
90 void set_flush_on_exit();
92 void set_coarse_timestamps(bool coarse
);
93 void set_max_new(std::size_t n
);
94 void set_max_recent(std::size_t n
);
95 void set_log_file(std::string_view fn
);
96 void reopen_log_file();
97 void chown_log_file(uid_t uid
, gid_t gid
);
98 void set_log_stderr_prefix(std::string_view p
);
104 void set_syslog_level(int log
, int crash
);
105 void set_stderr_level(int log
, int crash
);
106 void set_graylog_level(int log
, int crash
);
108 void start_graylog();
111 std::shared_ptr
<Graylog
> graylog() { return m_graylog
; }
113 void submit_entry(Entry
&& e
);
118 /// true if the log lock is held by our thread
119 bool is_inside_log_lock();
121 /// induce a segv on the next log event