freewtp/src/common/capwap_logging.c

197 lines
4.5 KiB
C
Raw Normal View History

2016-02-29 14:23:41 +01:00
#include <ctype.h>
2013-05-01 14:52:55 +02:00
#include "capwap.h"
#include "capwap_logging.h"
#ifdef CAPWAP_MULTITHREADING_ENABLE
#include "capwap_lock.h"
static capwap_lock_t l_loglock;
#endif
/* */
static int logginglevel = LOG_NONE;
2013-05-01 14:52:55 +02:00
static int loggingoutputstdout = 0;
static int loggingoutputstderr = 0;
/* */
static char logginglevelid[] = { 'N', 'F', 'E', 'W', 'I', 'D' };
/* */
static void prefix_logging(int level, char* buffer) {
time_t timenow;
struct tm* tmnow;
2013-11-19 23:25:07 +01:00
#ifdef CAPWAP_MULTITHREADING_ENABLE
pthread_t threadid = pthread_self();
#endif
2013-05-01 14:52:55 +02:00
time(&timenow);
tmnow = localtime(&timenow);
2013-11-19 23:25:07 +01:00
#ifdef CAPWAP_MULTITHREADING_ENABLE
sprintf(buffer, "[%02d/%02d/%04d %02d:%02d:%02d] [%08x] <%c> ", tmnow->tm_mday, tmnow->tm_mon + 1, tmnow->tm_year + 1900, tmnow->tm_hour, tmnow->tm_min, tmnow->tm_sec, (unsigned int)threadid, logginglevelid[level]);
#else
2013-05-01 14:52:55 +02:00
sprintf(buffer, "[%02d/%02d/%04d %02d:%02d:%02d] <%c> ", tmnow->tm_mday, tmnow->tm_mon + 1, tmnow->tm_year + 1900, tmnow->tm_hour, tmnow->tm_min, tmnow->tm_sec, logginglevelid[level]);
2013-11-19 23:25:07 +01:00
#endif
2013-05-01 14:52:55 +02:00
}
/* */
void capwap_logging_init() {
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_init(&l_loglock);
#endif
}
/* */
void capwap_logging_close() {
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_destroy(&l_loglock);
#endif
}
/* */
void capwap_logging_verboselevel(int level) {
2013-05-01 14:52:55 +02:00
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
logginglevel = level;
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
}
/* */
void capwap_logging_disable_allinterface() {
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
loggingoutputstdout = 0;
loggingoutputstderr = 0;
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
}
/* */
void capwap_logging_enable_console(int error) {
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
/* Enable only one of stdout/stderr */
loggingoutputstdout = (error ? 0 : 1);
loggingoutputstderr = !loggingoutputstdout;
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
}
2013-06-16 12:09:57 +02:00
/* */
void capwap_logging_disable_console(void) {
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
loggingoutputstdout = 0;
loggingoutputstderr = 0;
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
}
2013-05-01 14:52:55 +02:00
/* */
#ifdef ENABLE_LOGGING
void capwap_logging_printf(int level, const char* format, ...)
{
int errsv = errno;
2013-05-01 14:52:55 +02:00
va_list args;
char prefix[256];
va_start(args, format);
2013-05-01 14:52:55 +02:00
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
if (level <= logginglevel) {
2013-05-01 14:52:55 +02:00
prefix_logging(level, prefix);
if (loggingoutputstdout || loggingoutputstderr) {
FILE* output = (loggingoutputstdout ? stdout : stderr);
fprintf(output, "%s", prefix);
2013-05-01 14:52:55 +02:00
vfprintf(output, format, args);
fprintf(output, "\n");
fflush(output);
}
}
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
va_end(args);
errno = errsv;
2013-05-01 14:52:55 +02:00
}
2016-02-29 14:23:41 +01:00
void capwap_logging_hexdump(int level, const char *title, const unsigned char *data, size_t len)
2016-02-29 14:23:41 +01:00
{
int errsv = errno;
2016-02-29 14:23:41 +01:00
char prefix[256];
if (level > logginglevel)
2016-02-29 14:23:41 +01:00
return;
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_enter(&l_loglock);
#endif
prefix_logging(level, prefix);
if (loggingoutputstdout || loggingoutputstderr) {
FILE* output = (loggingoutputstdout ? stdout : stderr);
const uint8_t *pos = data;
fprintf(output, "%s%s - hexdump(len=%zd):\n", prefix, title, len);
while (len) {
size_t llen;
int i;
llen = len > 16 ? 16 : len;
fprintf(output, "%s ", prefix);
for (i = 0; i < llen; i++)
fprintf(output, " %02x", pos[i]);
for (i = llen; i < 16; i++)
fprintf(output, " ");
fprintf(output, " ");
for (i = 0; i < llen; i++) {
if (isprint(pos[i]))
fprintf(output, "%c", pos[i]);
else
fprintf(output, ".");
}
for (i = llen; i < 16; i++)
fprintf(output, " ");
fprintf(output, "\n");
pos += llen;
len -= llen;
}
fflush(output);
}
#ifdef CAPWAP_MULTITHREADING_ENABLE
capwap_lock_exit(&l_loglock);
#endif
errno = errsv;
2016-02-29 14:23:41 +01:00
}
2013-05-01 14:52:55 +02:00
#endif