Debian Codegraph @

w4/logsubs.c

raw
1 /* File: logsubs.c
2 * Author: Ed Griffiths ([email protected])
3 * Copyright (c) J Thierry-Mieg and R Durbin, 2001
4 *-------------------------------------------------------------------
5 * Acedb is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
19 *-------------------------------------------------------------------
20 * This file is part of the ACEDB genome database package, written by
21 * Richard Durbin (Sanger Centre, UK) [email protected], and
22 * Jean Thierry-Mieg (CRBM du CNRS, France) [email protected]
23 *
24 * Description: A set of routines to log messages in a standard format
25 * for acedb programs, all logs are kept in database/
26 * subdirectory of the database.
27 * All messages have a standard prefix which includes
28 * process id, user id, timestamp.
29 * Used by session.c to write messages in database/log.wrm,
30 * i.e. this happens for _all_ acedb programs.
31 * Used by serverace.c to write messages in database/serverlog.wrm,
32 * this log contains server specific information.
33 *
34 * Exported functions: See wh/log.h
35 * HISTORY:
36 * Last edited: Oct 7 15:42 2002 (edgrif)
37 * Created: Mon Feb 26 13:15:08 2001 (edgrif)
38 * CVS info: $Id: logsubs.c,v 1.8 2002/10/18 12:43:59 edgrif Exp $
39 *-------------------------------------------------------------------
40 */
41
42 #include <wh/regular.h>
43 #include <wh/aceio.h>
44 #include <wh/dbpath.h>
45 #include <wh/aceversion.h>
46 #include <wh/mytime.h>
47 #include <wh/log.h>
48
49
50 /* This code shows the less good side of error handling in our code, really */
51 /* every single aceOut call should be tested for its return code but in */
52 /* fact this needlessly complicates the code here. It would be better to */
53 /* have macro versions of the aceOut calls that could be used instead, these */
54 /* would messcrash if the aceOut call failed giving the Line/file info. so */
55 /* no result testing would be required at this level. */
56
57 static void logPrintPrefix(ACEOUT logfile_out, char *logfile_host, int logfile_pid, BOOL timestamp) ;
58
59
60 /* Keep in step with aceLogExitType in log.h */
61 char *aceLogExitStrings[] = {"NORMAL EXIT", "ABNORMAL EXIT", "CRASH", "CLOSED"} ;
62
63
64 /* These are going to be the same for all logging within a process, really */
65 /* they should be in a context that is supplied to the logging calls. */
66 /* */
67 static char logfile_host[MAXHOSTNAMELEN + 1] = "" ;
68 static int logfile_pid = 0 ;
69 static BOOL add_timestamps_G = FALSE ; /* Add timestamps to all messages, not
70 just the initial session one. */
71
72
73 /* Open an existing logfile (or if logfile doesn't exist, create a new one), */
74 /* and write an initial "session start" message. */
75 /* The file will be opened in the ..../database subdirectory of the database.*/
76 /* */
77 BOOL openAceLogFile(char *logfile_name, ACEOUT *logfile_out, BOOL new_session)
78 {
79 BOOL result = FALSE ;
80 char *logfile_path = NULL ;
81
82 if (!logfile_name || logfile_out == NULL)
83 messcrash("Internal coding error, "
84 "logfile cannot be opened because: %s%s.",
85 (logfile_name ? "" : "logfile name is NULL "),
86 (logfile_out ? "" : "pointer to logfile ACEOUT is NULL ")) ;
87
88 /* Need permissions to write into database/ directory. */
89 if (euid != ruid)
90 seteuid(euid);
91
92 /* Should get these from some sort of program wide repository for this */
93 /* information. */
94 logfile_pid = getpid() ;
95 gethostname(logfile_host, MAXHOSTNAMELEN) ;
96
97 /* test whether we can append to avoid messerror from aceOutCreateToFile() */
98 logfile_path = dbPathStrictFilName ("database", logfile_name, "wrm", "a", 0);
99
100 /* ok, open the logfile. */
101 if (logfile_path)
102 {
103 ACEOUT new_logfile = NULL ;
104
105 if ((new_logfile = aceOutCreateToFile (logfile_path, "a", 0)))
106 {
107 *logfile_out = new_logfile ;
108 result = TRUE ;
109 }
110 messfree(logfile_path);
111 }
112
113 /* write an intial message that can be used to identify the start of a */
114 /* programs session with the database. */
115 if (result && new_session)
116 {
117 aceOutPrint(*logfile_out, "\n") ;
118 logPrintPrefix(*logfile_out, logfile_host, logfile_pid, TRUE) ;
119 aceOutPrint(*logfile_out, "SESSION_START - User:%s, Program:%s, Level:%s, %s\n",
120 getLogin(TRUE), messGetErrorProgram(),
121 aceGetVersionString(), aceGetLinkDateString()) ;
122 aceOutFlush(*logfile_out) ;
123 }
124
125
126 if (euid != ruid)
127 seteuid(ruid);
128
129 return result ;
130 }
131
132
133 /* Doing the time stamping is expensive computationally so this will allow this features to be
134 * turned on/off for all messages except the session start/end messages. */
135 void setTimeStampsAceLogFile(BOOL timestamps_on)
136 {
137 add_timestamps_G = timestamps_on ;
138 }
139
140
141 /* Add a message to the log. */
142 /* */
143 BOOL appendAceLogFile(ACEOUT logfile_out, char *text)
144 {
145 BOOL result = FALSE ;
146
147 if (logfile_out == NULL || text == NULL)
148 messcrash("Internal coding error, "
149 "logfile cannot be appended to because: %s%s.",
150 (logfile_out ? "" : "logfile ACEOUT is NULL "),
151 (text ? "" : "message text is NULL")) ;
152
153 logPrintPrefix(logfile_out, logfile_host, logfile_pid, add_timestamps_G) ;
154 aceOutPrint(logfile_out, "%s\n", text) ;
155 aceOutFlush(logfile_out) ;
156
157 result = TRUE ;
158
159 return result ;
160 }
161
162
163 /* Close the log file and put in a closing message for the session. */
164 /* */
165 BOOL closeAceLogFile(ACEOUT logfile_out, aceLogExitType exit_type)
166 {
167 BOOL result = FALSE ;
168
169 if (logfile_out == NULL)
170 messcrash("Internal coding error, "
171 "logfile cannot be closed because logfile ACEOUT is NULL.") ;
172
173
174 if (exit_type != ACELOG_NORMAL_EXIT && exit_type != ACELOG_ABNORMAL_EXIT
175 && exit_type != ACELOG_CRASH_EXIT
176 && exit_type != ACELOG_CLOSE_LOG)
177 messcrash("Internal coding error, "
178 "logfile cannot be closed because aceLogExitType is invalid: %d",
179 exit_type) ;
180
181 if (exit_type != ACELOG_CLOSE_LOG)
182 {
183 char *text ;
184
185 text = aceLogExitStrings[exit_type] ;
186
187 logPrintPrefix(logfile_out, logfile_host, logfile_pid, TRUE) ;
188 aceOutPrint(logfile_out, "SESSION_END - %s\n", text) ;
189 aceOutPrint(logfile_out, "\n") ;
190 }
191
192 aceOutFlush(logfile_out) ;
193 aceOutDestroy (logfile_out);
194
195 result = TRUE ;
196
197 return result ;
198 }
199
200 /* Rename the existing log file and start a new one. Gets called either when */
201 /* requested by user or when database is bootstrapped (start new log because */
202 /* old one is really only applicable to previous incarnation of database. */
203 /* */
204 /* The messages in logfiles only really apply to the database
205 * in its current state, if the database is being rebuilt the
206 * previous log becomes inappropriate.
207 * The previous log is therefore moved to a backup location,
208 * and a new logfile is started at this point (SANgc02773) */
209 /* */
210 /* if log_file_name is NULL then old log is called "log.<ace-time>.wrm" */
211 /* if log_msg is non-NULL then its written to end of old log. */
212 /* */
213 /* The old log always gets closed. */
214 /* If we fail to open the new log we return NULL, otherwise we return an */
215 /* ACEOUT to the opened log. */
216 /* */
217 ACEOUT startNewAceLogFile(ACEOUT current_log, char *old_logname, BOOL timestamp,
218 char *log_msg, BOOL new_session)
219 {
220 ACEOUT result = NULL ;
221 STORE_HANDLE handle = NULL ;
222 BOOL status = TRUE ;
223 char *current_logname = NULL ;
224
225
226 handle = handleCreate() ;
227
228 if (current_log == NULL)
229 messcrash("Internal coding error: start of new log file failed because startNewAceLogFile() "
230 "was passed a NULL log ptr.") ;
231
232 /* Print final message to log ? */
233 if (log_msg)
234 messdump(log_msg) ;
235
236 /* Make sure existing log is flushed but _NOT_ closed, otherwise the below */
237 /* messerrors will cause a crash because the log has disappeared from */
238 /* underneath their feet ! */
239 aceOutFlush(current_log) ;
240
241
242 current_logname = strnew(filGetFilenameBase(aceOutGetFilename(current_log)), 0) ;
243
244 /* Check we can write to the "oldlogs" directory (may need to create it). */
245 if (status)
246 {
247 if (dbPathStrictFilName("database", current_logname, "wrm", "r", handle))
248 {
249 if (!dbPathStrictFilName("database", "oldlogs", 0, "wd", handle))
250 {
251 /* There is no "oldlogs" directory so create it. */
252 if (!dbPathStrictFilName("database", "oldlogs", 0, "rd", handle))
253 {
254 if (mkdir(dbPathMakeFilName("database", "oldlogs", 0, handle), 0755) == -1)
255 {
256 /* during init we shouldn't have problems with this, that's
257 why we output an error (could be NFS timeout, because
258 permissions should be ok) */
259 messerror("failed to create directory database/oldlogs/ (%s)",
260 messSysErrorText());
261 status = FALSE ;
262 }
263 }
264 else if (chmod (dbPathMakeFilName("database", "oldlogs", 0, handle), 0755) == -1)
265 {
266 /* There is an "oldlogs" directory but perms are wrong. */
267 messerror("failed to chmod 755 directory database/oldlogs/ (%s)",
268 messSysErrorText());
269 status = FALSE ;
270 }
271 }
272 }
273 }
274
275 /* Rename/move existing log into "oldlogs" directory. */
276 if (status)
277 {
278 char logFileBackupName[MAXPATHLEN]; /* This is not actually going to do
279 it, sadly MAXPATHLEN does not do
280 what you think ! */
281 char *logFileBackupPath;
282
283 /* Make sure existing log is flushed and closed. */
284 /* Note that we have to do this _before_ renaming,
285 at least on cygwin */
286
287 aceOutFlush(current_log) ;
288 aceOutDestroy(current_log);
289
290 if (timestamp)
291 sprintf(logFileBackupName, "%s-%s", old_logname, timeShow(timeNow()));
292 else
293 sprintf(logFileBackupName, "%s", old_logname) ;
294
295 logFileBackupPath = dbPathStrictFilName("database/oldlogs", logFileBackupName, "wrm",
296 "w", handle);
297
298 if ((!logFileBackupPath)
299 || (rename(dbPathStrictFilName("database", current_logname, "wrm", "r", handle),
300 logFileBackupPath) == -1))
301 {
302 messerror ("failed to rename previous log-file "
303 "%s to %s (%s)",
304 dbPathStrictFilName("database", current_logname, "wrm", "r", handle),
305 logFileBackupPath,
306 messSysErrorText());
307 status = FALSE ;
308 }
309 }
310
311 /* If all is still OK then open the new log file. */
312 if (status)
313 {
314 if (!openAceLogFile(current_logname, &result, new_session))
315 {
316 result = NULL ;
317 }
318 }
319
320 if (handle)
321 handleDestroy(handle);
322
323 return result ;
324 }
325
326 /* Just close and reopen the existing logfile. This routine can be used in */
327 /* conjunction with utilities such as the Linux "rotatelogs" program to */
328 /* allow acedb logs to be manipulated by system utilities while acedb */
329 /* programs continue to use the log files uninterrupted. */
330 /* */
331 BOOL reopenAceLogFile(ACEOUT *current_log_inout)
332 {
333 BOOL result = FALSE ;
334 char *current_logname = NULL ;
335
336 if (current_log_inout == NULL || *current_log_inout == NULL)
337 messcrash("Internal coding error: start of new log file failed because reopenAceLogFile() "
338 "was passed a NULL logfile ptr.") ;
339
340 current_logname = strnew(filGetFilenameBase(aceOutGetFilename(*current_log_inout)), 0) ;
341
342 /* Do the close and reopen. */
343 if ((result = closeAceLogFile(*current_log_inout, ACELOG_CLOSE_LOG)))
344 {
345 result = openAceLogFile(current_logname, current_log_inout, FALSE) ;
346 }
347
348 return result ;
349 }
350
351
352
353 /* Common prefix for all log records: */
354 /* - prepend time, host, pid */
355 static void logPrintPrefix(ACEOUT logfile_out, char *logfile_host, int logfile_pid, BOOL timestamp)
356 {
357 aceOutPrint(logfile_out, "%s %s %d\t",
358 (timestamp ? timeShow(timeNow()) : ""),
359 logfile_host, logfile_pid) ;
360
361 return ;
362 }
363