2011-05-22 16:39:10 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
|
2011-09-03 09:46:17 +02:00
|
|
|
/*
|
2013-03-30 10:32:07 +01:00
|
|
|
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-29 21:48:49 +01:00
|
|
|
**
|
|
|
|
** 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 Free Software Foundation; either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
2011-09-03 09:46:17 +02:00
|
|
|
**
|
2009-11-29 21:48:49 +01:00
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
2011-09-03 09:46:17 +02:00
|
|
|
**
|
2009-11-29 21:48:49 +01:00
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
2011-09-03 09:46:17 +02:00
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2009-11-29 21:48:49 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-07 07:31:36 +01:00
|
|
|
#if HAVE_CONFIG_H
|
2010-08-16 23:41:37 +02:00
|
|
|
#include <config.h>
|
2010-10-09 11:44:48 +02:00
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
|
|
|
#include "mu-log.h"
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2010-08-16 23:41:37 +02:00
|
|
|
#include <stdio.h>
|
2009-11-29 21:48:49 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2010-07-24 19:28:39 +02:00
|
|
|
|
2010-01-04 19:18:23 +01:00
|
|
|
#include "mu-util.h"
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2011-01-07 07:31:36 +01:00
|
|
|
#define MU_MAX_LOG_FILE_SIZE 1000 * 1000 /* 1 MB (SI units) */
|
2009-12-05 15:00:23 +01:00
|
|
|
#define MU_LOG_FILE "mu.log"
|
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
struct _MuLog {
|
2011-01-07 07:31:36 +01:00
|
|
|
int _fd; /* log file descriptor */
|
2009-12-05 15:00:23 +01:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
MuLogOptions _opts;
|
|
|
|
|
|
|
|
gboolean _color_stdout; /* whether to use color */
|
|
|
|
gboolean _color_stderr;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2009-12-05 15:00:23 +01:00
|
|
|
GLogFunc _old_log_func;
|
2009-11-29 21:48:49 +01:00
|
|
|
};
|
|
|
|
typedef struct _MuLog MuLog;
|
|
|
|
|
2011-01-07 07:31:36 +01:00
|
|
|
/* we use globals, because logging is a global operation as it
|
|
|
|
* globally modifies the behaviour of g_warning and friends
|
|
|
|
*/
|
2009-11-29 21:48:49 +01:00
|
|
|
static MuLog* MU_LOG = NULL;
|
2011-09-03 09:46:17 +02:00
|
|
|
static void log_write (const char* domain, GLogLevelFlags level,
|
2010-01-15 21:11:51 +01:00
|
|
|
const gchar *msg);
|
2010-01-04 19:18:23 +01:00
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
static void
|
2010-01-15 21:11:51 +01:00
|
|
|
try_close (int fd)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
2010-01-04 19:18:23 +01:00
|
|
|
if (fd < 0)
|
|
|
|
return;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
|
|
|
if (close (fd) < 0)
|
2010-01-04 19:18:23 +01:00
|
|
|
g_printerr ("%s: close() of fd %d failed: %s\n",
|
2015-04-22 20:06:31 +02:00
|
|
|
__func__, fd, strerror(errno));
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
silence (void)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mu_log_init_silence (void)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (!MU_LOG, FALSE);
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
MU_LOG = g_new0 (MuLog, 1);
|
|
|
|
MU_LOG->_fd = -1;
|
|
|
|
|
|
|
|
mu_log_options_set (MU_LOG_OPTIONS_NONE);
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2009-12-05 15:00:23 +01:00
|
|
|
MU_LOG->_old_log_func =
|
|
|
|
g_log_set_default_handler ((GLogFunc)silence, NULL);
|
2010-01-06 00:26:18 +01:00
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_handler (const gchar* log_domain, GLogLevelFlags log_level,
|
|
|
|
const gchar* msg)
|
|
|
|
{
|
2012-09-15 16:58:37 +02:00
|
|
|
if ((log_level & G_LOG_LEVEL_DEBUG) &&
|
|
|
|
!(MU_LOG->_opts & MU_LOG_OPTIONS_DEBUG))
|
2009-11-29 21:48:49 +01:00
|
|
|
return;
|
|
|
|
|
2010-01-15 21:11:51 +01:00
|
|
|
log_write (log_domain ? log_domain : "mu", log_level, msg);
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
void
|
|
|
|
mu_log_options_set (MuLogOptions opts)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
2012-09-15 16:58:37 +02:00
|
|
|
g_return_if_fail (MU_LOG);
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
MU_LOG->_opts = opts;
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
/* when color is, only enable it when output is to a tty */
|
|
|
|
if (MU_LOG->_opts & MU_LOG_OPTIONS_COLOR) {
|
|
|
|
MU_LOG->_color_stdout = isatty(fileno(stdout));
|
|
|
|
MU_LOG->_color_stderr = isatty(fileno(stderr));
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MuLogOptions
|
|
|
|
mu_log_options_get (void)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (MU_LOG, MU_LOG_OPTIONS_NONE);
|
|
|
|
|
|
|
|
return MU_LOG->_opts;
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
|
2010-02-04 21:44:42 +01:00
|
|
|
static gboolean
|
2010-08-16 23:41:37 +02:00
|
|
|
move_log_file (const char *logfile)
|
2010-02-04 21:44:42 +01:00
|
|
|
{
|
2010-08-16 23:41:37 +02:00
|
|
|
gchar *logfile_old;
|
|
|
|
int rv;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2010-08-16 23:41:37 +02:00
|
|
|
logfile_old = g_strdup_printf ("%s.old", logfile);
|
|
|
|
rv = rename (logfile, logfile_old);
|
|
|
|
g_free (logfile_old);
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2010-08-16 23:41:37 +02:00
|
|
|
if (rv != 0) {
|
2010-11-30 21:20:27 +01:00
|
|
|
g_warning ("failed to move %s to %s.old: %s",
|
2010-08-16 23:41:37 +02:00
|
|
|
logfile, logfile, strerror(rv));
|
|
|
|
return FALSE;
|
|
|
|
} else
|
|
|
|
return TRUE;
|
2010-08-14 12:21:24 +02:00
|
|
|
|
2010-07-24 19:28:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-04 21:44:42 +01:00
|
|
|
|
2010-02-03 20:01:08 +01:00
|
|
|
static gboolean
|
|
|
|
log_file_backup_maybe (const char *logfile)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2010-02-03 20:01:08 +01:00
|
|
|
if (stat (logfile, &statbuf) != 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return TRUE; /* it did not exist yet, no problem */
|
|
|
|
else {
|
2011-01-07 07:31:36 +01:00
|
|
|
g_warning ("failed to stat(2) %s: %s",
|
|
|
|
logfile, strerror(errno));
|
2010-02-03 20:01:08 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* log file is still below the max size? */
|
|
|
|
if (statbuf.st_size <= MU_MAX_LOG_FILE_SIZE)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* log file is too big!; we move it to <logfile>.old, overwriting */
|
2010-02-04 21:44:42 +01:00
|
|
|
return move_log_file (logfile);
|
2010-02-03 20:01:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
gboolean
|
2012-09-15 16:58:37 +02:00
|
|
|
mu_log_init (const char* logfile, MuLogOptions opts)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
/* only init once... */
|
2011-09-03 09:46:17 +02:00
|
|
|
g_return_val_if_fail (!MU_LOG, FALSE);
|
2011-07-23 16:56:25 +02:00
|
|
|
g_return_val_if_fail (logfile, FALSE);
|
2010-02-03 20:01:08 +01:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
if (opts & MU_LOG_OPTIONS_BACKUP)
|
|
|
|
if (!log_file_backup_maybe(logfile)) {
|
|
|
|
g_warning ("failed to backup log file");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2010-07-30 19:52:30 +02:00
|
|
|
fd = open (logfile, O_WRONLY|O_CREAT|O_APPEND, 00600);
|
2012-09-15 16:58:37 +02:00
|
|
|
if (fd < 0) {
|
2015-04-22 20:06:31 +02:00
|
|
|
g_warning ("%s: open() of '%s' failed: %s", __func__,
|
2009-12-05 15:00:23 +01:00
|
|
|
logfile, strerror(errno));
|
2009-11-29 21:48:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
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);
|
|
|
|
|
2012-07-31 17:47:05 +02:00
|
|
|
MU_WRITE_LOG ("logging started");
|
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-09-03 09:46:17 +02:00
|
|
|
void
|
2009-11-29 21:48:49 +01:00
|
|
|
mu_log_uninit (void)
|
|
|
|
{
|
2010-11-20 15:14:26 +01:00
|
|
|
if (!MU_LOG)
|
|
|
|
return;
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2012-07-31 17:47:05 +02:00
|
|
|
MU_WRITE_LOG ("logging stopped");
|
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
try_close (MU_LOG->_fd);
|
2009-11-29 21:48:49 +01:00
|
|
|
g_free (MU_LOG);
|
2011-01-07 07:31:36 +01:00
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
MU_LOG = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char*
|
2012-10-03 09:36:36 +02:00
|
|
|
levelstr (GLogLevelFlags level)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
switch (level) {
|
2012-10-03 09:36:36 +02:00
|
|
|
case G_LOG_LEVEL_WARNING: return " [WARN] ";
|
|
|
|
case G_LOG_LEVEL_ERROR : return " [ERR ] ";
|
|
|
|
case G_LOG_LEVEL_DEBUG: return " [DBG ] ";
|
|
|
|
case G_LOG_LEVEL_CRITICAL: return " [CRIT] ";
|
|
|
|
case G_LOG_LEVEL_MESSAGE: return " [MSG ] ";
|
|
|
|
case G_LOG_LEVEL_INFO : return " [INFO] ";
|
|
|
|
default: return " [LOG ] ";
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
|
|
|
|
2012-09-17 12:17:49 +02:00
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
static void
|
2012-09-17 12:17:49 +02:00
|
|
|
log_write_fd (GLogLevelFlags level, const gchar *msg)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
time_t now;
|
2012-10-03 09:36:36 +02:00
|
|
|
char timebuf [22];
|
|
|
|
const char *mylevel;
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
/* get the time/date string */
|
|
|
|
now = time(NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
strftime (timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S",
|
|
|
|
localtime(&now));
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-10-03 09:36:36 +02:00
|
|
|
if (write (MU_LOG->_fd, timebuf, strlen (timebuf)) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
mylevel = levelstr (level);
|
|
|
|
if (write (MU_LOG->_fd, mylevel, strlen (mylevel)) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (write (MU_LOG->_fd, msg, strlen (msg)) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (write (MU_LOG->_fd, "\n", strlen ("\n")) < 0)
|
|
|
|
goto err;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-10-03 09:36:36 +02:00
|
|
|
return; /* all went well */
|
2012-09-20 20:24:49 +02:00
|
|
|
|
2012-10-03 09:36:36 +02:00
|
|
|
err:
|
|
|
|
fprintf (stderr, "%s: failed to write to log: %s\n",
|
2015-04-22 20:06:31 +02:00
|
|
|
__func__, strerror(errno));
|
2012-09-17 12:17:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_write_stdout_stderr (GLogLevelFlags level, const gchar *msg)
|
|
|
|
{
|
|
|
|
const char *mu;
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-09-15 16:58:37 +02:00
|
|
|
mu = MU_LOG->_opts & MU_LOG_OPTIONS_NEWLINE ?
|
|
|
|
"\nmu: " : "mu: ";
|
|
|
|
|
|
|
|
if (!(MU_LOG->_opts & MU_LOG_OPTIONS_QUIET) &&
|
|
|
|
(level & G_LOG_LEVEL_MESSAGE)) {
|
|
|
|
color_stdout_maybe (MU_COLOR_GREEN);
|
|
|
|
fputs (mu, stdout);
|
2011-05-24 20:35:36 +02:00
|
|
|
fputs (msg, stdout);
|
|
|
|
fputs ("\n", stdout);
|
2012-09-15 16:58:37 +02:00
|
|
|
color_stdout_maybe (MU_COLOR_DEFAULT);
|
2011-05-24 20:35:36 +02:00
|
|
|
}
|
2011-09-03 09:46:17 +02:00
|
|
|
|
2012-07-31 17:47:05 +02:00
|
|
|
/* for errors, log them to stderr as well */
|
2011-05-24 20:35:36 +02:00
|
|
|
if (level & G_LOG_LEVEL_ERROR ||
|
|
|
|
level & G_LOG_LEVEL_CRITICAL ||
|
2012-07-31 17:47:05 +02:00
|
|
|
level & G_LOG_LEVEL_WARNING) {
|
2012-09-15 16:58:37 +02:00
|
|
|
color_stderr_maybe (MU_COLOR_RED);
|
|
|
|
fputs (mu, stderr);
|
2011-05-24 20:35:36 +02:00
|
|
|
fputs (msg, stderr);
|
|
|
|
fputs ("\n", stderr);
|
2012-09-15 16:58:37 +02:00
|
|
|
color_stderr_maybe (MU_COLOR_DEFAULT);
|
2011-05-24 20:35:36 +02:00
|
|
|
}
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|
2012-09-17 12:17:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_write (const char* domain, GLogLevelFlags level, const gchar *msg)
|
|
|
|
{
|
|
|
|
g_return_if_fail (MU_LOG);
|
|
|
|
|
|
|
|
log_write_fd (level, msg);
|
|
|
|
log_write_stdout_stderr (level, msg);
|
|
|
|
}
|