-
+ A4CA272E00EBA069786CB1EDFB3F7054C5B31630A22AE66C012878D4BCEE96B11298141730589F0A0DDB04EEFF1C2F4EEF5A25D88447226F32EAA5346595EA96
mpi/logger.c
(0 . 0)(1 . 262)
5110 /* logger.c - log functions
5111 * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
5112 *
5113 * This file is part of GnuPG.
5114 *
5115 * GnuPG is free software; you can redistribute it and/or modify
5116 * it under the terms of the GNU General Public License as published by
5117 * the Free Software Foundation; either version 3 of the License, or
5118 * (at your option) any later version.
5119 *
5120 * GnuPG is distributed in the hope that it will be useful,
5121 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5122 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5123 * GNU General Public License for more details.
5124 *
5125 * You should have received a copy of the GNU General Public License
5126 * along with this program; if not, see <http://www.gnu.org/licenses/>.
5127 */
5128
5129 #include <config.h>
5130 #include <stdio.h>
5131 #include <stdlib.h>
5132 #include <stdarg.h>
5133 #include <string.h>
5134 #include <errno.h>
5135
5136 #include "util.h"
5137
5138 static char pidstring[15];
5139 static char *pgm_name;
5140 static int errorcount;
5141 static int strict;
5142 static FILE *logfp;
5143
5144 /****************
5145 * Set the logfile to use (not yet implemneted) or, if logfile is NULL,
5146 * the Fd where logoutputs should go.
5147 */
5148 void
5149 log_set_logfile( const char *name, int fd )
5150 {
5151 if( name )
5152 BUG();
5153
5154 if( logfp && logfp != stderr && logfp != stdout )
5155 fclose( logfp );
5156 if( fd == 1 )
5157 logfp = stdout;
5158 else if( fd == 2 )
5159 logfp = stderr;
5160 else
5161 logfp = fdopen( fd, "a" );
5162 if( !logfp ) {
5163 logfp = stderr;
5164 log_fatal("can't open fd %d for logging: %s\n", fd, strerror(errno));
5165 }
5166 }
5167
5168 FILE *
5169 log_stream()
5170 {
5171 if( !logfp )
5172 logfp = stderr;
5173 return logfp;
5174 }
5175
5176
5177 void
5178 log_set_name( const char *name )
5179 {
5180 xfree(pgm_name);
5181 if( name )
5182 pgm_name = xstrdup(name);
5183 else
5184 pgm_name = NULL;
5185 }
5186
5187 const char *
5188 log_get_name(void)
5189 {
5190 return pgm_name? pgm_name : "";
5191 }
5192
5193
5194 void
5195 log_set_pid( int pid )
5196 {
5197 if( pid )
5198 sprintf(pidstring,"[%u]", (unsigned)pid );
5199 else
5200 *pidstring = 0;
5201 }
5202
5203 int
5204 log_get_errorcount( int clear)
5205 {
5206 int n = errorcount;
5207 if( clear )
5208 errorcount = 0;
5209 return n;
5210 }
5211
5212 void
5213 log_inc_errorcount()
5214 {
5215 errorcount++;
5216 }
5217
5218 int
5219 log_set_strict(int val)
5220 {
5221 int old=strict;
5222 strict=val;
5223 return old;
5224 }
5225
5226 void
5227 g10_log_print_prefix(const char *text)
5228 {
5229 if( !logfp )
5230 logfp = stderr;
5231 if( pgm_name )
5232 fprintf(logfp, "%s%s: %s", pgm_name, pidstring, text );
5233 else
5234 fprintf(logfp, "?%s: %s", pidstring, text );
5235 #ifdef __riscos__
5236 fflush( logfp );
5237 #endif /* __riscos__ */
5238 }
5239
5240
5241 void
5242 g10_log_info( const char *fmt, ... )
5243 {
5244 va_list arg_ptr ;
5245
5246 g10_log_print_prefix("");
5247 va_start( arg_ptr, fmt ) ;
5248 vfprintf(logfp,fmt,arg_ptr) ;
5249 va_end(arg_ptr);
5250 #ifdef __riscos__
5251 fflush( logfp );
5252 #endif /* __riscos__ */
5253 }
5254
5255
5256 void
5257 g10_log_warning( const char *fmt, ... )
5258 {
5259 va_list arg_ptr ;
5260
5261 if(strict)
5262 {
5263 errorcount++;
5264 g10_log_print_prefix(_("ERROR: "));
5265 }
5266 else
5267 g10_log_print_prefix(_("WARNING: "));
5268
5269 va_start( arg_ptr, fmt ) ;
5270 vfprintf(logfp,fmt,arg_ptr) ;
5271 va_end(arg_ptr);
5272 #ifdef __riscos__
5273 fflush( logfp );
5274 #endif /* __riscos__ */
5275 }
5276
5277
5278 void
5279 g10_log_error( const char *fmt, ... )
5280 {
5281 va_list arg_ptr ;
5282
5283 g10_log_print_prefix("");
5284 va_start( arg_ptr, fmt ) ;
5285 vfprintf(logfp,fmt,arg_ptr) ;
5286 va_end(arg_ptr);
5287 errorcount++;
5288 #ifdef __riscos__
5289 fflush( logfp );
5290 #endif /* __riscos__ */
5291 }
5292
5293
5294 void
5295 g10_log_fatal( const char *fmt, ... )
5296 {
5297 va_list arg_ptr ;
5298
5299 g10_log_print_prefix("fatal: ");
5300 va_start( arg_ptr, fmt ) ;
5301 vfprintf(logfp,fmt,arg_ptr) ;
5302 va_end(arg_ptr);
5303 secmem_dump_stats();
5304 #ifdef __riscos__
5305 fflush( logfp );
5306 #endif /* __riscos__ */
5307 exit(2);
5308 }
5309
5310 void
5311 g10_log_bug( const char *fmt, ... )
5312 {
5313 va_list arg_ptr ;
5314
5315 putc('\n', stderr );
5316 g10_log_print_prefix("Ohhhh jeeee: ");
5317 va_start( arg_ptr, fmt ) ;
5318 vfprintf(stderr,fmt,arg_ptr) ;
5319 va_end(arg_ptr);
5320 fflush(stderr);
5321 secmem_dump_stats();
5322 abort();
5323 }
5324
5325 #if defined (__riscos__) \
5326 || ( __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ))
5327 void
5328 g10_log_bug0( const char *file, int line, const char *func )
5329 {
5330 log_bug(_("... this is a bug (%s:%d:%s)\n"), file, line, func );
5331 }
5332 #else
5333 void
5334 g10_log_bug0( const char *file, int line )
5335 {
5336 log_bug(_("you found a bug ... (%s:%d)\n"), file, line);
5337 }
5338 #endif
5339
5340 void
5341 g10_log_debug( const char *fmt, ... )
5342 {
5343 va_list arg_ptr ;
5344
5345 g10_log_print_prefix("DBG: ");
5346 va_start( arg_ptr, fmt ) ;
5347 vfprintf(logfp,fmt,arg_ptr) ;
5348 va_end(arg_ptr);
5349 #ifdef __riscos__
5350 fflush( logfp );
5351 #endif /* __riscos__ */
5352 }
5353
5354
5355
5356 void
5357 g10_log_hexdump( const char *text, const char *buf, size_t len )
5358 {
5359 int i;
5360
5361 g10_log_print_prefix(text);
5362 for(i=0; i < len; i++ )
5363 fprintf(logfp, " %02X", ((const byte*)buf)[i] );
5364 fputc('\n', logfp);
5365 #ifdef __riscos__
5366 fflush( logfp );
5367 #endif /* __riscos__ */
5368 }
5369
5370
5371