]> git.imager.perl.org - imager.git/blobdiff - log.c
convert t/t55trans.t to Test::More
[imager.git] / log.c
diff --git a/log.c b/log.c
index a571fda2fe70c0558432c49bbc52d23841408839..051977078800592be9123879f51ea88f76a668c1 100644 (file)
--- a/log.c
+++ b/log.c
@@ -1,4 +1,9 @@
+#include "imconfig.h"
 #include "log.h"
+#include <stdlib.h>
+#include <errno.h>
+
+#ifdef IMAGER_LOG
 
 #define DTBUFF 50
 #define DATABUFF DTBUFF+3+10+1+5+1+1
@@ -10,14 +15,13 @@ static char  date_buffer[DTBUFF];
 static char  data_buffer[DATABUFF];
 
 
-#ifdef IMAGER_LOG
-
 /*
  * Logging is active
  */
 
-void
-init_log(const char* name,int level) {
+int
+i_init_log(const char* name,int level) {
+  i_clear_error();
   log_level = level;
   if (level < 0) {
     lg_file = NULL;
@@ -26,17 +30,21 @@ init_log(const char* name,int level) {
       lg_file = stderr;
     } else {
       if (NULL == (lg_file = fopen(name, "w+")) ) { 
-       fprintf(stderr,"Cannot open file '%s'\n",name);
-       exit(2);
+       i_push_errorf(errno, "Cannot open file '%s': (%d)", name, errno);
+       return 0;
       }
     }
   }
-  setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
-  mm_log((0,"Imager - log started (level = %d)\n", level));
+  if (lg_file) {
+    setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
+    mm_log((0,"Imager - log started (level = %d)\n", level));
+  }
+
+  return lg_file != NULL;
 }
 
 void
-m_fatal(int exitcode,const char *fmt, ... ) {
+i_fatal(int exitcode,const char *fmt, ... ) {
   va_list ap;
   time_t timi;
   struct tm *str_tm;
@@ -53,21 +61,18 @@ m_fatal(int exitcode,const char *fmt, ... ) {
   exit(exitcode);
 }
 
-#else
 
 /*
- * Logging is inactive - insert dummy functions
- */
-
-void init_log(const char* name,int onoff) {}
-void m_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
-
+=item i_loog(level, format, ...)
+=category Logging
 
-#endif
+This is an internal function called by the mm_log() macro.
 
+=cut
+*/
 
 void
-m_loog(int level,const char *fmt, ... ) {
+i_loog(int level,const char *fmt, ... ) {
   va_list ap;
   if (level > log_level) return;
   if (lg_file != NULL) {
@@ -80,9 +85,17 @@ m_loog(int level,const char *fmt, ... ) {
   }
 }
 
+/*
+=item i_lhead(file, line)
+=category Logging
+
+This is an internal function called by the mm_log() macro.
+
+=cut
+*/
 
 void
-m_lhead(const char *file, int line) {
+i_lhead(const char *file, int line) {
   time_t timi;
   struct tm *str_tm;
   
@@ -93,3 +106,26 @@ m_lhead(const char *file, int line) {
     sprintf(data_buffer, "[%s] %10s:%-5d ", date_buffer, file, line);
   }
 }
+
+#else
+
+/*
+ * Logging is inactive - insert dummy functions
+ */
+
+int i_init_log(const char* name,int onoff) {
+  i_clear_error();
+  i_push_error(0, "Logging disabled");
+  return 0;
+}
+
+void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }
+
+void
+i_loog(int level,const char *fmt, ... ) {
+}
+
+void
+i_lhead(const char *file, int line) { }
+
+#endif