forked from linyacool/WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogStream.cpp
More file actions
executable file
·101 lines (84 loc) · 2.11 KB
/
Copy pathLogStream.cpp
File metadata and controls
executable file
·101 lines (84 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// @Author Lin Ya
// @Email [email protected]
#include "LogStream.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <limits>
const char digits[] = "9876543210123456789";
const char* zero = digits + 9;
// From muduo
template <typename T>
size_t convert(char buf[], T value) {
T i = value;
char* p = buf;
do {
int lsd = static_cast<int>(i % 10);
i /= 10;
*p++ = zero[lsd];
} while (i != 0);
if (value < 0) {
*p++ = '-';
}
*p = '\0';
std::reverse(buf, p);
return p - buf;
}
template class FixedBuffer<kSmallBuffer>;
template class FixedBuffer<kLargeBuffer>;
template <typename T>
void LogStream::formatInteger(T v) {
// buffer容不下kMaxNumericSize个字符的话会被直接丢弃
if (buffer_.avail() >= kMaxNumericSize) {
size_t len = convert(buffer_.current(), v);
buffer_.add(len);
}
}
LogStream& LogStream::operator<<(short v) {
*this << static_cast<int>(v);
return *this;
}
LogStream& LogStream::operator<<(unsigned short v) {
*this << static_cast<unsigned int>(v);
return *this;
}
LogStream& LogStream::operator<<(int v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(unsigned int v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(long v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(unsigned long v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(long long v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(unsigned long long v) {
formatInteger(v);
return *this;
}
LogStream& LogStream::operator<<(double v) {
if (buffer_.avail() >= kMaxNumericSize) {
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12g", v);
buffer_.add(len);
}
return *this;
}
LogStream& LogStream::operator<<(long double v) {
if (buffer_.avail() >= kMaxNumericSize) {
int len = snprintf(buffer_.current(), kMaxNumericSize, "%.12Lg", v);
buffer_.add(len);
}
return *this;
}