Improve logging

* Take advantage of gcc printf format checks
 * Move code dependent on log level to log.c
   This is a basis for runtime logging tweaks.
 * Remove the now useless LADISH_DEBUG defines
This commit is contained in:
Nedko Arnaudov 2012-12-02 20:21:36 +02:00
parent 9bbfff1608
commit 73acf78b8d
6 changed files with 100 additions and 48 deletions

View File

@ -2,7 +2,7 @@
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008, 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2008, 2009, 2010, 2012 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2008 Marc-Olivier Barre
*
**************************************************************************
@ -39,6 +39,7 @@
#define LADISH_XDG_SUBDIR "/" BASE_NAME
#define LADISH_XDG_LOG "/" BASE_NAME ".log"
#if !defined(LOG_OUTPUT_STDOUT)
static ino_t g_log_file_ino;
static FILE * g_logfile;
static char * g_log_filename;
@ -153,23 +154,56 @@ void ladish_log_uninit()
free(g_log_filename);
}
#endif /* #if !defined(LOG_OUTPUT_STDOUT) */
#if 0
# define log_debug(fmt, args...) ladish_log(LADISH_LOG_LEVEL_DEBUG, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, __func__, ## args)
# define log_info(fmt, args...) ladish_log(LADISH_LOG_LEVEL_INFO, fmt "\n", ## args)
# define log_warn(fmt, args...) ladish_log(LADISH_LOG_LEVEL_WARN, ANSI_COLOR_YELLOW "WARNING: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
# define log_error(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR, ANSI_COLOR_RED "ERROR: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
# define log_error_plain(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR_PLAIN, ANSI_COLOR_RED "ERROR: " ANSI_RESET fmt "\n", ## args)
#endif
static
bool
ladish_log_enabled(
unsigned int level,
const char * file,
unsigned int line,
const char * func)
{
return level != LADISH_LOG_LEVEL_DEBUG;
}
void
ladish_log(
unsigned int level,
const char * file,
unsigned int line,
const char * func,
const char * format,
...)
{
va_list ap;
FILE * stream;
#if !defined(LOG_OUTPUT_STDOUT)
time_t timestamp;
char timestamp_str[26];
#endif
const char * color;
if (!ladish_log_enabled(level, file, line, func))
{
return;
}
#if !defined(LOG_OUTPUT_STDOUT)
if (g_logfile != NULL && ladish_log_open())
{
stream = g_logfile;
}
else
#endif
{
switch (level)
{
@ -185,14 +219,44 @@ ladish_log(
}
}
#if !defined(LOG_OUTPUT_STDOUT)
time(&timestamp);
ctime_r(&timestamp, timestamp_str);
timestamp_str[24] = 0;
fprintf(stream, "%s: ", timestamp_str);
#endif
color = NULL;
switch (level)
{
case LADISH_LOG_LEVEL_DEBUG:
fprintf(stream, "%s:%d:%s ", file, line, func);
break;
case LADISH_LOG_LEVEL_WARN:
color = ANSI_COLOR_YELLOW;
break;
case LADISH_LOG_LEVEL_ERROR:
case LADISH_LOG_LEVEL_ERROR_PLAIN:
color = ANSI_COLOR_RED;
break;
}
if (color != NULL)
{
fputs(color, stream);
}
va_start(ap, format);
vfprintf(stream, format, ap);
fflush(stream);
va_end(ap);
if (color != NULL)
{
fputs(ANSI_RESET, stream);
}
fputs("\n", stream);
fflush(stream);
}

View File

@ -26,8 +26,6 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define LADISH_DEBUG
#include "common.h"
#include <unistd.h>

View File

@ -2,7 +2,7 @@
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2009,2010,2011 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2009,2010,2011,2012 Nedko Arnaudov <nedko@arnaudov.name>
*
**************************************************************************
* This file contains the liblash implementaiton
@ -34,8 +34,6 @@
#include "lash/lash.h"
//#define LADISH_DEBUG
#include "../../common.h"
#include "../../common/catdup.h"
#include "../../common/dirhelpers.h"

63
log.h
View File

@ -2,7 +2,7 @@
/*
* LADI Session Handler (ladish)
*
* Copyright (C) 2008, 2009 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2008, 2009, 2012 Nedko Arnaudov <nedko@arnaudov.name>
* Copyright (C) 2008 Juuso Alasuutari <juuso.alasuutari@gmail.com>
* Copyright (C) 2002 Robert Ham <rah@bash.sh>
*
@ -49,45 +49,32 @@
# endif
#endif
#ifndef LOG_OUTPUT_STDOUT
# define LADISH_LOG_LEVEL_DEBUG 0
# define LADISH_LOG_LEVEL_INFO 1
# define LADISH_LOG_LEVEL_WARN 2
# define LADISH_LOG_LEVEL_ERROR 3
# define LADISH_LOG_LEVEL_ERROR_PLAIN 4
#ifdef __cplusplus
extern "C"
#endif
void
ladish_log(unsigned int level,
const char *format,
...);
ladish_log(
unsigned int level,
const char * file,
unsigned int line,
const char * func,
const char * format,
...)
#if defined (__GNUC__)
__attribute__((format(printf, 5, 6)))
#endif
;
# ifdef LADISH_DEBUG
# define log_debug(fmt, args...) \
ladish_log(LADISH_LOG_LEVEL_DEBUG, "%s:%d:%s: " fmt "\n", __FILE__, __LINE__, __func__, ## args)
# else
# define log_debug(fmt, args...)
# endif /* LADISH_DEBUG */
#define LADISH_LOG_LEVEL_DEBUG 0
#define LADISH_LOG_LEVEL_INFO 1
#define LADISH_LOG_LEVEL_WARN 2
#define LADISH_LOG_LEVEL_ERROR 3
#define LADISH_LOG_LEVEL_ERROR_PLAIN 4
# define log_info(fmt, args...) ladish_log(LADISH_LOG_LEVEL_INFO, fmt "\n", ## args)
# define log_warn(fmt, args...) ladish_log(LADISH_LOG_LEVEL_WARN, ANSI_COLOR_YELLOW "WARNING: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
# define log_error(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR, ANSI_COLOR_RED "ERROR: " ANSI_RESET "%s: " fmt "\n", __func__, ## args)
# define log_error_plain(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR_PLAIN, ANSI_COLOR_RED "ERROR: " ANSI_RESET fmt "\n", ## args)
#else /* LOG_OUTPUT_STDOUT */
# ifdef LADISH_DEBUG
# define log_debug(fmt, args...) \
printf("%s:%d:%s: " fmt "\n", __FILE__, __LINE__, __func__, ## args)
# else
# define log_debug(fmt, args...)
# endif /* LADISH_DEBUG */
# define log_info(fmt, args...) printf(fmt "\n", ## args)
# define log_warn(fmt, args...) printf(fmt "\n", ## args)
# define log_error(fmt, args...) fprintf(stderr, "%s: " fmt "\n", __func__, ## args)
# define log_error_plain(fmt, args...) fprintf(stderr, fmt "\n", ## args)
#endif /* LOG_OUTPUT_STDOUT */
#define log_debug(fmt, args...) ladish_log(LADISH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __func__, fmt, ## args)
#define log_info(fmt, args...) ladish_log(LADISH_LOG_LEVEL_INFO, __FILE__, __LINE__, __func__, fmt, ## args)
#define log_warn(fmt, args...) ladish_log(LADISH_LOG_LEVEL_WARN, __FILE__, __LINE__, __func__, fmt, ## args)
#define log_error(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR, __FILE__, __LINE__, __func__, fmt, ## args)
#define log_error_plain(fmt, args...) ladish_log(LADISH_LOG_LEVEL_ERROR_PLAIN, __FILE__, __LINE__, __func__, fmt, ## args)
#endif /* __LADISH_LOG__ */

View File

@ -24,8 +24,6 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//#define LADISH_DEBUG
#include "jack_proxy.h"
jack_proxy_callback_server_started g_on_server_started;

View File

@ -348,7 +348,6 @@ def build(bld):
for source in [
'main.c',
'loader.c',
'log.c',
'sigsegv.c',
'proctitle.c',
'appdb.c',
@ -417,6 +416,7 @@ def build(bld):
daemon.source.append(os.path.join("cdbus", source))
for source in [
'log.c',
'time.c',
'dirhelpers.c',
'catdup.c',
@ -436,6 +436,11 @@ def build(bld):
jmcore.defines = ['LOG_OUTPUT_STDOUT']
jmcore.source = ['jmcore.c']
for source in [
'log.c',
]:
jmcore.source.append(os.path.join("common", source))
for source in [
#'signal.c',
'method.c',
@ -456,6 +461,7 @@ def build(bld):
ladiconfd.source = ['conf.c']
for source in [
'log.c',
'dirhelpers.c',
'catdup.c',
]:
@ -609,6 +615,7 @@ def build(bld):
gladish.source.append(os.path.join("cdbus", source))
for source in [
'log.c',
'catdup.c',
'file.c',
]: