]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log-conf.c
xtypes: Added FIXME with possible segfault.
[libucw.git] / ucw / log-conf.c
index 4e6c14366bc1b6819b4cc1c8089e03d131e83f72..d809c11d00145f9725cce7eb9222d7120994f86a 100644 (file)
@@ -7,21 +7,26 @@
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/log.h"
-#include "ucw/conf.h"
-#include "ucw/simple-lists.h"
-#include "ucw/tbf.h"
-#include "ucw/threads.h"
-
+#include <ucw/lib.h>
+#include <ucw/log.h>
+#include <ucw/log-internal.h>
+#include <ucw/conf.h>
+#include <ucw/simple-lists.h>
+#include <ucw/tbf.h>
+#include <ucw/threads.h>
+
+#include <stdio.h>
 #include <string.h>
 #include <syslog.h>
 #include <sys/time.h>
 
+/*** Configuration of streams ***/
+
 struct stream_config {
   cnode n;
   char *name;
   char *file_name;
+  int file_desc;
   char *syslog_facility;
   u32 levels;
   clist types;                         // simple_list of names
@@ -31,6 +36,7 @@ struct stream_config {
   int show_types;
   int syslog_pids;
   int errors_fatal;
+  int stderr_follows;
   struct log_stream *ls;
   int mark;                            // Used temporarily in log_config_commit()
 };
@@ -39,7 +45,7 @@ struct limit_config {
   cnode n;
   clist types;                         // simple_list of names
   double rate;
-  uns burst;
+  uint burst;
 };
 
 static char *
@@ -48,6 +54,7 @@ stream_init(void *ptr)
   struct stream_config *c = ptr;
 
   c->levels = ~0U;
+  c->file_desc = -1;
   return NULL;
 }
 
@@ -56,12 +63,17 @@ stream_commit(void *ptr)
 {
   struct stream_config *c = ptr;
 
-  if (c->file_name && c->syslog_facility)
-    return "Both FileName and SyslogFacility selected";
-  if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
-    return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
-  if (c->syslog_facility && c->microseconds)
-    return "Syslog streams do not support microsecond precision";
+  if (c->syslog_facility)
+    {
+      if (!log_syslog_facility_exists(c->syslog_facility))
+       return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
+      if (c->file_name)
+       return "Both FileName and SyslogFacility selected";
+      if (c->microseconds)
+       return "Syslog streams do not support microsecond precision";
+    }
+  if (c->stderr_follows && !c->file_name)
+    return "StdErrFollows requires a file-based stream";
   return NULL;
 }
 
@@ -78,7 +90,7 @@ static struct cf_section limit_config = {
 #define P(x) PTR_TO(struct limit_config, x)
     CF_LIST("Types", P(types), &cf_string_list_config),
     CF_DOUBLE("Rate", P(rate)),
-    CF_UNS("Burst", P(burst)),
+    CF_UINT("Burst", P(burst)),
 #undef P
     CF_END
   }
@@ -92,6 +104,7 @@ static struct cf_section stream_config = {
 #define P(x) PTR_TO(struct stream_config, x)
     CF_STRING("Name", P(name)),
     CF_STRING("FileName", P(file_name)),
+    CF_INT("FileDesc", P(file_desc)),
     CF_STRING("SyslogFacility", P(syslog_facility)),
     CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
     CF_LIST("Types", P(types), &cf_string_list_config),
@@ -101,6 +114,7 @@ static struct cf_section stream_config = {
     CF_INT("ShowTypes", P(show_types)),
     CF_INT("SyslogPID", P(syslog_pids)),
     CF_INT("ErrorsFatal", P(errors_fatal)),
+    CF_INT("StdErrFollows", P(stderr_follows)),
 #undef P
     CF_END
   }
@@ -170,22 +184,15 @@ log_config_init(void)
   cf_declare_section("Logging", &log_config, 0);
 }
 
-char *
-log_check_configured(const char *name)
-{
-  if (stream_find(name))
-    return NULL;
-  else
-    return cf_printf("Log stream `%s' not found", name);
-}
+/*** Type sets ***/
 
