]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/util/log-impl.hh
update source to Ceph Pacific 16.2.2
[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 class inserter_iterator {
54 public:
55 using iterator_category = std::output_iterator_tag;
56 using difference_type = std::ptrdiff_t;
57 using value_type = char;
58 using pointer = char*;
59 using reference = char&;
60
61 private:
62 log_buf* _buf;
63 char* _current;
64
65 public:
66 explicit inserter_iterator(log_buf& buf) noexcept : _buf(&buf), _current(_buf->_current) { }
67 inserter_iterator(const inserter_iterator& o) noexcept : _buf(o._buf), _current(o._current) { }
68
69 reference operator*() {
70 if (__builtin_expect(_current == _buf->_end, false)) {
71 _buf->realloc_buffer();
72 _current = _buf->_current;
73 }
74 return *_current;
75 }
76 inserter_iterator& operator++() noexcept {
77 if (__builtin_expect(_current == _buf->_current, true)) {
78 ++_buf->_current;
79 }
80 ++_current;
81 return *this;
82 }
83 inserter_iterator operator++(int) noexcept {
84 inserter_iterator o(*this);
85 ++(*this);
86 return o;
87 }
88 };
89
90 /// Default ctor.
91 ///
92 /// Allocates an internal buffer of 512 bytes.
93 log_buf();
94 /// External buffer ctor.
95 ///
96 /// Use the external buffer until its full, then switch to internally
97 /// allocated buffer. log_buf doesn't take ownership of the buffer.
98 log_buf(char* external_buf, size_t size) noexcept;
99 ~log_buf();
100 /// Create an output iterator which allows writing into the buffer.
101 inserter_iterator back_insert_begin() noexcept { return inserter_iterator(*this); }
102 /// The amount of data written so far.
103 const size_t size() const noexcept { return _current - _begin; }
104 /// The size of the buffer.
105 const size_t capacity() const noexcept { return _end - _begin; }
106 /// Read only pointer to the buffer.
107 /// Note that the buffer is not guaranteed to be null terminated. The writer
108 /// has to ensure that, should it wish to.
109 const char* data() const noexcept { return _begin; }
110 /// A view of the buffer content.
111 std::string_view view() const noexcept { return std::string_view(_begin, size()); }
112 };
113
114 } // namespace internal
115 /// \endcond
116
117 } // namespace seastar