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);
55 // Do the actual writes.
56 time_t now = time(NULL);
57 for (size_t i = 0; i < writes.size(); ++i) {
58 fprintf(logfp, "%llu %s %s %d %llu %llu %llu\n",
59 (long long unsigned)(writes[i].connect_time),
60 writes[i].remote_addr.c_str(),
61 writes[i].stream_id.c_str(),
62 int(now - writes[i].connect_time),
63 (long long unsigned)(writes[i].bytes_sent),
64 (long long unsigned)(writes[i].bytes_lost),
65 (long long unsigned)(writes[i].num_loss_events));
70 // Wait until the stop_fd pipe is closed, one second has passed.
71 // or a spurious signal arrives.
73 pfd.fd = stop_fd_read;
74 pfd.events = POLLIN | POLLRDHUP;
76 int nfds = poll(&pfd, 1, 1000);
77 if (nfds == 0 || (nfds == -1 && errno == EINTR)) {
92 if (fclose(logfp) == EOF) {