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