Usability, size modifiers to log rotate: 100k:5, 1G:3, and 100M:5

This applies to both the command line '-b SIZE' option and the
optional per log file setting.  Modifiers supported are: k, M, G

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2018-09-25 17:14:10 +02:00
parent 98f6f3ae3d
commit ea24aa5378
3 changed files with 49 additions and 8 deletions

View File

@ -116,6 +116,9 @@ per log file, see
.BR syslog.conf (5) .BR syslog.conf (5)
for details. for details.
The size argument takes optional modifiers; k, M, G. E.g., 100M is
100MB, 42k is 42 kB, etc.
Default: disabled (0). Default: disabled (0).
.TP .TP
.BI "\-c " "count" .BI "\-c " "count"

View File

@ -336,15 +336,16 @@ and store them in the file
.fi .fi
.LP .LP
The following is almost the same but will also log rotate and compress The following is almost the same but will also log rotate and compress
aged out messages. Notice the leading '-' to ensure the file is flushed aged out messages. The size argument takes the same modifiers as the
to disk after each message. command line '-b' option. Notice the leading '-' to ensure the file is
flushed to disk after each message.
.IP .IP
.nf .nf
# Log all messages, including kernel, to messages file # Log all messages, including kernel, to messages file
# rotated every 100 kiB and keep up to 10 aged out and # rotated every 100 kB and keep up to 10 aged out and
# compressed files. # compressed files.
*.*;kern,kern.none -/log/messages 1048576:10 *.*;kern,kern.none -/log/messages 100k:10
.fi .fi
.LP .LP

View File

@ -842,6 +842,7 @@ void die(int sig);
void doexit(int sig); void doexit(int sig);
#endif #endif
void init(); void init();
static int strtobytes(char *arg);
void cfline(char *line, struct filed *f); void cfline(char *line, struct filed *f);
int decode(char *name, struct code *codetab); int decode(char *name, struct code *codetab);
static void logit(char *, ...); static void logit(char *, ...);
@ -930,7 +931,7 @@ int main(int argc, char *argv[])
break; break;
case 'b': /* Max file size (bytes) before rotating log file. */ case 'b': /* Max file size (bytes) before rotating log file. */
RotateSz = atoi(optarg); RotateSz = strtobytes(optarg);
break; break;
case 'c': /* Number (count) of log files to keep. */ case 'c': /* Number (count) of log files to keep. */
@ -2744,6 +2745,35 @@ void init(void)
} /* balance parentheses for emacs */ } /* balance parentheses for emacs */
#endif #endif
static int strtobytes(char *arg)
{
int mod = 0, bytes;
size_t pos;
if (!arg)
return -1;
pos = strspn(arg, "0123456789");
if (arg[pos] != 0) {
if (arg[pos] == 'G')
mod = 3;
else if (arg[pos] == 'M')
mod = 2;
else if (arg[pos] == 'k')
mod = 1;
else
return -1;
arg[pos] = 0;
}
bytes = atoi(arg);
while (mod--)
bytes *= 1000;
return bytes;
}
/* /*
* Crack a configuration file line * Crack a configuration file line
*/ */
@ -2930,16 +2960,23 @@ void cfline(char *line, struct filed *f)
case '|': case '|':
case '/': case '/':
/* Look for optional per-file rotate BYTES:COUNT */ /* Look for optional per-file rotate BYTES:COUNT */
for (q = p; !isspace(*q); q++) for (q = p; *q && !isspace(*q); q++)
; ;
if (isspace(*q)) { if (isspace(*q)) {
char *c;
int sz = 0, cnt = 0; int sz = 0, cnt = 0;
*q++ = 0; *q++ = 0;
while (*q == '\t' || *q == ' ') while (*q && isspace(*q))
q++; q++;
sscanf(q, "%d:%d", &sz, &cnt); c = strchr(q, ':');
if (c) {
*c++ = 0;
cnt = atoi(c);
}
sz = strtobytes(q);
if (sz > 0 && cnt > 0) { if (sz > 0 && cnt > 0) {
f->f_rotatecount = cnt; f->f_rotatecount = cnt;
f->f_rotatesz = sz; f->f_rotatesz = sz;