]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/util/log-impl.hh
import quincy beta 17.1.0
[ceph.git] / ceph / src / seastar / include / seastar / util / log-impl.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2020 Cloudius Systems, Ltd.
20 */
21
22 #pragma once
23
24 #include <iterator>
25
26 /// \addtogroup logging
27 /// @{
28
29 namespace seastar {
30
31 /// \cond internal
32 namespace internal {
33
34 /// A buffer to format log messages into.
35 ///
36 /// It was designed to allow formatting the entire message into it, without any
37 /// intermediary buffers. To minimize the amount of reallocations it supports
38 /// using an external buffer. When this is full it moves to using buffers
39 /// allocated by itself.
40 /// To accommodate the most widely used way of formatting messages -- fmt --,
41 /// it provides an output iterator interface for writing into it.
42 class log_buf {
43 char* _begin;
44 char* _end;
45 char* _current;
46 bool _own_buf;
47
48 private:
49 void free_buffer() noexcept;
50 void realloc_buffer();
51
52 public:
53 // inserter_iterator is designed like std::back_insert_iterator:
54 // operator*, operator++ and operator++(int) are no-ops,
55 // and all the work happens in operator=, which pushes a character
56 // to the buffer.
57 // The iterator stores no state of its own.
58 //
59 // inserter_iterator is supposed to be used as an output_iterator.
60 // That is, assignment is expected to alternate with incrementing.
61 class inserter_iterator {
62 public:
63 using iterator_category = std::output_iterator_tag;
64 using difference_type = std::ptrdiff_t;
65 using value_type = void;
66 using pointer = void;
67 using reference = void;
68
69 private:
70 log_buf* _buf;
71
72 public:
73 explicit inserter_iterator(log_buf& buf) noexcept : _buf(&buf) { }
74
75 inserter_iterator& operator=(char c) {
76 if (__builtin_expect(_buf->_current == _buf->_end, false)) {
77 _buf->realloc_buffer();
78 }
79 *_buf->_current++ = c;
80 return *this;
81 }
82 inserter_iterator& operator*() {
83 return *this;
84 }
85 inserter_iterator& operator++() noexcept {
86 return *this;
87 }
88 inserter_iterator operator++(int) noexcept {
89 return *this;
90 }
91 };
92
93 /// Default ctor.
94 ///
95 /// Allocates an internal buffer of 512 bytes.
96 log_buf();
97 /// External buffer ctor.
98 ///
99 /// Use the external buffer until its full, then switch to internally
100 /// allocated buffer. log_buf doesn't take ownership of the buffer.
101 log_buf(char* external_buf, size_t size) noexcept;
102 ~log_buf();
103 /// Create an output iterator which allows writing into the buffer.
104 inserter_iterator back_insert_begin() noexcept { return inserter_iterator(*this); }
105 /// The amount of data written so far.
106 const size_t size() const noexcept { return _current - _begin; }
107 /// The size of the buffer.
108 const size_t capacity() const noexcept { return _end - _begin; }
109 /// Read only pointer to the buffer.
110 /// Note that the buffer is not guaranteed to be null terminated. The writer
111 /// has to ensure that, should it wish to.
112 const char* data() const noexcept { return _begin; }
113 /// A view of the buffer content.
114 std::string_view view() const noexcept { return std::string_view(_begin, size()); }
115 };
116
117 } // namespace internal
118 /// \endcond
119
120 } // namespace seastar