/*! * Reopen the log file descriptor. * * Reopen the log file descriptor in order to support rotation * of the log files. Has no effect when logging to the syslog. * * \returns 0 on success */ int ReOpenLog(void) { int flags = O_WRONLY | O_APPEND | O_CREAT; #ifdef HAVE_SYSLOG if (serverLogOpts.dest == logDest_syslog) { return 0; } #endif LOCK_SERVERLOG(); if (ourName == NULL) { UNLOCK_SERVERLOG(); return -1; } if (IsFIFO(ourName)) { flags |= O_NONBLOCK; } if (serverLogFD >= 0) close(serverLogFD); serverLogFD = open(ourName, flags, 0666); if (serverLogFD >= 0) { RedirectStdStreams(ourName); } UNLOCK_SERVERLOG(); return serverLogFD < 0 ? -1 : 0; }
void WriteLogBuffer(char *buf, afs_uint32 len) { LOCK_SERVERLOG(); if (serverLogFD > 0) (void)write(serverLogFD, buf, len); UNLOCK_SERVERLOG(); }
/*! * Write a block of bytes to the log. * * Write a block of bytes directly to the log without formatting * or prepending a timestamp. * * \param[in] buf pointer to bytes to write * \param[in] len number of bytes to write */ void WriteLogBuffer(char *buf, afs_uint32 len) { LOCK_SERVERLOG(); if (serverLogFD >= 0) { if (write(serverLogFD, buf, len) < 0) ; /* don't care */ } UNLOCK_SERVERLOG(); }
/*! * Rotate the log file by renaming then truncating. */ static void RotateLogFile(void) { LOCK_SERVERLOG(); if (ourName != NULL) { if (serverLogFD >= 0) { close(serverLogFD); serverLogFD = -1; } OpenLogFile(ourName); } UNLOCK_SERVERLOG(); }
/*! * Close the server log file. * * \note Must be preceeded by OpenLog(). */ void CloseLog(void) { LOCK_SERVERLOG(); #ifndef AFS_NT40_ENV if (serverLogSyslog) { closelog(); } else #endif if (serverLogFD >= 0) { close(serverLogFD); serverLogFD = -1; } UNLOCK_SERVERLOG(); }
/*! * Write a message to the log. * * \param[in] format printf-style format string * \param[in] args variable list of arguments */ void vFSLog(const char *format, va_list args) { time_t currenttime; char tbuffer[1024]; char *info; size_t len; struct tm tm; int num; currenttime = time(NULL); len = strftime(tbuffer, sizeof(tbuffer), "%a %b %d %H:%M:%S %Y ", localtime_r(¤ttime, &tm)); info = &tbuffer[len]; if (threadIdLogs) { num = (*threadNumProgram) (); if (num > -1) { snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ", num); info += strlen(info); } } vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args); len = strlen(tbuffer); LOCK_SERVERLOG(); #ifdef HAVE_SYSLOG if (serverLogOpts.dest == logDest_syslog) { syslog(LOG_INFO, "%s", info); } else #endif if (serverLogFD >= 0) { if (write(serverLogFD, tbuffer, len) < 0) { /* don't care */ } } UNLOCK_SERVERLOG(); #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV) if (serverLogOpts.dest == logDest_file) { fflush(stdout); fflush(stderr); /* in case they're sharing the same FD */ } #endif } /*vFSLog */
void vFSLog(const char *format, va_list args) { time_t currenttime; char *timeStamp; char tbuffer[1024]; char *info; size_t len; int num; currenttime = time(0); timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer)); timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */ info = &timeStamp[25]; if (mrafsStyleLogs || threadIdLogs) { num = (*threadNumProgram) (); if (num > -1) { (void)afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ", num); info += strlen(info); } } (void)afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer), format, args); len = strlen(tbuffer); LOCK_SERVERLOG(); #ifndef AFS_NT40_ENV if (serverLogSyslog) { syslog(LOG_INFO, "%s", info); } else #endif if (serverLogFD > 0) (void)write(serverLogFD, tbuffer, len); UNLOCK_SERVERLOG(); #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV) if (!serverLogSyslog) { fflush(stdout); fflush(stderr); /* in case they're sharing the same FD */ } #endif } /*vFSLog */
int ReOpenLog(const char *fileName) { int isfifo = 0; #if !defined(AFS_NT40_ENV) struct stat statbuf; #endif if (access(fileName, F_OK) == 0) return 0; /* exists, no need to reopen. */ #if !defined(AFS_NT40_ENV) if (serverLogSyslog) { return 0; } /* Support named pipes as logs by not rotating them */ if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) { isfifo = 1; } #endif LOCK_SERVERLOG(); if (serverLogFD > 0) close(serverLogFD); serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666); if (serverLogFD > 0) { if (freopen(fileName, "a", stdout) == NULL) ; /* don't care */ if (freopen(fileName, "a", stderr) != NULL) { #ifdef HAVE_SETVBUF #ifdef SETVBUF_REVERSED setvbuf(stderr, _IONBF, NULL, 0); #else setvbuf(stderr, NULL, _IONBF, 0); #endif #else setbuf(stderr, NULL); #endif } } UNLOCK_SERVERLOG(); return serverLogFD < 0 ? -1 : 0; }
/*! * Close the server log file. * * \note Must be preceeded by OpenLog(). */ void CloseLog(void) { LOCK_SERVERLOG(); #ifdef HAVE_SYSLOG if (serverLogOpts.dest == logDest_syslog) { closelog(); } else #endif { if (serverLogFD >= 0) { close(serverLogFD); serverLogFD = -1; } free(ourName); ourName = NULL; } UNLOCK_SERVERLOG(); }