Skip to content

Commit

Permalink
feat: add helper functions for loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
ktos committed May 11, 2024
1 parent ccfc06b commit 7281e39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/Mokosh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class Mokosh
Mokosh *registerService(const char *key, std::shared_ptr<MokoshService> service);
Mokosh *registerService(std::shared_ptr<MokoshService> service);

// registers a debug adapter
// registers a logger
Mokosh *registerLogger(const char *key, std::shared_ptr<MokoshLogger> service);

// registers a logger
Mokosh *registerLogger(std::shared_ptr<MokoshLogger> service);

// prints message of a desired debug level to all debug adapters
Expand All @@ -75,7 +77,7 @@ class Mokosh
// use rather mlog() macros instead of direct usage of this function
static void log(LogLevel level, const char *func, const char *file, int line, const char *fmt, ...);

// prints to the debug adapters the "busy" indicator
// prints to the loggers the "busy" indicator
static void debug_ticker_step();
static void debug_ticker_finish(bool success);

Expand Down
39 changes: 23 additions & 16 deletions src/MokoshLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Debug level - starts from 0 to 6, higher is more severe
typedef enum LogLevel
{
NONE = -1,
PROFILER = 0,
DEBUG = 1,
VERBOSE = 2,
Expand Down Expand Up @@ -43,24 +44,9 @@ class MokoshLogger : public MokoshService

protected:
LogLevel currentLevel;
};

class SerialLogger : public MokoshLogger
{
public:
virtual bool setup() override
{
this->setupFinished = true;
return true;
}

virtual void loop() override {}

virtual void log(LogLevel level, const char *func, const char *file, int line, long time, const char *msg) override
virtual char levelToChar(LogLevel level)
{
if (level < this->currentLevel)
return;

char lvl;
switch (level)
{
Expand All @@ -83,6 +69,27 @@ class SerialLogger : public MokoshLogger
lvl = 'A';
}

return lvl;
}
};

class SerialLogger : public MokoshLogger
{
public:
virtual bool setup() override
{
this->setupFinished = true;
return true;
}

virtual void loop() override {}

virtual void log(LogLevel level, const char *func, const char *file, int line, long time, const char *msg) override
{
if (level < this->currentLevel)
return;

char lvl = this->levelToChar(level);
Serial.printf("(%c t:%ldms) (%s %s:%d) %s\n", lvl, time, func, file, line, msg);
}

Expand Down

0 comments on commit 7281e39

Please sign in to comment.