]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
Print timestamp at top of debug output
[colorize.git] / colorize.c
index aad5e6247d81273f78672d3e74625b6c5b07d2de..59cab41c7ae8ae911d337431b9db08470ebbf1cc 100644 (file)
@@ -253,6 +253,7 @@ static char *exclude;
 
 static const char *program_name;
 
+static void print_tstamp (FILE *);
 static void process_opts (int, char **);
 static void process_opt_attr (const char *);
 static void write_attr (const struct attr *, unsigned int *);
@@ -327,6 +328,7 @@ main (int argc, char **argv)
 
 #if DEBUG
     log = open_file (DEBUG_FILE, "w");
+    print_tstamp (log);
 #endif
 
     attr[0] = '\0';
@@ -370,6 +372,31 @@ main (int argc, char **argv)
     exit (EXIT_SUCCESS);
 }
 
+static void
+print_tstamp (FILE *log)
+{
+    time_t t;
+    struct tm *tm;
+    char str[128];
+    size_t written;
+
+    t = time (NULL);
+    tm = localtime (&t);
+    if (tm == NULL)
+      {
+        perror ("localtime");
+        exit (EXIT_FAILURE);
+      }
+    written = strftime (str, sizeof (str), "%Y-%m-%d %H:%M:%S %Z", tm);
+    if (written == 0)
+      vfprintf_fail (formats[FMT_GENERIC], "strftime: 0 returned");
+
+    fprintf (log, "%s\n", str);
+    while (written--)
+      fprintf (log, "=");
+    fprintf (log, "\n");
+}
+
 #define PRINT_HELP_EXIT() \
     print_help ();        \
     exit (EXIT_SUCCESS);
@@ -1352,13 +1379,14 @@ realloc_wrap (void *ptr, size_t size)
     return p;
 }
 #else
+static const char *const format_debug = "%s: %10s %7lu bytes [source file %s, line %5u]\n";
 static void *
 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
 {
     void *p = malloc (size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+    fprintf (log, format_debug, program_name, "malloc'ed", (unsigned long)size, file, line);
     return p;
 }
 
@@ -1368,7 +1396,7 @@ calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int lin
     void *p = calloc (nmemb, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
+    fprintf (log, format_debug, program_name, "calloc'ed", (unsigned long)(nmemb * size), file, line);
     return p;
 }
 
@@ -1378,7 +1406,7 @@ realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
     void *p = realloc (ptr, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+    fprintf (log, format_debug, program_name, "realloc'ed", (unsigned long)size, file, line);
     return p;
 }
 #endif /* !DEBUG */