10 #include "mutexlock.h"
14 AccessLogThread::AccessLogThread()
16 pthread_mutex_init(&mutex, NULL);
19 AccessLogThread::AccessLogThread(const string &filename)
20 : filename(filename) {
21 pthread_mutex_init(&mutex, NULL);
24 void AccessLogThread::write(const ClientStats& client)
26 MutexLock lock(&mutex);
27 pending_writes.push_back(client);
30 void AccessLogThread::do_work()
33 if (filename.empty()) {
36 logfp = fopen(filename.c_str(), "a+");
38 log_perror(filename.c_str());
39 // Continue as before.
43 while (!should_stop()) {
45 vector<ClientStats> writes;
47 MutexLock lock(&mutex);
48 swap(pending_writes, writes);
52 // Do the actual writes.
53 time_t now = time(NULL);
54 for (size_t i = 0; i < writes.size(); ++i) {
55 fprintf(logfp, "%llu %s %s %d %llu %llu %llu\n",
56 (long long unsigned)(writes[i].connect_time),
57 writes[i].remote_addr.c_str(),
58 writes[i].url.c_str(),
59 int(now - writes[i].connect_time),
60 (long long unsigned)(writes[i].bytes_sent),
61 (long long unsigned)(writes[i].bytes_lost),
62 (long long unsigned)(writes[i].num_loss_events));
67 // Wait until we are being woken up, either to quit or because
68 // there is material in pending_writes.
69 wait_for_wakeup(NULL);
73 if (fclose(logfp) == EOF) {