public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/2] log: Make logfile_write() private
Date: Mon, 29 Jul 2024 14:22:59 +1000	[thread overview]
Message-ID: <20240729042300.280684-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240729042300.280684-1-david@gibson.dropbear.id.au>

logfile_write() is not used outside log.c, nor should it be.  It should
only be used externall via the general logging functions.  Make it static
in log.c.  To avoid forward declarations this requires moving a bunch of
functions earlier in the file.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 log.c | 341 +++++++++++++++++++++++++++++-----------------------------
 log.h |   1 -
 2 files changed, 171 insertions(+), 171 deletions(-)

diff --git a/log.c b/log.c
index 0fb25b77..eb3a780a 100644
--- a/log.c
+++ b/log.c
@@ -55,6 +55,177 @@ bool		log_runtime;		/* Daemonised, or ready in foreground */
 	(timespec_diff_us((x), &log_start) / 1000000LL),		\
 	(timespec_diff_us((x), &log_start) / 100LL)
 
+/* Prefixes for log file messages, indexed by priority */
+const char *logfile_prefix[] = {
+	NULL, NULL, NULL,	/* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */
+	"ERROR:   ",
+	"WARNING: ",
+	NULL,			/* Unused: LOG_NOTICE */
+	"info:    ",
+	"         ",		/* LOG_DEBUG */
+};
+
+#ifdef FALLOC_FL_COLLAPSE_RANGE
+/**
+ * logfile_rotate_fallocate() - Write header, set log_written after fallocate()
+ * @fd:		Log file descriptor
+ * @now:	Current timestamp
+ *
+ * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek
+ */
+static void logfile_rotate_fallocate(int fd, const struct timespec *now)
+{
+	char buf[BUFSIZ];
+	const char *nl;
+	int n;
+
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return;
+	if (read(fd, buf, BUFSIZ) == -1)
+		return;
+
+	n =  snprintf(buf, BUFSIZ, "%s - log truncated at ", log_header);
+	n += snprintf(buf + n, BUFSIZ - n, logtime_fmt_and_arg(now));
+
+	/* Avoid partial lines by padding the header with spaces */
+	nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
+	if (nl)
+		memset(buf + n, ' ', nl - (buf + n));
+
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return;
+	if (write(fd, buf, BUFSIZ) == -1)
+		return;
+
+	log_written -= log_cut_size;
+}
+#endif /* FALLOC_FL_COLLAPSE_RANGE */
+
+/**
+ * logfile_rotate_move() - Fallback: move recent entries toward start, then cut
+ * @fd:		Log file descriptor
+ * @now:	Current timestamp
+ *
+ * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek
+ * #syscalls ftruncate
+ */
+static void logfile_rotate_move(int fd, const struct timespec *now)
+{
+	int header_len, write_offset, end, discard, n;
+	char buf[BUFSIZ];
+	const char *nl;
+
+	header_len =  snprintf(buf, BUFSIZ, "%s - log truncated at ",
+			       log_header);
+	header_len += snprintf(buf + header_len, BUFSIZ - header_len,
+			       logtime_fmt_and_arg(now));
+
+	if (lseek(fd, 0, SEEK_SET) == -1)
+		return;
+	if (write(fd, buf, header_len) == -1)
+		return;
+
+	end = write_offset = header_len;
+	discard = log_cut_size + header_len;
+
+	/* Try to cut cleanly at newline */
+	if (lseek(fd, discard, SEEK_SET) == -1)
+		goto out;
+	if ((n = read(fd, buf, BUFSIZ)) <= 0)
+		goto out;
+	if ((nl = memchr(buf, '\n', n)))
+		discard += (nl - buf) + 1;
+
+	/* Go to first block to be moved */
+	if (lseek(fd, discard, SEEK_SET) == -1)
+		goto out;
+
+	while ((n = read(fd, buf, BUFSIZ)) > 0) {
+		end = header_len;
+
+		if (lseek(fd, write_offset, SEEK_SET) == -1)
+			goto out;
+		if ((n = write(fd, buf, n)) == -1)
+			goto out;
+		write_offset += n;
+
+		if ((n = lseek(fd, 0, SEEK_CUR)) == -1)
+			goto out;
+
+		if (lseek(fd, discard - header_len, SEEK_CUR) == -1)
+			goto out;
+
+		end = n;
+	}
+
+out:
+	if (ftruncate(fd, end))
+		return;
+
+	log_written = end;
+}
+
+/**
+ * logfile_rotate() - "Rotate" log file once it's full
+ * @fd:		Log file descriptor
+ * @now:	Current timestamp
+ *
+ * Return: 0 on success, negative error code on failure
+ *
+ * #syscalls fcntl
+ *
+ * fallocate() passed as EXTRA_SYSCALL only if FALLOC_FL_COLLAPSE_RANGE is there
+ */
+static int logfile_rotate(int fd, const struct timespec *now)
+{
+	if (fcntl(fd, F_SETFL, O_RDWR /* Drop O_APPEND: explicit lseek() */))
+		return -errno;
+
+#ifdef FALLOC_FL_COLLAPSE_RANGE
+	/* Only for Linux >= 3.15, extent-based ext4 or XFS, glibc >= 2.18 */
+	if (!fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, log_cut_size))
+		logfile_rotate_fallocate(fd, now);
+	else
+#endif
+		logfile_rotate_move(fd, now);
+
+	if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND))
+		return -errno;
+
+	return 0;
+}
+
+/**
+ * logfile_write() - Write entry to log file, trigger rotation if full
+ * @newline:	Append newline at the end of the message, if missing
+ * @pri:	Facility and level map, same as priority for vsyslog()
+ * @format:	Same as vsyslog() format
+ * @ap:		Same as vsyslog() ap
+ */
+static void logfile_write(bool newline, int pri, const char *format, va_list ap)
+{
+	struct timespec now;
+	char buf[BUFSIZ];
+	int n;
+
+	if (clock_gettime(CLOCK_MONOTONIC, &now))
+		return;
+
+	n  = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(&now));
+	n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]);
+
+	n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
+
+	if (newline && format[strlen(format)] != '\n')
+		n += snprintf(buf + n, BUFSIZ - n, "\n");
+
+	if ((log_written + n >= log_size) && logfile_rotate(log_file, &now))
+		return;
+
+	if ((n = write(log_file, buf, n)) >= 0)
+		log_written += n;
+}
+
 /**
  * vlogmsg() - Print or send messages to log or output files as configured
  * @newline:	Append newline at the end of the message, if missing
@@ -125,16 +296,6 @@ void logmsg_perror(int pri, const char *format, ...)
 	logmsg(true, pri, ": %s", strerror(errno_copy));
 }
 
-/* Prefixes for log file messages, indexed by priority */
-const char *logfile_prefix[] = {
-	NULL, NULL, NULL,	/* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */
-	"ERROR:   ",
-	"WARNING: ",
-	NULL,			/* Unused: LOG_NOTICE */
-	"info:    ",
-	"         ",		/* LOG_DEBUG */
-};
-
 /**
  * trace_init() - Set log_trace depending on trace (debug) mode
  * @enable:	Tracing debug mode enabled if non-zero
@@ -239,163 +400,3 @@ void logfile_init(const char *name, const char *path, size_t size)
 	log_cut_size = ROUND_UP(log_size * LOGFILE_CUT_RATIO / 100, PAGE_SIZE);
 }
 
-#ifdef FALLOC_FL_COLLAPSE_RANGE
-/**
- * logfile_rotate_fallocate() - Write header, set log_written after fallocate()
- * @fd:		Log file descriptor
- * @now:	Current timestamp
- *
- * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek
- */
-static void logfile_rotate_fallocate(int fd, const struct timespec *now)
-{
-	char buf[BUFSIZ];
-	const char *nl;
-	int n;
-
-	if (lseek(fd, 0, SEEK_SET) == -1)
-		return;
-	if (read(fd, buf, BUFSIZ) == -1)
-		return;
-
-	n =  snprintf(buf, BUFSIZ, "%s - log truncated at ", log_header);
-	n += snprintf(buf + n, BUFSIZ - n, logtime_fmt_and_arg(now));
-
-	/* Avoid partial lines by padding the header with spaces */
-	nl = memchr(buf + n + 1, '\n', BUFSIZ - n - 1);
-	if (nl)
-		memset(buf + n, ' ', nl - (buf + n));
-
-	if (lseek(fd, 0, SEEK_SET) == -1)
-		return;
-	if (write(fd, buf, BUFSIZ) == -1)
-		return;
-
-	log_written -= log_cut_size;
-}
-#endif /* FALLOC_FL_COLLAPSE_RANGE */
-
-/**
- * logfile_rotate_move() - Fallback: move recent entries toward start, then cut
- * @fd:		Log file descriptor
- * @now:	Current timestamp
- *
- * #syscalls lseek ppc64le:_llseek ppc64:_llseek arm:_llseek
- * #syscalls ftruncate
- */
-static void logfile_rotate_move(int fd, const struct timespec *now)
-{
-	int header_len, write_offset, end, discard, n;
-	char buf[BUFSIZ];
-	const char *nl;
-
-	header_len =  snprintf(buf, BUFSIZ, "%s - log truncated at ",
-			       log_header);
-	header_len += snprintf(buf + header_len, BUFSIZ - header_len,
-			       logtime_fmt_and_arg(now));
-
-	if (lseek(fd, 0, SEEK_SET) == -1)
-		return;
-	if (write(fd, buf, header_len) == -1)
-		return;
-
-	end = write_offset = header_len;
-	discard = log_cut_size + header_len;
-
-	/* Try to cut cleanly at newline */
-	if (lseek(fd, discard, SEEK_SET) == -1)
-		goto out;
-	if ((n = read(fd, buf, BUFSIZ)) <= 0)
-		goto out;
-	if ((nl = memchr(buf, '\n', n)))
-		discard += (nl - buf) + 1;
-
-	/* Go to first block to be moved */
-	if (lseek(fd, discard, SEEK_SET) == -1)
-		goto out;
-
-	while ((n = read(fd, buf, BUFSIZ)) > 0) {
-		end = header_len;
-
-		if (lseek(fd, write_offset, SEEK_SET) == -1)
-			goto out;
-		if ((n = write(fd, buf, n)) == -1)
-			goto out;
-		write_offset += n;
-
-		if ((n = lseek(fd, 0, SEEK_CUR)) == -1)
-			goto out;
-
-		if (lseek(fd, discard - header_len, SEEK_CUR) == -1)
-			goto out;
-
-		end = n;
-	}
-
-out:
-	if (ftruncate(fd, end))
-		return;
-
-	log_written = end;
-}
-
-/**
- * logfile_rotate() - "Rotate" log file once it's full
- * @fd:		Log file descriptor
- * @now:	Current timestamp
- *
- * Return: 0 on success, negative error code on failure
- *
- * #syscalls fcntl
- *
- * fallocate() passed as EXTRA_SYSCALL only if FALLOC_FL_COLLAPSE_RANGE is there
- */
-static int logfile_rotate(int fd, const struct timespec *now)
-{
-	if (fcntl(fd, F_SETFL, O_RDWR /* Drop O_APPEND: explicit lseek() */))
-		return -errno;
-
-#ifdef FALLOC_FL_COLLAPSE_RANGE
-	/* Only for Linux >= 3.15, extent-based ext4 or XFS, glibc >= 2.18 */
-	if (!fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, log_cut_size))
-		logfile_rotate_fallocate(fd, now);
-	else
-#endif
-		logfile_rotate_move(fd, now);
-
-	if (fcntl(fd, F_SETFL, O_RDWR | O_APPEND))
-		return -errno;
-
-	return 0;
-}
-
-/**
- * logfile_write() - Write entry to log file, trigger rotation if full
- * @newline:	Append newline at the end of the message, if missing
- * @pri:	Facility and level map, same as priority for vsyslog()
- * @format:	Same as vsyslog() format
- * @ap:		Same as vsyslog() ap
- */
-void logfile_write(bool newline, int pri, const char *format, va_list ap)
-{
-	struct timespec now;
-	char buf[BUFSIZ];
-	int n;
-
-	if (clock_gettime(CLOCK_MONOTONIC, &now))
-		return;
-
-	n  = snprintf(buf, BUFSIZ, logtime_fmt_and_arg(&now));
-	n += snprintf(buf + n, BUFSIZ - n, ": %s", logfile_prefix[pri]);
-
-	n += vsnprintf(buf + n, BUFSIZ - n, format, ap);
-
-	if (newline && format[strlen(format)] != '\n')
-		n += snprintf(buf + n, BUFSIZ - n, "\n");
-
-	if ((log_written + n >= log_size) && logfile_rotate(log_file, &now))
-		return;
-
-	if ((n = write(log_file, buf, n)) >= 0)
-		log_written += n;
-}
diff --git a/log.h b/log.h
index e03199c6..5cb16d6f 100644
--- a/log.h
+++ b/log.h
@@ -56,7 +56,6 @@ void trace_init(int enable);
 void __openlog(const char *ident, int option, int facility);
 void logfile_init(const char *name, const char *path, size_t size);
 void passt_vsyslog(bool newline, int pri, const char *format, va_list ap);
-void logfile_write(bool newline, int pri, const char *format, va_list ap);
 void __setlogmask(int mask);
 
 #endif /* LOG_H */
-- 
@@ -56,7 +56,6 @@ void trace_init(int enable);
 void __openlog(const char *ident, int option, int facility);
 void logfile_init(const char *name, const char *path, size_t size);
 void passt_vsyslog(bool newline, int pri, const char *format, va_list ap);
-void logfile_write(bool newline, int pri, const char *format, va_list ap);
 void __setlogmask(int mask);
 
 #endif /* LOG_H */
-- 
2.45.2


  reply	other threads:[~2024-07-29  4:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-29  4:22 [PATCH 0/2] Avoid duplicated clock_gettime() in logging David Gibson
2024-07-29  4:22 ` David Gibson [this message]
2024-08-05 19:02   ` [PATCH 1/2] log: Make logfile_write() private Stefano Brivio
2024-07-29  4:23 ` [PATCH 2/2] log: Avoid duplicate calls to clock_gettime() David Gibson
2024-07-29 17:55   ` Stefano Brivio
2024-08-03  7:53     ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240729042300.280684-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).