Since cxl/monitor.c will be using same logging functions that
ndctl/monitor.c will be using, move common functions to util/log.c.

Signed-off-by: Dave Jiang <[email protected]>
---
 ndctl/monitor.c |   41 ++++-------------------------------------
 util/log.c      |   34 ++++++++++++++++++++++++++++++++++
 util/log.h      |    8 +++++++-
 3 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/ndctl/monitor.c b/ndctl/monitor.c
index 54678d6100b9..89903def63d4 100644
--- a/ndctl/monitor.c
+++ b/ndctl/monitor.c
@@ -33,7 +33,6 @@ static struct monitor {
        const char *log;
        const char *configs;
        const char *dimm_event;
-       FILE *log_file;
        bool daemon;
        bool human;
        bool verbose;
@@ -61,38 +60,6 @@ do { \
                        VERSION, __func__, __LINE__, ##__VA_ARGS__); \
 } while (0)
 
-static void log_syslog(struct log_ctx *ctx, int priority, const char *file,
-               int line, const char *fn, const char *format, va_list args)
-{
-       vsyslog(priority, format, args);
-}
-
-static void log_standard(struct log_ctx *ctx, int priority, const char *file,
-               int line, const char *fn, const char *format, va_list args)
-{
-       if (priority == 6)
-               vfprintf(stdout, format, args);
-       else
-               vfprintf(stderr, format, args);
-}
-
-static void log_file(struct log_ctx *ctx, int priority, const char *file,
-               int line, const char *fn, const char *format, va_list args)
-{
-       FILE *f = monitor.log_file;
-
-       if (priority != LOG_NOTICE) {
-               struct timespec ts;
-
-               clock_gettime(CLOCK_REALTIME, &ts);
-               fprintf(f, "[%10ld.%09ld] [%d] ", ts.tv_sec, ts.tv_nsec, 
getpid());
-               vfprintf(f, format, args);
-       } else
-               vfprintf(f, format, args);
-
-       fflush(f);
-}
-
 static struct json_object *dimm_event_to_json(struct monitor_dimm *mdimm)
 {
        struct json_object *jevent, *jobj;
@@ -648,8 +615,8 @@ int cmd_monitor(int argc, const char **argv, struct 
ndctl_ctx *ctx)
                else if (strncmp(monitor.log, "./standard", 10) == 0)
                        monitor.ctx.log_fn = log_standard;
                else {
-                       monitor.log_file = fopen(monitor.log, "a+");
-                       if (!monitor.log_file) {
+                       monitor.ctx.log_file = fopen(monitor.log, "a+");
+                       if (!monitor.ctx.log_file) {
                                error("open %s failed\n", monitor.log);
                                rc = -errno;
                                goto out;
@@ -694,8 +661,8 @@ int cmd_monitor(int argc, const char **argv, struct 
ndctl_ctx *ctx)
 
        rc = monitor_event(ctx, &mfa);
 out:
-       if (monitor.log_file)
-               fclose(monitor.log_file);
+       if (monitor.ctx.log_file)
+               fclose(monitor.ctx.log_file);
        if (path)
                free(path);
        return rc;
diff --git a/util/log.c b/util/log.c
index 61ac509f4ac5..d4eef0a1fc5c 100644
--- a/util/log.c
+++ b/util/log.c
@@ -2,11 +2,45 @@
 // Copyright (C) 2016-2020, Intel Corporation. All rights reserved.
 #include <syslog.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
+#include <time.h>
 #include <util/log.h>
 
+void log_syslog(struct log_ctx *ctx, int priority, const char *file, int line,
+               const char *fn, const char *format, va_list args)
+{
+       vsyslog(priority, format, args);
+}
+
+void log_standard(struct log_ctx *ctx, int priority, const char *file, int 
line,
+                 const char *fn, const char *format, va_list args)
+{
+       if (priority == 6)
+               vfprintf(stdout, format, args);
+       else
+               vfprintf(stderr, format, args);
+}
+
+void log_file(struct log_ctx *ctx, int priority, const char *file, int line,
+             const char *fn, const char *format, va_list args)
+{
+       FILE *f = ctx->log_file;
+
+       if (priority != LOG_NOTICE) {
+               struct timespec ts;
+
+               clock_gettime(CLOCK_REALTIME, &ts);
+               fprintf(f, "[%10ld.%09ld] [%d] ", ts.tv_sec, ts.tv_nsec,
+                       getpid());
+       }
+
+       vfprintf(f, format, args);
+       fflush(f);
+}
+
 void do_log(struct log_ctx *ctx, int priority, const char *file,
                int line, const char *fn, const char *format, ...)
 {
diff --git a/util/log.h b/util/log.h
index 28f1c7bfcea4..6e09b0dc6494 100644
--- a/util/log.h
+++ b/util/log.h
@@ -14,9 +14,15 @@ struct log_ctx {
        log_fn log_fn;
        const char *owner;
        int log_priority;
+       FILE *log_file;
 };
 
-
+void log_syslog(struct log_ctx *ctx, int priority, const char *file, int line,
+               const char *fn, const char *format, va_list args);
+void log_standard(struct log_ctx *ctx, int priority, const char *file, int 
line,
+                 const char *fn, const char *format, va_list args);
+void log_file(struct log_ctx *ctx, int priority, const char *file, int line,
+             const char *fn, const char *format, va_list args);
 void do_log(struct log_ctx *ctx, int priority, const char *file, int line,
                const char *fn, const char *format, ...)
        __attribute__((format(printf, 6, 7)));



Reply via email to