* support different log files for different front ends (mu, mug, procmule etc.)

This commit is contained in:
Dirk-Jan C. Binnema 2011-07-23 17:56:25 +03:00
parent 0ce9f78ed8
commit 6af9b47405
4 changed files with 81 additions and 51 deletions

View File

@ -165,23 +165,14 @@ log_file_backup_maybe (const char *logfile)
gboolean gboolean
mu_log_init (const char* muhome, mu_log_init (const char* logfile, gboolean backup,
gboolean backup, gboolean quiet, gboolean debug) gboolean quiet, gboolean debug)
{ {
int fd; int fd;
gchar *logfile;
/* only init once... */ /* only init once... */
g_return_val_if_fail (!MU_LOG, FALSE); g_return_val_if_fail (!MU_LOG, FALSE);
g_return_val_if_fail (muhome, FALSE); g_return_val_if_fail (logfile, FALSE);
if (!mu_util_create_dir_maybe(muhome, 0700)) {
g_warning ("failed to init log in %s", muhome);
return FALSE;
}
logfile = g_strdup_printf ("%s%c%s", muhome,
G_DIR_SEPARATOR, MU_LOG_FILE);
if (backup && !log_file_backup_maybe(logfile)) { if (backup && !log_file_backup_maybe(logfile)) {
g_warning ("failed to backup log file"); g_warning ("failed to backup log file");
@ -192,7 +183,6 @@ mu_log_init (const char* muhome,
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));
g_free (logfile);
if (fd < 0 || !mu_log_init_with_fd (fd, FALSE, quiet, debug)) { if (fd < 0 || !mu_log_init_with_fd (fd, FALSE, quiet, debug)) {
try_close (fd); try_close (fd);

View File

@ -31,7 +31,8 @@ G_BEGIN_DECLS
/** /**
* write logging information to a log file * write logging information to a log file
* *
* @param muhome the mu home directory * @param full path to the log file (does not have to exist yet, but
* it's directory must)
* @param backup if TRUE and size of log file > MU_MAX_LOG_FILE_SIZE, move * @param backup if TRUE and size of log file > MU_MAX_LOG_FILE_SIZE, move
* the log file to <log file>.old and start a new one. The .old file will overwrite * the log file to <log file>.old and start a new one. The .old file will overwrite
* existing files of that name * existing files of that name
@ -40,7 +41,7 @@ G_BEGIN_DECLS
* *
* @return TRUE if initialization succeeds, FALSE otherwise * @return TRUE if initialization succeeds, FALSE otherwise
*/ */
gboolean mu_log_init (const char* muhome, gboolean backup, gboolean mu_log_init (const char *logfile, gboolean backup,
gboolean quiet, gboolean debug) gboolean quiet, gboolean debug)
G_GNUC_WARN_UNUSED_RESULT; G_GNUC_WARN_UNUSED_RESULT;

View File

@ -1,4 +1,4 @@
/* -*- 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) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
@ -42,6 +42,7 @@
struct _MuRuntimeData { struct _MuRuntimeData {
gchar *_str[MU_RUNTIME_PATH_NUM]; gchar *_str[MU_RUNTIME_PATH_NUM];
MuConfig *_config; MuConfig *_config;
gchar *_name; /* e.g., 'mu', 'mug' */
}; };
typedef struct _MuRuntimeData MuRuntimeData; typedef struct _MuRuntimeData MuRuntimeData;
@ -49,8 +50,10 @@ typedef struct _MuRuntimeData MuRuntimeData;
static gboolean _initialized = FALSE; static gboolean _initialized = FALSE;
static MuRuntimeData *_data = NULL; static MuRuntimeData *_data = NULL;
static void runtime_free (void); static void runtime_free (void);
static gboolean init_paths (const char* muhome, MuRuntimeData *data); static gboolean init_paths (const char* muhome, MuRuntimeData *data);
static const char* runtime_path (MuRuntimePath path);
static gboolean static gboolean
mu_dir_is_readable_and_writable (const char *muhome) mu_dir_is_readable_and_writable (const char *muhome)
@ -64,14 +67,38 @@ mu_dir_is_readable_and_writable (const char *muhome)
return FALSE; return FALSE;
} }
static gboolean
init_log (const char *muhome, const char *name,
gboolean log_stderr, gboolean quiet, gboolean debug)
{
gboolean rv;
char *logpath;
if (log_stderr)
return mu_log_init_with_fd (fileno(stderr), FALSE,
quiet, debug);
logpath = g_strdup_printf ("%s%c%s%c%s.log",
muhome, G_DIR_SEPARATOR,
MU_LOG_DIRNAME, G_DIR_SEPARATOR,
name);
rv = mu_log_init (logpath, TRUE, quiet, debug);
g_free (logpath);
return rv;
}
gboolean gboolean
mu_runtime_init (const char* muhome_arg) mu_runtime_init (const char* muhome_arg, const char *name)
{ {
gchar *muhome; gchar *muhome;
g_return_val_if_fail (!_initialized, FALSE); g_return_val_if_fail (!_initialized, FALSE);
g_return_val_if_fail (name, FALSE);
if (!mu_util_init_system()) if (!mu_util_init_system())
return FALSE; return FALSE;
@ -85,35 +112,28 @@ mu_runtime_init (const char* muhome_arg)
return FALSE; return FALSE;
} }
if (!mu_log_init (muhome, TRUE, FALSE, FALSE)) { _data = g_new0 (MuRuntimeData, 1);
_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;
init_paths (muhome, _data);
_data->_name = g_strdup (name);
if (!init_log (muhome, name, FALSE, FALSE, FALSE)) {
runtime_free ();
g_free (muhome); g_free (muhome);
return FALSE; return FALSE;
} }
_data = g_new0 (MuRuntimeData, 1);
_data->_str[MU_RUNTIME_PATH_MUHOME] = muhome;
init_paths (muhome, _data);
return _initialized = TRUE; return _initialized = TRUE;
} }
static gboolean
init_log (MuConfig *opts)
{
if (opts->log_stderr)
return mu_log_init_with_fd (fileno(stderr), FALSE,
opts->quiet, opts->debug);
else
return mu_log_init (opts->muhome, TRUE, opts->quiet,
opts->debug);
}
gboolean gboolean
mu_runtime_init_from_cmdline (int *pargc, char ***pargv) mu_runtime_init_from_cmdline (int *pargc, char ***pargv, const char *name)
{ {
g_return_val_if_fail (!_initialized, FALSE); g_return_val_if_fail (!_initialized, FALSE);
g_return_val_if_fail (name, FALSE);
if (!mu_util_init_system()) if (!mu_util_init_system())
return FALSE; return FALSE;
@ -129,15 +149,19 @@ mu_runtime_init_from_cmdline (int *pargc, char ***pargv)
return FALSE; return FALSE;
} }
if (!init_log (_data->_config)) { _data->_name = g_strdup (name);
runtime_free ();
return FALSE;
}
_data->_str[MU_RUNTIME_PATH_MUHOME] = _data->_str[MU_RUNTIME_PATH_MUHOME] =
g_strdup (_data->_config->muhome); g_strdup (_data->_config->muhome);
init_paths (_data->_str[MU_RUNTIME_PATH_MUHOME], _data); init_paths (_data->_str[MU_RUNTIME_PATH_MUHOME], _data);
if (!init_log (runtime_path(MU_RUNTIME_PATH_MUHOME), name,
_data->_config->log_stderr,
_data->_config->quiet,
_data->_config->debug)) {
runtime_free ();
return FALSE;
}
return _initialized = TRUE; return _initialized = TRUE;
} }
@ -150,6 +174,8 @@ runtime_free (void)
for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i) for (i = 0; i != MU_RUNTIME_PATH_NUM; ++i)
g_free (_data->_str[i]); g_free (_data->_str[i]);
g_free (_data->_name);
mu_config_destroy (_data->_config); mu_config_destroy (_data->_config);
mu_log_uninit(); mu_log_uninit();
@ -208,15 +234,23 @@ init_paths (const char* muhome, MuRuntimeData *data)
G_DIR_SEPARATOR, MU_CONTACTS_FILENAME); G_DIR_SEPARATOR, MU_CONTACTS_FILENAME);
data->_str [MU_RUNTIME_PATH_LOG] = data->_str [MU_RUNTIME_PATH_LOG] =
g_strdup_printf ("%s%c%s", muhome, G_DIR_SEPARATOR, g_strdup_printf ("%s%c%s", muhome,
MU_LOG_DIRNAME); G_DIR_SEPARATOR, MU_LOG_DIRNAME);
if (!create_dirs_maybe (data)) if (!create_dirs_maybe (data))
return FALSE; return FALSE;
return TRUE; return TRUE;
} }
/* so we can called when _initialized is FALSE still */
static const char*
runtime_path (MuRuntimePath path)
{
return _data->_str[path];
}
const char* const char*
mu_runtime_path (MuRuntimePath path) mu_runtime_path (MuRuntimePath path)
@ -224,7 +258,7 @@ mu_runtime_path (MuRuntimePath path)
g_return_val_if_fail (_initialized, NULL); g_return_val_if_fail (_initialized, NULL);
g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL); g_return_val_if_fail (path < MU_RUNTIME_PATH_NUM, NULL);
return _data->_str[path]; return runtime_path (path);
} }
MuConfig* MuConfig*

