2017-04-03 18:20:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "libp2p/utils/logger.h"
|
2017-04-03 22:26:08 +00:00
|
|
|
#include "libp2p/utils/vector.h"
|
2017-04-03 18:20:12 +00:00
|
|
|
|
2017-04-03 22:26:08 +00:00
|
|
|
/**
|
|
|
|
* A class to handle logging
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct Libp2pVector* logger_classes = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the logger. This should be done only once.
|
|
|
|
*/
|
|
|
|
void libp2p_logger_init() {
|
|
|
|
logger_classes = libp2p_utils_vector_new(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Checks to see if the logger has been initialized
|
|
|
|
*/
|
|
|
|
int libp2p_logger_initialized() {
|
|
|
|
if (logger_classes == NULL)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-17 16:57:37 +00:00
|
|
|
int libp2p_logger_free() {
|
2017-04-27 05:06:43 +00:00
|
|
|
if (logger_classes != NULL) {
|
|
|
|
for(int i = 0; i < logger_classes->total; i++) {
|
2017-07-26 12:37:28 +00:00
|
|
|
free((char*)libp2p_utils_vector_get(logger_classes, i));
|
2017-04-27 05:06:43 +00:00
|
|
|
}
|
|
|
|
libp2p_utils_vector_free(logger_classes);
|
2017-04-17 16:57:37 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-03 22:26:08 +00:00
|
|
|
/***
|
|
|
|
* Add a class to watch for logging messages
|
|
|
|
* @param str the class name to watch
|
|
|
|
*/
|
|
|
|
void libp2p_logger_add_class(const char* str) {
|
|
|
|
if (!libp2p_logger_initialized())
|
|
|
|
libp2p_logger_init();
|
|
|
|
char* ptr = malloc(strlen(str) + 1);
|
|
|
|
strcpy(ptr, str);
|
|
|
|
libp2p_utils_vector_add(logger_classes, ptr);
|
|
|
|
}
|
2017-04-03 18:20:12 +00:00
|
|
|
|
2017-07-13 23:30:18 +00:00
|
|
|
/**
|
|
|
|
* Determines if this "class" is to be logged
|
|
|
|
* @param str the "class" to check
|
|
|
|
* @returns true(1) if found, false(0) otherwise
|
|
|
|
*/
|
|
|
|
int libp2p_logger_watching_class(const char* str) {
|
|
|
|
for(int i = 0; i < logger_classes->total; i++) {
|
|
|
|
if (strcmp(libp2p_utils_vector_get(logger_classes, i), str) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-03 18:20:12 +00:00
|
|
|
/**
|
|
|
|
* Log a message to the console
|
|
|
|
* @param area the class it is coming from
|
|
|
|
* @param log_level logger level
|
|
|
|
* @param format the logging string
|
|
|
|
* @param ... params
|
|
|
|
*/
|
|
|
|
void libp2p_logger_log(const char* area, int log_level, const char* format, ...) {
|
2017-04-03 22:26:08 +00:00
|
|
|
if (!libp2p_logger_initialized())
|
|
|
|
libp2p_logger_init();
|
2017-04-03 18:20:12 +00:00
|
|
|
if (log_level <= CURRENT_LOGLEVEL) {
|
2017-07-13 23:30:18 +00:00
|
|
|
if (libp2p_logger_watching_class(area)) {
|
2017-04-03 18:20:12 +00:00
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
|
|
|
vfprintf(stderr, format, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 18:30:07 +00:00
|
|
|
/**
|
|
|
|
* Log a message to the console
|
|
|
|
* @param area the class it is coming from
|
|
|
|
* @param log_level logger level
|
|
|
|
* @param format the logging string
|
|
|
|
* @param ... params
|
|
|
|
*/
|
|
|
|
void libp2p_logger_vlog(const char* area, int log_level, const char* format, va_list argptr) {
|
2017-04-03 22:26:08 +00:00
|
|
|
if (!libp2p_logger_initialized())
|
|
|
|
libp2p_logger_init();
|
2017-04-20 22:55:18 +00:00
|
|
|
// only allow a message if the message log level is less than the current loglevel
|
2017-04-03 18:30:07 +00:00
|
|
|
if (log_level <= CURRENT_LOGLEVEL) {
|
|
|
|
int found = 0;
|
2017-04-20 22:55:18 +00:00
|
|
|
// error should always be printed for now. We need to think about this more...
|
|
|
|
if (log_level <= LOGLEVEL_ERROR )
|
|
|
|
found = 1;
|
2017-07-13 23:30:18 +00:00
|
|
|
else
|
|
|
|
found = libp2p_logger_watching_class(area);
|
2017-04-03 18:30:07 +00:00
|
|
|
if (found) {
|
2017-07-31 17:49:41 +00:00
|
|
|
int new_format_size = strlen(format) + strlen(area) + 10;
|
|
|
|
char new_format[new_format_size];
|
|
|
|
sprintf(&new_format[0], "[%s] %s", area, format);
|
|
|
|
vfprintf(stderr, new_format, argptr);
|
2017-04-03 18:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 18:20:12 +00:00
|
|
|
/**
|
|
|
|
* Log a debug message to the console
|
|
|
|
* @param area the class it is coming from
|
|
|
|
* @param format the logging string
|
|
|
|
* @param ... params
|
|
|
|
*/
|
|
|
|
void libp2p_logger_debug(const char* area, const char* format, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
2017-04-03 18:30:07 +00:00
|
|
|
libp2p_logger_vlog(area, LOGLEVEL_DEBUG, format, argptr);
|
2017-04-03 18:20:12 +00:00
|
|
|
va_end(argptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log an error message to the console
|
|
|
|
* @param area the class it is coming from
|
|
|
|
* @param format the logging string
|
|
|
|
* @param ... params
|
|
|
|
*/
|
|
|
|
void libp2p_logger_error(const char* area, const char* format, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
2017-04-03 18:30:07 +00:00
|
|
|
libp2p_logger_vlog(area, LOGLEVEL_ERROR, format, argptr);
|
2017-04-03 18:20:12 +00:00
|
|
|
va_end(argptr);
|
|
|
|
}
|
2017-04-04 01:54:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log an info message to the console
|
|
|
|
* @param area the class it is coming from
|
|
|
|
* @param format the logging string
|
|
|
|
* @param ... params
|
|
|
|
*/
|
|
|
|
void libp2p_logger_info(const char* area, const char* format, ...) {
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, format);
|
|
|
|
libp2p_logger_vlog(area, LOGLEVEL_INFO, format, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
}
|