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