10 #include "accesslog.h"
13 #include "mutexlock.h"
17 AccessLogThread::AccessLogThread()
19 pthread_mutex_init(&mutex, NULL);
22 AccessLogThread::AccessLogThread(const string &filename)
23 : filename(filename) {
24 pthread_mutex_init(&mutex, NULL);
27 void AccessLogThread::write(const ClientStats& client)
29 MutexLock lock(&mutex);
30 pending_writes.push_back(client);
33 void AccessLogThread::do_work()
36 if (filename.empty()) {
39 logfp = fopen(filename.c_str(), "a+");
41 log_perror(filename.c_str());
42 // Continue as before.
46 while (!should_stop) {
48 vector<ClientStats> writes;
50 MutexLock lock(&mutex);
51 swap(pending_writes, writes);
58 // Do the actual writes.
59 time_t now = time(NULL);
60 for (size_t i = 0; i < writes.size(); ++i) {
61 fprintf(logfp, "%llu %s %s %d %llu %llu %llu\n",
62 (long long unsigned)(writes[i].connect_time),
63 writes[i].remote_addr.c_str(),
64 writes[i].stream_id.c_str(),
65 int(now - writes[i].connect_time),
66 (long long unsigned)(writes[i].bytes_sent),
67 (long long unsigned)(writes[i].bytes_lost),
68 (long long unsigned)(writes[i].num_loss_events));
72 // Wait until the stop_fd pipe is closed, one second has passed.
73 // or a spurious signal arrives.
75 pfd.fd = stop_fd_read;
76 pfd.events = POLLIN | POLLRDHUP;
78 int nfds = poll(&pfd, 1, 1000);
79 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
93 if (fclose(logfp) == EOF) {