mrafs-server-log-handling-20010212
[openafs.git] / src / util / serverLog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*  serverLog.c     - Server logging                                      */
11 /*                                                                        */
12 /*  Information Technology Center                                         */
13 /*  Date: 05/21/97                                                        */
14 /*                                                                        */
15 /*  Function    - These routiens implement logging from the servers       */
16 /*                                                                        */
17 /* ********************************************************************** */
18
19 #include <afs/param.h>
20 #include <stdio.h>
21 #ifdef AFS_NT40_ENV
22 #include <io.h>
23 #include <time.h>
24 #else
25 #ifdef AFS_AIX_ENV
26 #include <time.h>
27 #endif
28 #include <sys/param.h>
29 #include <sys/time.h>
30 #endif
31 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
32 #include <fcntl.h>
33 #include <afs/stds.h>
34 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
35 #include <string.h>
36 #else
37 #include <strings.h>
38 #endif
39 #include "afsutil.h"
40 #include "fileutil.h"
41 #if defined(AFS_PTHREAD_ENV)
42 #include <assert.h>
43 #include <pthread.h>
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)
47
48 #ifdef AFS_NT40_ENV
49 #define NULLDEV "NUL"
50 #else
51 #define NULLDEV "/dev/null"
52 #endif
53
54 #else /* AFS_PTHREAD_ENV */
55 #define LOCK_SERVERLOG() 
56 #define UNLOCK_SERVERLOG() 
57 #endif  /* AFS_PTHREAD_ENV */
58
59 #ifdef AFS_NT40_ENV
60 #define F_OK 0
61 #endif
62
63 char *threadname();
64
65 static int serverLogFD = -1;
66
67 #include <stdarg.h>
68 int LogLevel;
69 int mrafsStyleLogs = 0;
70 int printLocks = 0;
71 static char ourName[MAXPATHLEN];
72
73 void WriteLogBuffer(buf,len)
74     char *buf;
75     afs_uint32 len;
76 {
77     LOCK_SERVERLOG();
78     if (serverLogFD > 0)
79         write(serverLogFD, buf, len);
80     UNLOCK_SERVERLOG();
81 }
82
83 /* VARARGS1 */
84 void FSLog (const char *format, ...)
85 {
86     va_list args;
87
88     time_t currenttime;
89     char *timeStamp;
90     char tbuffer[1024];
91     char *info;
92     int len;
93     int i;
94     char *name;
95
96     currenttime = time(0);
97     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
98     timeStamp[24] = ' ';  /* ts[24] is the newline, 25 is the null */
99     info = &timeStamp[25];
100
101     if (mrafsStyleLogs) {
102        name = threadname();
103        sprintf(info, "[%s] ", name);
104        info += strlen(info);
105     }
106
107     va_start(args, format);
108     (void) vsprintf(info, format, args);
109     va_end(args);
110
111     len = strlen(tbuffer);
112     LOCK_SERVERLOG();
113     if (serverLogFD > 0)
114         write(serverLogFD, tbuffer, len);
115     UNLOCK_SERVERLOG();
116
117 #if !defined(AFS_PTHREAD_ENV)
118     fflush(stdout);
119     fflush(stderr);     /* in case they're sharing the same FD */
120 #endif
121 }
122
123 static void DebugOn(int loglevel)
124 {
125     if (loglevel == 0) {
126         ViceLog(0, ("Reset Debug levels to 0\n"));
127     } else {
128         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
129     }
130 } /*DebugOn*/
131
132
133
134 void SetDebug_Signal(int signo)
135 {
136     extern int IOMGR_SoftSig();
137
138     if (LogLevel > 0) {
139         LogLevel *= 5;
140     }
141     else {
142         LogLevel = 1;
143     }
144     printLocks = 2;
145 #if defined(AFS_PTHREAD_ENV)
146     DebugOn(LogLevel);
147 #else /* AFS_PTHREAD_ENV */
148     IOMGR_SoftSig(DebugOn, LogLevel);
149 #endif /* AFS_PTHREAD_ENV */
150
151     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
152                                       /* handler needs to be set again */
153 } /*SetDebug_Signal*/
154
155 void ResetDebug_Signal(int signo)
156 {
157     LogLevel = 0;
158
159     if (printLocks >0) --printLocks;
160 #if defined(AFS_PTHREAD_ENV)
161     DebugOn(LogLevel);
162 #else /* AFS_PTHREAD_ENV */
163     IOMGR_SoftSig(DebugOn, LogLevel);
164 #endif /* AFS_PTHREAD_ENV */
165
166     signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
167                                         /* handler needs to be set again */
168     if (mrafsStyleLogs)
169         OpenLog((char *)&ourName);
170 } /*ResetDebug_Signal*/
171
172
173 void SetupLogSignals(void)
174 {
175     signal(SIGHUP, ResetDebug_Signal);
176     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
177     signal(SIGTSTP, SetDebug_Signal);
178 }
179
180 int OpenLog(const char *fileName) 
181 {
182     /*
183      * This function should allow various libraries that inconsistently
184      * use stdout/stderr to all go to the same place
185      */
186     int tempfd;
187     char oldName[MAXPATHLEN];
188     struct timeval Start;
189     struct tm *TimeFields;
190     char FileName[MAXPATHLEN]; 
191
192     if (mrafsStyleLogs) {
193         TM_GetTimeOfDay(&Start, 0);
194         TimeFields = localtime(&Start.tv_sec);
195         if (fileName) {
196             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
197             strcpy((char *)&ourName, (char *) fileName);
198         }
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); 
205     } else {
206         strcpy(oldName, fileName);
207         strcat(oldName, ".old");
208
209         /* don't check error */
210         renamefile(fileName, oldName);
211         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
212     }
213
214     if(tempfd < 0)
215     {
216         printf("Unable to open log file %s\n", fileName);
217         return -1;
218     }
219
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);
224
225     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
226
227     serverLogFD = tempfd;
228 #else
229     close(tempfd); /* just checking.... */
230     freopen(fileName, "w", stdout);
231     freopen(fileName, "w", stderr);
232     serverLogFD = fileno(stdout);
233 #endif /* AFS_PTHREAD_ENV */
234
235     return 0;
236 } /*OpenLog*/
237
238 int ReOpenLog(const char *fileName) 
239 {
240 #if !defined(AFS_PTHREAD_ENV)
241     int tempfd;
242 #endif
243
244     if (access(fileName, F_OK)==0)
245         return 0; /* exists, no need to reopen. */
246
247 #if defined(AFS_PTHREAD_ENV)
248     LOCK_SERVERLOG();
249     if (serverLogFD > 0)
250         close(serverLogFD);
251     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
252     UNLOCK_SERVERLOG();
253     return serverLogFD < 0 ? -1 : 0;
254 #else
255
256     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
257     if(tempfd < 0)
258     {
259         printf("Unable to open log file %s\n", fileName);
260         return -1;
261     }
262     close(tempfd);
263
264     freopen(fileName, "a", stdout);
265     freopen(fileName, "a", stderr);
266     serverLogFD = fileno(stdout);
267   
268
269     return 0;
270 #endif /* AFS_PTHREAD_ENV */
271 }