11 #include "input_stats.h"
17 InputStatsThread::InputStatsThread(const string &stats_file, int stats_interval, const vector<Input*> &inputs)
18 : stats_file(stats_file),
19 stats_interval(stats_interval),
24 void InputStatsThread::do_work()
26 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) {
52 for (size_t i = 0; i < inputs.size(); ++i) {
53 InputStats stats = inputs[i]->get_stats();
54 if (stats.connect_time == -1) {
55 fprintf(fp, "%s %llu %llu -\n", stats.url.c_str(),
56 (long long unsigned)(stats.bytes_received),
57 (long long unsigned)(stats.data_bytes_received));
59 fprintf(fp, "%s %llu %llu %d\n", stats.url.c_str(),
60 (long long unsigned)(stats.bytes_received),
61 (long long unsigned)(stats.data_bytes_received),
62 int(now - stats.connect_time));
65 if (fclose(fp) == EOF) {
67 if (unlink(filename) == -1) {
74 if (rename(filename, stats_file.c_str()) == -1) {
76 if (unlink(filename) == -1) {
83 // Wait until we are asked to quit, stats_interval timeout,
84 // or a spurious signal. (The latter will cause us to write stats
85 // too often, but that's okay.)
87 timeout_ts.tv_sec = stats_interval;
88 timeout_ts.tv_nsec = 0;
89 wait_for_wakeup(&timeout_ts);