diff src/wflogs-config.cpp @ 4:37eace15ef87

allow hourly/daily/weekly triggers for output generation, append to temp wflogs input files so daemon restart won't drop as much data
author Carl Byington <carl@five-ten-sg.com>
date Fri, 17 May 2013 12:03:21 -0700
parents 400b1de6e1c6
children a043b7d7269c
line wrap: on
line diff
--- a/src/wflogs-config.cpp	Fri May 17 10:37:25 2013 -0700
+++ b/src/wflogs-config.cpp	Fri May 17 12:03:21 2013 -0700
@@ -13,9 +13,16 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <limits.h>
+#include <time.h>
+
+
+string_set      all_strings;// owns all the strings, only modified by the config loader thread
+const int maxlen = 1000;    // used for snprintf buffers
 
 const char *token_context;
+const char *token_daily;
 const char *token_file;
+const char *token_hourly;
 const char *token_include;
 const char *token_lbrace;
 const char *token_output;
@@ -24,13 +31,11 @@
 const char *token_rbrace;
 const char *token_semi;
 const char *token_tempin;
+const char *token_trigger;
 const char *token_versions;
+const char *token_weekly;
 const char *token_wflogs;
 
-string_set      all_strings;// owns all the strings, only modified by the config loader thread
-const int maxlen = 1000;    // used for snprintf buffers
-
-
 
 ////////////////////////////////////////////////
 //
@@ -41,6 +46,7 @@
     fdo                = -1;
     period             = 120;
     versions           = 3;
+    trigger            = NULL;
     output             = NULL;
     tempin             = NULL;
     wflogs             = NULL;
@@ -58,6 +64,7 @@
     printf("context %s {\n", name);
     printf("    period   %d; \n", period);
     printf("    versions %d; \n", versions);
+    if (trigger) printf("    trigger  \"%s\";\n", trigger);
     printf("    output   \"%s\";\n", output);
     printf("    tempin   \"%s\";\n", tempin);
     printf("    wflogs   \"%s\";\n", wflogs);
@@ -69,7 +76,8 @@
 
 void CONTEXT::openo(bool msg) {
     open_time = time(NULL);
-    fdo = ::creat(tempin, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+    localtime_r(&open_time, &open_tm);
+    fdo = ::open(tempin, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
     if (fdo == -1) {
         if (msg) {
             char buf[maxlen];
@@ -77,6 +85,9 @@
             tokp->token_error(buf);
         }
     }
+    else {
+        lseek(fdo, 0, SEEK_END);
+    }
 }
 
 
@@ -191,9 +202,19 @@
 }
 
 
+bool CONTEXT::check_wflog_time() {
+    time_t now_time = time(NULL);
+    tm now_tm;
+    localtime_r(&now_time, &now_tm);
+    return (open_time + period < now_time) || \
+           ((trigger == token_hourly) && (now_tm.tm_hour != open_tm.tm_hour)) || \
+           ((trigger == token_daily)  && (now_tm.tm_wday != open_tm.tm_wday)) || \
+           ((trigger == token_weekly) && (now_tm.tm_wday != open_tm.tm_wday) && (now_tm.tm_wday == 0));
+}
+
+
 void CONTEXT::check_wflog() {
-    time_t now = time(NULL);
-    if ((fdo != -1) && (open_time + period < now)) {
+    if ((fdo != -1) && check_wflog_time()) {
         closeo();
         // rename previous wflog html output files
         char buf[maxlen];
@@ -368,7 +389,16 @@
                 tok.token_error(buf);
                 con->pattern = NULL;
             }
-
+            if (!tsa(tok, token_semi)) return false;
+        }
+        else if (have == token_trigger) {
+            have = tok.next();
+            if ((have == token_hourly) || (have == token_daily) || (have == token_weekly)) {
+                con->trigger = have;
+            }
+            else {
+                tok.token_error("hourly/daily/weekly", have);
+            }
             if (!tsa(tok, token_semi)) return false;
         }
         else {
@@ -416,7 +446,9 @@
 //
 void token_init() {
     token_context    = register_string("context");
+    token_daily      = register_string("daily");
     token_file       = register_string("file");
+    token_hourly     = register_string("hourly");
     token_include    = register_string("include");
     token_lbrace     = register_string("{");
     token_output     = register_string("output");
@@ -425,6 +457,8 @@
     token_rbrace     = register_string("}");
     token_semi       = register_string(";");
     token_tempin     = register_string("tempin");
+    token_trigger    = register_string("trigger");
     token_versions   = register_string("versions");
+    token_weekly     = register_string("weekly");
     token_wflogs     = register_string("wflogs");
 }