2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* serverLog.c - Server logging */
12 /* Information Technology Center */
15 /* Function - These routiens implement logging from the servers */
17 /* ********************************************************************** */
19 #include <afs/param.h>
28 #include <sys/param.h>
31 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
34 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
41 #if defined(AFS_PTHREAD_ENV)
44 static pthread_mutex_t serverLogMutex;
45 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
46 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
51 #define NULLDEV "/dev/null"
54 #else /* AFS_PTHREAD_ENV */
55 #define LOCK_SERVERLOG()
56 #define UNLOCK_SERVERLOG()
57 #endif /* AFS_PTHREAD_ENV */
65 static int serverLogFD = -1;
69 int mrafsStyleLogs = 0;
71 static char ourName[MAXPATHLEN];
73 void WriteLogBuffer(buf,len)
79 write(serverLogFD, buf, len);
84 void FSLog (const char *format, ...)
96 currenttime = time(0);
97 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
98 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
99 info = &timeStamp[25];
101 if (mrafsStyleLogs) {
103 sprintf(info, "[%s] ", name);
104 info += strlen(info);
107 va_start(args, format);
108 (void) vsprintf(info, format, args);
111 len = strlen(tbuffer);
114 write(serverLogFD, tbuffer, len);
117 #if !defined(AFS_PTHREAD_ENV)
119 fflush(stderr); /* in case they're sharing the same FD */
123 static void DebugOn(int loglevel)
126 ViceLog(0, ("Reset Debug levels to 0\n"));
128 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
134 void SetDebug_Signal(int signo)
136 extern int IOMGR_SoftSig();
145 #if defined(AFS_PTHREAD_ENV)
147 #else /* AFS_PTHREAD_ENV */
148 IOMGR_SoftSig(DebugOn, LogLevel);
149 #endif /* AFS_PTHREAD_ENV */
151 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
152 /* handler needs to be set again */
153 } /*SetDebug_Signal*/
155 void ResetDebug_Signal(int signo)
159 if (printLocks >0) --printLocks;
160 #if defined(AFS_PTHREAD_ENV)
162 #else /* AFS_PTHREAD_ENV */
163 IOMGR_SoftSig(DebugOn, LogLevel);
164 #endif /* AFS_PTHREAD_ENV */
166 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
167 /* handler needs to be set again */
169 OpenLog((char *)&ourName);
170 } /*ResetDebug_Signal*/
173 void SetupLogSignals(void)
175 signal(SIGHUP, ResetDebug_Signal);
176 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
177 signal(SIGTSTP, SetDebug_Signal);
180 int OpenLog(const char *fileName)
183 * This function should allow various libraries that inconsistently
184 * use stdout/stderr to all go to the same place
187 char oldName[MAXPATHLEN];
188 struct timeval Start;
189 struct tm *TimeFields;
190 char FileName[MAXPATHLEN];
192 if (mrafsStyleLogs) {
193 TM_GetTimeOfDay(&Start, 0);
194 TimeFields = localtime(&Start.tv_sec);
196 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
197 strcpy((char *)&ourName, (char *) fileName);
199 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
200 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1,
201 TimeFields->tm_mday, TimeFields->tm_hour,
202 TimeFields->tm_min, TimeFields->tm_sec);
203 rename (fileName, FileName); /* don't check error code */
204 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
206 strcpy(oldName, fileName);
207 strcat(oldName, ".old");
209 /* don't check error */
210 renamefile(fileName, oldName);
211 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
216 printf("Unable to open log file %s\n", fileName);
220 #if defined(AFS_PTHREAD_ENV)
221 /* redirect stdout and stderr so random printf's don't write to data */
222 assert(freopen(NULLDEV, "w", stdout) != NULL);
223 assert(freopen(NULLDEV, "w", stderr) != NULL);
225 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
227 serverLogFD = tempfd;
229 close(tempfd); /* just checking.... */
230 freopen(fileName, "w", stdout);
231 freopen(fileName, "w", stderr);
232 serverLogFD = fileno(stdout);
233 #endif /* AFS_PTHREAD_ENV */
238 int ReOpenLog(const char *fileName)
240 #if !defined(AFS_PTHREAD_ENV)
244 if (access(fileName, F_OK)==0)
245 return 0; /* exists, no need to reopen. */
247 #if defined(AFS_PTHREAD_ENV)
251 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
253 return serverLogFD < 0 ? -1 : 0;
256 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
259 printf("Unable to open log file %s\n", fileName);
264 freopen(fileName, "a", stdout);
265 freopen(fileName, "a", stderr);
266 serverLogFD = fileno(stdout);
270 #endif /* AFS_PTHREAD_ENV */