-static uns
+static uint
 log_type_mask(clist *l)
 {
   if (clist_empty(l))
     return ~0U;
 
-  uns types = 0;
+  uint types = 0;
   CLIST_FOR_EACH(simp_node *, s, *l)
     if (!strcmp(s->s, "all"))
       return ~0U;
@@ -203,6 +210,8 @@ log_type_mask(clist *l)
   return types;
 }
 
+/*** Generating limiters ***/
+
 /*
  *  When limiting is enabled, we let log_stream->filter point to this function
  *  and log_stream->user_data point to an array of pointers to token bucket
@@ -219,17 +228,36 @@ log_limiter(struct log_stream *ls, struct log_msg *m)
     return 0;
 
   ASSERT(!(m->flags & L_SIGHANDLER));
-  timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
+  if (m->flags & L_LOGGER_ERR)
+    return 0;
 
+  timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
   ucwlib_lock();
   int res = tbf_limit(tbf, now);
   ucwlib_unlock();
-  return !res;
+
+  if (res < 0)
+    {
+      if (res == -1)
+       {
+         struct log_msg mm = *m;
+         mm.flags |= L_LOGGER_ERR;
+         mm.raw_msg = "(maximum logging rate exceeded, some messages will be suppressed)";
+         log_pass_msg(ls, &mm);
+       }
+      return 1;
+    }
+  else
+    return 0;
 }
 
 static void
 log_apply_limits(struct log_stream *ls, struct limit_config *lim)
 {
+  uint mask = log_type_mask(&lim->types);
+  if (!mask)
+    return;
+
   if (!ls->user_data)
     {
       ls->user_data = cf_malloc_zero(LS_NUM_TYPES * sizeof(struct token_bucket_filter *));
@@ -241,12 +269,22 @@ log_apply_limits(struct log_stream *ls, struct limit_config *lim)
   tbf->burst = lim->burst;
   tbf_init(tbf);
 
-  uns mask = log_type_mask(&lim->types);
-  for (uns i=0; i < LS_NUM_TYPES; i++)
+  for (uint i=0; i < LS_NUM_TYPES; i++)
     if (mask & (1 << i))
       limits[i] = tbf;
 }
 
+/*** Generating streams ***/
+
+char *
+log_check_configured(const char *name)
+{
+  if (stream_find(name))
+    return NULL;
+  else
+    return cf_printf("Log stream `%s' not found", name);
+}
+
 static struct log_stream *
 do_new_configured(struct stream_config *c)
 {
@@ -257,7 +295,9 @@ do_new_configured(struct stream_config *c)
     return c->ls;
 
   if (c->file_name)
-    ls = log_new_file(c->file_name);
+    ls = log_new_file(c->file_name, (c->stderr_follows ? FF_FD2_FOLLOWS : 0));
+  else if (c->file_desc >= 0)
+    ls = log_new_fd(c->file_desc, (c->stderr_follows ? FF_FD2_FOLLOWS : 0));
   else if (c->syslog_facility)
     ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
   else
@@ -296,16 +336,13 @@ log_new_configured(const char *name)
 void
 log_configured(const char *name)
 {
-  struct log_stream *ls = log_new_configured(name);
-  struct log_stream *def = log_stream_by_flags(0);
-  log_rm_substream(def, NULL);
-  log_add_substream(def, ls);
-  log_close_stream(ls);
+  log_set_default_stream(log_new_configured(name));
 }
 
 #ifdef TEST
 
-#include "ucw/getopt.h"
+#include <unistd.h>
+#include <ucw/getopt.h>
 
 int main(int argc, char **argv)
 {
@@ -316,7 +353,12 @@ int main(int argc, char **argv)
 
   int type = log_register_type("foo");
   struct log_stream *ls = log_new_configured("combined");
-  msg(L_INFO | ls->regnum | type, "Hello, universe!");
+  for (uint i=0; i<10; i++)
+    {
+      msg(L_INFO | ls->regnum | type, "Hello, universe!");
+      usleep(200000);
+    }
+  fprintf(stderr, "Alas, this was printed to stderr.\n");
 
   log_close_all();
   return 0;