Skip to content

Commit

Permalink
added commands to set log level
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ha committed Sep 10, 2024
1 parent 60f53e6 commit 426ae7e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 30 deletions.
88 changes: 65 additions & 23 deletions commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,56 @@ void view_help(), view_screen1(), view_splash(), view_crossings(), view_events()
void show_touch(), hide_touch();
void show_centerline(), hide_centerline();
void run_unittest();
void enable_console_log(), disable_console_log();
void log_level_debug(), log_level_fence(), log_level_info(), log_level_warning(), log_level_error();

// ----- table of commands
#define Newline true // use this to insert a CRLF before listing this command in help text
struct Command {
bool crlf;
char text[20];
simpleFunction function;
bool crlf;
};
Command cmdList[] = {
{"help", help, 0},
{"version", version, 0},
{0, "help", help},
{0, "version", version},

{Newline, "dump kml", dump_kml},
{0, "dump gps", dump_gps_history},
{0, "erase history", erase_gps_history},

{Newline, "start nmea", start_nmea},
{0, "stop nmea", stop_nmea},

{"dump kml", dump_kml, Newline},
{"dump gps", dump_gps_history, 0},
{"erase history", erase_gps_history, 0},
{Newline, "start gmt", start_gmt},
{0, "stop gmt", stop_gmt},

{"start nmea", start_nmea, Newline},
{"stop nmea", stop_nmea, 0},
{Newline, "show touch", show_touch},
{0, "hide touch", hide_touch},

{"start gmt", start_gmt, Newline},
{"stop gmt", stop_gmt, 0},
{Newline, "show centerline", show_centerline},
{0, "hide centerline", hide_centerline},

{"show touch", show_touch, Newline},
{"hide touch", hide_touch, 0},
{Newline, "view help", view_help},
{0, "view splash", view_splash},
{Newline, "view screen1", view_screen1},
{0, "view crossings", view_crossings},
{0, "view events", view_events},

{"show centerline", show_centerline, Newline},
{"hide centerline", hide_centerline, 0},
{Newline, "dir", list_files},
{0, "list files", list_files},

{"view help", view_help, Newline},
{"view splash", view_splash, 0},
{"view screen1", view_screen1, 0},
{"view crossings", view_crossings, 0},
{"view events", view_events, 0},
{Newline, "type gpshistory", type_gpshistory},
{0, "run unittest", run_unittest},

{"dir", list_files, Newline},
{"list files", list_files, 0},
{Newline, "enable console log", enable_console_log},
{0, "disable console log", disable_console_log},

{"type gpshistory", type_gpshistory, Newline},
{"run unittest", run_unittest, 0},
{Newline, "log level debug", log_level_debug},
{0, "log level fence", log_level_fence},
{0, "log level info", log_level_info},
{0, "log level warning", log_level_warning},
{0, "log level error", log_level_error},
};
const int numCmds = sizeof(cmdList) / sizeof(cmdList[0]);

Expand Down Expand Up @@ -226,6 +237,37 @@ void run_unittest() {
runUnitTest(); // see "unit_test.cpp"
}

void enable_console_log() {
logger.log(COMMAND, CONSOLE, "enabling logging to console");
logger.log_enabled = true;
}

void disable_console_log() {
logger.log(COMMAND, CONSOLE, "disabling all logging to console");
logger.log_enabled = false;
}

void log_level_debug() {
logger.log(COMMAND, CONSOLE, "setting log level debug, fence, info, warning, error, console");
logger.setLevel(DEBUG);
}
void log_level_fence() {
logger.log(COMMAND, CONSOLE, "setting log level fence, info, warning, error, console");
logger.setLevel(POST);
}
void log_level_info() {
logger.log(COMMAND, CONSOLE, "setting log level info, warning, error, console");
logger.setLevel(INFO);
}
void log_level_warning() {
logger.log(COMMAND, CONSOLE, "setting log level warning, error, console");
logger.setLevel(WARNING);
}
void log_level_error() {
logger.log(COMMAND, CONSOLE, "setting log level error, console");
logger.setLevel(ERROR);
}

void removeCRLF(char *pBuffer) {
// remove 0x0d and 0x0a from character arrays, shortening the array in-place
const char key[] = "\r\n";
Expand Down
28 changes: 21 additions & 7 deletions logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ enum LogLevel {

// ----- Subsystem
enum LogSystem {
NMEA = 0,
GMT,
NMEA = 0, //
GMT, //
FENCE, // fencepost debug
COMMAND, //
GPS_SETUP, //
Expand Down Expand Up @@ -90,14 +90,27 @@ class Logger {

// severities
bool printLevel[numLevels] = {
false, // DEBUG = 0, // verbose
false, // FENCE, // fencepost debug
false, // INFO, // non critical
true, // DEBUG = 0, // verbose
true, // FENCE, // fencepost debug
true, // INFO, // non critical
true, // WARNING, // important
true, // ERROR, // critical
true, // CONSOLE, // required output
};

void setLevel(LogLevel lvl) {
if (lvl >= numLevels) {
log(COMMAND, ERROR, "Level %d is not allowed", lvl);
}
for (int ii = 0; ii < numLevels; ii++) {
if (ii < lvl) {
printLevel[ii] = false;
} else {
printLevel[ii] = true;
}
}
}

// subsystems
bool print_nmea = true; // set TRUE to send NMEA sentences to the console (for NmeaTime2 by www.visualgps.net)

Expand Down Expand Up @@ -173,7 +186,8 @@ class Logger {
}

bool ok_to_log(LogSystem system, LogLevel severity) {
if (severity == CONSOLE) return true;
if (severity == CONSOLE)
return true;
if (log_enabled && printLevel[severity]) {
return true;
}
Expand Down Expand Up @@ -305,7 +319,7 @@ class Logger {
Serial.print("Error, ");
break;
case CONSOLE:
Serial.print("(c)" );
Serial.print("(c)");
break;
default:
break;
Expand Down

0 comments on commit 426ae7e

Please sign in to comment.