mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-04 17:02:56 +03:00
Improved logging with various log levels
This commit is contained in:
1 parent
56048956ed
commit
e643c3256e
2 files changed
+66
-12
No files matched your search
@@ -6,10 +6,9 @@
|
||||
* README for terms of use.
|
||||
*/
|
||||
|
||||
#ifndef NDEBUG
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
@@ -25,6 +24,25 @@
|
||||
#include "debug.h"
|
||||
#include "net.h"
|
||||
|
||||
static coap_log_t maxlog = LOG_WARN; /* default maximum log level */
|
||||
|
||||
coap_log_t
|
||||
coap_get_log_level() {
|
||||
return maxlog;
|
||||
}
|
||||
|
||||
void
|
||||
coap_set_log_level(coap_log_t level) {
|
||||
maxlog = level;
|
||||
}
|
||||
|
||||
/* this array has the same order as the type log_t */
|
||||
static char *loglevels[] = {
|
||||
"EMRG", "ALRT", "CRIT", "WARN", "NOTE", "INFO", "DEBG"
|
||||
};
|
||||
|
||||
#ifndef NDEBUG
|
||||
|
||||
#ifdef HAVE_TIME_H
|
||||
|
||||
static inline size_t
|
||||
@@ -62,18 +80,27 @@ strnlen(unsigned char *s, size_t maxlen) {
|
||||
return n;
|
||||
}
|
||||
|
||||
void debug(char *format, ...) {
|
||||
static char timebuf[32];
|
||||
|
||||
void
|
||||
coap_log(coap_log_t level, char *format, ...) {
|
||||
char timebuf[32];
|
||||
coap_tick_t now;
|
||||
va_list ap;
|
||||
FILE *log_fd;
|
||||
|
||||
if (maxlog < level)
|
||||
return;
|
||||
|
||||
log_fd = level <= LOG_CRIT ? COAP_ERR_FD : COAP_DEBUG_FD;
|
||||
|
||||
coap_ticks(&now);
|
||||
if (print_timestamp(timebuf,sizeof(timebuf), now))
|
||||
fprintf(COAP_DEBUG_FD, "%s ", timebuf);
|
||||
fprintf(log_fd, "%s ", timebuf);
|
||||
|
||||
if (level <= LOG_DEBUG)
|
||||
fprintf(log_fd, "%s ", loglevels[level]);
|
||||
|
||||
va_start(ap, format);
|
||||
vfprintf(COAP_DEBUG_FD, format, ap);
|
||||
vfprintf(log_fd, format, ap);
|
||||
va_end(ap);
|
||||
fflush(stdout);
|
||||
}
|
||||
@@ -194,6 +221,4 @@ coap_show_pdu(const coap_pdu_t *pdu) {
|
||||
fflush(COAP_DEBUG_FD);
|
||||
}
|
||||
|
||||
#else
|
||||
static inline void placeholder(void) {}
|
||||
#endif /* NDEBUG */
|
||||
@@ -9,13 +9,39 @@
|
||||
#ifndef _COAP_DEBUG_H_
|
||||
#define _COAP_DEBUG_H_
|
||||
|
||||
#ifndef NDEBUG
|
||||
|
||||
#ifndef COAP_DEBUG_FD
|
||||
#define COAP_DEBUG_FD stdout
|
||||
#endif
|
||||
|
||||
void debug(char *,...);
|
||||
#ifndef COAP_ERR_FD
|
||||
#define COAP_ERR_FD stderr
|
||||
#endif
|
||||
|
||||
/** Pre-defined log levels akin to what is used in \b syslog. */
|
||||
typedef enum { LOG_EMERG=0, LOG_ALERT, LOG_CRIT, LOG_WARN,
|
||||
LOG_NOTICE, LOG_INFO, LOG_DEBUG
|
||||
} coap_log_t;
|
||||
|
||||
/** Returns the current log level. */
|
||||
coap_log_t coap_get_log_level();
|
||||
|
||||
/** Sets the log level to the specified value. */
|
||||
void coap_set_log_level(coap_log_t level);
|
||||
|
||||
/**
|
||||
* Writes the given text to @c COAP_ERR_FD (for @p level <= @c
|
||||
* LOG_CRIT) or @c COAP_DEBUG_FD (for @p level >= @c LOG_WARN). The
|
||||
* text is output only when @p level is below or equal to the log
|
||||
* level that set by coap_set_log_level().
|
||||
*/
|
||||
void coap_log(coap_log_t level, char *format, ...);
|
||||
|
||||
#ifndef NDEBUG
|
||||
|
||||
/* A set of convenience macros for common log levels. */
|
||||
#define info(...) coap_log(LOG_INFO, __VA_ARGS__)
|
||||
#define warn(...) coap_log(LOG_WARN, __VA_ARGS__)
|
||||
#define debug(...) coap_log(LOG_DEBUG, __VA_ARGS__)
|
||||
|
||||
#include "pdu.h"
|
||||
void coap_show_pdu(const coap_pdu_t *);
|
||||
@@ -26,6 +52,9 @@ size_t coap_print_addr(const struct __coap_address_t *, unsigned char *, size_t)
|
||||
#else
|
||||
|
||||
#define debug(...)
|
||||
#define info(...)
|
||||
#define warn(...)
|
||||
|
||||
#define coap_show_pdu(x)
|
||||
#define coap_print_addr(...)
|
||||
|
||||
|
||||
Reference in new issue
Block a user