-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging.c
167 lines (159 loc) · 3.29 KB
/
logging.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010-13 Edward Cree
See quirc.c for license information
logging: routines to log activity
*/
#include "logging.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include "bits.h"
#include "osconf.h"
int log_add_plain(FILE *logf, mtype lm, prio lq, char lp, bool ls, const char *lt, const char *ltag, time_t ts);
int log_add_symbolic(FILE *logf, mtype lm, prio lq, char lp, bool ls, const char *lt, const char *ltag, time_t ts);
void safeprint(FILE *logf, const char *text, bool escape_spaces);
int log_init(FILE *logf, logtype logt)
{
if(!logf) return(1);
time_t now=time(NULL);
switch(logt)
{
case LOGT_PLAIN:;
char stamp[40];
struct tm *td=gmtime(&now);
strftime(stamp, 40, "[%H:%M:%S]", td);
fprintf(logf, "* Started PLAIN logging at %s\n", stamp);
fflush(logf);
return(0);
case LOGT_SYMBOLIC:
fprintf(logf, "u+%lld LOGSTART\n", (signed long long)now);
fflush(logf);
return(0);
default:
return(2);
}
}
int log_add(FILE *logf, logtype logt, mtype lm, prio lq, char lp, bool ls, const char *lt, const char *ltag, time_t ts)
{
if(!logf) return(1);
int e;
switch(logt)
{
case LOGT_PLAIN:
e=log_add_plain(logf, lm, lq, lp, ls, lt, ltag, ts);
fflush(logf);
return(e);
case LOGT_SYMBOLIC:
e=log_add_symbolic(logf, lm, lq, lp, ls, lt, ltag, ts);
fflush(logf);
return(e);
default:
return(2);
}
}
int log_add_plain(FILE *logf, mtype lm, __attribute__((unused)) prio lq, char lp, __attribute__((unused)) bool ls, const char *lt, const char *ltag, time_t ts)
{
char stamp[40];
struct tm *td=gmtime(&ts);
strftime(stamp, 40, "[%H:%M:%S] ", td);
char *tag=strdup(ltag?ltag:"");
switch(lm)
{
case MSG:
{
char mk[6]="<%s> ";
if(lp)
mk[0]=mk[3]=lp;
crush(&tag, 16);
char *ntag=mktag(mk, tag);
free(tag);
tag=ntag;
}
break;
case NOTICE:
{
if(*tag)
{
crush(&tag, 16);
char *ntag=mktag("(from %s) ", tag);
free(tag);
tag=ntag;
}
}
break;
case PREFORMAT:
break;
case ACT:
{
crush(&tag, 16);
char *ntag=mktag("* %s ", tag);
free(tag);
tag=ntag;
}
break;
case QUIT_PREFORMAT:
break;
case JOIN:
case PART:
case QUIT:
case NICK:
{
crush(&tag, 16);
char *ntag=mktag("=%s= ", tag);
free(tag);
tag=ntag;
}
break;
case MODE:
break;
case STA:
free(tag);
return(0);
break;
case ERR:
break;
case UNK:
break;
case UNK_NOTICE:
if(*tag)
{
crush(&tag, 16);
char *ntag=mktag("(from %s) ", tag);
free(tag);
tag=ntag;
}
break;
case UNN:
break;
default:
break;
}
fprintf(logf, "%s%s%s\n", stamp, tag, lt);
free(tag);
return(0);
}
int log_add_symbolic(FILE *logf, mtype lm, prio lq, char lp, bool ls, const char *lt, const char *ltag, time_t ts)
{
if(!logf) return(1);
fprintf(logf, "u+"PRINTMAX" %s %s %c %c ", CASTINTMAX ts, mtype_name(lm), prio_name(lq), lp?lp:'0', ls?'>':'<');
safeprint(logf, ltag, true);
fputc(' ', logf);
safeprint(logf, lt, false);
fputc('\n', logf);
return(0);
}
void safeprint(FILE *logf, const char *text, bool es)
{
if(!logf) return;
if(!text) return;
while(*text)
{
if((!isprint(*text)) || (es&&isspace(*text)))
fprintf(logf, "\\%03o", (unsigned char)*text);
else
fputc(*text, logf);
text++;
}
}