2022-03-11 20:54:58

by Thiago Becker

[permalink] [raw]
Subject: [RFC v2 PATCH 3/7] readahead: create logging facility

Create logs for nfs-readahead-udev, logging to syslog.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1946283
Signed-off-by: Thiago Becker <[email protected]>
---
tools/nfs-readahead-udev/Makefile.am | 2 +-
tools/nfs-readahead-udev/log.h | 16 ++++++++++
tools/nfs-readahead-udev/main.c | 8 +++++
tools/nfs-readahead-udev/syslog.c | 47 ++++++++++++++++++++++++++++
4 files changed, 72 insertions(+), 1 deletion(-)
create mode 100644 tools/nfs-readahead-udev/log.h
create mode 100644 tools/nfs-readahead-udev/syslog.c

diff --git a/tools/nfs-readahead-udev/Makefile.am b/tools/nfs-readahead-udev/Makefile.am
index 873cc175..5078db9a 100644
--- a/tools/nfs-readahead-udev/Makefile.am
+++ b/tools/nfs-readahead-udev/Makefile.am
@@ -1,5 +1,5 @@
libexec_PROGRAMS = nfs-readahead-udev
-nfs_readahead_udev_SOURCES = main.c
+nfs_readahead_udev_SOURCES = main.c syslog.c

udev_rulesdir = /etc/udev/rules.d
udev_rules_DATA = 99-nfs_bdi.rules
diff --git a/tools/nfs-readahead-udev/log.h b/tools/nfs-readahead-udev/log.h
new file mode 100644
index 00000000..2a14e552
--- /dev/null
+++ b/tools/nfs-readahead-udev/log.h
@@ -0,0 +1,16 @@
+#ifndef __ra_log_h__
+#define __ra_log_h__
+#include <stdarg.h>
+
+extern void log_open(void);
+extern void log_close(void);
+
+extern void debug(const char *, ...);
+extern void info(const char *, ...);
+extern void notice(const char *, ...);
+extern void warn(const char *, ...);
+extern void err(const char *, ...);
+extern void crit(const char *, ...);
+extern void alert(const char *, ...);
+extern void emerg(const char *, ...);
+#endif
diff --git a/tools/nfs-readahead-udev/main.c b/tools/nfs-readahead-udev/main.c
index e454108e..dd2c9f8c 100644
--- a/tools/nfs-readahead-udev/main.c
+++ b/tools/nfs-readahead-udev/main.c
@@ -1,7 +1,15 @@
#include <stdio.h>

+#include "log.h"
+
int main(int argc, char **argv, char **envp)
{
unsigned int readahead = 128;
+
+ log_open();
+
+ info("Setting the readahead to 128\n");
+
+ log_close();
printf("%d\n", readahead);
}
diff --git a/tools/nfs-readahead-udev/syslog.c b/tools/nfs-readahead-udev/syslog.c
new file mode 100644
index 00000000..5eeb2579
--- /dev/null
+++ b/tools/nfs-readahead-udev/syslog.c
@@ -0,0 +1,47 @@
+#include <syslog.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "log.h"
+
+#define MSG_SIZE (1024 * sizeof(char))
+
+void log_open(void)
+{
+ openlog("nfs_readahead", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
+ setlogmask(LOG_UPTO(LOG_DEBUG));
+}
+
+void log_close(void)
+{
+ closelog();
+}
+
+static void vlog(int level, const char *fmt, va_list *args)
+{
+ char *msg = malloc(MSG_SIZE);
+ if (!msg)
+ return;
+
+ vsnprintf(msg, MSG_SIZE, fmt, *args);
+ syslog(level, "%s", msg);
+
+ free(msg);
+}
+
+#define GENERATE_LOGGER(name, level) \
+ void name(const char *fmt, ...) { \
+ va_list args; \
+ va_start(args, fmt); \
+ vlog(LOG_##level, fmt, &args); \
+ va_end(args); \
+}
+
+GENERATE_LOGGER(debug, DEBUG);
+GENERATE_LOGGER(info, INFO);
+GENERATE_LOGGER(notice, NOTICE);
+GENERATE_LOGGER(warn, WARNING);
+GENERATE_LOGGER(err, ERR);
+GENERATE_LOGGER(crit, CRIT);
+GENERATE_LOGGER(alert, ALERT);
+GENERATE_LOGGER(emerg, EMERG);
--
2.35.1