#include <pthread.h>
#include <stdbool.h>
#include <sys/statvfs.h>
+#include <syslog.h>
#include "platform_defs.h"
#include "xfs.h"
#include "xfs_fs.h"
#include "common.h"
#include "progress.h"
+extern char *progname;
+
/*
* Reporting Status to the Console
*
[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)
{
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,
#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(
* 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;
}
* 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 */
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);
extern bool want_fstrim;
extern bool stderr_isatty;
extern bool stdout_isatty;
+extern bool is_service;
enum scrub_mode {
SCRUB_MODE_DRY_RUN,