Repository navigation
Expand file tree
/
Copy pathaccess-log.cpp
More file actions
220 lines (200 loc) · 7.39 KB
/
Copy pathaccess-log.cpp
File metadata and controls
220 lines (200 loc) · 7.39 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/server/access-log.h"
#include <string>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include "hphp/runtime/base/timestamp.h"
#include "hphp/runtime/server/log-writer.h"
namespace HPHP {
///////////////////////////////////////////////////////////////////////////////
Mutex AccessLogFileData::m_lock;
std::unordered_map<std::string, AccessLogFileData::factory_t>
AccessLogFileData::m_factories;
AccessLogFileData::AccessLogFileData(const std::string& baseDir,
const std::string& fil,
const std::string& lnk,
const std::string& fmt,
int mpl)
: AccessLogFileData(PATH_CONCAT(baseDir, fil), PATH_CONCAT(baseDir, lnk), fmt, mpl) {}
AccessLogFileData::AccessLogFileData(const std::string& fil,
const std::string& lnk,
const std::string& fmt,
int mpl)
: file(fil)
, symLink(lnk)
, format(fmt)
, periodMultiplier(mpl)
{
/*
* a LogWriter with it's format can be selected between colons like:
* Format = :thrift: [["%{%s}t", "out-name", "STRING"], ...]
*/
m_logOutputType = ClassicWriter::handle;
auto fmt_ = folly::StringPiece(fmt);
while (!fmt_.empty() && std::isspace(fmt_.front())) fmt_.pop_front();
if (fmt_.removePrefix(':')) {
size_t close = fmt_.find(':');
if (close != fmt_.npos) {
m_logOutputType = fmt_.subpiece(0, close).str();
fmt_.advance(close + 1);
format = folly::trimWhitespace(fmt_).str();
}
}
}
std::unique_ptr<LogWriter> AccessLogFileData::Writer(LogChannel chan) const {
Lock l(m_lock);
auto ifactory = m_factories.find(m_logOutputType);
if (ifactory != m_factories.end()) {
return ifactory->second(*this, chan);
}
throw std::runtime_error(
("LogWriter not registered: " + m_logOutputType).c_str());
}
void AccessLogFileData::registerWriter(const std::string& handle,
factory_t factory) {
Lock l(m_lock);
m_factories[handle] = factory;
}
AccessLog::~AccessLog() {
fini();
}
void AccessLog::init(const std::string &defaultFormat,
std::vector<AccessLogFileData> &files,
const std::string &username) {
Lock l(m_lock);
if (m_initialized) return;
m_initialized = true;
m_defaultWriter =
AccessLogFileData("", "", defaultFormat, 0).Writer(LogChannel::THREADLOCAL);
m_defaultWriter->init(username, m_fGetThreadData);
for (auto const& file : files) {
auto ch = Logger::UseCronolog ? LogChannel::CRONOLOG : LogChannel::REGULAR;
auto writer = std::shared_ptr<LogWriter>(file.Writer(ch));
writer->init(username, m_fGetThreadData);
m_files.push_back(writer);
}
}
void AccessLog::init(const std::string &defaultFormat,
std::map<std::string, AccessLogFileData> &files,
const std::string &username) {
Lock l(m_lock);
if (m_initialized) return;
m_initialized = true;
m_defaultWriter =
AccessLogFileData("", "", defaultFormat, 0).Writer(LogChannel::THREADLOCAL);
m_defaultWriter->init(username, m_fGetThreadData);
for (auto const& file : files) {
auto ch = Logger::UseCronolog ? LogChannel::CRONOLOG : LogChannel::REGULAR;
auto writer = std::shared_ptr<LogWriter>(file.second.Writer(ch));
writer->init(username, m_fGetThreadData);
m_files.push_back(writer);
}
}
void AccessLog::init(const std::string &format,
const std::string &baseDir,
const std::string &symLink,
const std::string &file,
const std::string &username) {
init(format, PATH_CONCAT(baseDir, symLink), PATH_CONCAT(baseDir, file), username);
}
void AccessLog::init(const std::string &format,
const std::string &symLink,
const std::string &file,
const std::string &username) {
Lock l(m_lock);
if (m_initialized) return;
m_initialized = true;
m_defaultWriter =
AccessLogFileData("", "", format, 0).Writer(LogChannel::THREADLOCAL);
m_defaultWriter->init(username, m_fGetThreadData);
if (!file.empty() && !format.empty()) {
auto ch = Logger::UseCronolog ? LogChannel::CRONOLOG : LogChannel::REGULAR;
auto writer = std::shared_ptr<LogWriter>(
AccessLogFileData(file, symLink, format, 0).Writer(ch));
writer->init(username, m_fGetThreadData);
m_files.push_back(writer);
}
}
void AccessLog::fini() {
if (!m_initialized) return;
Lock l(m_lock);
flushAllWriters();
m_files.clear();
m_defaultWriter.reset();
m_initialized = false;
}
void AccessLog::log(Transport *transport, const VirtualHost *vhost) {
assertx(transport);
if (!m_initialized) return;
m_defaultWriter->write(transport, vhost);
for (auto& file : m_files) file->write(transport, vhost);
}
void AccessLog::onNewRequest() {
if (!m_initialized) return;
ThreadData *threadData = m_fGetThreadData();
threadData->startTime = TimeStamp::Current();
}
void AccessLog::flushAllWriters() {
if (!m_initialized) return;
m_defaultWriter->flush();
for (auto& file : m_files) file->flush();
}
bool AccessLog::setThreadLog(const char *file) {
return (m_fGetThreadData()->log = fopen(file, "a")) != nullptr;
}
void AccessLog::clearThreadLog() {
FILE* &threadLog = m_fGetThreadData()->log;
if (threadLog) {
fclose(threadLog);
}
threadLog = nullptr;
}
FILE* LogWriter::getOutputFile() const {
FILE* outfile = nullptr;
switch (m_channel) {
case LogChannel::THREADLOCAL:
{
auto tData = (m_threadDataFn ? m_threadDataFn() : nullptr);
outfile = (tData ? tData->log : nullptr);
}
break;
case LogChannel::CRONOLOG:
outfile = (m_cronolog.get() ? m_cronolog->getOutputFile() : nullptr);
break;
case LogChannel::REGULAR:
outfile = m_filelog;
break;
}
return outfile;
}
void LogWriter::recordWriteAndMaybeDropCaches(FILE* out, int bytes) {
switch (m_channel) {
case LogChannel::THREADLOCAL:
{
auto tData = (m_threadDataFn ? m_threadDataFn() : nullptr);
if (tData) tData->flusher.recordWriteAndMaybeDropCaches(out, bytes);
}
break;
case LogChannel::CRONOLOG:
case LogChannel::REGULAR:
m_flusher.recordWriteAndMaybeDropCaches(out, bytes);
break;
}
}
///////////////////////////////////////////////////////////////////////////////
}