]> git.sesse.net Git - cubemap/blob - accesslog.h
Set close-on-exec on all file descriptors we open.
[cubemap] / accesslog.h
1 #ifndef _ACCESSLOG_H
2 #define _ACCESSLOG_H
3
4 // A class to log clients that just disconnected. Since this is shared by all
5 // Server instances, we try not to let write() block too much, and rather do
6 // all the I/O in a separate I/O thread.
7
8 #include <pthread.h>
9 #include <stdio.h>
10 #include <mutex>
11 #include <string>
12 #include <vector>
13
14 #include "client.h"
15 #include "thread.h"
16
17 class AccessLogThread : public Thread {
18 public:
19         // Used if we do not have a file to log to. The thread will still exist,
20         // but won't actually write anywhere.
21         AccessLogThread();
22
23         // Log to a given file. If the file can't be opened, log an error
24         // to the error log, and work as if we didn't have a log file.
25         AccessLogThread(const std::string &filename);
26
27         // Add a log entry. Entries are written out at least once every second.
28         void write(const ClientStats& client);
29
30 private:
31         virtual void do_work();
32
33         // The file we are logging to. If nullptr, do not log.
34         FILE *logfp;
35         std::string filename;
36
37         std::mutex mu;
38         std::vector<ClientStats> pending_writes;
39 };
40
41 #endif  // _ACCESSLOG_H