[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Implement several 008pre1 items: needs more testing



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv7021/src/common

Modified Files:
	log.c 
Log Message:
Implement several 008pre1 items: needs more testing

Index: log.c
===================================================================
RCS file: /home/or/cvsroot/src/common/log.c,v
retrieving revision 1.44
retrieving revision 1.45
diff -u -d -r1.44 -r1.45
--- log.c	2 Jun 2004 19:18:37 -0000	1.44
+++ log.c	21 Jun 2004 04:37:26 -0000	1.45
@@ -48,20 +48,12 @@
 /** Linked list of logfile_t. */
 static logfile_t *logfiles = NULL;
 
-/** Helper: Format a log message into a fixed-sized buffer. (This is
- * factored out of <b>logv</b> so that we never format a message more
- * than once.)
- */
-static INLINE void format_msg(char *buf, size_t buf_len,
-                              int severity, const char *funcname,
-                              const char *format, va_list ap)
+static INLINE size_t _log_prefix(char *buf, size_t buf_len, int severity)
 {
   time_t t;
   struct timeval now;
   size_t n;
 
-  buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
-
   tor_gettimeofday(&now);
   t = (time_t)now.tv_sec;
 
@@ -73,6 +65,45 @@
     n = buf_len-1; /* the *nprintf funcs return how many bytes they
                     * _would_ print, if the output is truncated.
                     * Subtract one because the count doesn't include the \0 */
+  return n;
+}
+
+/** If lf refers to an actual file that we have just opened, and the file
+ * contains no data, log an "opening new logfile" message at the top. **/
+static void log_tor_version(logfile_t *lf)
+{
+  char buf[256];
+  size_t n;
+
+  if (!lf->needs_close)
+    /* If it doesn't get closed, it isn't really a file. */
+    return;
+  if (lf->is_temporary)
+    /* If it's temporary, it isn't really a file. */
+    return;
+  if (ftell(lf->file) != 0)
+    /* We aren't at the start of the file; no need to log. */
+    return;
+  n = _log_prefix(buf, 250, LOG_NOTICE);
+  n += snprintf(buf+n, 250-n, "Tor %s creating new log file\n", VERSION);
+  if (n>250)
+    n = 250;
+  buf[n+1]='\0';
+  fputs(buf, lf->file);
+}
+
+/** Helper: Format a log message into a fixed-sized buffer. (This is
+ * factored out of <b>logv</b> so that we never format a message more
+ * than once.)
+ */
+static INLINE void format_msg(char *buf, size_t buf_len,
+                              int severity, const char *funcname,
+                              const char *format, va_list ap)
+{
+  size_t n;
+  buf_len -= 2; /* subtract 2 characters so we have room for \n\0 */
+
+  n = _log_prefix(buf, buf_len, severity);
 
   if (funcname) {
     n += snprintf(buf+n, buf_len-n, "%s(): ", funcname);
@@ -159,6 +190,7 @@
     if (lf->needs_close) {
       fclose(lf->file);
       lf->file = fopen(lf->filename, "a");
+      log_tor_version(lf);
     }
   }
 }
@@ -224,6 +256,7 @@
   if (!f) return -1;
   add_stream_log(loglevelMin, loglevelMax, filename, f);
   logfiles->needs_close = 1;
+  log_tor_version(logfiles);
   return 0;
 }