Refactor and drop '-n' option for rotation

The '-n' option is commonly used for remote servers, so let's
consolidate the log rotation under the '-r' flag instead, with
the same syntax as previously established in syslog.conf

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2019-10-25 15:52:14 +02:00
parent 76b6981467
commit bdb1d45f90
2 changed files with 83 additions and 34 deletions

View File

@ -39,6 +39,7 @@
#include <syslog.h>
#include <unistd.h>
#include <sys/stat.h>
#include "compat.h"
static const char version_info[] = PACKAGE_NAME " v" PACKAGE_VERSION;
@ -123,6 +124,19 @@ static int checksz(FILE *fp, off_t sz)
return 0;
}
static int logit(int level, char *buf, size_t len)
{
if (buf[0]) {
syslog(level, "%s", buf);
return 0;
}
while ((fgets(buf, len, stdin)))
syslog(level, "%s", buf);
return 0;
}
static int flogit(char *logfile, int num, off_t sz, char *buf, size_t len)
{
FILE *fp;
@ -155,19 +169,6 @@ reopen:
return fclose(fp);
}
static int logit(int level, char *buf, size_t len)
{
if (buf[0]) {
syslog(level, "%s", buf);
return 0;
}
while ((fgets(buf, len, stdin)))
syslog(level, "%s", buf);
return 0;
}
static int parse_prio(char *arg, int *f, int *l)
{
char *ptr;
@ -202,14 +203,14 @@ static int usage(int code)
"\n"
"Write MESSAGE (or stdin) to syslog, or file (with logrotate)\n"
"\n"
" -? This help text\n"
" -p PRIO Priority (numeric or facility.level pair)\n"
" -p PRIO Log message priority (numeric or facility.level pair)\n"
" -t TAG Log using the specified tag (defaults to user name)\n"
" -s Log to stderr as well as the system log\n"
"\n"
" -f FILE File to write log messages to, instead of syslog\n"
" -n SIZE Number of bytes before rotating, default: 200 kB\n"
" -r NUM Number of rotated files to keep, default: 5\n"
" -f FILE Log file to write messages to, instead of syslog daemon\n"
" -r S:R Log file rotation, default: 200 kB max \e[4ms\e[0mize, 5 \e[4mr\e[0motations\n"
"\n"
" -? This help text\n"
" -v Show program version\n"
"\n"
"This version of logger is distributed as part of sysklogd.\n"
@ -218,6 +219,26 @@ static int usage(int code)
return code;
}
static void parse_rotation(char *optarg, off_t *size, int *num)
{
char buf[100];
char *c;
int sz = 0, cnt = 0;
strlcpy(buf, optarg, sizeof(buf));
c = strchr(buf, ':');
if (c) {
*c++ = 0;
cnt = atoi(c);
}
sz = strtobytes(buf);
if (sz > 0)
*size = sz;
if (cnt)
*num = cnt;
}
int main(int argc, char *argv[])
{
int c, rc, num = 5;
@ -228,23 +249,19 @@ int main(int argc, char *argv[])
char *ident = NULL, *logfile = NULL;
char buf[512] = "";
while ((c = getopt(argc, argv, "?f:n:p:r:st:v")) != EOF) {
while ((c = getopt(argc, argv, "?f:p:r:st:v")) != EOF) {
switch (c) {
case 'f':
logfile = optarg;
break;
case 'n':
size = atoi(optarg);
break;
case 'p':
if (parse_prio(optarg, &facility, &level))
return usage(1);
break;
case 'r':
num = atoi(optarg);
parse_rotation(optarg, &size, &num);
break;
case 's':