Import ezmlm-idx 0.40
[ezmlm] / log.c
1 #include "substdio.h"
2 #include "readwrite.h"
3 #include "stralloc.h"
4 #include "log.h"
5 #include "now.h"
6 #include "fmt.h"
7 #include "open.h"
8
9 /* appends (not crash-proof) a line to "Log". The format is: */
10 /* "timestamp event address[ comment]\n". address is free of ' ' */
11 /* Unprintable chars are changed to '?'. Comment may have spaces */
12
13 static substdio ss;
14 static char buf[1];
15 static char num[FMT_ULONG];
16 static stralloc line = {0};
17 static stralloc fn = {0};
18
19 void log(dir,event,addr,comment)
20 char *dir;
21 char *event;
22 char *addr;
23 char *comment;
24 {
25 char ch;
26 int fd;
27
28 if (!stralloc_copyb(&line,num,fmt_ulong(num,(unsigned long) now()))) return;
29 if (!stralloc_cats(&line," ")) return;
30 if (!stralloc_cats(&line,event)) return;
31 if (!stralloc_cats(&line," ")) return;
32 while (ch = *addr++) {
33 if ((ch < 33) || (ch > 126)) ch = '?';
34 if (!stralloc_append(&line,&ch)) return;
35 }
36 if (comment && *comment) {
37 if (!stralloc_cats(&line," ")) return;
38 while (ch = *comment++) {
39 if (ch == '\t')
40 ch = ' ';
41 else
42 if ((ch < 32) || (ch > 126)) ch = '?';
43 if (!stralloc_append(&line,&ch)) return;
44 }
45 }
46 if (!stralloc_cats(&line,"\n")) return;
47
48 if (!stralloc_copys(&fn,dir)) return;
49 if (!stralloc_cats(&fn,"/Log")) return;
50 if (!stralloc_0(&fn)) return;
51 fd = open_append(fn.s);
52 if (fd == -1) return;
53 substdio_fdbuf(&ss,write,fd,buf,sizeof(buf));
54 substdio_putflush(&ss,line.s,line.len);
55 close(fd);
56 return;
57 }