Provides the means to log debug text to various log areas and at various severity levels and send it to a MODSERIAL serial port object for output.

Committer:
paul_harris77
Date:
Fri Jun 30 16:36:48 2017 +0000
Revision:
2:f1b41864f865
Parent:
1:e1be56fa979a
Updated for compatibility with LidarLitev3 program.  Removed extern global declaration of logger object.  cSystemControl object now instantiates all required objects and injects cLogger object dependency in to constructor of each object.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
paul_harris77 0:23f7bb983ae0 1 //*********************************************************************************************************************
paul_harris77 0:23f7bb983ae0 2 // LOGGER LIBRARY - SOURCE FILE
paul_harris77 0:23f7bb983ae0 3 // PAUL HARRIS, OCTOBER 2016
paul_harris77 0:23f7bb983ae0 4
paul_harris77 0:23f7bb983ae0 5 // *********************************************************************************************************************
paul_harris77 0:23f7bb983ae0 6
paul_harris77 0:23f7bb983ae0 7 #include "cLogger.h"
paul_harris77 0:23f7bb983ae0 8
paul_harris77 0:23f7bb983ae0 9 //Constructor
paul_harris77 0:23f7bb983ae0 10 cLogger::cLogger(MODSERIAL* std_out)
paul_harris77 0:23f7bb983ae0 11 {
paul_harris77 0:23f7bb983ae0 12 pStdOut = std_out;
paul_harris77 0:23f7bb983ae0 13
paul_harris77 0:23f7bb983ae0 14 pStdOut->baud(38400);
paul_harris77 0:23f7bb983ae0 15
paul_harris77 0:23f7bb983ae0 16 //Register the LOG area as the first log area for use by the cLogger itself
paul_harris77 0:23f7bb983ae0 17 registerLogArea(LOGGER, ERROR, "LOGGER>> ");
paul_harris77 1:e1be56fa979a 18
paul_harris77 1:e1be56fa979a 19 //Register required log areas here and set default log levels:
paul_harris77 1:e1be56fa979a 20 registerLogArea(MAIN, NORMAL, "MAIN>>");
paul_harris77 2:f1b41864f865 21 registerLogArea(SYSTEM_CONTROL, DETAILED, "MAIN_CONTROL>>");
paul_harris77 1:e1be56fa979a 22 registerLogArea(LIDAR_IF, SUPPRESSED, "LIDAR_IF>>");
paul_harris77 1:e1be56fa979a 23 registerLogArea(LIDAR_CONTROL, SUPPRESSED, "LIDAR_CONTROL>>");
paul_harris77 1:e1be56fa979a 24 registerLogArea(MOTOR_CONTROL, SUPPRESSED, "MOTOR_CONTROL>>");
paul_harris77 1:e1be56fa979a 25 registerLogArea(ENCODER, SUPPRESSED, "ENCODER>>");
paul_harris77 1:e1be56fa979a 26 registerLogArea(EVENT, DETAILED, "EVENT>>");
paul_harris77 1:e1be56fa979a 27 registerLogArea(OBSERVER, DETAILED, "OBSERVER>>");
paul_harris77 1:e1be56fa979a 28
paul_harris77 0:23f7bb983ae0 29 }
paul_harris77 0:23f7bb983ae0 30
paul_harris77 0:23f7bb983ae0 31 //***********Begin public member functions************
paul_harris77 0:23f7bb983ae0 32
paul_harris77 0:23f7bb983ae0 33 void cLogger::registerLogArea(eLogArea area, eLogLevel defaultLevel, const char* log_prefix)
paul_harris77 0:23f7bb983ae0 34 {
paul_harris77 0:23f7bb983ae0 35 //Only add if log area not already registered
paul_harris77 0:23f7bb983ae0 36 if (getLogListIndex(area) <0){
paul_harris77 0:23f7bb983ae0 37 log_list.push_back(log_t());
paul_harris77 0:23f7bb983ae0 38 log_list[log_list.size()-1].area = area;
paul_harris77 0:23f7bb983ae0 39 log_list[log_list.size()-1].level = defaultLevel;
paul_harris77 0:23f7bb983ae0 40 log_list[log_list.size()-1].prefix = log_prefix;
paul_harris77 0:23f7bb983ae0 41
paul_harris77 0:23f7bb983ae0 42 if(area != LOGGER) //Don't print anything about adding the cLogger area itself (not interested)
paul_harris77 0:23f7bb983ae0 43 {
paul_harris77 0:23f7bb983ae0 44 log(LOGGER, NORMAL, "Added new log area with area %s, level %s and prefix %s", sLogArea[log_list[log_list.size()-1].area], sLogLevel[log_list[log_list.size()-1].level], log_list[log_list.size()-1].prefix);
paul_harris77 0:23f7bb983ae0 45 log(LOGGER, NORMAL, "log_list now has %d entries", log_list.size());
paul_harris77 0:23f7bb983ae0 46 }
paul_harris77 0:23f7bb983ae0 47 }
paul_harris77 0:23f7bb983ae0 48 else{
paul_harris77 2:f1b41864f865 49 log(LOGGER, ERROR, "Attempt to register log area \"%s\" failed: Log area already registered!", sLogArea[area]);
paul_harris77 0:23f7bb983ae0 50 }
paul_harris77 0:23f7bb983ae0 51 }
paul_harris77 0:23f7bb983ae0 52
paul_harris77 0:23f7bb983ae0 53 void cLogger::log(eLogArea area, eLogLevel text_level, char* log_text, ...)
paul_harris77 0:23f7bb983ae0 54 {
paul_harris77 0:23f7bb983ae0 55 //******N.B. Don't call the "log" function in any functions that are called as part of log - causes recursion issues!*****
paul_harris77 0:23f7bb983ae0 56
paul_harris77 0:23f7bb983ae0 57 va_list args;
paul_harris77 0:23f7bb983ae0 58 va_start(args, log_text);
paul_harris77 0:23f7bb983ae0 59
paul_harris77 0:23f7bb983ae0 60 int idx = getLogListIndex(area);
paul_harris77 0:23f7bb983ae0 61 //If log area fully registered
paul_harris77 0:23f7bb983ae0 62 if (idx >= 0)
paul_harris77 0:23f7bb983ae0 63 {
paul_harris77 0:23f7bb983ae0 64 //Implicitly cast the text log level and area log level to ints for comparison
paul_harris77 0:23f7bb983ae0 65 int intTextLevel = text_level;
paul_harris77 0:23f7bb983ae0 66 int intLogAreaLevel = log_list[idx].level;
paul_harris77 0:23f7bb983ae0 67
paul_harris77 0:23f7bb983ae0 68 //Compare the text's log level to the area's current log level to work out if we should print log text
paul_harris77 0:23f7bb983ae0 69 if((intTextLevel <= intLogAreaLevel) && intLogAreaLevel != SUPPRESSED)
paul_harris77 0:23f7bb983ae0 70 {
paul_harris77 0:23f7bb983ae0 71 cLogger::printf(area, log_text, args);
paul_harris77 0:23f7bb983ae0 72 }
paul_harris77 0:23f7bb983ae0 73 }
paul_harris77 0:23f7bb983ae0 74 else //Log area not registered
paul_harris77 0:23f7bb983ae0 75 {
paul_harris77 0:23f7bb983ae0 76 log(LOGGER, ERROR, "Attempt to log to an un-registered log area!"); //This doesn't cause log recursion issues...
paul_harris77 0:23f7bb983ae0 77 }
paul_harris77 0:23f7bb983ae0 78
paul_harris77 0:23f7bb983ae0 79 va_end(args);
paul_harris77 0:23f7bb983ae0 80 }
paul_harris77 0:23f7bb983ae0 81
paul_harris77 0:23f7bb983ae0 82 void cLogger::plainText(char* log_text, ...)
paul_harris77 0:23f7bb983ae0 83 {
paul_harris77 0:23f7bb983ae0 84 char text_buffer[256];
paul_harris77 0:23f7bb983ae0 85
paul_harris77 0:23f7bb983ae0 86 //Extract arguments
paul_harris77 0:23f7bb983ae0 87 va_list args;
paul_harris77 0:23f7bb983ae0 88 va_start(args, log_text);
paul_harris77 0:23f7bb983ae0 89
paul_harris77 0:23f7bb983ae0 90 //Lock the stdio mutex
paul_harris77 0:23f7bb983ae0 91 stdio_mutex.lock();
paul_harris77 0:23f7bb983ae0 92
paul_harris77 0:23f7bb983ae0 93 //Print formated log text
paul_harris77 0:23f7bb983ae0 94 vsprintf(text_buffer, log_text, args);
paul_harris77 0:23f7bb983ae0 95 pStdOut->printf(text_buffer);
paul_harris77 0:23f7bb983ae0 96
paul_harris77 0:23f7bb983ae0 97 //Unlock the stdio mutex
paul_harris77 0:23f7bb983ae0 98 stdio_mutex.unlock();
paul_harris77 0:23f7bb983ae0 99 }
paul_harris77 0:23f7bb983ae0 100
paul_harris77 0:23f7bb983ae0 101 void cLogger::newLine(void)
paul_harris77 0:23f7bb983ae0 102 {
paul_harris77 0:23f7bb983ae0 103 pStdOut->printf("\n\r");
paul_harris77 0:23f7bb983ae0 104 }
paul_harris77 0:23f7bb983ae0 105
paul_harris77 0:23f7bb983ae0 106 void cLogger::setLogLevel(eLogArea area, eLogLevel level)
paul_harris77 0:23f7bb983ae0 107 {
paul_harris77 0:23f7bb983ae0 108 int idx = getLogListIndex(area);
paul_harris77 0:23f7bb983ae0 109 if (idx >= 0)
paul_harris77 0:23f7bb983ae0 110 {
paul_harris77 0:23f7bb983ae0 111 log_list[idx].level = level;
paul_harris77 0:23f7bb983ae0 112 }
paul_harris77 0:23f7bb983ae0 113 else
paul_harris77 0:23f7bb983ae0 114 {
paul_harris77 0:23f7bb983ae0 115 log(LOGGER, ERROR, "Attempt to set log level of un-registered log area!");
paul_harris77 0:23f7bb983ae0 116 }
paul_harris77 0:23f7bb983ae0 117 }
paul_harris77 0:23f7bb983ae0 118
paul_harris77 0:23f7bb983ae0 119 void cLogger::setLogPrefix(eLogArea area, const char* log_prefix)
paul_harris77 0:23f7bb983ae0 120 {
paul_harris77 0:23f7bb983ae0 121 int idx = getLogListIndex(area);
paul_harris77 0:23f7bb983ae0 122 if (idx >= 0)
paul_harris77 0:23f7bb983ae0 123 {
paul_harris77 0:23f7bb983ae0 124 log_list[idx].prefix = log_prefix;
paul_harris77 0:23f7bb983ae0 125 }
paul_harris77 0:23f7bb983ae0 126 else
paul_harris77 0:23f7bb983ae0 127 {
paul_harris77 0:23f7bb983ae0 128 log(LOGGER, ERROR, "Attempt to set log prefix of un-registered log area!");
paul_harris77 0:23f7bb983ae0 129 }
paul_harris77 0:23f7bb983ae0 130 }
paul_harris77 0:23f7bb983ae0 131
paul_harris77 0:23f7bb983ae0 132 void cLogger::setAllAreaLevels(eLogLevel level)
paul_harris77 0:23f7bb983ae0 133 {
paul_harris77 0:23f7bb983ae0 134 for(unsigned i=0; i<log_list.size(); i++)
paul_harris77 0:23f7bb983ae0 135 {
paul_harris77 0:23f7bb983ae0 136 log_list[i].level = level;
paul_harris77 0:23f7bb983ae0 137 }
paul_harris77 0:23f7bb983ae0 138 }
paul_harris77 0:23f7bb983ae0 139
paul_harris77 0:23f7bb983ae0 140
paul_harris77 0:23f7bb983ae0 141 //***********End public member functions************
paul_harris77 0:23f7bb983ae0 142
paul_harris77 0:23f7bb983ae0 143 //***********Begin private member functions************
paul_harris77 0:23f7bb983ae0 144
paul_harris77 0:23f7bb983ae0 145 int cLogger::getLogListIndex(eLogArea area)
paul_harris77 0:23f7bb983ae0 146 {
paul_harris77 0:23f7bb983ae0 147 int idx;
paul_harris77 0:23f7bb983ae0 148 bool match_found = false;
paul_harris77 0:23f7bb983ae0 149
paul_harris77 0:23f7bb983ae0 150 for(idx=0; idx<log_list.size(); idx++)
paul_harris77 0:23f7bb983ae0 151 {
paul_harris77 0:23f7bb983ae0 152 if(log_list[idx].area == area)
paul_harris77 0:23f7bb983ae0 153 {
paul_harris77 0:23f7bb983ae0 154 match_found = true;
paul_harris77 0:23f7bb983ae0 155 break;
paul_harris77 0:23f7bb983ae0 156 }
paul_harris77 0:23f7bb983ae0 157 }
paul_harris77 0:23f7bb983ae0 158
paul_harris77 0:23f7bb983ae0 159 if(match_found)
paul_harris77 0:23f7bb983ae0 160 {
paul_harris77 0:23f7bb983ae0 161 return idx;
paul_harris77 0:23f7bb983ae0 162 }
paul_harris77 0:23f7bb983ae0 163 else
paul_harris77 0:23f7bb983ae0 164 {
paul_harris77 0:23f7bb983ae0 165 return -1;
paul_harris77 0:23f7bb983ae0 166 }
paul_harris77 0:23f7bb983ae0 167 }
paul_harris77 0:23f7bb983ae0 168
paul_harris77 0:23f7bb983ae0 169 void cLogger::printf(eLogArea area, char* log_text, va_list args)
paul_harris77 0:23f7bb983ae0 170 {
paul_harris77 0:23f7bb983ae0 171 char text_buffer[256];
paul_harris77 0:23f7bb983ae0 172
paul_harris77 0:23f7bb983ae0 173 int idx = getLogListIndex(area);
paul_harris77 0:23f7bb983ae0 174
paul_harris77 0:23f7bb983ae0 175 if(idx >=0)
paul_harris77 0:23f7bb983ae0 176 {
paul_harris77 0:23f7bb983ae0 177 //Lock the stdio mutex
paul_harris77 0:23f7bb983ae0 178 stdio_mutex.lock();
paul_harris77 0:23f7bb983ae0 179
paul_harris77 0:23f7bb983ae0 180 //Print log area prefix
paul_harris77 0:23f7bb983ae0 181 pStdOut->printf("%s(0x%02x) ", log_list[idx].prefix, osThreadGetId());
paul_harris77 0:23f7bb983ae0 182
paul_harris77 0:23f7bb983ae0 183 //Print formated log text
paul_harris77 0:23f7bb983ae0 184 vsprintf(text_buffer, log_text, args);
paul_harris77 0:23f7bb983ae0 185 pStdOut->printf(text_buffer);
paul_harris77 0:23f7bb983ae0 186
paul_harris77 0:23f7bb983ae0 187 //New line characters
paul_harris77 0:23f7bb983ae0 188 pStdOut->printf("\n\r");
paul_harris77 0:23f7bb983ae0 189
paul_harris77 0:23f7bb983ae0 190 //Unlock the stdio mutex
paul_harris77 0:23f7bb983ae0 191 stdio_mutex.unlock();
paul_harris77 0:23f7bb983ae0 192 }
paul_harris77 0:23f7bb983ae0 193 else
paul_harris77 0:23f7bb983ae0 194 {
paul_harris77 0:23f7bb983ae0 195 log(LOGGER, ERROR, "Attempt to log to an un-registered log area!"); //This doesn't cause log recursion issues...
paul_harris77 0:23f7bb983ae0 196 }
paul_harris77 0:23f7bb983ae0 197 }
paul_harris77 0:23f7bb983ae0 198 //***********End private member functions************
paul_harris77 0:23f7bb983ae0 199
paul_harris77 0:23f7bb983ae0 200
paul_harris77 1:e1be56fa979a 201
paul_harris77 1:e1be56fa979a 202