]> git.sesse.net Git - cubemap/blobdiff - input_stats.cpp
Create $(libdir) on make install.
[cubemap] / input_stats.cpp
index e3e89e635d24b392c23d1ac3b02e3564878aad44..5b7dc59ae762696f2eca6e9ad12fc4e54fc6272b 100644 (file)
@@ -5,12 +5,12 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <math.h>
 #include <vector>
 
-#include "client.h"
-#include "log.h"
 #include "input.h"
 #include "input_stats.h"
+#include "log.h"
 #include "util.h"
 
 using namespace std;
@@ -31,7 +31,7 @@ void InputStatsThread::do_work()
 
                // Open a new, temporary file.
                char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
-               fd = mkostemp(filename, O_WRONLY);
+               fd = mkostemp(filename, O_WRONLY | O_CLOEXEC);
                if (fd == -1) {
                        log_perror(filename);
                        free(filename);
@@ -39,7 +39,7 @@ void InputStatsThread::do_work()
                }
 
                fp = fdopen(fd, "w");
-               if (fp == NULL) {
+               if (fp == nullptr) {
                        log_perror("fdopen");
                        safe_close(fd);
                        if (unlink(filename) == -1) {
@@ -49,19 +49,31 @@ void InputStatsThread::do_work()
                        goto sleep;
                }
 
-               now = time(NULL);
+               now = time(nullptr);
                for (size_t i = 0; i < inputs.size(); ++i) {
                        InputStats stats = inputs[i]->get_stats();
+                       for (const char ch : stats.url) {
+                               if (isspace(ch) || !isprint(ch)) {
+                                       putc('_', fp);
+                               } else {
+                                       putc(ch, fp);
+                               }
+                       }
+                       fprintf(fp, " %llu %llu",
+                               (long long unsigned)(stats.bytes_received),
+                               (long long unsigned)(stats.data_bytes_received));
                        if (stats.connect_time == -1) {
-                               fprintf(fp, "%s %llu %llu -\n", stats.url.c_str(),
-                                       (long long unsigned)(stats.bytes_received),
-                                       (long long unsigned)(stats.data_bytes_received));
+                               fprintf(fp, " -");
+                       } else {
+                               fprintf(fp, " %d", int(now - stats.connect_time));
+                       }
+                       fprintf(fp, " %llu", (long long unsigned)(stats.metadata_bytes_received));
+                       if (!isfinite(stats.latency_sec)) {
+                               fprintf(fp, " -");
                        } else {
-                               fprintf(fp, "%s %llu %llu %d\n", stats.url.c_str(),
-                                       (long long unsigned)(stats.bytes_received),
-                                       (long long unsigned)(stats.data_bytes_received),
-                                       int(now - stats.connect_time));
+                               fprintf(fp, " %.6f", stats.latency_sec);
                        }
+                       fprintf(fp, "\n");
                }
                if (fclose(fp) == EOF) {
                        log_perror("fclose");