]> git.proxmox.com Git - ceph.git/blame - ceph/src/log/Entry.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / log / Entry.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#ifndef __CEPH_LOG_ENTRY_H
5#define __CEPH_LOG_ENTRY_H
6
11fdf7f2
TL
7#include "log/LogClock.h"
8
9#include "common/StackStringStream.h"
10
11#include "boost/container/small_vector.hpp"
12
7c673cae 13#include <pthread.h>
7c673cae 14
11fdf7f2 15#include <string_view>
7c673cae
FG
16
17namespace ceph {
18namespace logging {
19
11fdf7f2
TL
20class Entry {
21public:
22 using time = log_time;
23
24 Entry() = delete;
25 Entry(short pr, short sub) :
26 m_stamp(clock().now()),
27 m_thread(pthread_self()),
28 m_prio(pr),
29 m_subsys(sub)
30 {}
31 Entry(const Entry &) = default;
32 Entry& operator=(const Entry &) = default;
33 Entry(Entry &&e) = default;
34 Entry& operator=(Entry &&e) = default;
35 virtual ~Entry() = default;
36
37 virtual std::string_view strv() const = 0;
38 virtual std::size_t size() const = 0;
39
40 time m_stamp;
7c673cae
FG
41 pthread_t m_thread;
42 short m_prio, m_subsys;
7c673cae 43
11fdf7f2
TL
44 static log_clock& clock() {
45 static log_clock clock;
46 return clock;
47 }
48};
94b18763 49
11fdf7f2
TL
50/* This should never be moved to the heap! Only allocate this on the stack. See
51 * CachedStackStringStream for rationale.
52 */
53class MutableEntry : public Entry {
94b18763 54public:
11fdf7f2
TL
55 MutableEntry() = delete;
56 MutableEntry(short pr, short sub) : Entry(pr, sub) {}
57 MutableEntry(const MutableEntry&) = delete;
58 MutableEntry& operator=(const MutableEntry&) = delete;
59 MutableEntry(MutableEntry&&) = delete;
60 MutableEntry& operator=(MutableEntry&&) = delete;
61 ~MutableEntry() override = default;
62
63 std::ostream& get_ostream() {
64 return *cos;
7c673cae
FG
65 }
66
11fdf7f2
TL
67 std::string_view strv() const override {
68 return cos->strv();
7c673cae 69 }
11fdf7f2
TL
70 std::size_t size() const override {
71 return cos->strv().size();
7c673cae
FG
72 }
73
11fdf7f2
TL
74private:
75 CachedStackStringStream cos;
76};
7c673cae 77
11fdf7f2
TL
78class ConcreteEntry : public Entry {
79public:
80 ConcreteEntry() = delete;
81 ConcreteEntry(const Entry& e) : Entry(e) {
82 auto strv = e.strv();
83 str.reserve(strv.size());
84 str.insert(str.end(), strv.begin(), strv.end());
7c673cae 85 }
11fdf7f2
TL
86 ConcreteEntry& operator=(const Entry& e) {
87 Entry::operator=(e);
88 auto strv = e.strv();
89 str.reserve(strv.size());
90 str.assign(strv.begin(), strv.end());
91 return *this;
92 }
1e59de90 93 ConcreteEntry(ConcreteEntry&& e) noexcept : Entry(e), str(std::move(e.str)) {}
11fdf7f2
TL
94 ConcreteEntry& operator=(ConcreteEntry&& e) {
95 Entry::operator=(e);
96 str = std::move(e.str);
97 return *this;
98 }
99 ~ConcreteEntry() override = default;
94b18763 100
11fdf7f2
TL
101 std::string_view strv() const override {
102 return std::string_view(str.data(), str.size());
103 }
104 std::size_t size() const override {
105 return str.size();
94b18763 106 }
11fdf7f2
TL
107
108private:
109 boost::container::small_vector<char, 1024> str;
7c673cae
FG
110};
111
112}
113}
114
115#endif