]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Let log_new_file() and log_new_fd() accept flags.
authorMartin Mares <mj@ucw.cz>
Sat, 21 Feb 2009 21:05:29 +0000 (22:05 +0100)
committerMartin Mares <mj@ucw.cz>
Sat, 21 Feb 2009 21:05:29 +0000 (22:05 +0100)
This way, we can request a stream cloned to stderr (originally, this was
available only via log_file()) or a FD-based stream which closes the FD
upon closing.

ucw/doc/log.txt
ucw/log-conf.c
ucw/log-file.c
ucw/log.h

index 4eb15769e5a3a9f07dcb70c207b9fade63c02cd5..28265f7290af6c4795cb5d85e8736f75df062435 100644 (file)
@@ -82,7 +82,7 @@ Example
        int main(int argc, char **argv)
        {
          log_init(argv[0]);
-         struct log_stream *ls = log_new_file("/var/log/utterances");
+         struct log_stream *ls = log_new_file("/var/log/utterances", 0);
          msg(L_INFO | ls->regnum, "Aye captain, we have a log file");
          msg(L_INFO, "Alas, stderr still works");
          return 0;
index bda938fe93324ff5f40d3f3dacd0146ca48f04f9..8989595d9885fd260e93a8a9dddde4cff85f557f 100644 (file)
@@ -282,7 +282,7 @@ 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, 0);
   else if (c->syslog_facility)
     ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
   else
index 645280819c660c54ae1e0ab4665fba17f9ce89f2..efa0665f99bde467954390a085f267b6687e502c 100644 (file)
@@ -29,12 +29,6 @@ struct file_stream {
   char *orig_name;             // Original name with strftime escapes
 };
 
-enum log_file_flag {
-  FF_FORMAT_NAME = 1,          // Name contains strftime escapes
-  FF_CLOSE_FD = 2,             // Close the fd with the stream
-  FF_FD2_FOLLOWS = 4,          // Maintain stderr as a clone of this stream
-};
-
 #define MAX_EXPAND 64          // Maximum size of expansion of strftime escapes
 
 static int log_switch_nest;
@@ -116,11 +110,12 @@ file_handler(struct log_stream *ls, struct log_msg *m)
 }
 
 struct log_stream *
-log_new_fd(int fd)
+log_new_fd(int fd, uns flags)
 {
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
   fs->fd = fd;
+  fs->flags = flags;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
@@ -129,8 +124,8 @@ log_new_fd(int fd)
   return ls;
 }
 
-static struct log_stream *
-do_log_new_file(const char *path, uns more_flags)
+struct log_stream *
+log_new_file(const char *path, uns flags)
 {
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
@@ -138,7 +133,7 @@ do_log_new_file(const char *path, uns more_flags)
   fs->orig_name = xstrdup(path);
   if (strchr(path, '%'))
     fs->flags = FF_FORMAT_NAME;
-  fs->flags |= FF_CLOSE_FD | more_flags;
+  fs->flags |= FF_CLOSE_FD | flags;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
@@ -150,12 +145,6 @@ do_log_new_file(const char *path, uns more_flags)
   return ls;
 }
 
-struct log_stream *
-log_new_file(const char *path)
-{
-  return do_log_new_file(path, 0);
-}
-
 int
 log_switch(void)
 {
@@ -189,7 +178,7 @@ log_file(const char *name)
   if (!name)
     return;
 
-  struct log_stream *ls = do_log_new_file(name, FF_FD2_FOLLOWS);
+  struct log_stream *ls = log_new_file(name, FF_FD2_FOLLOWS);
   struct log_stream *def = log_stream_by_flags(0);
   log_rm_substream(def, NULL);
   log_add_substream(def, ls);
@@ -202,8 +191,8 @@ int main(int argc, char **argv)
 {
   log_init(argv[0]);
   log_file("/proc/self/fd/1");
-  // struct log_stream *ls = log_new_fd(1);
-  // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S");
+  // struct log_stream *ls = log_new_fd(1, 0);
+  // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S", 0);
   for (int i=1; i<argc; i++)
     msg(L_INFO, argv[i]);
   log_close_all();
index 94dfa9c0fb902c8fb2cfaeabc1acb03bc10c5031..f2d99a0729d19e5ff79fdd06a26946743654d8d8 100644 (file)
--- a/ucw/log.h
+++ b/ucw/log.h
@@ -192,8 +192,14 @@ void log_close_all(void);
  * even in multi-threaded programs.
  ***/
 
-struct log_stream *log_new_file(const char *path);             /** Create a stream bound to a log file. **/
-struct log_stream *log_new_fd(int fd);                         /** Create a stream bound to a file descriptor. **/
+struct log_stream *log_new_file(const char *path, uns flags);  /** Create a stream bound to a log file. See `FF_xxx` for @flags. **/
+struct log_stream *log_new_fd(int fd, uns flags);              /** Create a stream bound to a file descriptor. See `FF_xxx` for @flags. **/
+
+enum log_file_flag {           /** Flags used for file-based logging **/
+  FF_FORMAT_NAME = 1,          // Internal: Name contains strftime escapes
+  FF_CLOSE_FD = 2,             // Close the fd with the stream (use with log_new_fd())
+  FF_FD2_FOLLOWS = 4,          // Maintain stderr as a clone of this stream
+};
 
 /**
  * When a time-based name of the log file changes, the logger switches to a new