Cleanup + implement conditional logging

This patch:
* fixes signatures for log_debug and log_pdu macros
* collapses __log_<LEVEL> functions into one
* adds conditional logging

Here conditional logging means that the logging priority and logging level may
vary depending on some dynamic parameters. This is helpful when software is
able to recover from errors which occur periodically and hence, there is no
need to pollute system logs with tons of repetitive non-critical lines.

The following macros are added to support this feature:
* log_info_cond
* log_warning_cond
* log_error_cond

They take an additional first parameter called "level". If it is 0 then they
act as their non-cond counterparts, otherwise they act like log_debug with the
specified level.

Those who are not interested in conditional logging are not affected: they can
continue using plain old logging macros without any changes.

Signed-off-by: Sergey Myasnikov <tigra564@gmail.com>



git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@4161 d57e44dd-8a1f-0410-8b47-8ef2f437770f
This commit is contained in:
Vladislav Bolkhovitin
2012-03-12 22:31:13 +00:00
parent 5c2cde6757
commit 7a87bf25a6
2 changed files with 34 additions and 47 deletions

View File

@@ -23,6 +23,7 @@
#include <sys/poll.h>
#include <assert.h>
#include <netdb.h>
#include <syslog.h>
#include "types.h"
#ifdef INSIDE_KERNEL_TREE
@@ -264,21 +265,27 @@ extern int log_daemon;
extern int log_level;
extern void log_init(void);
extern void __log_info(const char *func, int line, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
extern void __log_warning(const char *func, int line, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
extern void __log_error(const char *func, int line, const char *fmt, ...)
__attribute__ ((format (printf, 3, 4)));
extern void __log_debug(const char *func, int line, int level, const char *fmt, ...)
__attribute__ ((format (printf, 4, 5)));
extern void __log(const char *func, int line, int prio, int level, const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
extern void __log_pdu(const char *func, int line, int level, struct PDU *pdu);
#define log_info(args...) __log_info(__func__, __LINE__, ## args)
#define log_warning(args...) __log_warning(__func__, __LINE__, ## args)
#define log_error(args...) __log_error(__func__, __LINE__, ## args)
#define log_debug(args...) __log_debug(__func__, __LINE__, ## args)
#define log_pdu(args...) __log_pdu(__func__, __LINE__, ## args)
#define log_info(args...) __log(__func__, __LINE__, LOG_INFO, 0, ## args)
#define log_warning(args...) __log(__func__, __LINE__, LOG_WARNING, 0, ## args)
#define log_error(args...) __log(__func__, __LINE__, LOG_ERR, 0, ## args)
#define log_debug(level, args...) __log(__func__, __LINE__, LOG_DEBUG, level, ## args)
#define log_pdu(level, args...) __log_pdu(__func__, __LINE__, level, ## args)
/* Conditional versions of log_* functions. Useful when log priority depends
* on some parameter, say recurrence of some event. In these cases the first
* occurence could be logged as log_info while the latter ones may be logged
* with log_debug. So, if level != 0 then log_debug is called.
*/
#define log_info_cond(level, args...) \
__log(__func__, __LINE__, LOG_INFO, level, ## args)
#define log_warning_cond(level, args...) \
__log(__func__, __LINE__, LOG_WARNING, level, ## args)
#define log_error_cond(level, args...) \
__log(__func__, __LINE__, LOG_ERR, level, ## args)
/* session.c */
extern struct session *session_find_name(u32 tid, const char *iname, union iscsi_sid sid);

View File

@@ -82,38 +82,18 @@ static void dolog(int prio, const char *func, int line, const char *fmt, va_list
}
}
void __log_info(const char *func, int line, const char *fmt, ...)
void __log(const char *func, int line, int prio, int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dolog(LOG_INFO, func, line, fmt, ap);
va_end(ap);
}
void __log_warning(const char *func, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dolog(LOG_WARNING, func, line, fmt, ap);
va_end(ap);
}
void __log_error(const char *func, int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
dolog(LOG_ERR, func, line, fmt, ap);
va_end(ap);
}
void __log_debug(const char *func, int line, int level, const char *fmt, ...)
{
if (log_level >= level) {
va_list ap;
va_start(ap, fmt);
dolog(LOG_DEBUG, func, line, fmt, ap);
va_end(ap);
if (level) {
prio = LOG_DEBUG;
if (log_level < level)
return;
}
va_list ap;
va_start(ap, fmt);
dolog(prio, func, line, fmt, ap);
va_end(ap);
}
/* Definition for __log_pdu buffer */
@@ -145,7 +125,7 @@ static void __dump_line(const char *func, int line_num, int level, unsigned char
}
/* buf is not \0-terminated! */
__log_debug(func, line_num, level, "%s %.*s |", line, BUFFER_SIZE, buf);
__log(func, line_num, LOG_DEBUG, level, "%s %.*s |", line, BUFFER_SIZE, buf);
*cp = 0;
}
@@ -172,19 +152,19 @@ void __log_pdu(const char *func, int line, int level, struct PDU *pdu)
return;
buf = (void *)&pdu->bhs;
__log_debug(func, line, level, "BHS: (%p)", buf);
__log(func, line, LOG_DEBUG, level, "BHS: (%p)", buf);
for (i = 0; i < BHS_SIZE; i++)
dump_char(*buf++);
dump_line();
buf = (void *)pdu->ahs;
__log_debug(func, line, level, "AHS: (%p)", buf);
__log(func, line, LOG_DEBUG, level, "AHS: (%p)", buf);
for (i = 0; i < pdu->ahssize; i++)
dump_char(*buf++);
dump_line();
buf = (void *)pdu->data;
__log_debug(func, line, level, "Data: (%p)", buf);
__log(func, line, LOG_DEBUG, level, "Data: (%p)", buf);
for (i = 0; i < pdu->datasize; i++)
dump_char(*buf++);
dump_line();