2023-10-06 17:24:48 -06:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
2024-06-03 08:13:45 -06:00
|
|
|
#include "Utilities/OS.h"
|
|
|
|
|
|
2023-12-20 11:52:48 -07:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
2023-10-06 17:24:48 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
using namespace RNS;
|
|
|
|
|
|
2023-12-20 11:52:48 -07:00
|
|
|
//LogLevel _level = LOG_VERBOSE;
|
2024-07-03 11:30:01 -06:00
|
|
|
LogLevel _level = LOG_TRACE;
|
2023-12-20 11:52:48 -07:00
|
|
|
//LogLevel _level = LOG_MEM;
|
|
|
|
|
RNS::log_callback _on_log = nullptr;
|
2024-06-03 08:13:45 -06:00
|
|
|
char _datetime[20];
|
2023-12-20 11:52:48 -07:00
|
|
|
|
|
|
|
|
const char* RNS::getLevelName(LogLevel level) {
|
2023-10-06 17:24:48 -06:00
|
|
|
switch (level) {
|
|
|
|
|
case LOG_CRITICAL:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "!!!";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_ERROR:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "ERR";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_WARNING:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "WRN";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_NOTICE:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "NOT";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_INFO:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "INF";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_VERBOSE:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "VRB";
|
2023-10-06 17:24:48 -06:00
|
|
|
case LOG_DEBUG:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "DBG";
|
2024-07-03 11:30:01 -06:00
|
|
|
case LOG_TRACE:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "---";
|
2023-11-26 18:10:46 -07:00
|
|
|
case LOG_MEM:
|
2023-12-20 11:52:48 -07:00
|
|
|
return "...";
|
2023-10-06 17:24:48 -06:00
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 11:52:48 -07:00
|
|
|
const char* RNS::getTimeString() {
|
2024-06-03 08:13:45 -06:00
|
|
|
#ifdef ARDUINO
|
|
|
|
|
uint64_t time = Utilities::OS::ltime();
|
|
|
|
|
if (time < 86400000) {
|
|
|
|
|
snprintf(_datetime, sizeof(_datetime), "%02d:%02d:%02d.%03d", (int)(time/3600000), (int)((time/60000)%60), (int)((time/1000)%60), (int)(time%1000));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
snprintf(_datetime, sizeof(_datetime), "%02d-%02d:%02d:%02d.%03d", (int)(time/86400000), (int)((time/3600000)%24), (int)((time/60000)%60), (int)((time/1000)%60), (int)(time%1000));
|
|
|
|
|
}
|
|
|
|
|
return _datetime;
|
|
|
|
|
#else
|
2023-12-20 11:52:48 -07:00
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
|
int millis = lrint(tv.tv_usec/1000.0);
|
|
|
|
|
if (millis >= 1000) {
|
|
|
|
|
millis -= 1000;
|
|
|
|
|
tv.tv_sec++;
|
|
|
|
|
}
|
|
|
|
|
struct tm* tm = localtime(&tv.tv_sec);
|
|
|
|
|
size_t len = strftime(_datetime, sizeof(_datetime), "%Y-%m-%d %H:%M:%S", tm);
|
2024-07-16 10:40:46 -06:00
|
|
|
snprintf(_datetime+len, sizeof(_datetime)-len, ".%03d", millis);
|
2023-12-20 11:52:48 -07:00
|
|
|
return _datetime;
|
2024-06-03 08:13:45 -06:00
|
|
|
#endif
|
2023-12-20 11:52:48 -07:00
|
|
|
}
|
2023-10-06 17:24:48 -06:00
|
|
|
|
|
|
|
|
void RNS::loglevel(LogLevel level) {
|
|
|
|
|
_level = level;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-07 01:03:15 -06:00
|
|
|
LogLevel RNS::loglevel() {
|
|
|
|
|
return _level;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 11:52:48 -07:00
|
|
|
void RNS::setLogCallback(log_callback on_log /*= nullptr*/) {
|
|
|
|
|
_on_log = on_log;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 10:40:46 -06:00
|
|
|
void RNS::doLog(LogLevel level, const char* msg) {
|
2023-10-06 17:24:48 -06:00
|
|
|
if (level > _level) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-20 11:52:48 -07:00
|
|
|
if (_on_log != nullptr) {
|
|
|
|
|
_on_log(msg, level);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-26 18:10:46 -07:00
|
|
|
#ifdef ARDUINO
|
2026-02-09 13:52:13 -07:00
|
|
|
if (Serial) {
|
|
|
|
|
Serial.print(getTimeString());
|
|
|
|
|
Serial.print(" [");
|
|
|
|
|
Serial.print(getLevelName(level));
|
|
|
|
|
Serial.print("] ");
|
|
|
|
|
Serial.println(msg);
|
|
|
|
|
Serial.flush();
|
|
|
|
|
}
|
2023-10-06 17:24:48 -06:00
|
|
|
#else
|
2023-12-20 11:52:48 -07:00
|
|
|
printf("%s [%s] %s\n", getTimeString(), getLevelName(level), msg);
|
2023-10-06 17:24:48 -06:00
|
|
|
#endif
|
|
|
|
|
}
|
2023-11-19 08:51:10 -07:00
|
|
|
|
2024-07-03 11:30:01 -06:00
|
|
|
void HEAD(const char* msg, LogLevel level) {
|
2023-11-19 08:51:10 -07:00
|
|
|
if (level > _level) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-26 18:10:46 -07:00
|
|
|
#ifdef ARDUINO
|
2023-11-19 08:51:10 -07:00
|
|
|
Serial.println("");
|
|
|
|
|
#else
|
|
|
|
|
printf("\n");
|
|
|
|
|
#endif
|
2024-07-16 10:40:46 -06:00
|
|
|
doLog(level, msg);
|
2024-07-03 06:55:06 -06:00
|
|
|
}
|