Skip to content

Commit 14c0b52

Browse files
committed
Modify the LogStream class
1 parent eddb7c5 commit 14c0b52

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

trantor/tests/LoggerTest.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
#include <thread>
44
int main()
55
{
6+
int i;
7+
LOG_DEBUG << (float)3.14;
8+
LOG_DEBUG << (const char)'8';
9+
LOG_DEBUG << &i;
10+
LOG_DEBUG << (long double)3.1415;
11+
LOG_DEBUG << trantor::Fmt("%.3g", 3.1415926);
612
LOG_DEBUG << "debug log!" << 1;
713
LOG_TRACE << "trace log!" << 2;
814
LOG_INFO << "info log!" << 3;

trantor/utils/LogStream.cc

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,10 @@ void FixedBuffer<SIZE>::cookieEnd()
100100
{
101101
}
102102

103-
void LogStream::staticCheck()
104-
{
105-
static_assert(kMaxNumericSize - 10 > std::numeric_limits<double>::digits10,
106-
"");
107-
static_assert(kMaxNumericSize - 10 >
108-
std::numeric_limits<long double>::digits10,
109-
"");
110-
static_assert(kMaxNumericSize - 10 > std::numeric_limits<long>::digits10,
111-
"");
112-
static_assert(kMaxNumericSize - 10 >
113-
std::numeric_limits<long long>::digits10,
114-
"");
115-
}
116-
117103
template <typename T>
118104
void LogStream::formatInteger(T v)
119105
{
106+
constexpr static int kMaxNumericSize = std::numeric_limits<T>::digits10 + 4;
120107
if (_exBuffer.empty())
121108
{
122109
if (_buffer.avail() >= kMaxNumericSize)
@@ -172,13 +159,13 @@ LogStream &LogStream::operator<<(unsigned long v)
172159
return *this;
173160
}
174161

175-
LogStream &LogStream::operator<<(long long v)
162+
LogStream &LogStream::operator<<(const long long &v)
176163
{
177164
formatInteger(v);
178165
return *this;
179166
}
180167

181-
LogStream &LogStream::operator<<(unsigned long long v)
168+
LogStream &LogStream::operator<<(const unsigned long long &v)
182169
{
183170
formatInteger(v);
184171
return *this;
@@ -187,6 +174,8 @@ LogStream &LogStream::operator<<(unsigned long long v)
187174
LogStream &LogStream::operator<<(const void *p)
188175
{
189176
uintptr_t v = reinterpret_cast<uintptr_t>(p);
177+
constexpr static int kMaxNumericSize =
178+
std::numeric_limits<uintptr_t>::digits / 4 + 4;
190179
if (_exBuffer.empty())
191180
{
192181
if (_buffer.avail() >= kMaxNumericSize)
@@ -214,8 +203,9 @@ LogStream &LogStream::operator<<(const void *p)
214203
}
215204

216205
// TODO: replace this with Grisu3 by Florian Loitsch.
217-
LogStream &LogStream::operator<<(double v)
206+
LogStream &LogStream::operator<<(const double &v)
218207
{
208+
constexpr static int kMaxNumericSize = 32;
219209
if (_exBuffer.empty())
220210
{
221211
if (_buffer.avail() >= kMaxNumericSize)
@@ -236,6 +226,29 @@ LogStream &LogStream::operator<<(double v)
236226
return *this;
237227
}
238228

229+
LogStream &LogStream::operator<<(const long double &v)
230+
{
231+
constexpr static int kMaxNumericSize = 48;
232+
if (_exBuffer.empty())
233+
{
234+
if (_buffer.avail() >= kMaxNumericSize)
235+
{
236+
int len = snprintf(_buffer.current(), kMaxNumericSize, "%.12Lg", v);
237+
_buffer.add(len);
238+
return *this;
239+
}
240+
else
241+
{
242+
_exBuffer.append(_buffer.data(), _buffer.length());
243+
}
244+
}
245+
auto oldLen = _exBuffer.length();
246+
_exBuffer.resize(oldLen + kMaxNumericSize);
247+
int len = snprintf(&(_exBuffer[oldLen]), kMaxNumericSize, "%.12Lg", v);
248+
_exBuffer.resize(oldLen + len);
249+
return *this;
250+
}
251+
239252
template <typename T>
240253
Fmt::Fmt(const char *fmt, T val)
241254
{

trantor/utils/LogStream.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,18 @@ class LogStream : NonCopyable
135135
self &operator<<(unsigned int);
136136
self &operator<<(long);
137137
self &operator<<(unsigned long);
138-
self &operator<<(long long);
139-
self &operator<<(unsigned long long);
138+
self &operator<<(const long long &);
139+
self &operator<<(const unsigned long long &);
140140

141141
self &operator<<(const void *);
142142

143-
self &operator<<(float v)
143+
self &operator<<(float &v)
144144
{
145145
*this << static_cast<double>(v);
146146
return *this;
147147
}
148-
self &operator<<(double);
149-
// self& operator<<(long double);
148+
self &operator<<(const double &);
149+
self &operator<<(const long double &v);
150150

151151
self &operator<<(char v)
152152
{
@@ -250,14 +250,11 @@ class LogStream : NonCopyable
250250
}
251251

252252
private:
253-
void staticCheck();
254-
255253
template <typename T>
256254
void formatInteger(T);
257255

258256
Buffer _buffer;
259257
std::string _exBuffer;
260-
static const int kMaxNumericSize = 32;
261258
};
262259

263260
class Fmt // : boost::noncopyable
@@ -276,7 +273,7 @@ class Fmt // : boost::noncopyable
276273
}
277274

278275
private:
279-
char _buf[32];
276+
char _buf[48];
280277
int _length;
281278
};
282279

0 commit comments

Comments
 (0)