logger split up

This commit is contained in:
Thomas Kunze 2024-08-18 00:38:54 +02:00
parent 897acfe10f
commit 774a6b1d71
5 changed files with 140 additions and 120 deletions

View File

@ -41,6 +41,8 @@ set(${PROJECT_NAME}_SOURCES
../../src/knx/util/aes.c ../../src/knx/util/aes.c
../../src/knx/util/aes.h ../../src/knx/util/aes.h
../../src/knx/util/aes.hpp ../../src/knx/util/aes.hpp
../../src/knx/util/logger.h
../../src/knx/util/logger.cpp
../../src/knx/apdu.cpp ../../src/knx/apdu.cpp
../../src/knx/apdu.h ../../src/knx/apdu.h
../../src/knx/application_layer.cpp ../../src/knx/application_layer.cpp

View File

@ -176,6 +176,8 @@ knx/util/aes.c
knx/util/aes.h knx/util/aes.h
knx/util/aes.hpp knx/util/aes.hpp
knx/util/simple_map.h knx/util/simple_map.h
knx/util/logger.h
knx/util/logger.cpp
knx.h knx.h
knx_facade.cpp knx_facade.cpp
knx_facade.h knx_facade.h

View File

@ -357,14 +357,13 @@ void IpDataLinkLayer::loop()
break; break;
} }
#ifdef KNX_TUNNELING
case SearchRequestExt: case SearchRequestExt:
{ {
loopHandleSearchRequestExtended(buffer, len); loopHandleSearchRequestExtended(buffer, len);
break; break;
} }
#ifdef KNX_TUNNELING
case ConnectRequest: case ConnectRequest:
{ {
loopHandleConnectRequest(buffer, len, remoteAddr, remotePort); loopHandleConnectRequest(buffer, len, remoteAddr, remotePort);

125
src/knx/util/logger.cpp Normal file
View File

@ -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);
}

View File

@ -1,128 +1,20 @@
#include "../bits.h" #include "../bits.h"
#include <stdarg.h> #include <stdarg.h>
#include <string> #include <string>
using namespace std;
class Logger class Logger
{ {
public: public:
static Logger logger(const string name) static Logger logger(const std::string name);
{ void info(const std::string message, ...);
return Logger(name); void warning(const std::string message, ...);
} void error(const std::string message, ...);
inline void info(const string message, ...) void critical(const std::string message, ...);
{ void exception(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
}
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
}
private: private:
enum LogType { Info, Warning, Error, Critical, Exception}; enum LogType { Info, Warning, Error, Critical, Exception};
const string enum_name(LogType type) const std::string enum_name(LogType type);
{ const std::string _name;
switch (type) Logger(const std::string name) : _name(name) {}
{ inline void log(LogType type, const char* format, va_list args);
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
}
}; };