5#include "Hardware/Loom_Hypnos/Loom_Hypnos.h"
7#define FUNCTION_START Logger::getInstance()->startFunction(__FILE__, __func__, __LINE__, freeMemory())
8#define FUNCTION_END Logger::getInstance()->endFunction(freeMemory())
10#define SLOG(msg) Logger::getInstance()->debugLog(msg, true, __FILE__, __func__, __LINE__)
11#define LOG(msg) Logger::getInstance()->debugLog(msg, false, __FILE__, __func__, __LINE__)
12#define LOG_LONG(msg) Logger::getInstance()->logLong(msg, false)
13#define ERROR(msg) Logger::getInstance()->errorLog(msg, false, __FILE__, __func__, __LINE__)
14#define WARNING(msg) Logger::getInstance()->warningLog(msg, false, __FILE__, __func__, __LINE__)
16#define ENABLE_SD_LOGGING Logger::getInstance()->enableSD()
17#define ENABLE_FUNC_SUMMARIES Logger::getInstance()->enableSummaries()
39 unsigned long lineNumber;
46 std::queue<struct functionInfo*> callStack;
50 bool enableFunctionSummaries =
false;
51 bool enableSDLogging =
false;
63 void log(
char* message,
bool silent){
68 Serial.println(message);
70 snprintf_P(filePath, 100, PSTR(
"/debug/output_%i.log"), sdInst->getCurrentFileNumber());
72 if(sdInst !=
nullptr && enableSDLogging)
82 void truncateFileName(
const char* fileName,
char array[260]){
86 char *lastOccurance = strrchr(fileName,
'\\');
88 int lastSlash = lastOccurance-fileName+1;
91 for(i = lastSlash; i < strlen(fileName); i++){
92 array[i-lastSlash] = fileName[i];
94 array[i-lastSlash] =
'\0';
104 static Logger *getInstance(){
105 if(instance ==
nullptr){
135 void debugLog(
const char* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
136 genericLog(
"DEBUG", message, silent, file, func, lineNumber);
145 void errorLog(
const char* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
146 genericLog(
"ERROR", message, silent, file, func, lineNumber);
155 void warningLog(
const char* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
156 genericLog(
"WARNING", message, silent, file, func, lineNumber);
165 void debugLog(
const __FlashStringHelper* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
167 memcpy_P(buff, message, 150);
168 genericLog(
"DEBUG", buff, silent, file, func, lineNumber);
177 void warningLog(
const __FlashStringHelper* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
179 memcpy_P(buff, message, 150);
180 genericLog(
"WARNING", buff, silent, file, func, lineNumber);
189 void errorLog(
const __FlashStringHelper* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
191 memcpy_P(buff, message, 150);
192 genericLog(
"ERROR", buff, silent, file, func, lineNumber);
204 void genericLog(
const char* level,
const char* message,
bool silent,
const char* file,
const char* func,
unsigned long lineNumber){
205 char logMessage[OUTPUT_SIZE];
207 truncateFileName(file, fileName);
210 if(hypnosInst ==
nullptr || (hypnosInst !=
nullptr && !hypnosInst->isRTCInitialized()))
211 snprintf_P( logMessage,
213 PSTR(
"[%s] [%s:%s:%u] %s"),
221 snprintf_P( logMessage,
223 PSTR(
"[%s] [%s] [%s:%s:%u] %s"),
232 log(logMessage, silent);
238 void logLong(
char* message,
bool silent){
239 log(message, silent);
249 void startFunction(
const char* file,
const char* func,
unsigned long num,
int freeMemory){
250 if(enableFunctionSummaries){
253 memset(fileName,
'\0', 260);
254 truncateFileName(file, fileName);
255 struct functionInfo* newFunction = (
struct functionInfo*) malloc(
sizeof(
struct functionInfo));
256 strncpy(newFunction->fileName, fileName, 260);
257 strncpy(newFunction->funcName, func, 260);
258 newFunction->lineNumber = num;
259 newFunction->netMemoryUsage = freeMemory;
260 newFunction->time = millis();
261 newFunction->indentCount = callStack.size();
262 callStack.push(newFunction);
271 if(enableFunctionSummaries){
279 memset(indents,
'\0', 10);
281 struct functionInfo* info = callStack.front();
282 int memUsage = info->netMemoryUsage;
283 unsigned long time = millis() - info->time;
284 int percentage = ((float)memUsage / 32000.0) * 100;
287 strncpy(file, info->fileName, 260);
288 strncpy(func, info->funcName, 260);
291 if(info->indentCount > 10){
292 info->indentCount = 10;
296 memset(indents,
'\t', info->indentCount);
303 snprintf_P(fileName, 100,PSTR(
"/debug/funcSummaries_%i.log"), sdInst->getCurrentFileNumber());
304 snprintf_P(output, 300, PSTR(
"%s[%s:%s] Summary\n%s\tInitial Free Memory: %i B (%i %% Free)\n%s\tEnding Free Memory: %i B\n%s\tNet Usage: %i B\n%s\tElapsed Time: %u MS"), indents, file, func, indents, memUsage, percentage, indents, freeMemory, indents, memUsage-freeMemory, indents, time);
307 if(sdInst !=
nullptr && enableSDLogging)
313 void enableSummaries(){ enableFunctionSummaries =
true; };
316 void enableSD(){ enableSDLogging =
true; };
void debugLog(const char *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:135
void debugLog(const __FlashStringHelper *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:165
void warningLog(const char *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:155
void errorLog(const __FlashStringHelper *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:189
void startFunction(const char *file, const char *func, unsigned long num, int freeMemory)
Definition: Logger.h:249
void setSDManager(SDManager *manager)
Definition: Logger.h:118
void warningLog(const __FlashStringHelper *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:177
void endFunction(int freeMemory)
Definition: Logger.h:270
void setHypnos(Loom_Hypnos *hypnos)
Definition: Logger.h:124
void errorLog(const char *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:145
void genericLog(const char *level, const char *message, bool silent, const char *file, const char *func, unsigned long lineNumber)
Definition: Logger.h:204
Definition: Loom_Hypnos.h:70
SDManager * getSDManager()
Definition: Loom_Hypnos.h:199
DateTime getCurrentTime()
Definition: Loom_Hypnos.cpp:246
Definition: SDManager.h:15
bool writeLineToFile(const char *filename, const char *content)
Definition: SDManager.cpp:12