minix/include/minix/log.h
Lionel Sambuc 84d9c625bf Synchronize on NetBSD-CVS (2013/12/1 12:00:00 UTC)
- Fix for possible unset uid/gid in toproto
 - Fix for default mtree style
 - Update libelf
 - Importing libexecinfo
 - Resynchronize GCC, mpc, gmp, mpfr
 - build.sh: Replace params with show-params.
     This has been done as the make target has been renamed in the same
     way, while a new target named params has been added. This new
     target generates a file containing all the parameters, instead of
     printing it on the console.
 - Update test48 with new etc/services (Fix by Ben Gras <ben@minix3.org)
     get getservbyport() out of the inner loop

Change-Id: Ie6ad5226fa2621ff9f0dee8782ea48f9443d2091
2014-07-28 17:05:06 +02:00

117 lines
2.8 KiB
C

#ifndef __LOG_H__
#define __LOG_H__
/*
* Simple logging functions
*/
#include <stdarg.h>
/*
* LEVEL_NONE do not log anything.
* LEVEL_WARN Information that needs to be known.
* LEVEL_INFO Basic information like startup messages and occasional events.
* LEVEL_DEBUG debug statements about things happening that are less expected.
* LEVEL_TRACE Way to much information for anybody.
*/
#define LEVEL_NONE 0
#define LEVEL_WARN 1
#define LEVEL_INFO 2
#define LEVEL_DEBUG 3
#define LEVEL_TRACE 4
static const char *level_string[5] = {
"none",
"warn",
"info",
"debug",
"trace"
};
/*
* struct to be initialized by the user of the logging system.
*
* name: The name attribute is used in logging statements do differentiate
* drivers
*
* log_level The level attribute describes the requested logging level. a level
* of 1 will only print warnings while a level of 4 will print all the trace
* information.
*
* log_func The logging function to use to log, log.h provides default_log
* to display information on the kernel output buffer. As a bonus if the
* requested log level is debug or trace the method , file and line number will
* be printed to the steam.
*/
struct log
{
const char *name;
int log_level;
/* the logging function itself */
void (*log_func) (struct log * driver,
int level,
const char *file,
const char *function, int line, const char *fmt, ...);
};
#define __log(driver,log_level, fmt, args...) \
((driver)->log_func(driver,log_level, \
__FILE__, __FUNCTION__, __LINE__,\
fmt, ## args))
/* Log a warning */
#define log_warn(driver, fmt, args...) \
__log(driver, LEVEL_WARN, fmt, ## args)
/* Log an information message */
#define log_info(driver, fmt, args...) \
__log(driver, LEVEL_INFO, fmt, ## args)
/* log debugging output */
#define log_debug(driver, fmt, args...) \
__log(driver, LEVEL_DEBUG, fmt, ## args)
/* log trace output */
#define log_trace(driver, fmt, args...) \
__log(driver, LEVEL_TRACE, fmt, ## args)
#endif /* __LOG_H__ */
static void
default_log(struct log *driver,
int level,
const char *file, const char *function, int line, const char *fmt, ...)
{
va_list args;
if (level > driver->log_level) {
return;
}
/* If the wanted level is debug also display line/method information */
if (driver->log_level >= LEVEL_DEBUG) {
printf("%s(%s):%s+%d(%s):", driver->name,
level_string[level], file, line, function);
} else {
printf("%s(%s)", driver->name, level_string[level]);
}
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
#ifdef hacks
static void
hexdump(unsigned char *d, unsigned int size)
{
int s;
for (s = 0; s < size; s += 4) {
fprintf(stdout, "0x%04x 0x%02X%02X%02X%02X %c%c%c%c\n", s,
(unsigned int) d[s], (unsigned int) d[s + 1],
(unsigned int) d[s + 2], (unsigned int) d[s + 3], d[s],
d[s + 1], d[s + 2], d[s + 3]);
}
}
#endif