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>
32 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
35 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
42 #if defined(AFS_PTHREAD_ENV)
45 static pthread_mutex_t serverLogMutex;
46 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
47 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
52 #define NULLDEV "/dev/null"
55 #else /* AFS_PTHREAD_ENV */
56 #define LOCK_SERVERLOG()
57 #define UNLOCK_SERVERLOG()
58 #endif /* AFS_PTHREAD_ENV */
64 char *(*threadNameProgram)();
66 static int serverLogFD = -1;
69 int serverLogSyslog = 0;
70 int serverLogSyslogFacility = LOG_DAEMON;
75 int mrafsStyleLogs = 0;
77 static char ourName[MAXPATHLEN];
80 void FSLog (const char *format, ...)
92 currenttime = time(0);
93 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
94 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
95 info = &timeStamp[25];
98 name = (*threadNameProgram)();
99 sprintf(info, "[%s] ", name);
100 info += strlen(info);
103 va_start(args, format);
104 (void) vsprintf(info, format, args);
107 len = strlen(tbuffer);
110 if ( serverLogSyslog ){
111 syslog(LOG_INFO, "%s", info);
115 write(serverLogFD, tbuffer, len);
118 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
119 if ( ! serverLogSyslog ) {
121 fflush(stderr); /* in case they're sharing the same FD */
126 static void DebugOn(int loglevel)
129 ViceLog(0, ("Reset Debug levels to 0\n"));
131 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
137 void SetDebug_Signal(int signo)
139 extern int IOMGR_SoftSig();
148 #if defined(AFS_PTHREAD_ENV)
150 #else /* AFS_PTHREAD_ENV */
151 IOMGR_SoftSig(DebugOn, LogLevel);
152 #endif /* AFS_PTHREAD_ENV */
154 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
155 /* handler needs to be set again */
156 } /*SetDebug_Signal*/
158 void ResetDebug_Signal(int signo)
162 if (printLocks >0) --printLocks;
163 #if defined(AFS_PTHREAD_ENV)
165 #else /* AFS_PTHREAD_ENV */
166 IOMGR_SoftSig(DebugOn, LogLevel);
167 #endif /* AFS_PTHREAD_ENV */
169 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
170 /* handler needs to be set again */
172 OpenLog((char *)&ourName);
173 } /*ResetDebug_Signal*/
176 void SetupLogSignals(void)
178 signal(SIGHUP, ResetDebug_Signal);
179 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
180 signal(SIGTSTP, SetDebug_Signal);
183 int OpenLog(const char *fileName)
186 * This function should allow various libraries that inconsistently
187 * use stdout/stderr to all go to the same place
190 char oldName[MAXPATHLEN];
191 struct timeval Start;
192 struct tm *TimeFields;
193 char FileName[MAXPATHLEN];
196 if ( serverLogSyslog ) {
197 openlog(NULL, LOG_PID, serverLogSyslogFacility);
202 if (mrafsStyleLogs) {
203 TM_GetTimeOfDay(&Start, 0);
204 TimeFields = localtime(&Start.tv_sec);
206 if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
207 strcpy((char *)&ourName, (char *) fileName);
209 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
210 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1,
211 TimeFields->tm_mday, TimeFields->tm_hour,
212 TimeFields->tm_min, TimeFields->tm_sec);
213 rename (fileName, FileName); /* don't check error code */
214 tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666);
216 strcpy(oldName, fileName);
217 strcat(oldName, ".old");
219 /* don't check error */
220 renamefile(fileName, oldName);
221 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
226 printf("Unable to open log file %s\n", fileName);
230 #if defined(AFS_PTHREAD_ENV)
231 /* redirect stdout and stderr so random printf's don't write to data */
232 assert(freopen(NULLDEV, "w", stdout) != NULL);
233 assert(freopen(NULLDEV, "w", stderr) != NULL);
235 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
237 serverLogFD = tempfd;
239 close(tempfd); /* just checking.... */
240 freopen(fileName, "w", stdout);
241 freopen(fileName, "w", stderr);
242 serverLogFD = fileno(stdout);
243 #endif /* AFS_PTHREAD_ENV */
248 int ReOpenLog(const char *fileName)
250 #if !defined(AFS_PTHREAD_ENV)
254 if (access(fileName, F_OK)==0)
255 return 0; /* exists, no need to reopen. */
257 #if !defined(AFS_NT40_ENV)
258 if ( serverLogSyslog ) {
263 #if defined(AFS_PTHREAD_ENV)
267 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
269 return serverLogFD < 0 ? -1 : 0;
272 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
275 printf("Unable to open log file %s\n", fileName);
280 freopen(fileName, "a", stdout);
281 freopen(fileName, "a", stderr);
282 serverLogFD = fileno(stdout);
286 #endif /* AFS_PTHREAD_ENV */