Merge branch 'master' of /srv/git.sesse.net/www/cubemap
[cubemap] / accesslog.cpp
1 #include <stddef.h>
2 #include <stdio.h>
3 #include <time.h>
4 #include <string>
5 #include <vector>
6
7 #include "accesslog.h"
8 #include "client.h"
9 #include "log.h"
10 #include "mutexlock.h"
11
12 using namespace std;
13
14 AccessLogThread::AccessLogThread()
15 {
16         pthread_mutex_init(&mutex, NULL);
17 }
18
19 AccessLogThread::AccessLogThread(const string &filename)
20         : filename(filename) {
21         pthread_mutex_init(&mutex, NULL);
22 }
23
24 void AccessLogThread::write(const ClientStats& client)
25 {
26         {
27                 MutexLock lock(&mutex);
28                 pending_writes.push_back(client);
29         }
30         wakeup();
31 }
32
33 void AccessLogThread::do_work()
34 {
35         // Open the file.
36         if (filename.empty()) {
37                 logfp = NULL;
38         } else {
39                 logfp = fopen(filename.c_str(), "a+");
40                 if (logfp == NULL) {
41                         log_perror(filename.c_str());
42                         // Continue as before.
43                 }
44         }
45
46         while (!should_stop()) {
47                 // Empty the queue.
48                 vector<ClientStats> writes;
49                 {
50                         MutexLock lock(&mutex);
51                         swap(pending_writes, writes);
52                 }
53
54                 if (logfp != NULL) {
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].url.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));
66                         }
67                         fflush(logfp);
68                 }
69
70                 // Wait until we are being woken up, either to quit or because
71                 // there is material in pending_writes.
72                 wait_for_wakeup(NULL);
73         }
74
75         if (logfp != NULL) {    
76                 if (fclose(logfp) == EOF) {
77                         log_perror("fclose");
78                 }
79         }
80
81         logfp = NULL;
82 }