* lib/mu-log: cleanups and some improvements

use an option enum instead of boolean args for code clarity; allow for
  printing an \n before logging to tty (improved mu-index output). allow
  for color in logging to tty
This commit is contained in:
djcb 2012-09-15 17:58:37 +03:00
parent 06e66b96c9
commit 04523b4cd1
2 changed files with 123 additions and 72 deletions

View File

@ -1,7 +1,7 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/* /*
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify ** 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 ** it under the terms of the GNU General Public License as published by
@ -43,9 +43,10 @@
struct _MuLog { struct _MuLog {
int _fd; /* log file descriptor */ int _fd; /* log file descriptor */
gboolean _own; /* close _fd with log_destroy? */ MuLogOptions _opts;
gboolean _debug; /* add debug-level stuff? */
gboolean _quiet; /* don't write non-error to stdout/stderr */ gboolean _color_stdout; /* whether to use color */
gboolean _color_stderr;
GLogFunc _old_log_func; GLogFunc _old_log_func;
}; };
@ -80,9 +81,10 @@ mu_log_init_silence (void)
{ {
g_return_val_if_fail (!MU_LOG, FALSE); g_return_val_if_fail (!MU_LOG, FALSE);
MU_LOG = g_new(MuLog, 1); MU_LOG = g_new0 (MuLog, 1);
MU_LOG->_fd = -1; MU_LOG->_fd = -1;
MU_LOG->_own = FALSE; /* nobody owns silence */
mu_log_options_set (MU_LOG_OPTIONS_NONE);
MU_LOG->_old_log_func = MU_LOG->_old_log_func =
g_log_set_default_handler ((GLogFunc)silence, NULL); g_log_set_default_handler ((GLogFunc)silence, NULL);
@ -94,32 +96,39 @@ static void
log_handler (const gchar* log_domain, GLogLevelFlags log_level, log_handler (const gchar* log_domain, GLogLevelFlags log_level,
const gchar* msg) const gchar* msg)
{ {
if ((log_level & G_LOG_LEVEL_DEBUG) && !(MU_LOG->_debug)) if ((log_level & G_LOG_LEVEL_DEBUG) &&
!(MU_LOG->_opts & MU_LOG_OPTIONS_DEBUG))
return; return;
log_write (log_domain ? log_domain : "mu", log_level, msg); log_write (log_domain ? log_domain : "mu", log_level, msg);
} }
gboolean void
mu_log_init_with_fd (int fd, gboolean doclose, mu_log_options_set (MuLogOptions opts)
gboolean quiet, gboolean debug)
{ {
g_return_val_if_fail (!MU_LOG, FALSE); g_return_if_fail (MU_LOG);
MU_LOG = g_new(MuLog, 1); MU_LOG->_opts = opts;
MU_LOG->_fd = fd; /* when color is, only enable it when output is to a tty */
MU_LOG->_quiet = quiet; if (MU_LOG->_opts & MU_LOG_OPTIONS_COLOR) {
MU_LOG->_debug = debug; MU_LOG->_color_stdout = isatty(fileno(stdout));
MU_LOG->_own = doclose; /* if we now own the fd, close it MU_LOG->_color_stderr = isatty(fileno(stderr));
* in _destroy */
MU_LOG->_old_log_func =
g_log_set_default_handler ((GLogFunc)log_handler, NULL);
return TRUE; }
} }
MuLogOptions
mu_log_options_get (void)
{
g_return_val_if_fail (MU_LOG, MU_LOG_OPTIONS_NONE);
return MU_LOG->_opts;
}
static gboolean static gboolean
move_log_file (const char *logfile) move_log_file (const char *logfile)
{ {
@ -165,8 +174,7 @@ log_file_backup_maybe (const char *logfile)
gboolean gboolean
mu_log_init (const char* logfile, gboolean backup, mu_log_init (const char* logfile, MuLogOptions opts)
gboolean quiet, gboolean debug)
{ {
int fd; int fd;
@ -174,21 +182,27 @@ mu_log_init (const char* logfile, gboolean backup,
g_return_val_if_fail (!MU_LOG, FALSE); g_return_val_if_fail (!MU_LOG, FALSE);
g_return_val_if_fail (logfile, FALSE); g_return_val_if_fail (logfile, FALSE);
if (backup && !log_file_backup_maybe(logfile)) { if (opts & MU_LOG_OPTIONS_BACKUP)
g_warning ("failed to backup log file"); if (!log_file_backup_maybe(logfile)) {
return FALSE; g_warning ("failed to backup log file");
} return FALSE;
}
fd = open (logfile, O_WRONLY|O_CREAT|O_APPEND, 00600); fd = open (logfile, O_WRONLY|O_CREAT|O_APPEND, 00600);
if (fd < 0) if (fd < 0) {
g_warning ("%s: open() of '%s' failed: %s", __FUNCTION__, g_warning ("%s: open() of '%s' failed: %s", __FUNCTION__,
logfile, strerror(errno)); logfile, strerror(errno));
if (fd < 0 || !mu_log_init_with_fd (fd, FALSE, quiet, debug)) {
try_close (fd);
return FALSE; return FALSE;
} }
MU_LOG = g_new0 (MuLog, 1);
MU_LOG->_fd = fd;
mu_log_options_set (opts);
MU_LOG->_old_log_func =
g_log_set_default_handler ((GLogFunc)log_handler, NULL);
MU_WRITE_LOG ("logging started"); MU_WRITE_LOG ("logging started");
return TRUE; return TRUE;
@ -202,9 +216,7 @@ mu_log_uninit (void)
MU_WRITE_LOG ("logging stopped"); MU_WRITE_LOG ("logging stopped");
if (MU_LOG->_own) try_close (MU_LOG->_fd);
try_close (MU_LOG->_fd);
g_free (MU_LOG); g_free (MU_LOG);
MU_LOG = NULL; MU_LOG = NULL;
@ -225,14 +237,23 @@ pfx (GLogLevelFlags level)
} }
} }
#define color_stdout_maybe(C) \
do{if (MU_LOG->_color_stdout) fputs ((C),stdout);} while (0)
#define color_stderr_maybe(C) \
do{if (MU_LOG->_color_stderr) fputs ((C),stderr);} while (0)
static void static void
log_write (const char* domain, GLogLevelFlags level, log_write (const char* domain, GLogLevelFlags level,
const gchar *msg) const gchar *msg)
{ {
time_t now; time_t now;
ssize_t len; ssize_t len;
const char *mu;
/* log lines will be truncated at 768 chars */ /* log lines will be truncated at 768-1 chars */
char buf [768], timebuf [22]; char buf [768], timebuf [22];
g_return_if_fail (MU_LOG); g_return_if_fail (MU_LOG);
@ -250,18 +271,26 @@ log_write (const char* domain, GLogLevelFlags level,
fprintf (stderr, "%s: failed to write to log: %s\n", fprintf (stderr, "%s: failed to write to log: %s\n",
__FUNCTION__, strerror(errno)); __FUNCTION__, strerror(errno));
if (!(MU_LOG->_quiet) && (level & G_LOG_LEVEL_MESSAGE)) { mu = MU_LOG->_opts & MU_LOG_OPTIONS_NEWLINE ?
fputs ("mu: ", stdout); "\nmu: " : "mu: ";
if (!(MU_LOG->_opts & MU_LOG_OPTIONS_QUIET) &&
(level & G_LOG_LEVEL_MESSAGE)) {
color_stdout_maybe (MU_COLOR_GREEN);
fputs (mu, stdout);
fputs (msg, stdout); fputs (msg, stdout);
fputs ("\n", stdout); fputs ("\n", stdout);
color_stdout_maybe (MU_COLOR_DEFAULT);
} }
/* for errors, log them to stderr as well */ /* for errors, log them to stderr as well */
if (level & G_LOG_LEVEL_ERROR || if (level & G_LOG_LEVEL_ERROR ||
level & G_LOG_LEVEL_CRITICAL || level & G_LOG_LEVEL_CRITICAL ||
level & G_LOG_LEVEL_WARNING) { level & G_LOG_LEVEL_WARNING) {
fputs ("mu: ", stderr); color_stderr_maybe (MU_COLOR_RED);
fputs (mu, stderr);
fputs (msg, stderr); fputs (msg, stderr);
fputs ("\n", stderr); fputs ("\n", stderr);
color_stderr_maybe (MU_COLOR_DEFAULT);
} }
} }

View File

@ -1,22 +1,22 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/ /* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset:
8 -*-*/
/* /*
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify ** 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 ** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or ** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version. ** (at your option) any later version.
** **
** This program is distributed in the hope that it will be useful, ** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of ** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details. ** GNU General Public License for more details.
** **
** You should have received a copy of the GNU General Public License ** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation, ** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
** **
*/ */
#ifndef __MU_LOG_H__ #ifndef __MU_LOG_H__
@ -28,40 +28,49 @@
G_BEGIN_DECLS G_BEGIN_DECLS
/** enum _MuLogOptions {
* write logging information to a log file MU_LOG_OPTIONS_NONE = 0,
*
* @param full path to the log file (does not have to exist yet, but /* when size of log file > MU_MAX_LOG_FILE_SIZE, move the log
* it's directory must) * file to <log file>.old and start a new one. The .old file will
* @param backup if TRUE and size of log file > MU_MAX_LOG_FILE_SIZE, move * overwrite existing files of that name */
* the log file to <log file>.old and start a new one. The .old file will overwrite MU_LOG_OPTIONS_BACKUP = 1 << 1,
* existing files of that name
* @param quiet don't log non-errors to stdout/stderr /* quiet: don't log non-errors to stdout/stderr */
* @param debug include debug-level information. MU_LOG_OPTIONS_QUIET = 1 << 2,
*
* @return TRUE if initialization succeeds, FALSE otherwise /* should lines be preceded by \n? useful when errors come
*/ * during indexing */
gboolean mu_log_init (const char *logfile, gboolean backup, MU_LOG_OPTIONS_NEWLINE = 1 << 3,
gboolean quiet, gboolean debug)
G_GNUC_WARN_UNUSED_RESULT; /* color in output (iff output is to a tty) */
MU_LOG_OPTIONS_COLOR = 1 << 4,
/* log everything to stderr */
MU_LOG_OPTIONS_STDERR = 1 << 5,
/* debug: debug include debug-level information */
MU_LOG_OPTIONS_DEBUG = 1 << 6
};
typedef enum _MuLogOptions MuLogOptions;
/** /**
* write logging information to a file descriptor * write logging information to a log file
* *
* @param fd an open file descriptor * @param full path to the log file (does not have to exist yet, but
* @param doclose if true, mu-log will close it upon mu_log_uninit * it's directory must)
* @param quiet don't log non-errors to stdout/stderr * @param opts logging options
* @param debug include debug-level info *
*
* @return TRUE if initialization succeeds, FALSE otherwise * @return TRUE if initialization succeeds, FALSE otherwise
*/ */
gboolean mu_log_init_with_fd (int fd, gboolean doclose, gboolean quiet, gboolean mu_log_init (const char *logfile, MuLogOptions opts)
gboolean debug) G_GNUC_WARN_UNUSED_RESULT; G_GNUC_WARN_UNUSED_RESULT;
/** /**
* be silent except for runtime errors, which will be written to * be silent except for runtime errors, which will be written to
* stderr. * stderr.
* *
* @return TRUE if initialization succeeds, FALSE otherwise * @return TRUE if initialization succeeds, FALSE otherwise
*/ */
gboolean mu_log_init_silence (void) G_GNUC_WARN_UNUSED_RESULT; gboolean mu_log_init_silence (void) G_GNUC_WARN_UNUSED_RESULT;
@ -71,6 +80,19 @@ gboolean mu_log_init_silence (void) G_GNUC_WARN_UNUSED_RESULT;
*/ */
void mu_log_uninit (void); void mu_log_uninit (void);
/**
* set logging options, a logical-OR'd value of MuLogOptions
*
* @param opts the options (logically OR'd)
*/
void mu_log_options_set (MuLogOptions opts);
/**
* get logging options, a logical-OR'd value of MuLogOptions
*
* @param opts the options (logically OR'd)
*/
MuLogOptions mu_log_options_get (void);
G_END_DECLS G_END_DECLS