📄 log.c
字号:
static pLoggingFacilityInfo GetLogInfoPtr ( LogHandle hLog );static BOOL GetFreeLogInfo ( pLogHandle Dest );static void CloseAllLoggingFacilities ( void );static BOOL SetLogPriorityMask ( LogHandle hLog, u_int32 Priority );static BOOL SyslogOpen ( pLoggingFacilityInfo pInfo );/************************************************************* * GetCurrentTimeString - * * Writes the current date & time into *Buffer * *************************************************************/BOOL GetCurrentTimeString ( char *Buffer ) { /* Note: The specs for ctime_r and asctime_r say that Buffer needs to be 26 characters long. Not sure if that includes a triling NULL - SCM */ time_t t; struct tm tm; ASSERT(Buffer != NULL); time(&t); localtime_r(&t, &tm); asctime_r( &tm , &(Buffer[0]) ); /* asctime_r puts a \n at the end, so we'll remove that */ Buffer[strlen(Buffer)-1] = '\0'; return TRUE;}/*********************************************************************** * InitDataStructs - * * Called durining initalization to set up the LogInfo array * ***********************************************************************/static int InitDataStructs ( void ) { int i;#if defined(AIX) #pragma info(noini) struct syslog_data TempData = SYSLOG_DATA_INIT; #pragma info(restore)#endif for ( i = 0; i < (sizeof(LogInfo) / sizeof(LogInfo[0])); i++ ) { LogInfo[i].Initialized = FALSE; LogInfo[i].Descrip[0] = '\0'; LogInfo[i].LogOption = DefaultLogOption;#if defined(AIX) memcpy ( (void *) &(LogInfo[i].LogData), (void *) &TempData, sizeof(TempData) );#endif } Initialized = TRUE; return TRUE;}/********************************************************************** * GetFreeLogInfo - * * Return the handle for the next available Log Facility structure * * After calling this function, the facility will be marked as in use * ***********************************************************************/static BOOL GetFreeLogInfo ( pLogHandle Dest ) { u_int32 i; if ( ! Initialized ) { InitDataStructs(); } for ( i = 0; i < ( sizeof(LogInfo) / sizeof(LogInfo[0]) ); i++ ) { if ( LogInfo[i].Initialized == FALSE ) { /* Set this here so that we don't return the same identifier twice in the case where GetFreeLogInfo() is called twice in a row */ LogInfo[i].Initialized = TRUE; *Dest = i; return TRUE; } }#ifdef DEV fprintf(stderr, "No available thread logging structs.\n");#endif return FALSE;}/********************************************************************** * GetLogInfoPtr - * * Given a handle, return a pointer to the appropriate LoggingFacilityInfo structure * ***********************************************************************/static pLoggingFacilityInfo GetLogInfoPtr ( LogHandle hLog ) { if ( hLog >= (sizeof(LogInfo) / sizeof(LogInfo[0]) ) ) {#ifdef DEV fprintf(stderr, "Illegal LogHandle value: %#X\n", hLog);#endif return NULL; } if ( LogInfo[hLog].Initialized != TRUE ) {#ifdef DEV fprintf(stderr, "GetLogInfoPtr() called for a non-initialized handle\n");#endif return NULL; } return &(LogInfo[hLog]);}/*********************************************************************** * NewLoggingFacility - * * Given an ID ( char string which will appear in the messages ), * open a logging facility and return a handle to it in * pLoggingStuff->phLog * ***********************************************************************/BOOL NewLoggingFacility ( char *ID, pLoggingFacility pStuff ) { int LogFacility; pLoggingFacilityInfo pInfo = NULL; LogHandle hLog; pLogHandle Result; extern BOOL Daemon; /* We use Daemon here instead of IsDaemon because these facilities might get set up before we've actually daemonized */ if ( Daemon ) { LogFacility = LOG_DAEMON; } else { LogFacility = LOG_USER; } /* See if there's room in the array. This'd be nice if it were dynamically allocated */ if ( ! GetFreeLogInfo(&hLog) ) { return FALSE; } /* Get a pointer to the syslog_data structure */ if ( (pInfo = GetLogInfoPtr(hLog)) == NULL ) { return FALSE; } Result = pStuff->phLog; /* Set this before the filename is checked because we may want to use the descrip and/or filename in the logs */ pInfo->UseSyslog = pStuff->UseSyslog; pInfo->LogOption = DefaultLogOption; pInfo->pid = 0; pInfo->LogLevel = pStuff->LogLevel; sprintf( pInfo->Descrip, "%s %s", pStuff->Label, ID ); /* ensure that the last character is a NULL */ pInfo->Descrip[sizeof(pInfo->Descrip)-1] = '\0'; /* Some sanity checking on filename... */ if ( (pStuff->Filename != NULL) && (strlen(pStuff->Filename) > 0) ) { FILE *fd; #if TRUNCATE_LOGS_ON_START /* * Truncating files on the start will present problems if the user creates * their own logging facilities after the program's been running for a while * But the non-syslog logging is intended for debug purposes only, anyway. * */ char FileMode[] = "w"; #else char FileMode[] = "a"; #endif /* TRUNCATE_LOGS_ON_START */ if ( ( fd = fopen((pStuff->Filename), FileMode ) ) == NULL ) {#ifdef DEV fprintf(stderr, "%s could not be opened\n", pStuff->Filename);#endif pInfo->Filename = NULL; } else { /* Tag the file */ char buf[100]; GetCurrentTimeString( &(buf[0]) );#ifdef DEV #if TRUNCATE_LOGS_ON_START /* buf contains the date stamp */ fprintf(fd, "********* %s %s truncated *********\n", buf, pStuff->Filename); #else fprintf(fd, "********* %s \"%s\" logging to %s *********\n", buf, pInfo->Descrip, pStuff->Filename); #endif /* TRUNCATE_LOGS_ON_START */#endif fflush(fd); fclose(fd); pInfo->Filename = pStuff->Filename; } } else { pInfo->Filename = NULL; } if ( pInfo->UseSyslog ) { /* open the logging facility */ if (! SyslogOpen( pInfo ) ) { return FALSE; } } /* Redundant; Initialized is set to 1 in GetFreeLogInfo */ pInfo->Initialized = TRUE; *Result = hLog; return TRUE;}/*********************************************************************** * CloseLoggingFacility - * * Closes the logging facility whose handle is hLog. * Sets up the data structure for reuse later if desired * ***********************************************************************/BOOL CloseLoggingFacility ( LogHandle hLog ) { pLoggingFacilityInfo pInfo = NULL;#if defined(AIX) #pragma info(noini) struct syslog_data TempData = SYSLOG_DATA_INIT; #pragma info(restore)#endif if ( (pInfo = GetLogInfoPtr(hLog)) == NULL ) { return FALSE; } pInfo->Descrip[0] = '\0'; pInfo->LogOption = 0; pInfo->Filename = NULL; pInfo->pid = 0; if ( pInfo->UseSyslog ) {#if defined(AIX) closelog_r( &(pInfo->LogData) );#else closelog( );#endif }#if defined(AIX) memcpy ( &(pInfo->LogData), &TempData, sizeof(TempData) );#endif pInfo->Initialized = FALSE; return TRUE;}/***************************************** * CloseAllLoggingFacilities - * * Closes down all the logging stuff we've set up *****************************************/static void CloseAllLoggingFacilities ( void ) { u_int32 i = 0; for ( i = 0; i < (sizeof(LogInfo) / sizeof(LogInfo[0])); i++ ) { /* Makes assumption that these handles all are sequential. Bad Style */ if ( LogInfo[i].Initialized ) { CloseLoggingFacility(i); } } return;}/*********************************************************************** * SetLogPriorityMask - * * Sets the log priority mask to exactly what is passed in as a parameter * This means that you should pass in LOG_UPTO(LOG_DEBUG) if that's what you mean * Passing in LOG_DEBUG will ONLY print debug messages * ***********************************************************************/static BOOL SetLogPriorityMask ( LogHandle hLog, u_int32 Priority ) { u_int32 OldMaskPriority; pLoggingFacilityInfo pInfo = NULL; /* Get a pointer to the syslog_data structure */ if ( (pInfo = GetLogInfoPtr(hLog)) == NULL ) { return FALSE; } /* Make sure that they called NewLoggingFacility() for this handle */ if ( pInfo->Initialized != TRUE ) { return FALSE; } if ( ! pInfo->UseSyslog ) {#ifdef DEV fprintf(stderr, "SetLogPriorityMask: Tried to set the mask on a log handle that isn't using syslog\n");#endif return FALSE; } else {#if defined(AIX) setlogmask_r( Priority, &(pInfo->LogData) );#else setlogmask( Priority );#endif } return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -