-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmlog.c
262 lines (225 loc) · 4.25 KB
/
mmlog.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/**
* Log rotation tool
*
* Author Rui Liu
*
* Syntax:
* <cmd> | mmlog
* Or:
* mmlog <log-message>
*
* Environment Varibles:
* MM_LOGFILE_NUM: Number of log files to rotate. Default is 10.
* MM_LOGFILE: Name of the log file including file path. Default is "mmlog"
* MM_LOGSIZE: Max size of one log file. Default is 10M.
* MM_LOG_PREFIX: Prefix of log record. Default is no prefix, "timestamp" means time stamp.
*/
#define MM_LOGFILE_NUM "MM_LOGFILE_NUM"
#define MM_LOGFILE "MM_LOGFILE"
#define MM_LOGSIZE "MM_LOGSIZE"
#define MM_LOG_PREFIX "MM_LOG_PREFIX"
#define DEFAULT_FILE_NUM 10
#define DEFAULT_FILE_NAME "mmlog"
#define DEFAULT_FILE_SIZE 10000000L
#define PREFIX_TIMESTAMP "timestamp"
#define PREFIX_TYPE_TIMESTAMP 1
int LogFileNum = DEFAULT_FILE_NUM;
char *LogFile0 = DEFAULT_FILE_NAME;
char **LogFile;
FILE *LogFp = 0;
long LogMaxSize = DEFAULT_FILE_SIZE;
char LogTimeStr[40];
int PrefixType = 0;
/*
*/
void toEnd(int code, char *msg, char *msg1)
{
fprintf(stderr, "Quit: %d - %s %s\n", code, msg, msg1);
exit(code);
}
/*
*/
void init()
{
char *p;
int n1, i;
p = getenv(MM_LOGFILE_NUM);
if (p != 0)
{
LogFileNum = atoi(p);
if (LogFileNum < 1)
LogFileNum = 1;
}
LogFile = (char **)calloc(LogFileNum, sizeof(char *));
p = getenv(MM_LOGFILE);
if (p != 0)
{
LogFile0 = p;
}
p = getenv(MM_LOGSIZE);
if (p != 0)
{
LogMaxSize = atol(p);
if (LogMaxSize < 1000)
LogMaxSize = 1000;
}
n1 = strlen(LogFile0) + 10;
for (i = 0; i < LogFileNum; i++)
{
LogFile[i] = malloc(n1);
if (!LogFile[i])
{
toEnd(-11, "Cannot allocate memory:", "for file names");
}
if (!i)
{
sprintf(LogFile[0], "%s.log", LogFile0);
}
else
{
sprintf(LogFile[i], "%s%d.log", LogFile0, i);
}
}
p = getenv(MM_LOG_PREFIX);
if (p != 0)
{
if (!strcmp(p, PREFIX_TIMESTAMP))
{
PrefixType = PREFIX_TYPE_TIMESTAMP;
}
}
}
/*
*/
void toClose()
{
int i;
if (LogFp)
{
fclose(LogFp);
LogFp = 0;
}
for (i = 0; i < LogFileNum; i++)
{
free(LogFile[i]);
}
}
/*
*/
long chkfilestat()
{
int i;
if (!LogFp)
{
LogFp = fopen(LogFile[0], "a");
if (!LogFp)
{
toEnd(-2, "Error open file:", LogFile[0]);
}
}
return ftell(LogFp);
}
/*
*/
void rotate()
{
int i;
fclose(LogFp);
remove(LogFile[LogFileNum - 1]);
for (i = LogFileNum - 1; i > 0; i--)
{
rename(LogFile[i - 1], LogFile[i]);
}
LogFp = fopen(LogFile[0], "a");
if (!LogFp)
{
toEnd(-3, "Error open file:", LogFile[0]);
}
}
/*
*/
char *getTimeString()
{
time_t times = time(NULL);
struct tm *utcTime = gmtime(×);
sprintf(LogTimeStr, "%04d-%02d-%02dT%02d:%02d:%02dZ", utcTime->tm_year + 1900, utcTime->tm_mon + 1, utcTime->tm_mday, utcTime->tm_hour, utcTime->tm_min, utcTime->tm_sec);
return LogTimeStr;
}
/*
*/
char *getPrefix()
{
if (PrefixType == PREFIX_TYPE_TIMESTAMP)
{
return getTimeString();
}
else
{
return 0;
}
}
/*
*/
void appendMsg(char *logmsg)
{
char *prefix = getPrefix();
if (prefix)
{
fwrite(prefix, 1, strlen(prefix), LogFp);
fwrite(" ", 1, 1, LogFp);
}
fwrite(logmsg, 1, strlen(logmsg), LogFp);
fwrite("\n", 1, 1, LogFp);
fflush(LogFp);
}
/*
*/
int main(int argc, char *argv[])
{
char *logmsg;
char *buf;
int n;
init();
if (argc > 2)
{
fprintf(stderr, "Syntax:\n %s [<log-message>]\n", argv[0]);
toEnd(-1, "arguments number must not greater than 2 @", getTimeString());
}
if (argc == 2)
{
logmsg = argv[1];
/*printf("Got message: %s\n", logmsg);*/
if (chkfilestat() > LogMaxSize)
rotate();
if (logmsg)
appendMsg(logmsg);
}
else
{
buf = malloc(65536);
if (!buf)
{
toEnd(-10, "Cannot allocate memory:", "Reading message from stdio");
}
do
{
logmsg = fgets(buf, 65536, stdin);
if (!logmsg)
break;
n = strlen(logmsg);
if (n > 0 && logmsg[n - 1] == '\n')
logmsg[n - 1] = 0;
if (chkfilestat() > LogMaxSize)
rotate();
appendMsg(logmsg);
} while (1);
free(buf);
}
toClose();
toEnd(0, "Exit normally @", getTimeString());
}