-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.c
126 lines (107 loc) · 2 KB
/
log.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
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct log {
char *prefix;
size_t maxbytes;
size_t maxcount;
size_t bytes;
size_t count;
int fd;
};
static int log_next(struct log *log)
{
char *filename;
int olderr;
if (log->fd > 0) {
close(log->fd);
log->fd = -1;
}
if (log->maxbytes > 0) {
char *s;
int guess;
int length;
int index;
index = log->count;
if (log->maxcount > 0)
index %= log->maxcount;
guess = strlen(log->prefix) + 10;
s = malloc(guess);
if (!s)
return -1;
length = snprintf(s, guess, "%s%d", log->prefix, index);
if (length >= guess) {
filename = realloc(s, length + 1);
if (!filename) {
free(s);
return -1;
}
sprintf(filename, "%s%d", log->prefix, index);
} else {
filename = s;
}
} else {
filename = strdup(log->prefix);
if (!filename)
return -1;
}
unlink(filename);
log->fd = creat(filename, 0600);
if (log->fd < 0) {
olderr = errno;
free(filename);
errno = olderr;
return -1;
}
free(filename);
log->count++;
log->bytes = 0;
return 0;
}
struct log *log_start(char *prefix, size_t maxbytes, size_t maxcount)
{
struct log *log;
log = malloc(sizeof(*log));
if (!log)
return NULL;
log->prefix = strdup(prefix);
log->maxbytes = maxbytes;
log->maxcount = maxcount;
log->bytes = 0;
log->count = 0;
log->fd = -1;
log_next(log);
return log;
}
size_t log_write(struct log *log, char *data, size_t size)
{
size_t n;
size_t total = 0;
ssize_t bytes;
while (total < size) {
n = size - total;
if (log->maxbytes > 0 && log->bytes + n > log->maxbytes)
n = log->maxbytes - log->bytes;
bytes = write(log->fd, &data[total], n);
if (bytes <= 0)
break;
total += bytes;
log->bytes += bytes;
if (log->maxbytes > 0 && log->bytes >= log->maxbytes) {
if (log_next(log) < 0)
break;
}
}
return total;
}
void log_stop(struct log *log)
{
if (log->fd > 0)
close(log->fd);
free(log->prefix);
free(log);
}