mirror of https://github.com/djcb/mu.git
* mu-log: set the default handler, fixes logging
This commit is contained in:
parent
63a2680873
commit
3f169f72aa
51
src/mu-log.c
51
src/mu-log.c
|
@ -30,11 +30,14 @@
|
||||||
|
|
||||||
#include "mu-log.h"
|
#include "mu-log.h"
|
||||||
|
|
||||||
|
#define MU_LOG_FILE "mu.log"
|
||||||
|
|
||||||
struct _MuLog {
|
struct _MuLog {
|
||||||
int _fd; /* log file descriptor */
|
int _fd; /* log file descriptor */
|
||||||
int _own; /* close _fd with log_destroy? */
|
int _own; /* close _fd with log_destroy? */
|
||||||
int _log_id; /* log handler id */
|
|
||||||
int _debug; /* add debug-level stuff? */
|
int _debug; /* add debug-level stuff? */
|
||||||
|
|
||||||
|
GLogFunc _old_log_func;
|
||||||
};
|
};
|
||||||
typedef struct _MuLog MuLog;
|
typedef struct _MuLog MuLog;
|
||||||
|
|
||||||
|
@ -67,12 +70,8 @@ mu_log_init_silence (void)
|
||||||
MU_LOG->_fd = -1;
|
MU_LOG->_fd = -1;
|
||||||
MU_LOG->_own = FALSE; /* nobody owns silence */
|
MU_LOG->_own = FALSE; /* nobody owns silence */
|
||||||
|
|
||||||
MU_LOG->_log_id =
|
MU_LOG->_old_log_func =
|
||||||
g_log_set_handler (NULL,
|
g_log_set_default_handler ((GLogFunc)silence, NULL);
|
||||||
G_LOG_LEVEL_DEBUG|
|
|
||||||
G_LOG_LEVEL_MESSAGE|
|
|
||||||
G_LOG_LEVEL_INFO,
|
|
||||||
(GLogFunc)silence, NULL);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,34 +98,32 @@ mu_log_init_with_fd (int fd, gboolean doclose, gboolean debug)
|
||||||
MU_LOG->_debug = debug;
|
MU_LOG->_debug = debug;
|
||||||
MU_LOG->_own = doclose; /* if we now own the fd, close it
|
MU_LOG->_own = doclose; /* if we now own the fd, close it
|
||||||
* in _destroy */
|
* in _destroy */
|
||||||
|
MU_LOG->_old_log_func =
|
||||||
g_log_set_handler ("mu", G_LOG_LEVEL_MASK,
|
g_log_set_default_handler ((GLogFunc)log_handler, NULL);
|
||||||
(GLogFunc)log_handler, NULL);
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
mu_log_init_with_file (const char* file, gboolean append, gboolean debug)
|
mu_log_init (const char* muhome, gboolean append, 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);
|
||||||
|
|
||||||
fd = -1;
|
logfile = g_strdup_printf ("%s%c%s", muhome,
|
||||||
if (file) {
|
G_DIR_SEPARATOR, MU_LOG_FILE);
|
||||||
fd = open (file,
|
fd = open (logfile, O_WRONLY|O_CREAT|(append ? O_APPEND : O_TRUNC),
|
||||||
O_WRONLY|O_CREAT|(append ? O_APPEND : O_TRUNC),
|
00600);
|
||||||
00600);
|
if (fd < 0)
|
||||||
if (fd < 0) {
|
g_warning ("%s: open() of '%s' failed: %s\n", __FUNCTION__,
|
||||||
fprintf (stderr, "%s: open() of '%s' failed: %s\n",
|
logfile, strerror(errno));
|
||||||
__FUNCTION__, file, strerror(errno));
|
g_free (logfile);
|
||||||
return FALSE;
|
|
||||||
}
|
if (fd < 0 || !mu_log_init_with_fd (fd, FALSE, debug)) {
|
||||||
}
|
|
||||||
|
|
||||||
if (!mu_log_init_with_fd (fd, FALSE, debug)) {
|
|
||||||
try_close (fd);
|
try_close (fd);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
@ -178,8 +175,8 @@ log_write (const char* domain, GLogLevelFlags level,
|
||||||
strftime (timebuf, sizeof(timebuf), "%F %T", localtime(&now));
|
strftime (timebuf, sizeof(timebuf), "%F %T", localtime(&now));
|
||||||
|
|
||||||
/* now put it all together */
|
/* now put it all together */
|
||||||
len = snprintf (buf, sizeof(buf), "%s [%s] [%s] %s\n", timebuf,
|
len = snprintf (buf, sizeof(buf), "%s [%s] %s\n", timebuf,
|
||||||
pfx(level), domain, msg);
|
pfx(level), msg);
|
||||||
|
|
||||||
len = write (MU_LOG->_fd, buf, len);
|
len = write (MU_LOG->_fd, buf, len);
|
||||||
if (len < 0)
|
if (len < 0)
|
||||||
|
|
23
src/mu-log.h
23
src/mu-log.h
|
@ -24,6 +24,18 @@
|
||||||
|
|
||||||
/* mu log is the global logging system */
|
/* mu log is the global logging system */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* write logging information to a log file
|
||||||
|
*
|
||||||
|
* @param muhome the mu home directory
|
||||||
|
* @param append append to logfile, instead of overwriting
|
||||||
|
* @param debug include debug-level information.
|
||||||
|
*
|
||||||
|
* @return TRUE if initialization succeeds, FALSE otherwise
|
||||||
|
*/
|
||||||
|
gboolean mu_log_init (const char* muhome, gboolean append,
|
||||||
|
gboolean debug);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* write logging information to a file descriptor
|
* write logging information to a file descriptor
|
||||||
*
|
*
|
||||||
|
@ -36,17 +48,6 @@
|
||||||
gboolean mu_log_init_with_fd (int fd, gboolean doclose, gboolean debug);
|
gboolean mu_log_init_with_fd (int fd, gboolean doclose, gboolean debug);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* write logging information to a file descriptor
|
|
||||||
*
|
|
||||||
* @param a filename to log to
|
|
||||||
* @param append append to logfile, instead of overwriting
|
|
||||||
* @param debug include debug-level information.
|
|
||||||
*
|
|
||||||
* @return TRUE if initialization succeeds, FALSE otherwise
|
|
||||||
*/
|
|
||||||
gboolean mu_log_init_with_file (const char* file, gboolean append,
|
|
||||||
gboolean debug);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* be absolutely silent, except for runtime errors, which will be
|
* be absolutely silent, except for runtime errors, which will be
|
||||||
|
|
Loading…
Reference in New Issue