summaryrefslogtreecommitdiffstats
path: root/src/log/Entry.h
blob: 536f1a9dc8a223956c3450545484794eb739b154 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef __CEPH_LOG_ENTRY_H
#define __CEPH_LOG_ENTRY_H

#include "log/LogClock.h"

#include "common/StackStringStream.h"

#include "boost/container/small_vector.hpp"

#include <pthread.h>

#include <string_view>

namespace ceph {
namespace logging {

class Entry {
public:
  using time = log_time;

  Entry() = delete;
  Entry(short pr, short sub) :
    m_stamp(clock().now()),
    m_thread(pthread_self()),
    m_prio(pr),
    m_subsys(sub)
  {}
  Entry(const Entry &) = default;
  Entry& operator=(const Entry &) = default;
  Entry(Entry &&e) = default;
  Entry& operator=(Entry &&e) = default;
  virtual ~Entry() = default;

  virtual std::string_view strv() const = 0;
  virtual std::size_t size() const = 0;

  time m_stamp;
  pthread_t m_thread;
  short m_prio, m_subsys;

  static log_clock& clock() {
    static log_clock clock;
    return clock;
  }
};

/* This should never be moved to the heap! Only allocate this on the stack. See
 * CachedStackStringStream for rationale.
 */
class MutableEntry : public Entry {
public:
  MutableEntry() = delete;
  MutableEntry(short pr, short sub) : Entry(pr, sub) {}
  MutableEntry(const MutableEntry&) = delete;
  MutableEntry& operator=(const MutableEntry&) = delete;
  MutableEntry(MutableEntry&&) = delete;
  MutableEntry& operator=(MutableEntry&&) = delete;
  ~MutableEntry() override = default;

  std::ostream& get_ostream() {
    return *cos;
  }

  std::string_view strv() const override {
    return cos->strv();
  }
  std::size_t size() const override {
    return cos->strv().size();
  }

private:
  CachedStackStringStream cos;
};

class ConcreteEntry : public Entry {
public:
  ConcreteEntry() = delete;
  ConcreteEntry(const Entry& e) : Entry(e) {
    auto strv = e.strv();
    str.reserve(strv.size());
    str.insert(str.end(), strv.begin(), strv.end());
  }
  ConcreteEntry& operator=(const Entry& e) {
    Entry::operator=(e);
    auto strv = e.strv();
    str.reserve(strv.size());
    str.assign(strv.begin(), strv.end());
    return *this;
  }
  ConcreteEntry(ConcreteEntry&& e) : Entry(e), str(std::move(e.str)) {}
  ConcreteEntry& operator=(ConcreteEntry&& e) {
    Entry::operator=(e);
    str = std::move(e.str);
    return *this;
  }
  ~ConcreteEntry() override = default;

  std::string_view strv() const override {
    return std::string_view(str.data(), str.size());
  }
  std::size_t size() const override {
    return str.size();
  }

private:
  boost::container::small_vector<char, 1024> str;
};

}
}

#endif