2009-11-29 21:48:49 +01:00
|
|
|
/*
|
2010-10-09 11:44:48 +02:00
|
|
|
** Copyright (C) 2008-2010 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.
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
2010-10-09 11:44:48 +02:00
|
|
|
#ifdef 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
|
|
|
|
2009-12-05 15:00:23 +01:00
|
|
|
#define MU_LOG_FILE "mu.log"
|
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
struct _MuLog {
|
|
|
|
int _fd; /* log file descriptor */
|
2009-12-05 15:00:23 +01:00
|
|
|
|
2010-01-06 20:42:59 +01:00
|
|
|
gboolean _own; /* close _fd with log_destroy? */
|
|
|
|
gboolean _debug; /* add debug-level stuff? */
|
|
|
|
gboolean _quiet; /* don't write non-error to stdout/stderr */
|
|
|
|
|
2009-12-05 15:00:23 +01:00
|
|
|
GLogFunc _old_log_func;
|
2009-11-29 21:48:49 +01:00
|
|
|
};
|
|
|
|
typedef struct _MuLog MuLog;
|
|
|
|
|
|
|
|
static MuLog* MU_LOG = NULL;
|
|
|
|
|
2010-01-15 21:11:51 +01:00
|
|
|
static void log_write (const char* domain, GLogLevelFlags level,
|
|
|
|
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;
|
|
|
|
|
2009-11-29 21:48:49 +01:00
|
|
|
if (close (fd) < 0)
|
2010-01-04 19:18:23 +01:00
|
|
|
g_printerr ("%s: close() of fd %d failed: %s\n",
|
|
|
|
__FUNCTION__, 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);
|
|
|
|
|
2010-10-09 11:44:48 +02:00
|
|
|
MU_LOG = g_new(MuLog, 1);
|
|
|
|
MU_LOG->_fd = -1;
|
|
|
|
MU_LOG->_own = FALSE; /* nobody owns silence */
|
2009-11-29 23:04:43 +01: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)
|
|
|
|
{
|
2010-01-06 00:26:18 +01:00
|
|
|
if (log_level == G_LOG_LEVEL_DEBUG && !MU_LOG->_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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2010-01-08 19:53:40 +01:00
|
|
|
mu_log_init_with_fd (int fd, gboolean doclose,
|
|
|
|
gboolean quiet, gboolean debug)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (!MU_LOG, FALSE);
|
|
|
|
|
|
|
|
MU_LOG = g_new(MuLog, 1);
|
|
|
|
|
|
|
|
MU_LOG->_fd = fd;
|
2010-01-06 20:42:59 +01:00
|
|
|
MU_LOG->_quiet = quiet;
|
2009-11-29 21:48:49 +01:00
|
|
|
MU_LOG->_debug = debug;
|
|
|
|
MU_LOG->_own = doclose; /* if we now own the fd, close it
|
|
|
|
* in _destroy */
|
2009-12-05 15:00:23 +01:00
|
|
|
MU_LOG->_old_log_func =
|
|
|
|
g_log_set_default_handler ((GLogFunc)log_handler, NULL);
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
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;
|
2010-02-04 21:44:42 +01: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);
|
2010-02-04 21:44:42 +01:00
|
|
|
|
2010-08-16 23:41:37 +02:00
|
|
|
if (rv != 0) {
|
2010-07-24 19:28:39 +02: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;
|
|
|
|
|
|
|
|
if (stat (logfile, &statbuf) != 0) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return TRUE; /* it did not exist yet, no problem */
|
|
|
|
else {
|
2010-10-09 11:44:48 +02:00
|
|
|
g_warning ("failed to stat(2) %s", logfile);
|
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
|
2010-02-03 20:01:08 +01:00
|
|
|
mu_log_init (const char* muhome,
|
|
|
|
gboolean backup, gboolean quiet, gboolean debug)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
int fd;
|
2009-12-05 15:00:23 +01:00
|
|
|
gchar *logfile;
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
/* only init once... */
|
2009-12-05 15:00:23 +01:00
|
|
|
g_return_val_if_fail (!MU_LOG, FALSE);
|
|
|
|
g_return_val_if_fail (muhome, FALSE);
|
2010-01-04 19:18:23 +01:00
|
|
|
|
|
|
|
if (!mu_util_create_dir_maybe(muhome)) {
|
2010-10-09 11:44:48 +02:00
|
|
|
g_warning ("failed to init log in %s", muhome);
|
2010-01-04 19:18:23 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2009-12-05 15:00:23 +01:00
|
|
|
logfile = g_strdup_printf ("%s%c%s", muhome,
|
|
|
|
G_DIR_SEPARATOR, MU_LOG_FILE);
|
2010-02-03 20:01:08 +01:00
|
|
|
|
|
|
|
if (backup && !log_file_backup_maybe(logfile)) {
|
2010-10-09 11:44:48 +02:00
|
|
|
g_warning ("failed to backup log file");
|
2010-02-03 20:01:08 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-30 19:52:30 +02:00
|
|
|
fd = open (logfile, O_WRONLY|O_CREAT|O_APPEND, 00600);
|
2009-12-05 15:00:23 +01:00
|
|
|
if (fd < 0)
|
2010-09-12 15:25:06 +02:00
|
|
|
g_warning ("%s: open() of '%s' failed: %s", __FUNCTION__,
|
2009-12-05 15:00:23 +01:00
|
|
|
logfile, strerror(errno));
|
|
|
|
g_free (logfile);
|
|
|
|
|
2010-01-06 20:42:59 +01:00
|
|
|
if (fd < 0 || !mu_log_init_with_fd (fd, FALSE, quiet, debug)) {
|
2010-01-15 21:11:51 +01:00
|
|
|
try_close (fd);
|
2009-11-29 21:48:49 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_log_uninit (void)
|
|
|
|
{
|
2010-11-20 15:14:26 +01:00
|
|
|
if (!MU_LOG)
|
|
|
|
return;
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
if (MU_LOG->_own)
|
2010-01-15 21:11:51 +01:00
|
|
|
try_close (MU_LOG->_fd);
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
g_free (MU_LOG);
|
|
|
|
MU_LOG = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
pfx (GLogLevelFlags level)
|
|
|
|
{
|
|
|
|
switch (level) {
|
|
|
|
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 ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-15 21:11:51 +01:00
|
|
|
log_write (const char* domain, GLogLevelFlags level,
|
|
|
|
const gchar *msg)
|
2009-11-29 21:48:49 +01:00
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
/* log lines will be truncated at 255 chars */
|
2010-01-02 15:05:55 +01:00
|
|
|
char buf [512], timebuf [32];
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
/* get the time/date string */
|
|
|
|
now = time(NULL);
|
|
|
|
strftime (timebuf, sizeof(timebuf), "%F %T", localtime(&now));
|
|
|
|
|
|
|
|
/* now put it all together */
|
2009-12-05 15:00:23 +01:00
|
|
|
len = snprintf (buf, sizeof(buf), "%s [%s] %s\n", timebuf,
|
|
|
|
pfx(level), msg);
|
2010-01-02 15:05:55 +01:00
|
|
|
/* if the buffer is full, add a newline */
|
|
|
|
if (len == sizeof(buf))
|
|
|
|
buf[sizeof(buf)-2] = '\n';
|
2009-11-29 21:48:49 +01:00
|
|
|
|
2010-09-26 16:29:54 +02:00
|
|
|
len = write (MU_LOG->_fd, buf, (size_t)len);
|
2009-11-29 21:48:49 +01:00
|
|
|
if (len < 0)
|
|
|
|
fprintf (stderr, "%s: failed to write to log: %s\n",
|
|
|
|
__FUNCTION__, strerror(errno));
|
2010-01-06 00:26:18 +01:00
|
|
|
|
2010-07-30 19:52:30 +02:00
|
|
|
if (!(MU_LOG->_quiet) && (level & G_LOG_LEVEL_MESSAGE))
|
2010-01-06 00:26:18 +01:00
|
|
|
g_print ("mu: %s\n", msg);
|
2009-11-29 21:48:49 +01:00
|
|
|
|
|
|
|
/* for serious errors, log them to stderr as well */
|
|
|
|
if (level & G_LOG_LEVEL_ERROR)
|
2010-02-08 20:19:17 +01:00
|
|
|
g_printerr ("mu: %s\n", msg);
|
2009-11-29 21:48:49 +01:00
|
|
|
else if (level & G_LOG_LEVEL_CRITICAL)
|
2010-02-08 20:19:17 +01:00
|
|
|
g_printerr ("mu: %s\n", msg);
|
2009-11-29 21:48:49 +01:00
|
|
|
else if (level & G_LOG_LEVEL_WARNING)
|
2010-02-08 20:19:17 +01:00
|
|
|
g_printerr ("mu: %s\n", msg);
|
2009-11-29 21:48:49 +01:00
|
|
|
}
|