-
+ A4CA272E00EBA069786CB1EDFB3F7054C5B31630A22AE66C012878D4BCEE96B11298141730589F0A0DDB04EEFF1C2F4EEF5A25D88447226F32EAA5346595EA96
mpi/logger.c
(0 . 0)(1 . 262)
5006 /* logger.c - log functions
5007 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5008 *
5009 * This file is part of GnuPG.
5010 *
5011 * GnuPG is free software; you can redistribute it and/or modify
5012 * it under the terms of the GNU General Public License as published by
5013 * the Free Software Foundation; either version 3 of the License, or
5014 * (at your option) any later version.
5015 *
5016 * GnuPG is distributed in the hope that it will be useful,
5017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5019 * GNU General Public License for more details.
5020 *
5021 * You should have received a copy of the GNU General Public License
5022 * along with this program; if not, see <http://www.gnu.org/licenses/>.
5023 */
5024
5025 #include <config.h>
5026 #include <stdio.h>
5027 #include <stdlib.h>
5028 #include <stdarg.h>
5029 #include <string.h>
5030 #include <errno.h>
5031
5032 #include "util.h"
5033
5034 static char pidstring[15];
5035 static char *pgm_name;
5036 static int errorcount;
5037 static int strict;
5038 static FILE *logfp;
5039
5040 /****************
5041 * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
5042 * the Fd where logoutputs should go.
5043 */
5044 void
5045 log_set_logfile( const char *name, int fd )
5046 {
5047 if( name )
5048 BUG();
5049
5050 if( logfp && logfp != stderr && logfp != stdout )
5051 fclose( logfp );
5052 if( fd == 1 )
5053 logfp = stdout;
5054 else if( fd == 2 )
5055 logfp = stderr;
5056 else
5057 logfp = fdopen( fd, "a" );
5058 if( !logfp ) {
5059 logfp = stderr;
5060 log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
5061 }
5062 }
5063
5064 FILE *
5065 log_stream()
5066 {
5067 if( !logfp )
5068 logfp = stderr;
5069 return logfp;
5070 }
5071
5072
5073 void
5074 log_set_name( const char *name )
5075 {
5076 xfree(pgm_name);
5077 if( name )
5078 pgm_name = xstrdup(name);
5079 else
5080 pgm_name = NULL;
5081 }
5082
5083 const char *
5084 log_get_name(void)
5085 {
5086 return pgm_name? pgm_name : "";
5087 }
5088
5089
5090 void
5091 log_set_pid( int pid )
5092 {
5093 if( pid )
5094 sprintf(pidstring,"[%u]", (unsigned)pid );
5095 else
5096 *pidstring = 0;
5097 }
5098
5099 int
5100 log_get_errorcount( int clear)
5101 {
5102 int n = errorcount;
5103 if( clear )
5104 errorcount = 0;
5105 return n;
5106 }
5107
5108 void
5109 log_inc_errorcount()
5110 {
5111 errorcount++;
5112 }
5113
5114 int
5115 log_set_strict(int val)
5116 {
5117 int old=strict;
5118 strict=val;
5119 return old;
5120 }
5121
5122 void
5123 g10_log_print_prefix(const char *text)
5124 {
5125 if( !logfp )
5126 logfp = stderr;
5127 if( pgm_name )
5128 fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
5129 else
5130 fprintf(logfp, "?%s: %s", pidstring, text );
5131 #ifdef __riscos__
5132 fflush( logfp );
5133 #endif /* __riscos__ */
5134 }
5135
5136
5137 void
5138 g10_log_info( const char *fmt, ... )
5139 {
5140 va_list arg_ptr ;
5141
5142 g10_log_print_prefix("");
5143 va_start( arg_ptr, fmt ) ;
5144 vfprintf(logfp,fmt,arg_ptr) ;
5145 va_end(arg_ptr);
5146 #ifdef __riscos__
5147 fflush( logfp );
5148 #endif /* __riscos__ */
5149 }
5150
5151
5152 void
5153 g10_log_warning( const char *fmt, ... )
5154 {
5155 va_list arg_ptr ;
5156
5157 if(strict)
5158 {
5159 errorcount++;
5160 g10_log_print_prefix(_("ERROR: "));
5161 }
5162 else
5163 g10_log_print_prefix(_("WARNING: "));
5164
5165 va_start( arg_ptr, fmt ) ;
5166 vfprintf(logfp,fmt,arg_ptr) ;
5167 va_end(arg_ptr);
5168 #ifdef __riscos__
5169 fflush( logfp );
5170 #endif /* __riscos__ */
5171 }
5172
5173
5174 void
5175 g10_log_error( const char *fmt, ... )
5176 {
5177 va_list arg_ptr ;
5178
5179 g10_log_print_prefix("");
5180 va_start( arg_ptr, fmt ) ;
5181 vfprintf(logfp,fmt,arg_ptr) ;
5182 va_end(arg_ptr);
5183 errorcount++;
5184 #ifdef __riscos__
5185 fflush( logfp );
5186 #endif /* __riscos__ */
5187 }
5188
5189
5190 void
5191 g10_log_fatal( const char *fmt, ... )
5192 {
5193 va_list arg_ptr ;
5194
5195 g10_log_print_prefix("fatal: ");
5196 va_start( arg_ptr, fmt ) ;
5197 vfprintf(logfp,fmt,arg_ptr) ;
5198 va_end(arg_ptr);
5199 secmem_dump_stats();
5200 #ifdef __riscos__
5201 fflush( logfp );
5202 #endif /* __riscos__ */
5203 exit(2);
5204 }
5205
5206 void
5207 g10_log_bug( const char *fmt, ... )
5208 {
5209 va_list arg_ptr ;
5210
5211 putc('\n', stderr );
5212 g10_log_print_prefix("Ohhhh jeeee: ");
5213 va_start( arg_ptr, fmt ) ;
5214 vfprintf(stderr,fmt,arg_ptr) ;
5215 va_end(arg_ptr);
5216 fflush(stderr);
5217 secmem_dump_stats();
5218 abort();
5219 }
5220
5221 #if defined (__riscos__) \
5222 || ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
5223 void
5224 g10_log_bug0( const char *file, int line, const char *func )
5225 {
5226 log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
5227 }
5228 #else
5229 void
5230 g10_log_bug0( const char *file, int line )
5231 {
5232 log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
5233 }
5234 #endif
5235
5236 void
5237 g10_log_debug( const char *fmt, ... )
5238 {
5239 va_list arg_ptr ;
5240
5241 g10_log_print_prefix("DBG: ");
5242 va_start( arg_ptr, fmt ) ;
5243 vfprintf(logfp,fmt,arg_ptr) ;
5244 va_end(arg_ptr);
5245 #ifdef __riscos__
5246 fflush( logfp );
5247 #endif /* __riscos__ */
5248 }
5249
5250
5251
5252 void
5253 g10_log_hexdump( const char *text, const char *buf, size_t len )
5254 {
5255 int i;
5256
5257 g10_log_print_prefix(text);
5258 for(i=0; i < len; i++ )
5259 fprintf(logfp, " %02X", ((const byte*)buf)[i] );
5260 fputc('\n', logfp);
5261 #ifdef __riscos__
5262 fflush( logfp );
5263 #endif /* __riscos__ */
5264 }
5265
5266
5267