int LogLevel;
int mrafsStyleLogs = 0;
static int threadIdLogs = 0;
-int printLocks = 0;
static int resetSignals = 0;
-static char ourName[MAXPATHLEN];
+static char *ourName = NULL;
+
+static void RotateLogFile(void);
+
+/*!
+ * Determine if the file is a named pipe.
+ *
+ * This check is performed to support named pipes as logs by not rotating them
+ * and opening them with a non-blocking flags.
+ *
+ * \param[in] fileName log file name
+ *
+ * \returns non-zero if log file is a named pipe.
+ */
+static int
+IsFIFO(const char *fileName)
+{
+ struct stat statbuf;
+ return (lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode));
+}
void
SetLogThreadNumProgram(int (*func) (void) )
localtime_r(¤ttime, &tm));
info = &tbuffer[len];
- if (mrafsStyleLogs || threadIdLogs) {
+ if (threadIdLogs) {
num = (*threadNumProgram) ();
if (num > -1) {
snprintf(info, (sizeof tbuffer) - strlen(tbuffer), "[%d] ",
ViceLog(0, ("\n"));
}
+/*!
+ * Move the current log file out of the way so a new one can be started.
+ *
+ * The format of the new name depends on the logging style. The traditional
+ * Transarc style appends ".old" to the log file name. When MR-AFS style
+ * logging is in effect, a time stamp is appended to the log file name instead
+ * of ".old".
+ *
+ * \bug Unfortunately, no check is made to avoid overwriting
+ * old logs in the traditional Transarc mode.
+ *
+ * \param fileName fully qualified log file path
+ */
+static void
+RenameLogFile(const char *fileName)
+{
+ int code;
+ char *nextName = NULL;
+
+ if (mrafsStyleLogs) {
+ int tries;
+ time_t t;
+ struct stat buf;
+ struct tm *timeFields;
+
+ time(&t);
+ for (tries = 0; nextName == NULL && tries < 100; t++, tries++) {
+ timeFields = localtime(&t);
+ code = asprintf(&nextName, "%s.%d%02d%02d%02d%02d%02d",
+ fileName, timeFields->tm_year + 1900,
+ timeFields->tm_mon + 1, timeFields->tm_mday,
+ timeFields->tm_hour, timeFields->tm_min,
+ timeFields->tm_sec);
+ if (code < 0) {
+ nextName = NULL;
+ break;
+ }
+ if (lstat(nextName, &buf) == 0) {
+ /* Avoid clobbering a log. */
+ free(nextName);
+ nextName = NULL;
+ }
+ }
+ } else {
+ code = asprintf(&nextName, "%s.old", fileName);
+ if (code < 0) {
+ nextName = NULL;
+ }
+ }
+ if (nextName != NULL) {
+ rk_rename(fileName, nextName); /* Don't check the error code. */
+ free(nextName);
+ }
+}
+
static void*
DebugOn(void *param)
{
threadIdLogs = 0;
#endif
}
- printLocks = 2;
#if defined(AFS_PTHREAD_ENV)
DebugOn((void *)(intptr_t)LogLevel);
#else /* AFS_PTHREAD_ENV */
{
LogLevel = 0;
- if (printLocks > 0)
- --printLocks;
#if defined(AFS_PTHREAD_ENV)
DebugOn((void *)(intptr_t)LogLevel);
#else /* AFS_PTHREAD_ENV */
if (threadIdLogs == 1)
threadIdLogs = 0;
#endif
- if (mrafsStyleLogs)
- OpenLog((char *)&ourName);
+ if (mrafsStyleLogs) {
+ RotateLogFile();
+ }
} /*ResetDebug_Signal */
}
#endif /* AFS_PTHREAD_ENV */
+/*!
+ * Redirect stdout and stderr to the log file.
+ *
+ * \note Call directly after opening the log file.
+ *
+ * \param[in] fileName log file name
+ */
+static void
+RedirectStdStreams(const char *fileName)
+{
+ if (freopen(fileName, "a", stdout) == NULL)
+ ; /* don't care */
+ if (freopen(fileName, "a", stderr) != NULL) {
+#ifdef HAVE_SETVBUF
+ setvbuf(stderr, NULL, _IONBF, 0);
+#else
+ setbuf(stderr, NULL);
+#endif
+ }
+}
+
int
OpenLog(const char *fileName)
{
* This function should allow various libraries that inconsistently
* use stdout/stderr to all go to the same place
*/
- int tempfd, isfifo = 0;
- char oldName[MAXPATHLEN];
- struct timeval Start;
- struct tm *TimeFields;
- char FileName[MAXPATHLEN];
-
-#ifndef AFS_NT40_ENV
- struct stat statbuf;
-#endif
+ int tempfd;
+ int flags = O_WRONLY | O_TRUNC | O_CREAT | O_APPEND;
#if defined(AFS_PTHREAD_ENV)
opr_Verify(pthread_once(&serverLogOnce, InitServerLogMutex) == 0);
openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
return (0);
}
-
- /* Support named pipes as logs by not rotating them */
- if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
- isfifo = 1;
- }
#endif
- if (mrafsStyleLogs) {
- time_t t;
- struct stat buf;
- gettimeofday(&Start, NULL);
- t = Start.tv_sec;
- TimeFields = localtime(&t);
- if (fileName) {
- if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
- strcpy((char *)&ourName, (char *)fileName);
- }
- makefilename:
- snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
- ourName, TimeFields->tm_year + 1900,
- TimeFields->tm_mon + 1, TimeFields->tm_mday,
- TimeFields->tm_hour, TimeFields->tm_min,
- TimeFields->tm_sec);
- if(lstat(FileName, &buf) == 0) {
- /* avoid clobbering a log */
- TimeFields->tm_sec++;
- goto makefilename;
- }
- if (!isfifo)
- rk_rename(fileName, FileName); /* don't check error code */
- } else {
- strcpy(oldName, fileName);
- strcat(oldName, ".old");
+ opr_Assert(fileName != NULL);
- /* don't check error */
- if (!isfifo)
- rk_rename(fileName, oldName);
+ if (IsFIFO(fileName)) {
+ /* Support named pipes as logs by not rotating them. */
+ flags |= O_NONBLOCK;
+ } else {
+ RenameLogFile(fileName);
}
- tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT | O_APPEND | (isfifo?O_NONBLOCK:0), 0666);
+ tempfd = open(fileName, flags, 0666);
if (tempfd < 0) {
printf("Unable to open log file %s\n", fileName);
return -1;
}
- /* redirect stdout and stderr so random printf's don't write to data */
- if (freopen(fileName, "a", stdout) == NULL)
- ; /* don't care */
- if (freopen(fileName, "a", stderr) != NULL) {
-#ifdef HAVE_SETVBUF
- setvbuf(stderr, NULL, _IONBF, 0);
-#else
- setbuf(stderr, NULL);
-#endif
- }
+ RedirectStdStreams(fileName);
+
+ /* Save our name for reopening. */
+ free(ourName);
+ ourName = strdup(fileName);
+ opr_Assert(ourName != NULL);
serverLogFD = tempfd;
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. */
+ int flags = O_WRONLY | O_APPEND | O_CREAT;
#if !defined(AFS_NT40_ENV)
if (serverLogSyslog) {
return 0;
}
+#endif
- /* Support named pipes as logs by not rotating them */
- if ((lstat(fileName, &statbuf) == 0) && (S_ISFIFO(statbuf.st_mode))) {
- isfifo = 1;
+ if (IsFIFO(fileName)) {
+ flags |= O_NONBLOCK;
}
-#endif
LOCK_SERVERLOG();
if (serverLogFD >= 0)
close(serverLogFD);
- serverLogFD = open(fileName, O_WRONLY | O_APPEND | O_CREAT | (isfifo?O_NONBLOCK:0), 0666);
+ serverLogFD = open(fileName, flags, 0666);
if (serverLogFD >= 0) {
- if (freopen(fileName, "a", stdout) == NULL)
- ; /* don't care */
- if (freopen(fileName, "a", stderr) != NULL) {
-#ifdef HAVE_SETVBUF
- setvbuf(stderr, NULL, _IONBF, 0);
-#else
- setbuf(stderr, NULL);
-#endif
- }
-
+ RedirectStdStreams(fileName);
}
UNLOCK_SERVERLOG();
return serverLogFD < 0 ? -1 : 0;
}
/*!
+ * Rotate the log file by renaming then truncating.
+ */
+static void
+RotateLogFile(void)
+{
+ LOCK_SERVERLOG();
+ if (ourName != NULL) {
+ if (serverLogFD >= 0) {
+ close(serverLogFD);
+ serverLogFD = -1;
+ }
+ OpenLog(ourName);
+ }
+ UNLOCK_SERVERLOG();
+}
+
+/*!
* Close the server log file.
*
* \note Must be preceeded by OpenLog().
closelog();
} else
#endif
- if (serverLogFD >= 0) {
- close(serverLogFD);
- serverLogFD = -1;
+ {
+ if (serverLogFD >= 0) {
+ close(serverLogFD);
+ serverLogFD = -1;
+ }
+ free(ourName);
+ ourName = NULL;
}
UNLOCK_SERVERLOG();
}