📄 log.c
字号:
/*********************************************************************** * PKCS_Log - * * The primitive logging function which logs a message on hLog * ***********************************************************************/BOOL PKCS_Log ( pLogHandle phLog, char *Format, va_list ap ) { char Buffer[2048]; pLoggingFacilityInfo pInfo; if ( Format == NULL ) { return FALSE; } if ( (pInfo = GetLogInfoPtr(*phLog)) == NULL ) { return FALSE; } if ( (pInfo->pid != getpid() ) && (pInfo->UseSyslog) ) { /* Looks like our PID changed since the last call. We have to re-open */ if (! SyslogOpen(pInfo) ) { return FALSE; } } if ( vsprintf(&(Buffer[0]), Format, ap) < 0 ) { /* Error reporting functions should be rather robust, don't you think? */ /* vsprintf reporting an error */ //fprintf(stderr, "PKCS_ErrLog - vsprintf error for format string %s\n", Format); return FALSE; } /* Get rid of trailing newlines. */ while ( strlen(Buffer) && (Buffer[strlen(Buffer)-1] == '\n') ) { Buffer[strlen(Buffer)-1] = '\0'; } // Development work only. No loging to anything other than syslog for // production level code /* 1/17/00 SCM - If we're not a daemon, we need to print something to stderr for warnings and errors regardless of development/production. This is for errors that occur during startup. I'll agree that we don't need to write to a log file in production mode, however. */ /* Production mode: Write to stderr if we're not a daemon, and the priority of the message is at least LOG_WARNING Development mode: Write to stderr if we're not a daemon */ if ( ! IsDaemon() ) { BOOL WriteNow; #ifdef DEV WriteNow = TRUE; #else WriteNow = (pInfo->LogLevel <= LOG_WARNING); #endif /* DEV */ if ( WriteNow ) { fprintf(stderr, "%s[%d.%d]: %s\n", pInfo->Descrip, getpid(), pthread_self(), Buffer); } } /* Don't log to a separate log file in production mode */ #ifdef DEV if ( pInfo->Filename != NULL ) { FILE *fd; if ( (fd = fopen ( pInfo->Filename, "a+" ) ) == NULL ) { fprintf(stderr, "PKCS_Log: fopen failed for %s\n", pInfo->Filename); } else { char buf[32]; /* Specs say 26-character array */ GetCurrentTimeString( &(buf[0]) ); /* Date/Time stamp, descrip, Error message */ fprintf ( fd, "%s %s[%d.%d]: ", buf, pInfo->Descrip, getpid(), pthread_self() ); fprintf ( fd, "%s\n", Buffer); fflush ( fd ); fclose ( fd ); } } /* end if pInfo->Filename */ #endif /* DEV */ /* Always log to syslog, if we're using it */ if ( pInfo->UseSyslog ) {#if defined(AIX) syslog_r(pInfo->LogLevel, &(pInfo->LogData), Buffer);#else syslog(pInfo->LogLevel, Buffer);#endif } return TRUE;}/**************************************************************************** * * Would like to have a generic function to which I pass the hLog where I'd * like to do the logging and have a #defined macro which passes it along... * * But the preprocessor and variable # args don't work & play well together * ****************************************************************************//***************************************** * DbgLog - * * Log messages using the debug facility *****************************************/void DbgLog ( u_int32 DebugLevel, char *Format, ... ) { va_list ap; if ( DebugLevel > SysDebugLevel ) { return; } if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogDebug, Format, ap); va_end ( ap ) ; return;}/***************************************** * ErrLog - * * Log Messges using the error facility *****************************************/void ErrLog ( char *Format, ... ) { va_list ap; if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogErr, Format, ap); va_end ( ap ) ; return; }/***************************************** * LogLog - * * Log messages using the log facility *****************************************/void LogLog ( char *Format, ... ) { va_list ap; if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogLog, Format, ap); va_end ( ap ) ; return; }/***************************************** * WarnLog - * * Log messages using the warning facility *****************************************/void WarnLog ( char *Format, ... ) { va_list ap; if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogWarn, Format, ap); va_end ( ap ) ; return; }/***************************************** * TraceLog - * * Log messages using the trace facility *****************************************/void TraceLog ( char *Format, ... ) { va_list ap; if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogTrace, Format, ap); va_end ( ap ) ; return; }/***************************************** * InfoLog - * * Log messages using the info facility *****************************************/void InfoLog ( char *Format, ... ) { va_list ap; if ( ! LoggingInitialized ) { InitLogging(); } va_start( ap, Format ); PKCS_Log( &hLogInfo, Format, ap); va_end ( ap ) ; return; }/*********************************************************************** * InitLogging - * * Sets up the various logging facilities. Must be called before * any of the logging functions can be used. ***********************************************************************/static BOOL InitLogging ( void ) { int i; char *s = ProgramName; /* if ProgramName is NULL, we'll just print the level... */ if ( ProgramName == NULL ) { s = ""; } /* Set up logging for all the facilities in SystemLogFacilities[] */ for ( i = 0; i < ( sizeof(SystemLogFacilities) / (sizeof(SystemLogFacilities[0])) ); i++ ) { if (! NewLoggingFacility(s, &(SystemLogFacilities[i]) ) ) {#ifdef DEV fprintf(stderr, "InitLogging: NewLoggingFacility failed: %s\n", s);#endif return FALSE; } } /* end for i */ atexit(CloseAllLoggingFacilities); LoggingInitialized = TRUE; return TRUE;}/************************************************************* * SetDebugLevel - * * * Sets the level at which debug messages get logged to Val. * Returns the old value *************************************************************/u_int32 SetDebugLevel ( u_int32 Val ) { u_int32 OldVal = SysDebugLevel; SysDebugLevel = Val; return OldVal;}/************************************************************* * GetDebugLevel * * Returns the level at which the program will log debug messages * *************************************************************/u_int32 GetDebugLevel ( void ) { return SysDebugLevel;}#pragma info(none)#if 0int main ( int argc, char *argv[], char *envp[] ) { ErrLog("This is an error test, attempt 1"); DbgLog(DEBUG_LEVEL0, "This is a DEBUG test level 0, attempt 1"); DbgLog(DEBUG_LEVEL1, "This is a DEBUG test level 1, attempt 1"); SetDebugLevel(DEBUG_NONE); DbgLog(DEBUG_LEVEL1, "This is a DEBUG test level 1, attempt 2"); DbgLog(DEBUG_LEVEL0, "This is a DEBUG test level 0, attempt 2"); ErrLog("This is an error test, attempt 2"); return 0;}#endif /* 0 */#pragma info(restore)static BOOL SyslogOpen ( pLoggingFacilityInfo pInfo ) { extern BOOL Daemon; ASSERT(pInfo != NULL); if ( !( pInfo->UseSyslog ) ) { /* it's not really an error to call SyslogOpen for a facility that doesn't use it */ return TRUE; } if ( pInfo->pid != 0 ) { /* We've been initialized before, so close the previous instance */#if defined(AIX) closelog_r( &(pInfo->LogData) );#else closelog();#endif }#if defined(AIX) if ( openlog_r ( pInfo->Descrip, pInfo->LogOption, (Daemon ? LOG_DAEMON : LOG_USER), &(pInfo->LogData) ) != 0) { #ifdef DEV fprintf(stderr, "SyslogOpen: openlog_r failed\n");#endif return FALSE; }#endif /* Default to log all messages. This can be changed by a subsequent call to SetLogPriorityMask */#if defined(AIX) setlogmask_r( LOG_UPTO(LOG_DEBUG), &(pInfo->LogData) );#else setlogmask( LOG_UPTO(LOG_DEBUG));#endif /* Mark this as having been set by this process */ pInfo->pid = getpid(); return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -