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 */
63 static int serverLogFD = -1;
69 void FSLog (const char *format, ...)
79 currenttime = time(0);
80 timeStamp = afs_ctime(¤ttime, tbuffer, sizeof(tbuffer));
81 timeStamp[24] = ' '; /* ts[24] is the newline, 25 is the null */
82 info = &timeStamp[25];
84 va_start(args, format);
85 (void) vsprintf(info, format, args);
88 len = strlen(tbuffer);
91 write(serverLogFD, tbuffer, len);
94 #if !defined(AFS_PTHREAD_ENV)
96 fflush(stderr); /* in case they're sharing the same FD */
100 static void DebugOn(int loglevel)
103 ViceLog(0, ("Reset Debug levels to 0\n"));
105 ViceLog(0, ("Set Debug On level = %d\n",loglevel));
111 void SetDebug_Signal(int signo)
113 extern int IOMGR_SoftSig();
121 #if defined(AFS_PTHREAD_ENV)
123 #else /* AFS_PTHREAD_ENV */
124 IOMGR_SoftSig(DebugOn, LogLevel);
125 #endif /* AFS_PTHREAD_ENV */
127 signal(signo, SetDebug_Signal); /* on some platforms, this signal */
128 /* handler needs to be set again */
129 } /*SetDebug_Signal*/
131 void ResetDebug_Signal(int signo)
135 #if defined(AFS_PTHREAD_ENV)
137 #else /* AFS_PTHREAD_ENV */
138 IOMGR_SoftSig(DebugOn, LogLevel);
139 #endif /* AFS_PTHREAD_ENV */
141 signal(signo, ResetDebug_Signal); /* on some platforms, this signal */
142 /* handler needs to be set again */
143 } /*ResetDebug_Signal*/
146 void SetupLogSignals(void)
148 signal(SIGHUP, ResetDebug_Signal);
149 /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
150 signal(SIGTSTP, SetDebug_Signal);
153 int OpenLog(const char *fileName)
156 * This function should allow various libraries that inconsistently
157 * use stdout/stderr to all go to the same place
160 char oldName[MAXPATHLEN];
162 strcpy(oldName, fileName);
163 strcat(oldName, ".old");
165 /* don't check error */
166 renamefile(fileName, oldName);
167 tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
170 printf("Unable to open log file %s\n", fileName);
174 #if defined(AFS_PTHREAD_ENV)
175 /* redirect stdout and stderr so random printf's don't write to data */
176 assert(freopen(NULLDEV, "w", stdout) != NULL);
177 assert(freopen(NULLDEV, "w", stderr) != NULL);
179 assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
181 serverLogFD = tempfd;
183 close(tempfd); /* just checking.... */
184 freopen(fileName, "w", stdout);
185 freopen(fileName, "w", stderr);
186 serverLogFD = fileno(stdout);
187 #endif /* AFS_PTHREAD_ENV */
192 int ReOpenLog(const char *fileName)
194 #if !defined(AFS_PTHREAD_ENV)
198 if (access(fileName, F_OK)==0)
199 return 0; /* exists, no need to reopen. */
201 #if defined(AFS_PTHREAD_ENV)
205 serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
207 return serverLogFD < 0 ? -1 : 0;
210 tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
213 printf("Unable to open log file %s\n", fileName);
218 freopen(fileName, "a", stdout);
219 freopen(fileName, "a", stderr);
220 serverLogFD = fileno(stdout);
224 #endif /* AFS_PTHREAD_ENV */