10 #include "accesslog.h"
13 #include "mutexlock.h"
17 AccessLogThread::AccessLogThread() {}
19 AccessLogThread::AccessLogThread(const string &filename)
20 : filename(filename) {}
22 void AccessLogThread::write(const ClientStats& client)
24 MutexLock lock(&mutex);
25 pending_writes.push_back(client);
28 void AccessLogThread::do_work()
31 if (filename.empty()) {
34 logfp = fopen(filename.c_str(), "a+");
36 log_perror(filename.c_str());
37 // Continue as before.
41 while (!should_stop) {
43 vector<ClientStats> writes;
45 MutexLock lock(&mutex);
46 swap(pending_writes, writes);
53 // Do the actual writes.
54 time_t now = time(NULL);
55 for (size_t i = 0; i < writes.size(); ++i) {
56 fprintf(logfp, "%llu %s %s %d %llu %llu %llu\n",
57 (long long unsigned)(writes[i].connect_time),
58 writes[i].remote_addr.c_str(),
59 writes[i].stream_id.c_str(),
60 int(now - writes[i].connect_time),
61 (long long unsigned)(writes[i].bytes_sent),
62 (long long unsigned)(writes[i].bytes_lost),
63 (long long unsigned)(writes[i].num_loss_events));
67 // Wait until the stop_fd pipe is closed, one second has passed.
68 // or a spurious signal arrives.
70 pfd.fd = stop_fd_read;
71 pfd.events = POLLIN | POLLRDHUP;
73 int nfds = poll(&pfd, 1, 1000);
74 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
88 if (fclose(logfp) == EOF) {