13 #include "input_stats.h"
18 InputStatsThread::InputStatsThread(const string &stats_file, int stats_interval, const vector<Input*> &inputs)
19 : stats_file(stats_file),
20 stats_interval(stats_interval),
25 void InputStatsThread::do_work()
27 while (!should_stop()) {
30 vector<ClientStats> client_stats;
32 // Open a new, temporary file.
33 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
34 fd = mkostemp(filename, O_WRONLY);
45 if (unlink(filename) == -1) {
52 for (size_t i = 0; i < inputs.size(); ++i) {
53 InputStats stats = inputs[i]->get_stats();
54 fprintf(fp, "%s %llu %llu\n", stats.url.c_str(),
55 (long long unsigned)(stats.bytes_received),
56 (long long unsigned)(stats.data_bytes_received));
58 if (fclose(fp) == EOF) {
60 if (unlink(filename) == -1) {
67 if (rename(filename, stats_file.c_str()) == -1) {
69 if (unlink(filename) == -1) {
76 // Wait until we are asked to quit, stats_interval timeout,
77 // or a spurious signal. (The latter will cause us to write stats
78 // too often, but that's okay.)
80 timeout_ts.tv_sec = stats_interval;
81 timeout_ts.tv_nsec = 0;
82 wait_for_wakeup(&timeout_ts);