2 * Bacula Event File handling
4 * Kern Sibbald, August MMI
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 void log_event(UPSINFO *ups, int level, char *fmt, ...)
33 char msg[2*MAXSTRING];
34 char datetime[MAX_TIME_LENGTH];
35 int event_fd = ups->event_fd;
37 event_fd = ups->event_fd;
39 /*****FIXME***** use pool memory */
40 va_start(arg_ptr, fmt);
41 vsprintf(msg, fmt, arg_ptr);
44 syslog(level, msg); /* log the event */
46 /* Write out to our temp file. LOG_INFO is DATA logging, so
47 do not write it to our temp events file. */
48 if (event_fd >= 0 && level != LOG_INFO) {
54 localtime_r(&nowtime, &tm);
55 strftime(datetime, sizeof(datetime), "%a %b %d %X %Z %Y ", &tm);
56 write(event_fd, datetime, strlen(datetime));
58 if (msg[lm-1] != '\n')
60 write(event_fd, msg, lm);
65 #define NLE 10 /* number of events to send and keep */
66 #define MAXLE 50 /* truncate file when this many events */
69 * If the EVENTS file exceeds MAXLE records, truncate it.
73 * 0 if file not truncated
76 int truncate_events_file(UPSINFO *ups)
86 if ((events_file = fopen(ups->eventfile, "r+")) == NULL)
91 if ((le[i] = malloc(MAXSTRING)) == NULL)
94 while (fgets(le[i], MAXSTRING, events_file) != NULL) {
97 if (i >= NLE) /* wrap */
101 trunc = TRUE; /* file too large, truncate it */
103 nrec = NLE; /* number of records to output */
104 i -= (i/NLE)*NLE; /* first record to output */
107 /* get total length to output */
108 for (j=0; j < nrec; j++)
109 tlen += strlen(le[j]);
110 if ((buf = malloc(tlen+1)) == NULL)
113 /* Put records in single buffer in correct order */
114 for (j=0; j < nrec; j++) {
115 strcat(buf, le[i++]);
120 ftruncate(fileno(events_file), 0L);
122 fwrite(buf, tlen, 1, events_file); /* write last NLE records to file */
123 stat = 1; /* we truncated it */
131 for (i=0; i<NLE; i++)
137 #endif /* IMPLEMENTED */
143 extern UPSINFO myUPS;
147 * Fill the Events list box with the last events
150 void FillEventsBox(HWND hwnd, int idlist)
156 if (!shm_OK || myUPS.eventfile[0] == 0 ||
157 (events_file = fopen(myUPS.eventfile, "r")) == NULL) {
158 SendDlgItemMessage(hwnd, idlist, LB_ADDSTRING, 0,
159 (LONG)"Events not available");
163 while (fgets(buf, sizeof(buf), events_file) != NULL) {
165 /* strip trailing cr/lfs */
166 while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r'))
168 SendDlgItemMessage(hwnd, idlist, LB_ADDSTRING, 0, (LONG)buf);
173 #endif /* HAVE_CYGWIN */