]> git.refcnt.org Git - colorize.git/commitdiff
Log debug output to file
authorSteven Schubiger <stsc@refcnt.org>
Tue, 20 Jan 2015 21:20:57 +0000 (22:20 +0100)
committerSteven Schubiger <stsc@refcnt.org>
Tue, 20 Jan 2015 21:20:57 +0000 (22:20 +0100)
Makefile
colorize.c

index 78602380f04b35efea60675976677ab762bcc763..4f7e84365f946281b1b35426a7a7a807d46b33ab 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -16,4 +16,4 @@ check:
                        perl ./test.pl
 
 clean:
-                       rm -f colorize version.h
+                       rm -f colorize debug.txt version.h
index 718b74d6f77b67c8ffaefc7c36307cc21111fbf5..690283a8347b5d95bacf433ee143da6f74080d25 100644 (file)
 
 #define COLOR_SEP_CHAR '/'
 
+#define DEBUG_FILE "debug.txt"
+
 #define VERSION "0.54"
 
 typedef enum { false, true } bool;
@@ -185,6 +187,9 @@ static const struct {
 };
 
 static FILE *stream = NULL;
+#if DEBUG
+static FILE *log = NULL;
+#endif
 
 static unsigned int stacked_vars = 0;
 static void **vars_list = NULL;
@@ -224,6 +229,7 @@ static char *str_concat_wrap (const char *, const char *, const char *, unsigned
 static bool get_bytes_size (unsigned long, struct bytes_size *);
 static char *get_file_type (mode_t);
 static bool has_color_name (const char *, const char *);
+static FILE *open_file (const char *, const char *);
 static void vfprintf_diag (const char *, ...);
 static void vfprintf_fail (const char *, ...);
 static void stack_var (void ***, unsigned int *, unsigned int, void *);
@@ -276,6 +282,10 @@ main (int argc, char **argv)
 
     setvbuf (stdout, NULL, _IOLBF, 0);
 
+#if DEBUG
+    log = open_file (DEBUG_FILE, "w");
+#endif
+
     while ((opt = getopt_long (argc, argv, "hv", long_opts, NULL)) != -1)
       {
         PARSE_OPT:
@@ -451,6 +461,10 @@ cleanup (void)
 
     if (stream && fileno (stream) != STDIN_FILENO)
       fclose (stream);
+#if DEBUG
+    if (log)
+      fclose (log);
+#endif
 
     if (vars_list)
       {
@@ -1011,7 +1025,7 @@ malloc_wrap_debug (size_t size, const char *file, unsigned int line)
     void *p = malloc (size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    vfprintf_diag ("malloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size, file, line);
+    fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
     return p;
 }
 
@@ -1021,7 +1035,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);
-    vfprintf_diag ("calloc'ed %lu bytes [source file %s, line %u]", (unsigned long)(nmemb * size), file, line);
+    fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
     return p;
 }
 
@@ -1031,7 +1045,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);
-    vfprintf_diag ("realloc'ed %lu bytes [source file %s, line %u]", (unsigned long)size, file, line);
+    fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
     return p;
 }
 #endif /* !DEBUG */
@@ -1130,6 +1144,19 @@ has_color_name (const char *str, const char *name)
     return true;
 }
 
+static FILE *
+open_file (const char *file, const char *mode)
+{
+    FILE *stream;
+
+    errno = 0;
+    stream = fopen (file, mode);
+    if (!stream)
+      vfprintf_fail (formats[FMT_FILE], file, strerror (errno));
+
+    return stream;
+}
+
 #define DO_VFPRINTF(fmt)                    \
     va_list ap;                             \
     fprintf (stderr, "%s: ", program_name); \