Commit | Line | Data |
---|---|---|
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 |
13 | static substdio ss; |
14 | static char buf[1]; | |
15 | static char num[FMT_ULONG]; | |
16 | static stralloc line = {0}; | |
f8beb284 | 17 | static stralloc fn = {0}; |
5b62e993 | 18 | |
f8beb284 MW |
19 | void log(dir,event,addr,comment) |
20 | char *dir; | |
5b62e993 MW |
21 | char *event; |
22 | char *addr; | |
f8beb284 | 23 | char *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 | } |