Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
28
29namespace seastar {
30
32namespace internal {
33
42class log_buf {
43 char* _begin;
44 char* _end;
45 char* _current;
46 bool _own_buf;
47 bool _alloc_failure = false;
48private:
49 void free_buffer() noexcept;
50 void realloc_buffer_and_append(char c) noexcept;
51
52public:
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) noexcept {
76 if (__builtin_expect(_buf->_current == _buf->_end, false)) {
77 _buf->realloc_buffer_and_append(c);
78 return *this;
79 }
80 *_buf->_current++ = c;
81 return *this;
82 }
83 inserter_iterator& operator*() noexcept {
84 return *this;
85 }
86 inserter_iterator& operator++() noexcept {
87 return *this;
88 }
89 inserter_iterator operator++(int) noexcept {
90 return *this;
91 }
92 };
93
97 log_buf();
102 log_buf(char* external_buf, size_t size) noexcept;
103 ~log_buf();
107 void clear() { _current = _begin; }
109 inserter_iterator back_insert_begin() noexcept { return inserter_iterator(*this); }
111 size_t size() const noexcept { return _current - _begin; }
113 size_t capacity() const noexcept { return _end - _begin; }
117 const char* data() const noexcept { return _begin; }
119 std::string_view view() const noexcept { return std::string_view(_begin, size()); }
120};
121
122} // namespace internal
124
125} // namespace seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:26