rework logging system

reimplement the old mu-log.[ch] into mu-logging.{cc,hh}

If available (and using an appropriately equipped glib), log to the
systemd journal

Only g_criticals have stderr output, all the other g_* go to the log
file / journal.
This commit is contained in:
Dirk-Jan C. Binnema
2020-05-30 13:24:53 +03:00
parent 73be015cd0
commit 3e233cba9a
18 changed files with 68 additions and 548 deletions

View File

@ -1,7 +1,5 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2008-2016 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
@ -37,7 +35,6 @@
#include "mu-runtime.h"
#include "utils/mu-util.h"
#include "utils/mu-log.h"
static gboolean MU_CAUGHT_SIGNAL;
@ -180,22 +177,14 @@ show_time (unsigned t, unsigned processed, gboolean color)
g_print ("\n");
}
/* when logging to console, print a newline before doing so; this
* makes it more clear when something happens during the
* indexing/cleanup progress output */
#define newline_before_on() \
mu_log_options_set(mu_log_options_get() | MU_LOG_OPTIONS_NEWLINE)
#define newline_before_off() \
mu_log_options_set(mu_log_options_get() & ~MU_LOG_OPTIONS_NEWLINE)
static MuError
cleanup_missing (MuIndex *midx, MuConfig *opts, MuIndexStats *stats,
GError **err)
{
MuError rv;
time_t t;
IndexData idata;
gboolean show_progress;
MuError rv;
time_t t;
IndexData idata;
gboolean show_progress;
if (!opts->quiet)
g_print ("cleaning up messages [%s]\n",
@ -206,14 +195,12 @@ cleanup_missing (MuIndex *midx, MuConfig *opts, MuIndexStats *stats,
t = time (NULL);
idata.color = !opts->nocolor;
newline_before_on();
rv = mu_index_cleanup
(midx, stats,
show_progress ?
(MuIndexCleanupDeleteCallback)index_msg_cb :
(MuIndexCleanupDeleteCallback)index_msg_silent_cb,
&idata, err);
newline_before_off();
if (!opts->quiet) {
print_stats (stats, TRUE, !opts->nocolor);
@ -235,8 +222,6 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, GError **err)
show_progress = !opts->quiet && isatty(fileno(stdout));
idata.color = !opts->nocolor;
newline_before_on();
rv = mu_index_run (midx,
opts->rebuild,
opts->lazycheck, stats,
@ -244,11 +229,9 @@ cmd_index (MuIndex *midx, MuConfig *opts, MuIndexStats *stats, GError **err)
(MuIndexMsgCallback)index_msg_cb :
(MuIndexMsgCallback)index_msg_silent_cb,
NULL, &idata);
newline_before_off();
if (rv == MU_OK || rv == MU_STOP) {
MU_WRITE_LOG ("index: processed: %u; updated/new: %u",
stats->_processed, stats->_updated);
g_message ("index: processed: %u; updated/new: %u",
stats->_processed, stats->_updated);
} else
mu_util_g_set_error (err, rv, "error while indexing");

View File

@ -33,7 +33,6 @@
#include "mu-runtime.h"
#include "mu-flags.h"
#include "utils/mu-log.h"
#include "utils/mu-util.h"
#include "utils/mu-str.h"
#include "utils/mu-date.h"
@ -342,15 +341,15 @@ foreach_msg_file (MuStore *store, MuConfig *opts,
if (!check_file_okay (path, TRUE)) {
all_ok = FALSE;
MU_WRITE_LOG ("not a valid message file: %s", path);
g_warning ("not a valid message file: %s", path);
continue;
}
if (!foreach_func (store, path, err)) {
all_ok = FALSE;
MU_WRITE_LOG ("error with %s: %s", path,
(err&&*err) ? (*err)->message :
"something went wrong");
g_warning ("error with %s: %s", path,
(err&&*err) ? (*err)->message :
"something went wrong");
g_clear_error (err);
continue;
}
@ -656,23 +655,6 @@ check_params (MuConfig *opts, GError **err)
return TRUE;
}
static void
set_log_options (MuConfig *opts)
{
MuLogOptions logopts;
logopts = MU_LOG_OPTIONS_NONE;
if (opts->quiet)
logopts |= MU_LOG_OPTIONS_QUIET;
if (!opts->nocolor)
logopts |= MU_LOG_OPTIONS_COLOR;
if (opts->log_stderr)
logopts |= MU_LOG_OPTIONS_STDERR;
if (opts->debug)
logopts |= MU_LOG_OPTIONS_DEBUG;
}
MuError
mu_cmd_execute (MuConfig *opts, GError **err)
{
@ -683,8 +665,6 @@ mu_cmd_execute (MuConfig *opts, GError **err)
if (!check_params(opts, err))
return MU_G_ERROR_CODE(err);
set_log_options (opts);
switch (opts->cmd) {
/* already handled in mu-config.c */
@ -692,10 +672,10 @@ mu_cmd_execute (MuConfig *opts, GError **err)
/* no store needed */
case MU_CONFIG_CMD_MKDIR: merr = cmd_mkdir (opts, err); break;
case MU_CONFIG_CMD_MKDIR: merr = cmd_mkdir (opts, err); break;
case MU_CONFIG_CMD_SCRIPT: merr = mu_cmd_script (opts, err); break;
case MU_CONFIG_CMD_VIEW: merr = cmd_view (opts, err); break;
case MU_CONFIG_CMD_VERIFY: merr = cmd_verify (opts, err); break;
case MU_CONFIG_CMD_VIEW: merr = cmd_view (opts, err); break;
case MU_CONFIG_CMD_VERIFY: merr = cmd_verify (opts, err); break;
case MU_CONFIG_CMD_EXTRACT: merr = mu_cmd_extract (opts, err); break;
/* read-only store */

View File

@ -17,9 +17,7 @@
**
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif /*HAVE_CONFIG_H*/
#include <glib.h>
#include <string.h> /* memset */

View File

@ -97,8 +97,8 @@ struct _MuConfig {
char *cmdstr; /* cmd string, for user
* info */
/* general options */
gboolean quiet; /* don't give any output */
gboolean debug; /* spew out debug info */
gboolean quiet; /* don't give any output */
gboolean debug; /* log debug-level info */
gchar *muhome; /* the House of Mu */
gboolean version; /* request mu version */
gboolean log_stderr; /* log to stderr (not logfile) */

View File

@ -91,9 +91,9 @@ handle_error (MuConfig *conf, MuError merr, GError **err)
int
main (int argc, char *argv[])
{
GError *err;
MuError rv;
MuConfig *conf;
GError *err;
MuError rv;
MuConfig *conf;
setlocale (LC_ALL, "");
@ -113,7 +113,7 @@ main (int argc, char *argv[])
if (conf->cmd == MU_CONFIG_CMD_NONE)
return 0;
if (!mu_runtime_init (conf->muhome, PACKAGE_NAME)) {
if (!mu_runtime_init (conf->muhome, PACKAGE_NAME, conf->debug)) {
mu_config_uninit (conf);
return 1;
}