2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
11 * Permission to use, copy, modify, distribute, and sell this software and
12 * its documentation, in whole or in part, for any purpose, is hereby granted,
15 * 1. This copyright and permission notice appear in all copies of the
16 * software and its documentation. Notices of copyright or attribution
17 * which appear at the beginning of any file must remain unchanged.
19 * 2. The names of EUROPAGATE or the project partners may not be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * 3. Users of this software (implementors and gateway operators) agree to
24 * inform the EUROPAGATE consortium of their use of the software. This
25 * information will be used to evaluate the EUROPAGATE project and the
26 * software, and to plan further developments. The consortium may use
27 * the information in later publications.
29 * 4. Users of this software agree to make their best efforts, when
30 * documenting their use of the software, to acknowledge the EUROPAGATE
31 * consortium, and the role played by the software in their work.
33 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34 * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36 * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37 * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38 * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39 * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40 * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41 * USE OR PERFORMANCE OF THIS SOFTWARE.
45 * Implementation of logging facilities.
47 * Europagate, 1994-1995.
50 * Revision 1.12 1995/12/01 12:24:17 adam
51 * New function: gw_log_mask_str.
53 * Revision 1.11 1995/11/09 09:54:28 adam
54 * More readable logging format.
56 * Revision 1.10 1995/05/16 09:40:48 adam
59 * Revision 1.9 1995/04/19 12:12:06 adam
60 * Resource system uses only one log debug level.
62 * Revision 1.8 1995/04/17 09:36:16 adam
65 * Revision 1.7 1995/04/10 13:20:25 adam
66 * Use gettimeofday(2) instead of time(2) to get log time in milliseconds.
68 * Revision 1.6 1995/03/28 08:01:51 adam
69 * Bug fix: GW_LOG_ERRNO.
71 * Revision 1.5 1995/03/27 12:51:10 adam
72 * New log level in use: GW_LOG_ERRNO.
74 * Revision 1.4 1995/02/23 08:32:22 adam
77 * Revision 1.2 1995/02/17 17:06:56 adam
78 * Remove everything before '/' in app_name. Use compact date format.
80 * Revision 1.1.1.1 1995/02/09 17:27:12 adam
81 * Initial version of email gateway under CVS control.
83 * Initial: Dec 7, 94 (Adam Dickmeiss)
99 static char *app_name = NULL;
100 static unsigned level = GW_LOG_DEFAULT;
101 static int session = 0;
104 unsigned mask; /* level mask for this file entry */
105 int fd; /* file descriptor for this file */
106 char *fname; /* name of file ("" if stdout) */
107 struct file_mask *next; /* next file in chain */
110 struct file_mask *file_mask_list = NULL;
112 char *gw_strdup (const char *s)
114 char *n = malloc (strlen(s)+1);
120 void gw_log_init (const char *app_name_a)
122 struct file_mask *list, *list1;
125 if ((cp = strrchr (app_name_a, '/')))
126 app_name = gw_strdup (cp+1);
128 app_name = gw_strdup (app_name_a);
129 level = GW_LOG_DEFAULT;
132 /* clean up all output file masks... */
133 for (list = file_mask_list; list; list = list1)
135 if (list->fd > 2) /* avoid closing stdout/stderr */
141 file_mask_list = NULL;
144 void gw_log_level (unsigned level_a)
149 void gw_log_session (int session_a)
154 int gw_log_file (unsigned level_a, const char *fname_a)
156 struct file_mask **listp, *new_file_mask;
157 listp = &file_mask_list;
159 /* go through file mask list and close files already associated */
163 if (!((*listp)->mask &= ~level_a)) /* any levels left on this one? */
165 if ((*listp)->fd > 2) /* close if not stdout/stderr */
166 close ((*listp)->fd);
167 free ((*listp)->fname);
168 *listp = (*listp)->next;
170 listp = &(*listp)->next;
172 if (!fname_a) /* stderr? */
173 return 0; /* stderr doesn't appear on list */
174 new_file_mask = malloc (sizeof(*new_file_mask));
177 new_file_mask->mask = level_a;
178 new_file_mask->next = file_mask_list;
179 file_mask_list = new_file_mask;
180 if (!(file_mask_list->fname = gw_strdup (fname_a)))
182 if (*fname_a == '\0') /* stdout? */
183 new_file_mask->fd = 1;
184 else /* no, open as usual */
186 new_file_mask->fd = open (fname_a, O_WRONLY|O_CREAT|O_APPEND, 0666);
187 if (new_file_mask->fd == -1)
193 int gw_log (unsigned level_a, const char *event_type, const char *format, ...)
195 static char emit_str[4096];
196 struct file_mask *list;
200 unsigned e_level = level_a & level;
206 if (!e_level) /* any effective level(s)? */
209 va_start (ap, format);
210 gettimeofday (&tv, &tz);
212 memcpy (&tms, localtime (&tv.tv_sec), sizeof(tms));
213 sprintf (emit_str, "%s %d %02d%02d%02d %02d%02d%02d %03d %d %s ",
215 tms.tm_year, 1+tms.tm_mon, tms.tm_mday,
216 tms.tm_hour, tms.tm_min, tms.tm_sec,
217 (int) (tv.tv_usec/1000),
218 e_level, event_type);
219 if ((cp = strchr (emit_str, '\n'))) /* remove \n from ctime-str */
221 count = strlen (emit_str);
222 vsprintf (emit_str+count, format, ap);
223 if (level_a & GW_LOG_ERRNO)
225 strcat (emit_str, ": ");
226 strcat (emit_str, strerror (errno));
228 strcat (emit_str, "\n");
229 count = strlen (emit_str);
231 /* go through file mask list... */
232 for (list = file_mask_list; list; list = list->next)
233 if (list->mask & e_level) /* output this one? */
235 e_level &= ~list->mask; /* remove from effective level */
236 if (write (list->fd, emit_str, count) != count)
239 if (e_level) /* bits left on effective level? */
240 write (2, emit_str, count);
250 { GW_LOG_ALL, "all" },
251 { GW_LOG_DEFAULT, "default"},
252 { GW_LOG_DEFAULT, "def" },
253 { GW_LOG_FATAL, "fatal" },
254 { GW_LOG_WARN, "warn" },
255 { GW_LOG_ACCT, "acct" },
256 { GW_LOG_STAT, "stat" },
257 { GW_LOG_DEBUG, "debug" },
258 { GW_LOG_DEBUGN(0), "debug0" },
259 { GW_LOG_DEBUGN(1), "debug1" },
260 { GW_LOG_DEBUGN(2), "debug2" },
261 { GW_LOG_DEBUGN(3), "debug3" },
262 { GW_LOG_DEBUGN(4), "debug4" },
263 { GW_LOG_DEBUGN(5), "debug5" },
264 { GW_LOG_DEBUGN(6), "debug6" },
265 { GW_LOG_DEBUGN(7), "debug7" },
266 { GW_LOG_DEBUGN(8), "debug8" },
267 { GW_LOG_DEBUGN(8), "debug9" },
272 unsigned gw_log_mask_str (const char *str)
276 unsigned level = GW_LOG_DEFAULT;
280 for (p = str; *p && *p != ','; p++)
282 if (*str == '-' || isdigit(*str))
285 for (i = 0; mask_names[i].name; i++)
286 if (strlen (mask_names[i].name) == p-str &&
287 memcmp (mask_names[i].name, str, p-str) == 0)
289 if (mask_names[i].mask)
290 level |= mask_names[i].mask;