]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/events.c
Initial revision
[bacula/bacula] / bacula / src / lib / events.c
1 /*
2  *  Bacula Event File handling
3  *
4  *    Kern Sibbald, August MMI
5  *
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
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.
14
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.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28
29 #ifdef IMPLEMENTED
30 void log_event(UPSINFO *ups, int level, char *fmt, ...)
31 {
32     va_list  arg_ptr;
33     char msg[2*MAXSTRING];
34     char datetime[MAX_TIME_LENGTH];
35     int event_fd = ups->event_fd;
36
37     event_fd = ups->event_fd;
38
39     /*****FIXME***** use pool memory */
40     va_start(arg_ptr, fmt);
41     vsprintf(msg, fmt, arg_ptr);
42     va_end(arg_ptr);
43
44     syslog(level, msg);           /* log the event */
45
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) {
49         int lm;
50         time_t nowtime;
51         struct tm tm;
52
53         time(&nowtime);
54         localtime_r(&nowtime, &tm);
55         strftime(datetime, sizeof(datetime), "%a %b %d %X %Z %Y  ", &tm);
56         write(event_fd, datetime, strlen(datetime));
57         lm = strlen(msg);
58         if (msg[lm-1] != '\n') 
59            msg[lm++] = '\n';
60         write(event_fd, msg, lm);
61     }
62 }
63
64
65 #define NLE   10                      /* number of events to send and keep */
66 #define MAXLE 50                      /* truncate file when this many events */
67
68 /*
69  * If the EVENTS file exceeds MAXLE records, truncate it. 
70  *
71  * Returns:
72  *
73  *  0 if file not truncated
74  *  1 if file truncated
75  */
76 int truncate_events_file(UPSINFO *ups)
77 {
78     char *le[NLE], *buf;
79     int i, j;
80     int nrec = 0;
81     int tlen = 0;
82     int trunc = FALSE;
83     FILE *events_file;
84     int stat = 0;
85     
86     if ((events_file = fopen(ups->eventfile, "r+")) == NULL)
87         return 0;
88     for (i=0; i<NLE; i++)
89         le[i] = NULL;
90     for (i=0; i<NLE; i++)
91         if ((le[i] = malloc(MAXSTRING)) == NULL)
92             goto bailout;
93     i = 0;
94     while (fgets(le[i], MAXSTRING, events_file) != NULL) {
95         nrec++;
96         i++;
97         if (i >= NLE)                  /* wrap */
98             i = 0;
99     }
100     if (nrec > MAXLE)
101         trunc = TRUE;                  /* file too large, truncate it */
102     if (nrec > NLE) {
103         nrec = NLE;                    /* number of records to output */
104         i -= (i/NLE)*NLE;              /* first record to output */
105     } else
106         i = 0;
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)
111         goto bailout;
112     *buf = 0;
113     /* Put records in single buffer in correct order */
114     for (j=0; j < nrec; j++) {
115         strcat(buf, le[i++]); 
116         if (i >= NLE)
117             i = 0;
118     }
119     if (trunc) {
120         ftruncate(fileno(events_file), 0L);
121         rewind(events_file);
122         fwrite(buf, tlen, 1, events_file); /* write last NLE records to file */
123         stat = 1;                          /* we truncated it */
124     }
125
126     free(buf);
127
128 bailout:
129
130    fclose(events_file);
131    for (i=0; i<NLE; i++)
132        if (le[i] != NULL)
133            free(le[i]);
134    return stat;
135 }
136
137 #endif /* IMPLEMENTED */
138
139 #ifdef HAVE_CYGWIN
140
141 #include <windows.h>
142
143 extern UPSINFO myUPS;
144 extern int shm_OK;
145
146 /*  
147  * Fill the Events list box with the last events
148  * 
149  */
150 void FillEventsBox(HWND hwnd, int idlist)
151 {
152     char buf[1000];
153     int len;
154     FILE *events_file;
155     
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");
160         return;
161     }
162
163     while (fgets(buf, sizeof(buf), events_file) != NULL) {
164         len = strlen(buf);
165         /* strip trailing cr/lfs */
166         while (len > 0 && (buf[len-1] == '\n' || buf[len-1] == '\r'))
167             buf[--len] = 0;
168         SendDlgItemMessage(hwnd, idlist, LB_ADDSTRING, 0, (LONG)buf);
169     }
170     return;
171 }
172
173 #endif /* HAVE_CYGWIN */