]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_scrub: log operational messages when interactive
authorDarrick J. Wong <darrick.wong@oracle.com>
Fri, 9 Mar 2018 02:35:23 +0000 (20:35 -0600)
committerEric Sandeen <sandeen@redhat.com>
Fri, 9 Mar 2018 02:35:23 +0000 (20:35 -0600)
Record the summary of an interactive session in the system log so that
future support requests can get a better picture of what happened.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
scrub/common.c
scrub/common.h
scrub/phase1.c
scrub/xfs_scrub.c
scrub/xfs_scrub.h

index 17c3699aed37510ba92ca593583178936dae2ae6..4f26bf8d9b52437ce3ab9536408304fef7eef219 100644 (file)
@@ -21,6 +21,7 @@
 #include <pthread.h>
 #include <stdbool.h>
 #include <sys/statvfs.h>
+#include <syslog.h>
 #include "platform_defs.h"
 #include "xfs.h"
 #include "xfs_fs.h"
@@ -29,6 +30,8 @@
 #include "common.h"
 #include "progress.h"
 
+extern char            *progname;
+
 /*
  * Reporting Status to the Console
  *
@@ -64,6 +67,12 @@ static const char *err_str[] = {
        [S_PREEN]       = "Optimized",
 };
 
+static int log_level[] = {
+       [S_ERROR]       = LOG_ERR,
+       [S_WARN]        = LOG_WARNING,
+       [S_INFO]        = LOG_INFO,
+};
+
 /* If stream is a tty, clear to end of line to clean up progress bar. */
 static inline const char *stream_start(FILE *stream)
 {
@@ -131,6 +140,46 @@ out_record:
        pthread_mutex_unlock(&ctx->lock);
 }
 
+/* Log a message to syslog. */
+#define LOG_BUFSZ      4096
+#define LOGNAME_BUFSZ  256
+void
+__str_log(
+       struct scrub_ctx        *ctx,
+       enum error_level        level,
+       const char              *format,
+       ...)
+{
+       va_list                 args;
+       char                    logname[LOGNAME_BUFSZ];
+       char                    buf[LOG_BUFSZ];
+       int                     sz;
+
+       /* We only want to hear about optimizing when in debug/verbose mode. */
+       if (level == S_PREEN && !debug && !verbose)
+               return;
+
+       /*
+        * Skip logging if we're being run as a service (presumably the
+        * service will log stdout/stderr); if we're being run in a non
+        * interactive manner (assume we're a service); or if we're in
+        * debug mode.
+        */
+       if (is_service || !isatty(fileno(stdin)) || debug)
+               return;
+
+       snprintf(logname, LOGNAME_BUFSZ, "%s@%s", progname, ctx->mntpoint);
+       openlog(logname, LOG_PID, LOG_DAEMON);
+
+       sz = snprintf(buf, LOG_BUFSZ, "%s: ", _(err_str[level]));
+       va_start(args, format);
+       vsnprintf(buf + sz, LOG_BUFSZ - sz, format, args);
+       va_end(args);
+       syslog(log_level[level], "%s", buf);
+
+       closelog();
+}
+
 double
 timeval_subtract(
        struct timeval          *tv1,
index 287bd4dc394eb476f52f997ca3327fb6df5740a3..4f1f0cd627a1e7f77b4c4379e82a784c4d8139fd 100644 (file)
@@ -56,6 +56,16 @@ void __str_out(struct scrub_ctx *ctx, const char *descr, enum error_level level,
 #define dbg_printf(fmt, ...) \
        do {if (debug > 1) {printf(fmt, __VA_ARGS__);}} while (0)
 
+void __str_log(struct scrub_ctx *ctx, enum error_level level,
+               const char *format, ...);
+
+#define log_info(ctx, ...) \
+       __str_log(ctx, S_INFO,  __VA_ARGS__)
+#define log_warn(ctx, ...) \
+       __str_log(ctx, S_WARN,  __VA_ARGS__)
+#define log_err(ctx, ...) \
+       __str_log(ctx, S_ERROR, __VA_ARGS__)
+
 /* Is this debug tweak enabled? */
 static inline bool
 debug_tweak_on(
index 6cd544233c94728fad881a1cf84396f1738c6bf3..b856a7f508369fa1e5876cb6ac003d527447df8a 100644 (file)
@@ -236,6 +236,7 @@ _("Unable to find realtime device path."));
         * this point are most probably corruption errors (as opposed to
         * purely setup errors).
         */
+       log_info(ctx, _("Invoking online scrub."), ctx);
        ctx->need_repair = true;
        return true;
 }
index ab26e63359480b7c334b70eca9066b77099eaeab..7ab0c3ee62aa6ce630bb62d4d648c4134b0ce976 100644 (file)
@@ -162,7 +162,7 @@ bool                                stdout_isatty;
  * If we are running as a service, we need to be careful about what
  * error codes we return to the calling process.
  */
-static bool                    is_service;
+bool                           is_service;
 
 #define SCRUB_RET_SUCCESS      (0)     /* no problems left behind */
 #define SCRUB_RET_CORRUPT      (1)     /* corruption remains on fs */
@@ -501,19 +501,27 @@ report_outcome(
 
        total_errors = ctx->errors_found + ctx->runtime_errors;
 
-       if (total_errors == 0 && ctx->warnings_found == 0)
+       if (total_errors == 0 && ctx->warnings_found == 0) {
+               log_info(ctx, _("No errors found."));
                return;
+       }
 
-       if (total_errors == 0)
+       if (total_errors == 0) {
                fprintf(stderr, _("%s: warnings found: %llu\n"), ctx->mntpoint,
                                ctx->warnings_found);
-       else if (ctx->warnings_found == 0)
+               log_warn(ctx, _("warnings found: %llu"), ctx->warnings_found);
+       } else if (ctx->warnings_found == 0) {
                fprintf(stderr, _("%s: errors found: %llu\n"), ctx->mntpoint,
                                total_errors);
-       else
+               log_err(ctx, _("errors found: %llu"), total_errors);
+       } else {
                fprintf(stderr, _("%s: errors found: %llu; warnings found: %llu\n"),
                                ctx->mntpoint, total_errors,
                                ctx->warnings_found);
+               log_err(ctx, _("errors found: %llu; warnings found: %llu"),
+                               total_errors, ctx->warnings_found);
+       }
+
        if (ctx->need_repair)
                fprintf(stderr, _("%s: Unmount and run xfs_repair.\n"),
                                ctx->mntpoint);
index 89b46a4ffd94ba7f7bc9fe9ece5cac5bf54346c9..b455747dd056589116259fbe244122812ab0cf29 100644 (file)
@@ -31,6 +31,7 @@ extern long                   page_size;
 extern bool                    want_fstrim;
 extern bool                    stderr_isatty;
 extern bool                    stdout_isatty;
+extern bool                    is_service;
 
 enum scrub_mode {
        SCRUB_MODE_DRY_RUN,