diff --git a/examples/knx-cc1310/CMakeLists.txt b/examples/knx-cc1310/CMakeLists.txt index a8561ff..8a69c0a 100644 --- a/examples/knx-cc1310/CMakeLists.txt +++ b/examples/knx-cc1310/CMakeLists.txt @@ -41,6 +41,8 @@ set(${PROJECT_NAME}_SOURCES ../../src/knx/util/aes.c ../../src/knx/util/aes.h ../../src/knx/util/aes.hpp + ../../src/knx/util/logger.h + ../../src/knx/util/logger.cpp ../../src/knx/apdu.cpp ../../src/knx/apdu.h ../../src/knx/application_layer.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 212721d..46f41ba 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -176,6 +176,8 @@ knx/util/aes.c knx/util/aes.h knx/util/aes.hpp knx/util/simple_map.h +knx/util/logger.h +knx/util/logger.cpp knx.h knx_facade.cpp knx_facade.h diff --git a/src/knx/ip/ip_data_link_layer.cpp b/src/knx/ip/ip_data_link_layer.cpp index 85765b6..bd2eec4 100644 --- a/src/knx/ip/ip_data_link_layer.cpp +++ b/src/knx/ip/ip_data_link_layer.cpp @@ -357,14 +357,13 @@ void IpDataLinkLayer::loop() break; } +#ifdef KNX_TUNNELING case SearchRequestExt: { loopHandleSearchRequestExtended(buffer, len); break; } - -#ifdef KNX_TUNNELING case ConnectRequest: { loopHandleConnectRequest(buffer, len, remoteAddr, remotePort); diff --git a/src/knx/util/logger.cpp b/src/knx/util/logger.cpp new file mode 100644 index 0000000..13117b3 --- /dev/null +++ b/src/knx/util/logger.cpp @@ -0,0 +1,125 @@ +#include "logger.h" + +Logger Logger::logger(const std::string name) +{ + return Logger(name); +} + +void Logger::info(const std::string message, ...) +{ +#ifndef KNX_NO_PRINT + va_list objects; + va_start( objects, message); + log(LogType::Info, message.c_str(), objects); + va_end(objects); +#endif +} + +void Logger::warning(const std::string message, ...) +{ +#ifndef KNX_NO_PRINT + va_list objects; + va_start( objects, message); + log(LogType::Warning, message.c_str(), objects); + va_end(objects); +#endif +} + +void Logger::error(const std::string message, ...) +{ +#ifndef KNX_NO_PRINT + va_list objects; + va_start( objects, message); + log(LogType::Error, message.c_str(), objects); + va_end(objects); +#endif +} + +void Logger::critical(const std::string message, ...) +{ +#ifndef KNX_NO_PRINT + va_list objects; + va_start( objects, message); + log(LogType::Critical, message.c_str(), objects); + va_end(objects); +#endif +} + +void Logger::exception(const std::string message, ...) +{ +#ifndef KNX_NO_PRINT + va_list objects; + va_start( objects, message); + log(LogType::Exception, message.c_str(), objects); + va_end(objects); +#endif +} + +void Logger::log(LogType type, const char* format, va_list args) +{ +#ifndef KNX_NO_PRINT + print(millis()); + print(" "); + print(_name.c_str()); + print("\t"); + print(enum_name(type).c_str()); + print(" "); + + while (*format) + { + if (*format == '%') + { + format++; + + if (*format == 'd') + { + print(va_arg(args, int)); + } + else if (*format == 's') + { + print(va_arg(args, char*)); + } + else if (*format == 'S') + { + print(va_arg(args, std::string).c_str()); + } + else if (*format == 'f') + { + print(va_arg(args, double)); + } + } + else + { + print(*format); + } + + format++; + } + + va_end(args); + println(); +#endif +} + +const std::string Logger::enum_name(LogType type) +{ + switch (type) + { + case LogType::Info: + return "INFO"; + + case LogType::Warning: + return "WARN"; + + case LogType::Error: + return "ERR "; + + case LogType::Critical: + return "CRIT"; + + case LogType::Exception: + return "EXCE"; + } + + return std::to_string(type); +} diff --git a/src/knx/util/logger.h b/src/knx/util/logger.h index d3bd39d..73eb40d 100644 --- a/src/knx/util/logger.h +++ b/src/knx/util/logger.h @@ -1,128 +1,20 @@ #include "../bits.h" #include #include -using namespace std; class Logger { public: - static Logger logger(const string name) - { - return Logger(name); - } - inline void info(const string message, ...) - { -#ifndef KNX_NO_PRINT - va_list objects; - va_start( objects, message); - log(LogType::Info, message.c_str(), objects); - va_end(objects); -#endif - } - inline void warning(const string message, ...) - { -#ifndef KNX_NO_PRINT - va_list objects; - va_start( objects, message); - log(LogType::Warning, message.c_str(), objects); - va_end(objects); -#endif - } - inline void error(const string message, ...) - { -#ifndef KNX_NO_PRINT - va_list objects; - va_start( objects, message); - log(LogType::Error, message.c_str(), objects); - va_end(objects); -#endif - } - inline void critical(const string message, ...) - { -#ifndef KNX_NO_PRINT - va_list objects; - va_start( objects, message); - log(LogType::Critical, message.c_str(), objects); - va_end(objects); -#endif - } - inline void exception(const string message, ...) - { -#ifndef KNX_NO_PRINT - va_list objects; - va_start( objects, message); - log(LogType::Exception, message.c_str(), objects); - va_end(objects); -#endif - } + static Logger logger(const std::string name); + void info(const std::string message, ...); + void warning(const std::string message, ...); + void error(const std::string message, ...); + void critical(const std::string message, ...); + void exception(const std::string message, ...); private: enum LogType { Info, Warning, Error, Critical, Exception}; - const string enum_name(LogType type) - { - switch (type) - { - case LogType::Info: - return "INFO"; - - case LogType::Warning: - return "WARN"; - - case LogType::Error: - return "ERR "; - - case LogType::Critical: - return "CRIT"; - - case LogType::Exception: - return "EXCE"; - } - return to_string(type); - } - const string _name; - Logger(const string name) : _name(name) {} - inline void log(LogType type, const char* format, va_list args) - { -#ifndef KNX_NO_PRINT - print(millis()); - print(" "); - print(_name.c_str()); - print("\t"); - print(enum_name(type).c_str()); - print(" "); - - while (*format) - { - if (*format == '%') - { - format++; - - if (*format == 'd') - { - print(va_arg(args, int)); - } - else if (*format == 's') - { - print(va_arg(args, char*)); - } - else if (*format == 'S') - { - print(va_arg(args, std::string).c_str()); - } - else if (*format == 'f') - { - print(va_arg(args, double)); - } - } - else - { - print(*format); - } - - format++; - } - - va_end(args); - println(); -#endif - } + const std::string enum_name(LogType type); + const std::string _name; + Logger(const std::string name) : _name(name) {} + inline void log(LogType type, const char* format, va_list args); }; \ No newline at end of file