Use a monotonic clock where appropriate.
[darkstat] / daylog.c
1 /* darkstat 3
2 * copyright (c) 2007-2011 Emil Mikulic.
3 *
4 * daylog.c: daily usage log
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #define _GNU_SOURCE 1 /* for O_NOFOLLOW on Linux */
11
12 #include "err.h"
13 #include "daylog.h"
14 #include "str.h"
15 #include "now.h"
16
17 #include <assert.h>
18 #include <fcntl.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 #include <unistd.h>
24
25 static const char *daylog_fn = NULL;
26 static long today_real, tomorrow_real;
27 static uint64_t bytes_in, bytes_out, pkts_in, pkts_out;
28
29 #define DAYLOG_DATE_LEN 26 /* strlen("1900-01-01 00:00:00 +1234") + 1 */
30 static char datebuf[DAYLOG_DATE_LEN];
31
32 static char *fmt_date(time_t when) {
33 if (strftime(datebuf,
34 DAYLOG_DATE_LEN,
35 "%Y-%m-%d %H:%M:%S %z",
36 localtime(&when)) == 0)
37 errx(1, "strftime() failed in fmt_date()");
38 return datebuf;
39 }
40
41 /* Given some time today, find the first second of tomorrow. */
42 static time_t tomorrow(time_t t_before) {
43 time_t t_after;
44 struct tm tm, *lt;
45
46 lt = localtime(&t_before);
47 memcpy(&tm, lt, sizeof(tm));
48 tm.tm_sec = 0;
49 tm.tm_min = 0;
50 tm.tm_hour = 0;
51 tm.tm_mday = lt->tm_mday + 1; /* tomorrow */
52 t_after = mktime(&tm);
53 assert(t_after > t_before);
54 return t_after;
55 }
56
57 /* Warns on error. */
58 static void daylog_write(const char *format, ...) {
59 int fd;
60 ssize_t wr;
61 va_list va;
62 struct str *buf;
63
64 assert(daylog_fn != NULL);
65 fd = open(daylog_fn, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW, 0600);
66 if (fd == -1) {
67 warn("daylog_write: couldn't open '%s' for append", daylog_fn);
68 return;
69 }
70
71 buf = str_make();
72 va_start(va, format);
73 str_vappendf(buf, format, va);
74 va_end(va);
75
76 wr = str_write(buf, fd);
77 if (wr == -1)
78 warn("daylog_write: couldn't write to '%s'", daylog_fn);
79 else if (wr != (ssize_t)str_len(buf))
80 warnx("daylog_write: truncated write to '%s': wrote %d of %d bytes",
81 daylog_fn,
82 (int)wr,
83 (int)str_len(buf));
84 close(fd);
85 str_free(buf);
86 }
87
88 static void daylog_emit(void) {
89 daylog_write("%s|%ld|%qu|%qu|%qu|%qu\n",
90 fmt_date(today_real), (long)today_real,
91 bytes_in, bytes_out, pkts_in, pkts_out);
92 }
93
94 void daylog_init(const char *filename) {
95 daylog_fn = filename;
96 today_real = now_real();
97 tomorrow_real = tomorrow(today_real);
98 verbosef("today is %ld, tomorrow is %ld",
99 (long)today_real, (long)tomorrow_real);
100 bytes_in = bytes_out = pkts_in = pkts_out = 0;
101
102 daylog_write("# logging started at %s (%ld)\n",
103 fmt_date(today_real), (long)today_real);
104 }
105
106 void daylog_free(void) {
107 today_real = now_real();
108 daylog_emit(); /* Emit what's currently accumulated before we exit. */
109 daylog_write("# logging stopped at %s (%ld)\n",
110 fmt_date(today_real), (long)today_real);
111 }
112
113 void daylog_acct(uint64_t amount, enum graph_dir dir) {
114 if (daylog_fn == NULL)
115 return; /* daylogging disabled */
116
117 /* Check if we need to update the log. */
118 if (now_real() >= tomorrow_real) {
119 daylog_emit();
120
121 today_real = now_real();
122 tomorrow_real = tomorrow(today_real);
123 bytes_in = bytes_out = pkts_in = pkts_out = 0;
124 verbosef("updated daylog, tomorrow = %ld", (long)tomorrow_real);
125 }
126
127 /* Accounting. */
128 if (dir == GRAPH_IN) {
129 bytes_in += amount;
130 pkts_in++;
131 } else {
132 assert(dir == GRAPH_OUT);
133 bytes_out += amount;
134 pkts_out++;
135 }
136 }
137
138 /* vim:set ts=3 sw=3 tw=78 et: */