Use gettimeofday(2) instead of time(2) to get log time in milliseconds.
[egate.git] / res+log / gw-log.c
1 /*
2  * Implementation of logging facilities.
3  *
4  * Europagate, 1994-1995.
5  *
6  * $Log: gw-log.c,v $
7  * Revision 1.7  1995/04/10 13:20:25  adam
8  * Use gettimeofday(2) instead of time(2) to get log time in milliseconds.
9  *
10  * Revision 1.6  1995/03/28  08:01:51  adam
11  * Bug fix: GW_LOG_ERRNO.
12  *
13  * Revision 1.5  1995/03/27  12:51:10  adam
14  * New log level in use: GW_LOG_ERRNO.
15  *
16  * Revision 1.4  1995/02/23  08:32:22  adam
17  * Changed header.
18  *
19  * Revision 1.2  1995/02/17  17:06:56  adam
20  * Remove everything before '/' in app_name. Use compact date format.
21  *
22  * Revision 1.1.1.1  1995/02/09  17:27:12  adam
23  * Initial version of email gateway under CVS control.
24  *
25  * Initial:       Dec  7, 94 (Adam Dickmeiss)
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <fcntl.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <errno.h>
37
38 #include <gw-log.h>
39
40 static char *app_name = NULL;
41 static unsigned level = GW_LOG_DEFAULT;
42 static int session = 0;
43
44 struct file_mask {
45     unsigned mask;           /* level mask for this file entry */
46     int fd;                  /* file descriptor for this file */
47     char *fname;             /* name of file ("" if stdout) */
48     struct file_mask *next;  /* next file in chain */
49 };
50
51 struct file_mask *file_mask_list = NULL;
52
53 char *gw_strdup (const char *s)
54 {
55     char *n = malloc (strlen(s)+1);
56     if (n)
57         strcpy (n, s);
58     return n;
59 }
60
61 void gw_log_init (const char *app_name_a)
62 {
63     struct file_mask *list, *list1;
64     const char *cp;
65
66     if ((cp = strrchr (app_name_a, '/')))
67         app_name = gw_strdup (cp+1);
68     else
69         app_name = gw_strdup (app_name_a);
70     level = GW_LOG_DEFAULT;
71     session = 0;
72
73     /* clean up all output file masks... */
74     for (list = file_mask_list; list; list = list1)
75     {
76         if (list->fd > 2)                  /* avoid closing stdout/stderr */
77             close (list->fd);              
78         free (list->fname);
79         list1 = list->next;
80         free (list);
81     }
82     file_mask_list = NULL;
83 }
84
85 void gw_log_level (unsigned level_a)
86 {
87     level = level_a;
88 }
89
90 void gw_log_session (int session_a)
91 {
92     session = session_a;
93 }
94
95 int gw_log_file (unsigned level_a, const char *fname_a)
96 {
97     struct file_mask **listp, *new_file_mask;
98     listp = &file_mask_list;
99
100     /* go through file mask list and close files already associated */
101     /* on new level */
102     while (*listp)
103     {
104         if (!((*listp)->mask &= ~level_a)) /* any levels left on this one? */
105         {
106             if ((*listp)->fd > 2)          /* close if not stdout/stderr */
107                 close ((*listp)->fd);      
108             free ((*listp)->fname);
109             *listp = (*listp)->next;
110         }
111         listp = &(*listp)->next;
112     }
113     if (!fname_a)                          /* stderr? */
114         return 0;                          /* stderr doesn't appear on list */
115     new_file_mask = malloc (sizeof(*new_file_mask));
116     if (!new_file_mask)
117         return -1;
118     new_file_mask->mask = level_a;
119     new_file_mask->next = file_mask_list;
120     file_mask_list = new_file_mask;
121     if (!(file_mask_list->fname = gw_strdup (fname_a)))
122         return -1;
123     if (*fname_a == '\0')                  /* stdout? */
124         new_file_mask->fd = 1;            
125     else                                   /* no, open as usual */
126     {
127         new_file_mask->fd = open (fname_a, O_WRONLY|O_CREAT|O_APPEND, 0666);
128         if (new_file_mask->fd == -1)
129             return -1;
130     }
131     return 0;
132 }
133
134 int gw_log (unsigned level_a, const char *event_type, const char *format, ...)
135 {
136     static char emit_str[2048];
137     struct file_mask *list;
138     struct timeval tv;
139     va_list ap;
140     unsigned e_level = level_a & level;
141     int count;
142     int err = 0;
143     struct tm tms;
144     char *cp;
145
146     if (!e_level)                          /* any effective level(s)? */      
147         return 0;
148
149     va_start (ap, format);
150     gettimeofday (&tv, NULL);
151
152     memcpy (&tms, localtime (&tv.tv_sec), sizeof(tms));
153     sprintf (emit_str, "%s %d %02d%02d%02d%02d%02d%02d%03d %d %s ",
154              app_name, session,
155              tms.tm_year, tms.tm_mon, tms.tm_mday,
156              tms.tm_hour, tms.tm_min, tms.tm_sec,
157              (int) (tv.tv_usec/1000),
158              e_level, event_type);
159     if ((cp = strchr (emit_str, '\n')))    /* remove \n from ctime-str */
160         *cp = ' ';
161     count = strlen (emit_str);
162     vsprintf (emit_str+count, format, ap);
163     if (level_a & GW_LOG_ERRNO)
164     {
165         strcat (emit_str, ": ");
166         strcat (emit_str, strerror (errno));
167     }
168     strcat (emit_str, "\n");
169     count = strlen (emit_str);
170
171     /* go through file mask list... */
172     for (list = file_mask_list; list; list = list->next)
173         if (list->mask & e_level)          /* output this one? */
174         {
175             e_level &= ~list->mask;        /* remove from effective level */
176             if (write (list->fd, emit_str, count) != count)
177                 err = 1;
178         }
179     if (e_level)                           /* bits left on effective level? */
180         write (2, emit_str, count);
181     va_end (ap);
182     return err;
183 }