#include <sys/types.h>
#include <assert.h>
#include <fcntl.h>
+#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
return mktime(&tm);
}
-static int
-daylog_open(void)
-{
- return open(daylog_fn, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW, 0600);
-}
+/* Warns on error. */
+static void daylog_write(const char *format, ...) {
+ int fd;
+ ssize_t wr;
+ va_list va;
+ struct str *buf;
-static void
-daylog_emit(void)
-{
- int fd = daylog_open();
-
- if (fd != -1) {
- struct str *buf = str_make();
- char *s;
- size_t len;
- str_appendf(buf, "%s|%u|%qu|%qu|%qu|%qu\n",
- fmt_date(today_time), (unsigned int)today_time,
- bytes_in, bytes_out, pkts_in, pkts_out);
- str_extract(buf, &len, &s);
-
- (void)write(fd, s, len); /* ignore write errors */
- close(fd);
- free(s);
+ assert(daylog_fn != NULL);
+ fd = open(daylog_fn, O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW, 0600);
+ if (fd == -1) {
+ warn("daylog_write: couldn't open '%s' for append", daylog_fn);
+ return;
}
+
+ buf = str_make();
+ va_start(va, format);
+ str_vappendf(buf, format, va);
+ va_end(va);
+
+ wr = str_write(buf, fd);
+ if (wr == -1)
+ warn("daylog_write: couldn't write to '%s'", daylog_fn);
+ else if (wr != (ssize_t)str_len(buf))
+ warnx("daylog_write: truncated write to '%s': wrote %d of %d bytes",
+ daylog_fn,
+ (int)wr,
+ (int)str_len(buf));
+ close(fd);
+ str_free(buf);
}
-void
-daylog_init(const char *filename)
-{
- int fd;
- struct str *buf;
- char *s;
- size_t len;
+static void daylog_emit(void) {
+ daylog_write("%s|%u|%qu|%qu|%qu|%qu\n",
+ fmt_date(today_time), (unsigned int)today_time,
+ bytes_in, bytes_out, pkts_in, pkts_out);
+}
+void daylog_init(const char *filename) {
daylog_fn = filename;
today_time = time(NULL);
tomorrow_time = tomorrow(today_time);
(unsigned int)today_time, (unsigned int)tomorrow_time);
bytes_in = bytes_out = pkts_in = pkts_out = 0;
- fd = daylog_open();
- if (fd == -1)
- err(1, "couldn't open(\"%s\") for append", filename);
-
- buf = str_make();
- str_appendf(buf, "# logging started at %s (%u)\n",
- fmt_date(today_time), (unsigned int)today_time);
- str_extract(buf, &len, &s);
- (void)write(fd, s, len); /* ignore write errors */
- close(fd);
- free(s);
+ daylog_write("# logging started at %s (%u)\n",
+ fmt_date(today_time), (unsigned int)today_time);
}
-void daylog_free(void)
-{
- int fd;
- struct str *buf;
- char *s;
- size_t len;
-
+void daylog_free(void) {
today_time = time(NULL);
-
- /* Emit what's currently accumulated. */
- daylog_emit();
-
- fd = daylog_open();
- if (fd == -1) return;
-
- buf = str_make();
- str_appendf(buf, "# logging stopped at %s (%u)\n",
- fmt_date(today_time), (unsigned int)today_time);
- str_extract(buf, &len, &s);
- (void)write(fd, s, len); /* ignore write errors */
- close(fd);
- free(s);
+ daylog_emit(); /* Emit what's currently accumulated before we exit. */
+ daylog_write("# logging stopped at %s (%u)\n",
+ fmt_date(today_time), (unsigned int)today_time);
}
-void
-daylog_acct(uint64_t amount, enum graph_dir dir)
-{
+void daylog_acct(uint64_t amount, enum graph_dir dir) {
if (daylog_fn == NULL) return; /* disabled */
- /* Check if we need to rotate. */
+ /* Check if we need to update the log. */
if (now >= tomorrow_time) {
daylog_emit();
today_time = now;
tomorrow_time = tomorrow(today_time);
bytes_in = bytes_out = pkts_in = pkts_out = 0;
- verbosef("rotated daylog, tomorrow = %u",
- (unsigned int)tomorrow_time);
+ verbosef("updated daylog, tomorrow = %u", (unsigned int)tomorrow_time);
}
/* Accounting. */
#ifdef __GNUC__
/* amusing efficiency hack */
-#include <string.h>
-#define str_append(buf, s) str_appendn(buf, s, \
- (__builtin_constant_p(s) ? sizeof(s)-1 : strlen(s)) )
+# include <string.h>
+# define str_append(buf, s) \
+ str_appendn(buf, s, \
+ (__builtin_constant_p(s) ? sizeof(s)-1 : strlen(s)) )
#else
void str_append(struct str *buf, const char *s);
#endif
size_t xvasprintf(char **result, const char *format, va_list va);
size_t xasprintf(char **result, const char *format, ...);
void str_appendf(struct str *buf, const char *format, ...);
+void str_vappendf(struct str *s, const char *format, va_list va);
struct str *length_of_time(const time_t t);
+ssize_t str_write(const struct str * const buf, const int fd);
+size_t str_len(const struct str * const buf);
/* vim:set ts=3 sw=3 tw=78 expandtab: */