Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding togglable ANSI colored logs #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion ArduinoLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ SOFTWARE.

#include "ArduinoLog.h"

void Logging::begin(int level, Print* logOutput, bool showLevel)
void Logging::begin(int level, Print* logOutput, bool showLevel, bool showColors)
{
#ifndef DISABLE_LOGGING
setLevel(level);
setShowLevel(showLevel);
setShowColors(showColors);
_logOutput = logOutput;
#endif
}
Expand Down Expand Up @@ -72,6 +73,22 @@ bool Logging::getShowLevel() const
#endif
}

void Logging::setShowColors(bool showColors)
{
#ifndef DISABLE_LOGGING
_showColors = showColors;
#endif
}

bool Logging::getShowColors() const
{
#ifndef DISABLE_LOGGING
return _showColors;
#else
return false;
#endif
}

void Logging::setPrefix(printfunction f)
{
#ifndef DISABLE_LOGGING
Expand Down
91 changes: 74 additions & 17 deletions ArduinoLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ typedef void (*printfunction)(Print*, int);
#define LOG_LEVEL_TRACE 5
#define LOG_LEVEL_VERBOSE 6

#define BOLDRED F("\033[1;31m")
#define BOLDWHITE F("\033[1;37m")
#define RED "\033[;31m"
#define YELLOW "\033[;33m"
#define GREEN "\033[;32m"
#define WHITE "\033[;37m"
#define BLACK "\033[;38;5;240m"
#define ENDCOLOUR "\033[0m"
#define CR "\n"
#define LF "\r"
#define NL "\n\r"
Expand Down Expand Up @@ -97,7 +105,8 @@ class Logging
Logging()
#ifndef DISABLE_LOGGING
: _level(LOG_LEVEL_SILENT),
_showLevel(true),
_showLevel(true),
_showColors(false),
_logOutput(NULL)
#endif
{
Expand All @@ -111,10 +120,12 @@ class Logging
*
* \param level - logging levels <= this will be logged.
* \param printer - place that logging output will be sent to.
* \param showLevel - if true, the level will be shown in the output.
* \param showColors - if true, the color will be shown in the output.
* \return void
*
*/
void begin(int level, Print *output, bool showLevel = true);
void begin(int level, Print *output, bool showLevel = true, bool showColors = false);

/**
* Set the log level.
Expand Down Expand Up @@ -148,6 +159,23 @@ class Logging
*/
bool getShowLevel() const;

/**
* Set whether to show the log colors.
*
* \param showColors - true if the log colors should be shown for each log
* false otherwise.
* \return void
*/
void setShowColors(bool showColors);

/**
* Get whether the log colors are shown during logging
*
* \return true if the log colors are shown for each log
* false otherwise.
*/
bool getShowColors() const;

/**
* Sets a function to be called before each log command.
*
Expand Down Expand Up @@ -190,13 +218,13 @@ class Logging
*/
template <class T, typename... Args> void fatal(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_FATAL, false, msg, args...);
printLevel(LOG_LEVEL_FATAL, false, msg, args...);
#endif
}

template <class T, typename... Args> void fatalln(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_FATAL, true, msg, args...);
printLevel(LOG_LEVEL_FATAL, true, msg, args...);
#endif
}

Expand All @@ -212,13 +240,13 @@ class Logging
*/
template <class T, typename... Args> void error(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_ERROR, false, msg, args...);
printLevel(LOG_LEVEL_ERROR, false, msg, args...);
#endif
}

template <class T, typename... Args> void errorln(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_ERROR, true, msg, args...);
printLevel(LOG_LEVEL_ERROR, true, msg, args...);
#endif
}
/**
Expand All @@ -233,13 +261,13 @@ class Logging
*/
template <class T, typename... Args> void warning(T msg, Args...args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_WARNING, false, msg, args...);
printLevel(LOG_LEVEL_WARNING, false, msg, args...);
#endif
}

template <class T, typename... Args> void warningln(T msg, Args...args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_WARNING, true, msg, args...);
printLevel(LOG_LEVEL_WARNING, true, msg, args...);
#endif
}

Expand All @@ -255,25 +283,25 @@ class Logging
*/
template <class T, typename... Args> void notice(T msg, Args...args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_NOTICE, false, msg, args...);
printLevel(LOG_LEVEL_NOTICE, false, msg, args...);
#endif
}

template <class T, typename... Args> void noticeln(T msg, Args...args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_NOTICE, true, msg, args...);
printLevel(LOG_LEVEL_NOTICE, true, msg, args...);
#endif
}

template <class T, typename... Args> void info(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_INFO, false, msg, args...);
printLevel(LOG_LEVEL_INFO, false, msg, args...);
#endif
}

template <class T, typename... Args> void infoln(T msg, Args...args) {
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_INFO, true, msg, args...);
printLevel(LOG_LEVEL_INFO, true, msg, args...);
#endif
}

Expand Down Expand Up @@ -311,13 +339,13 @@ class Logging
*/
template <class T, typename... Args> void verbose(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_VERBOSE, false, msg, args...);
printLevel(LOG_LEVEL_VERBOSE, false, msg, args...);
#endif
}

template <class T, typename... Args> void verboseln(T msg, Args... args){
#ifndef DISABLE_LOGGING
printLevel(LOG_LEVEL_VERBOSE, true, msg, args...);
printLevel(LOG_LEVEL_VERBOSE, true, msg, args...);
#endif
}

Expand All @@ -344,14 +372,38 @@ class Logging
{
level = LOG_LEVEL_SILENT;
}


if (_showColors)
{
switch (level)
{
case LOG_LEVEL_FATAL:
_logOutput->print(BOLDRED);
break;
case LOG_LEVEL_ERROR:
_logOutput->print(RED);
break;
case LOG_LEVEL_WARNING:
_logOutput->print(YELLOW);
break;
case LOG_LEVEL_NOTICE:
_logOutput->print(GREEN);
break;
case LOG_LEVEL_VERBOSE:
_logOutput->print(BLACK);
break;
default:
break;
}
}

if (_prefix != NULL)
{
_prefix(_logOutput, level);
}

if (_showLevel) {
if (_showLevel)
{
static const char levels[] = "FEWITV";
_logOutput->print(levels[level - 1]);
_logOutput->print(": ");
Expand All @@ -369,17 +421,22 @@ class Logging
{
_logOutput->print(CR);
}
if (_showColors)
{
_logOutput->print(ENDCOLOUR);
}
#endif
}

#ifndef DISABLE_LOGGING
int _level;
bool _showLevel;
bool _showColors;
Print* _logOutput;

printfunction _prefix = NULL;
printfunction _suffix = NULL;
#endif
};

extern Logging Log;
extern Logging Log;