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()) {
31 // Open a new, temporary file.
32 char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
33 fd = mkostemp(filename, O_WRONLY);
44 if (unlink(filename) == -1) {
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));
57 if (fclose(fp) == EOF) {
59 if (unlink(filename) == -1) {
66 if (rename(filename, stats_file.c_str()) == -1) {
68 if (unlink(filename) == -1) {
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.)
79 timeout_ts.tv_sec = stats_interval;
80 timeout_ts.tv_nsec = 0;
81 wait_for_wakeup(&timeout_ts);