Two minor cleanups in the stats code.
[cubemap] / input_stats.cpp
1 #include <fcntl.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7 #include <unistd.h>
8 #include <vector>
9
10 #include "client.h"
11 #include "log.h"
12 #include "input.h"
13 #include "input_stats.h"
14 #include "util.h"
15
16 using namespace std;
17
18 InputStatsThread::InputStatsThread(const string &stats_file, int stats_interval, const vector<Input*> &inputs)
19         : stats_file(stats_file),
20           stats_interval(stats_interval),
21           inputs(inputs)
22 {
23 }
24
25 void InputStatsThread::do_work()
26 {
27         while (!should_stop()) {
28                 int fd;
29                 FILE *fp;
30
31                 // Open a new, temporary file.
32                 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
33                 fd = mkostemp(filename, O_WRONLY);
34                 if (fd == -1) {
35                         log_perror(filename);
36                         free(filename);
37                         goto sleep;
38                 }
39
40                 fp = fdopen(fd, "w");
41                 if (fp == NULL) {
42                         log_perror("fdopen");
43                         safe_close(fd);
44                         if (unlink(filename) == -1) {
45                                 log_perror(filename);
46                         }
47                         free(filename);
48                         goto sleep;
49                 }
50
51                 for (size_t i = 0; i < inputs.size(); ++i) {
52                         InputStats stats = inputs[i]->get_stats();
53                         fprintf(fp, "%s %llu %llu\n", stats.url.c_str(),
54                                 (long long unsigned)(stats.bytes_received),
55                                 (long long unsigned)(stats.data_bytes_received));
56                 }
57                 if (fclose(fp) == EOF) {
58                         log_perror("fclose");
59                         if (unlink(filename) == -1) {
60                                 log_perror(filename);
61                         }
62                         free(filename);
63                         goto sleep;
64                 }
65                 
66                 if (rename(filename, stats_file.c_str()) == -1) {
67                         log_perror("rename");
68                         if (unlink(filename) == -1) {
69                                 log_perror(filename);
70                         }
71                 }
72                 free(filename);
73
74 sleep:
75                 // Wait until we are asked to quit, stats_interval timeout,
76                 // or a spurious signal. (The latter will cause us to write stats
77                 // too often, but that's okay.)
78                 timespec timeout_ts;
79                 timeout_ts.tv_sec = stats_interval;
80                 timeout_ts.tv_nsec = 0;
81                 wait_for_wakeup(&timeout_ts);
82         }
83 }