2 * Bacula Event File handling
4 * Kern Sibbald, August MMI
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 void log_event(UPSINFO *ups, int level, char *fmt, ...)
35 char msg[2*MAXSTRING];
36 char datetime[MAX_TIME_LENGTH];
37 int event_fd = ups->event_fd;
39 event_fd = ups->event_fd;
41 /*****FIXME***** use pool memory */
42 va_start(arg_ptr, fmt);
43 vsprintf(msg, fmt, arg_ptr);
46 syslog(level, "%s", msg); /* log the event */
48 /* Write out to our temp file. LOG_INFO is DATA logging, so
49 do not write it to our temp events file. */
50 if (event_fd >= 0 && level != LOG_INFO) {
56 localtime_r(&nowtime, &tm);
57 strftime(datetime, sizeof(datetime), "%a %b %d %X %Z %Y ", &tm);
58 write(event_fd, datetime, strlen(datetime));
60 if (msg[lm-1] != '\n')
62 write(event_fd, msg, lm);
67 #define NLE 10 /* number of events to send and keep */
68 #define MAXLE 50 /* truncate file when this many events */
71 * If the EVENTS file exceeds MAXLE records, truncate it.
75 * 0 if file not truncated
78 int truncate_events_file(UPSINFO *ups)
88 if ((events_file = fopen(ups->eventfile, "r+")) == NULL)
93 if ((le[i] = malloc(MAXSTRING)) == NULL)
96 while (fgets(le[i], MAXSTRING, events_file) != NULL) {
99 if (i >= NLE) /* wrap */
103 trunc = TRUE; /* file too large, truncate it */
105 nrec = NLE; /* number of records to output */
106 i -= (i/NLE)*NLE; /* first record to output */
109 /* get total length to output */
110 for (j=0; j < nrec; j++)
111 tlen += strlen(le[j]);
112 if ((buf = malloc(tlen+1)) == NULL)
115 /* Put records in single buffer in correct order */
116 for (j=0; j < nrec; j++) {
117 strcat(buf, le[i++]);
122 ftruncate(fileno(events_file), 0L);
124 fwrite(buf, tlen, 1, events_file); /* write last NLE records to file */
125 stat = 1; /* we truncated it */
133 for (i=0; i<NLE; i++)
139 #endif /* IMPLEMENTED */
145 extern UPSINFO myUPS;
149 * Fill the Events list box with the last events
152 void FillEventsBox(HWND hwnd, int idlist)
158 if (!shm_OK || myUPS.eventfile[0] == 0 ||
159 (events_file = fopen(myUPS.eventfile, "r")) == NULL) {
160 SendDlgItemMessage(hwnd, idlist, LB_ADDSTRING, 0,
161 (LONG)"Events not available");
165 while (fgets(buf, sizeof(buf), events_file) != NULL) {
167 /* strip trailing cr/lfs */
168 while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r'))
170 SendDlgItemMessage(hwnd, idlist, LB_ADDSTRING, 0, (LONG)buf);
175 #endif /* HAVE_CYGWIN */