View File

@ -29,11 +29,13 @@ G_BEGIN_DECLS
* initialize the mu runtime system; initializes logging and other * initialize the mu runtime system; initializes logging and other
* systems. To uninitialize, use mu_runtime_uninit * systems. To uninitialize, use mu_runtime_uninit
* *
* @param muhome path where to find the mu home directory (typicaly, ~/.mu) * @param muhome path where to find the mu home directory (typicaly, ~/.mu)
* @param name of the main program, ie. 'mu', 'mug' or
* 'procmule'. this influences the name of the e.g. the logfile
* *
* @return TRUE if succeeded, FALSE in case of error * @return TRUE if succeeded, FALSE in case of error
*/ */
gboolean mu_runtime_init (const char* muhome); gboolean mu_runtime_init (const char *muhome, const char *name);
/** /**
@ -44,10 +46,13 @@ gboolean mu_runtime_init (const char* muhome);
* *
* @param ptr to the param count (typically, argc) * @param ptr to the param count (typically, argc)
* @param ptr to the params (typically, argv) * @param ptr to the params (typically, argv)
* @param name of the main program, ie. 'mu', 'mug' or
* 'procmule'. this influences the name of the e.g. the logfile
* *
* @return TRUE if succeeded, FALSE in case of error * @return TRUE if succeeded, FALSE in case of error
*/ */
gboolean mu_runtime_init_from_cmdline (int *pargc, char ***pargv); gboolean mu_runtime_init_from_cmdline (int *pargc, char ***pargv,
const char *name);
/** /**