aboutsummaryrefslogtreecommitdiffstats
path: root/util/logger.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--util/logger.c72
1 files changed, 56 insertions, 16 deletions
diff --git a/util/logger.c b/util/logger.c
index 6536802ee..2573f5b63 100644
--- a/util/logger.c
+++ b/util/logger.c
@@ -22,6 +22,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
#include "util.h"
#include "i18n.h"
@@ -29,6 +31,40 @@
static char pidstring[15];
static char *pgm_name;
static int errorcount;
+static FILE *logfp;
+
+/****************
+ * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
+ * the Fd where logoutputs should go.
+ */
+void
+log_set_logfile( const char *name, int fd )
+{
+ if( name )
+ BUG();
+
+ if( logfp && logfp != stderr && logfp != stdout )
+ fclose( logfp );
+ if( fd == 1 )
+ logfp = stdout;
+ else if( fd == 2 )
+ logfp = stderr;
+ else
+ logfp = fdopen( fd, "a" );
+ if( !logfp ) {
+ logfp = stderr;
+ log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
+ }
+}
+
+FILE *
+log_stream()
+{
+ if( !logfp )
+ logfp = stderr;
+ return logfp;
+}
+
void
log_set_name( const char *name )
@@ -69,19 +105,23 @@ log_get_errorcount( int clear)
static void
print_prefix(const char *text)
{
+ if( !logfp )
+ logfp = stderr;
if( pgm_name )
- fprintf(stderr, "%s%s: %s", pgm_name, pidstring, text );
+ fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
else
- fprintf(stderr, "?%s: %s", pidstring, text );
+ fprintf(logfp, "?%s: %s", pidstring, text );
}
static void
print_prefix_f(const char *text, const char *fname)
{
+ if( !logfp )
+ logfp = stderr;
if( pgm_name )
- fprintf(stderr, "%s%s:%s: %s", pgm_name, pidstring, fname, text );
+ fprintf(logfp, "%s%s:%s: %s", pgm_name, pidstring, fname, text );
else
- fprintf(stderr, "?%s:%s: %s", pidstring, fname, text );
+ fprintf(logfp, "?%s:%s: %s", pidstring, fname, text );
}
void
@@ -91,7 +131,7 @@ g10_log_info( const char *fmt, ... )
print_prefix("");
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
}
@@ -102,7 +142,7 @@ g10_log_info_f( const char *fname, const char *fmt, ... )
print_prefix_f("", fname);
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
}
@@ -113,7 +153,7 @@ g10_log_error( const char *fmt, ... )
print_prefix("");
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
errorcount++;
}
@@ -125,7 +165,7 @@ g10_log_error_f( const char *fname, const char *fmt, ... )
print_prefix_f("", fname);
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
errorcount++;
}
@@ -137,7 +177,7 @@ g10_log_fatal( const char *fmt, ... )
print_prefix("fatal: ");
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
secmem_dump_stats();
exit(2);
@@ -150,7 +190,7 @@ g10_log_fatal_f( const char *fname, const char *fmt, ... )
print_prefix_f("fatal: ", fname);
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
secmem_dump_stats();
exit(2);
@@ -192,7 +232,7 @@ g10_log_debug( const char *fmt, ... )
print_prefix("DBG: ");
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
}
@@ -203,7 +243,7 @@ g10_log_debug_f( const char *fname, const char *fmt, ... )
print_prefix_f("DBG: ", fname);
va_start( arg_ptr, fmt ) ;
- vfprintf(stderr,fmt,arg_ptr) ;
+ vfprintf(logfp,fmt,arg_ptr) ;
va_end(arg_ptr);
}
@@ -216,8 +256,8 @@ g10_log_hexdump( const char *text, const char *buf, size_t len )
print_prefix(text);
for(i=0; i < len; i++ )
- fprintf(stderr, " %02X", ((const byte*)buf)[i] );
- fputc('\n', stderr);
+ fprintf(logfp, " %02X", ((const byte*)buf)[i] );
+ fputc('\n', logfp);
}
@@ -225,7 +265,7 @@ void
g10_log_mpidump( const char *text, MPI a )
{
print_prefix(text);
- mpi_print(stderr, a, 1 );
- fputc('\n', stderr);
+ mpi_print(logfp, a, 1 );
+ fputc('\n', logfp);
}