-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtaccesslog.cpp
166 lines (133 loc) · 3.81 KB
/
taccesslog.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (c) 2011-2019, AOYAMA Kazuharu
* All rights reserved.
*
* This software may be used and distributed according to the terms of
* the New BSD License, which is incorporated herein by reference.
*/
#include "tsystemglobal.h"
#include <TAccessLog>
/*!
\class TAccessLog
\brief The TAccessLog class defines the log of access to the web
application server.
*/
TAccessLog::TAccessLog()
{
}
TAccessLog::TAccessLog(const QByteArray &host, const QByteArray &req, int dur) :
remoteHost(host),
request(req),
duration(dur)
{
}
QByteArray TAccessLog::toByteArray(const QByteArray &layout, const QByteArray &dateTimeFormat) const
{
QByteArray message;
int pos = 0;
QByteArray dig;
message.reserve(127);
while (pos < layout.length()) {
char c = layout.at(pos++);
if (c != '%') {
message.append(c);
continue;
}
dig.resize(0);
for (;;) {
if (pos >= layout.length()) {
message.append('%').append(dig);
break;
}
c = layout.at(pos++);
if (c >= '0' && c <= '9') {
dig += c;
continue;
}
switch (c) {
case 'h':
message.append(remoteHost);
break;
case 'd': // timestamp
if (!dateTimeFormat.isEmpty()) {
message.append(timestamp.toString(dateTimeFormat).toLocal8Bit());
} else {
message.append(timestamp.toString(Qt::ISODate).toLatin1());
}
break;
case 'r':
message.append(request);
break;
case 's':
message.append(QString::number(statusCode).toLatin1());
break;
case 'O':
if (dig.isEmpty()) {
message.append(QString::number(responseBytes).toLatin1());
} else {
const QChar fillChar = (dig[0] == '0') ? QLatin1Char('0') : QLatin1Char(' ');
message.append(QString("%1").arg(responseBytes, dig.toInt(), 10, fillChar).toLatin1());
}
break;
case 'e':
if (dig.isEmpty()) {
message.append(QString::number(duration).toLatin1());
} else {
const QChar fillChar = (dig[0] == '0') ? QLatin1Char('0') : QLatin1Char(' ');
message.append(QString("%1").arg(duration, dig.toInt(), 10, fillChar).toLatin1());
}
break;
case 'n': // %n : newline
message.append('\n');
break;
case '%':
message.append('%').append(dig);
dig.resize(0);
continue;
break;
default:
message.append('%').append(dig).append(c);
break;
}
break;
}
}
return message;
}
TAccessLogger::TAccessLogger()
{
}
TAccessLogger::TAccessLogger(TAccessLogger &&other)
{
_accessLog = std::move(other._accessLog);
_timer = std::move(other._timer);
other._accessLog = nullptr;
}
TAccessLogger::~TAccessLogger()
{
close();
}
TAccessLogger &TAccessLogger::operator=(TAccessLogger &&other)
{
_accessLog = std::move(other._accessLog);
_timer = std::move(other._timer);
other._accessLog = nullptr;
return *this;
}
void TAccessLogger::open()
{
if (!_accessLog) {
_accessLog = new TAccessLog();
}
}
void TAccessLogger::write()
{
if (_accessLog) {
_accessLog->duration = _timer.elapsed();
Tf::writeAccessLog(*_accessLog);
}
}
void TAccessLogger::close()
{
delete _accessLog;
_accessLog = nullptr;
}