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()) {
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) {
53 for (size_t i = 0; i < inputs.size(); ++i) {
54 InputStats stats = inputs[i]->get_stats();
55 if (stats.connect_time == -1) {
56 fprintf(fp, "%s %llu %llu -\n", stats.url.c_str(),
57 (long long unsigned)(stats.bytes_received),
58 (long long unsigned)(stats.data_bytes_received));
60 fprintf(fp, "%s %llu %llu %d\n", stats.url.c_str(),
61 (long long unsigned)(stats.bytes_received),
62 (long long unsigned)(stats.data_bytes_received),
63 int(now - stats.connect_time));
66 if (fclose(fp) == EOF) {
68 if (unlink(filename) == -1) {
75 if (rename(filename, stats_file.c_str()) == -1) {
77 if (unlink(filename) == -1) {
84 // Wait until we are asked to quit, stats_interval timeout,
85 // or a spurious signal. (The latter will cause us to write stats
86 // too often, but that's okay.)
88 timeout_ts.tv_sec = stats_interval;
89 timeout_ts.tv_nsec = 0;
90 wait_for_wakeup(&timeout_ts);