Import ezmlm-idx 0.40
[ezmlm] / log.c
CommitLineData
5b62e993
MW
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
f8beb284
MW
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
5b62e993
MW
13static substdio ss;
14static char buf[1];
15static char num[FMT_ULONG];
16static stralloc line = {0};
f8beb284 17static stralloc fn = {0};
5b62e993 18
f8beb284
MW
19void log(dir,event,addr,comment)
20char *dir;
5b62e993
MW
21char *event;
22char *addr;
f8beb284 23char *comment;
5b62e993
MW
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 }
f8beb284
MW
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 }
5b62e993
MW
46 if (!stralloc_cats(&line,"\n")) return;
47
f8beb284
MW
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);
5b62e993
MW
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}