diff mbox

[5/6] xenconsoled: options to control log rotation

Message ID 1465228781-22754-6-git-send-email-wei.liu2@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Wei Liu June 6, 2016, 3:59 p.m. UTC
Provide two options: one to control the maximum number of copies kept
and the other to control the maximum length of each log file.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/console/daemon/logfile.c | 11 +++++++----
 tools/console/daemon/main.c    | 13 ++++++++++++-
 2 files changed, 19 insertions(+), 5 deletions(-)

Comments

Ian Jackson July 8, 2016, 5:02 p.m. UTC | #1
Wei Liu writes ("[PATCH 5/6] xenconsoled: options to control log rotation"):
> Provide two options: one to control the maximum number of copies kept
> and the other to control the maximum length of each log file.

Ah, OK, good.  However, as I said, I think this makes the rotation
loop wrong because restarting xenconsoled to change the log level can
leave old logfiles ever unrotated.

Ian.
diff mbox

Patch

diff --git a/tools/console/daemon/logfile.c b/tools/console/daemon/logfile.c
index ad73f8e..3b95f84 100644
--- a/tools/console/daemon/logfile.c
+++ b/tools/console/daemon/logfile.c
@@ -25,6 +25,9 @@ 
 #include "utils.h"
 #include "logfile.h"
 
+extern unsigned int log_backups;
+extern unsigned int log_max_len;
+
 static void logfile_entry_free(struct logfile_entry *entry)
 {
 	if (!entry) return;
@@ -91,7 +94,7 @@  struct logfile *logfile_new(const char *path, mode_t mode)
 	}
 
 	logfile->mode = mode;
-	logfile->maxlen = LOGFILE_MAX_SIZE;
+	logfile->maxlen = log_max_len;
 
 
 	logfile->entry = logfile_entry_new(logfile->basepath, mode);
@@ -112,13 +115,13 @@  static int logfile_rollover(struct logfile *file)
 	unsigned i;
 
 	if (asprintf(&next, "%s.%u", file->basepath,
-		     LOGFILE_MAX_BACKUP_NUM) < 0) {
+		     log_backups) < 0) {
 		dolog(LOG_ERR, "Failed to asprintf %s.%u",
-		      file->basepath, LOGFILE_MAX_BACKUP_NUM);
+		      file->basepath, log_backups);
 		goto err;
 	}
 
-	for (i = LOGFILE_MAX_BACKUP_NUM; i > 0; i--) {
+	for (i = log_backups; i > 0; i--) {
 		if (i == 1) {
 			this = strdup(file->basepath);
 			if (!this) {
diff --git a/tools/console/daemon/main.c b/tools/console/daemon/main.c
index 23860d3..dd338ae 100644
--- a/tools/console/daemon/main.c
+++ b/tools/console/daemon/main.c
@@ -31,6 +31,7 @@ 
 
 #include "utils.h"
 #include "io.h"
+#include "logfile.h"
 
 int log_reload = 0;
 int log_guest = 0;
@@ -39,6 +40,8 @@  int log_time_hv = 0;
 int log_time_guest = 0;
 char *log_dir = NULL;
 int discard_overflowed_data = 1;
+unsigned int log_backups = LOGFILE_MAX_BACKUP_NUM;
+unsigned int log_max_len = LOGFILE_MAX_SIZE;
 
 static void handle_hup(int sig)
 {
@@ -47,7 +50,7 @@  static void handle_hup(int sig)
 
 static void usage(char *name)
 {
-	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep]\n", name);
+	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [--log-backups=NUM] [--log-max-len=NUM] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep]\n", name);
 }
 
 static void version(char *name)
@@ -100,6 +103,8 @@  int main(int argc, char **argv)
 		{ "interactive", 0, 0, 'i' },
 		{ "log", 1, 0, 'l' },
 		{ "log-dir", 1, 0, 'r' },
+		{ "log-backups", 1, 0, 'n' },
+		{ "log-max-len", 1, 0, 'm' },
 		{ "pid-file", 1, 0, 'p' },
 		{ "timestamp", 1, 0, 't' },
 		{ "overflow-data", 1, 0, 'o'},
@@ -144,6 +149,12 @@  int main(int argc, char **argv)
 		case 'r':
 		        log_dir = strdup(optarg);
 			break;
+		case 'n':
+			log_backups = strtoul(optarg, 0, 0);
+			break;
+		case 'm':
+			log_max_len = strtoul(optarg, 0, 0);
+			break;
 		case 'p':
 		        pidfile = strdup(optarg);
 			break;