11 #include "serverpool.h"
16 extern ServerPool *servers;
18 StatsThread::StatsThread(const std::string &stats_file, int stats_interval)
19 : stats_file(stats_file),
20 stats_interval(stats_interval)
24 void StatsThread::do_work()
26 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);
51 client_stats = servers->get_client_stats();
52 for (size_t i = 0; i < client_stats.size(); ++i) {
53 fprintf(fp, "%s %s %d %llu %llu %llu\n",
54 client_stats[i].remote_addr.c_str(),
55 client_stats[i].stream_id.c_str(),
56 int(now - client_stats[i].connect_time),
57 (long long unsigned)(client_stats[i].bytes_sent),
58 (long long unsigned)(client_stats[i].bytes_lost),
59 (long long unsigned)(client_stats[i].num_loss_events));
61 if (fclose(fp) == EOF) {
68 if (rename(filename, stats_file.c_str()) == -1) {
74 int left_to_sleep = stats_interval;
76 left_to_sleep = sleep(left_to_sleep);
77 } while (left_to_sleep > 0 && !should_stop);