trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
LogStream.h
Go to the documentation of this file.
1
14
15#pragma once
16
17// Taken from muduo lib and modified. Classes in this file are used internally.
19#include <trantor/exports.h>
20
21#include <assert.h>
22#include <string.h> // memcpy
23#include <string>
24
25namespace trantor
26{
27namespace detail
28{
29static constexpr size_t kSmallBuffer{4000};
30static constexpr size_t kLargeBuffer{4000 * 1000};
31
32template <int SIZE>
33class TRANTOR_EXPORT FixedBuffer : NonCopyable
34{
35 public:
36 FixedBuffer() : cur_(data_)
37 {
38 setCookie(cookieStart);
39 }
40
41 ~FixedBuffer()
42 {
43 setCookie(cookieEnd);
44 }
45
46 bool append(const char * /*restrict*/ buf, size_t len)
47 {
48 if ((size_t)(avail()) > len)
49 {
50 memcpy(cur_, buf, len);
51 cur_ += len;
52 return true;
53 }
54 return false;
55 }
56
57 const char *data() const
58 {
59 return data_;
60 }
61 int length() const
62 {
63 return static_cast<int>(cur_ - data_);
64 }
65
66 // write to data_ directly
67 char *current()
68 {
69 return cur_;
70 }
71 int avail() const
72 {
73 return static_cast<int>(end() - cur_);
74 }
75 void add(size_t len)
76 {
77 cur_ += len;
78 }
79
80 void reset()
81 {
82 cur_ = data_;
83 }
84 void zeroBuffer()
85 {
86 memset(data_, 0, sizeof(data_));
87 }
88
89 // for used by GDB
90 const char *debugString();
91 void setCookie(void (*cookie)())
92 {
93 cookie_ = cookie;
94 }
95 // for used by unit test
96 std::string toString() const
97 {
98 return std::string(data_, length());
99 }
100 // StringPiece toStringPiece() const { return StringPiece(data_, length());
101 // }
102
103 private:
104 const char *end() const
105 {
106 return data_ + sizeof data_;
107 }
108 // Must be outline function for cookies.
109 static void cookieStart();
110 static void cookieEnd();
111
112 void (*cookie_)();
113 char data_[SIZE];
114 char *cur_;
115};
116
117} // namespace detail
118
119class TRANTOR_EXPORT LogStream : NonCopyable
120{
121 using self = LogStream;
122
123 public:
125
126 self &operator<<(bool v)
127 {
128 append(v ? "1" : "0", 1);
129 return *this;
130 }
131
132 self &operator<<(short);
133 self &operator<<(unsigned short);
134 self &operator<<(int);
135 self &operator<<(unsigned int);
136 self &operator<<(long);
137 self &operator<<(unsigned long);
138 self &operator<<(const long long &);
139 self &operator<<(const unsigned long long &);
140
141 self &operator<<(const void *);
142
143 self &operator<<(float &v)
144 {
145 *this << static_cast<double>(v);
146 return *this;
147 }
148 self &operator<<(const double &);
149 self &operator<<(const long double &v);
150
151 self &operator<<(char v)
152 {
153 append(&v, 1);
154 return *this;
155 }
156
157 // self& operator<<(signed char);
158 // self& operator<<(unsigned char);
159 template <int N>
160 self &operator<<(const char (&buf)[N])
161 {
162 assert(strnlen(buf, N) == N - 1);
163 append(buf, N - 1);
164 return *this;
165 }
166
167 self &operator<<(char *str)
168 {
169 if (str)
170 {
171 append(str, strlen(str));
172 }
173 else
174 {
175 append("(null)", 6);
176 }
177 return *this;
178 }
179
180 self &operator<<(const char *str)
181 {
182 if (str)
183 {
184 append(str, strlen(str));
185 }
186 else
187 {
188 append("(null)", 6);
189 }
190 return *this;
191 }
192
193 self &operator<<(const unsigned char *str)
194 {
195 return operator<<(reinterpret_cast<const char *>(str));
196 }
197
198 self &operator<<(const std::string &v)
199 {
200 append(v.c_str(), v.size());
201 return *this;
202 }
203
204 void append(const char *data, size_t len)
205 {
206 if (exBuffer_.empty())
207 {
208 if (!buffer_.append(data, len))
209 {
210 exBuffer_.append(buffer_.data(), buffer_.length());
211 exBuffer_.append(data, len);
212 }
213 }
214 else
215 {
216 exBuffer_.append(data, len);
217 }
218 }
219 // const Buffer& buffer() const { return buffer_; }
220 const char *bufferData() const
221 {
222 if (!exBuffer_.empty())
223 {
224 return exBuffer_.data();
225 }
226 return buffer_.data();
227 }
228
229 size_t bufferLength() const
230 {
231 if (!exBuffer_.empty())
232 {
233 return exBuffer_.length();
234 }
235 return buffer_.length();
236 }
237 void resetBuffer()
238 {
239 buffer_.reset();
240 exBuffer_.clear();
241 }
242
243 private:
244 template <typename T>
245 void formatInteger(T);
246
247 Buffer buffer_;
248 std::string exBuffer_;
249};
250
251class TRANTOR_EXPORT Fmt // : boost::noncopyable
252{
253 public:
254 template <typename T>
255 Fmt(const char *fmt, T val);
256
257 const char *data() const
258 {
259 return buf_;
260 }
261 int length() const
262 {
263 return length_;
264 }
265
266 private:
267 char buf_[48];
268 int length_;
269};
270
271inline LogStream &operator<<(LogStream &s, const Fmt &fmt)
272{
273 s.append(fmt.data(), fmt.length());
274 return s;
275}
276
277} // namespace trantor
Definition LogStream.h:252
Definition LogStream.h:120
Definition LogStream.h:34
Definition EventLoop.h:34