Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cLogger.cpp
00001 //********************************************************************************************************************* 00002 // LOGGER LIBRARY - SOURCE FILE 00003 // PAUL HARRIS, OCTOBER 2016 00004 00005 // ********************************************************************************************************************* 00006 00007 #include "cLogger.h" 00008 00009 //Constructor 00010 cLogger::cLogger(MODSERIAL* std_out) 00011 { 00012 pStdOut = std_out; 00013 00014 pStdOut->baud(38400); 00015 00016 //Register the LOG area as the first log area for use by the cLogger itself 00017 registerLogArea(LOGGER, ERROR, "LOGGER>> "); 00018 00019 //Register required log areas here and set default log levels: 00020 registerLogArea(MAIN, NORMAL, "MAIN>>"); 00021 registerLogArea(SYSTEM_CONTROL, DETAILED, "MAIN_CONTROL>>"); 00022 registerLogArea(LIDAR_IF, SUPPRESSED, "LIDAR_IF>>"); 00023 registerLogArea(LIDAR_CONTROL, SUPPRESSED, "LIDAR_CONTROL>>"); 00024 registerLogArea(MOTOR_CONTROL, SUPPRESSED, "MOTOR_CONTROL>>"); 00025 registerLogArea(ENCODER, SUPPRESSED, "ENCODER>>"); 00026 registerLogArea(EVENT, DETAILED, "EVENT>>"); 00027 registerLogArea(OBSERVER, DETAILED, "OBSERVER>>"); 00028 00029 } 00030 00031 //***********Begin public member functions************ 00032 00033 void cLogger::registerLogArea(eLogArea area, eLogLevel defaultLevel, const char* log_prefix) 00034 { 00035 //Only add if log area not already registered 00036 if (getLogListIndex(area) <0){ 00037 log_list.push_back(log_t()); 00038 log_list[log_list.size()-1].area = area; 00039 log_list[log_list.size()-1].level = defaultLevel; 00040 log_list[log_list.size()-1].prefix = log_prefix; 00041 00042 if(area != LOGGER) //Don't print anything about adding the cLogger area itself (not interested) 00043 { 00044 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); 00045 log(LOGGER, NORMAL, "log_list now has %d entries", log_list.size()); 00046 } 00047 } 00048 else{ 00049 log(LOGGER, ERROR, "Attempt to register log area \"%s\" failed: Log area already registered!", sLogArea[area]); 00050 } 00051 } 00052 00053 void cLogger::log(eLogArea area, eLogLevel text_level, char* log_text, ...) 00054 { 00055 //******N.B. Don't call the "log" function in any functions that are called as part of log - causes recursion issues!***** 00056 00057 va_list args; 00058 va_start(args, log_text); 00059 00060 int idx = getLogListIndex(area); 00061 //If log area fully registered 00062 if (idx >= 0) 00063 { 00064 //Implicitly cast the text log level and area log level to ints for comparison 00065 int intTextLevel = text_level; 00066 int intLogAreaLevel = log_list[idx].level; 00067 00068 //Compare the text's log level to the area's current log level to work out if we should print log text 00069 if((intTextLevel <= intLogAreaLevel) && intLogAreaLevel != SUPPRESSED) 00070 { 00071 cLogger::printf(area, log_text, args); 00072 } 00073 } 00074 else //Log area not registered 00075 { 00076 log(LOGGER, ERROR, "Attempt to log to an un-registered log area!"); //This doesn't cause log recursion issues... 00077 } 00078 00079 va_end(args); 00080 } 00081 00082 void cLogger::plainText(char* log_text, ...) 00083 { 00084 char text_buffer[256]; 00085 00086 //Extract arguments 00087 va_list args; 00088 va_start(args, log_text); 00089 00090 //Lock the stdio mutex 00091 stdio_mutex.lock(); 00092 00093 //Print formated log text 00094 vsprintf(text_buffer, log_text, args); 00095 pStdOut->printf(text_buffer); 00096 00097 //Unlock the stdio mutex 00098 stdio_mutex.unlock(); 00099 } 00100 00101 void cLogger::newLine(void) 00102 { 00103 pStdOut->printf("\n\r"); 00104 } 00105 00106 void cLogger::setLogLevel(eLogArea area, eLogLevel level) 00107 { 00108 int idx = getLogListIndex(area); 00109 if (idx >= 0) 00110 { 00111 log_list[idx].level = level; 00112 } 00113 else 00114 { 00115 log(LOGGER, ERROR, "Attempt to set log level of un-registered log area!"); 00116 } 00117 } 00118 00119 void cLogger::setLogPrefix(eLogArea area, const char* log_prefix) 00120 { 00121 int idx = getLogListIndex(area); 00122 if (idx >= 0) 00123 { 00124 log_list[idx].prefix = log_prefix; 00125 } 00126 else 00127 { 00128 log(LOGGER, ERROR, "Attempt to set log prefix of un-registered log area!"); 00129 } 00130 } 00131 00132 void cLogger::setAllAreaLevels(eLogLevel level) 00133 { 00134 for(unsigned i=0; i<log_list.size(); i++) 00135 { 00136 log_list[i].level = level; 00137 } 00138 } 00139 00140 00141 //***********End public member functions************ 00142 00143 //***********Begin private member functions************ 00144 00145 int cLogger::getLogListIndex(eLogArea area) 00146 { 00147 int idx; 00148 bool match_found = false; 00149 00150 for(idx=0; idx<log_list.size(); idx++) 00151 { 00152 if(log_list[idx].area == area) 00153 { 00154 match_found = true; 00155 break; 00156 } 00157 } 00158 00159 if(match_found) 00160 { 00161 return idx; 00162 } 00163 else 00164 { 00165 return -1; 00166 } 00167 } 00168 00169 void cLogger::printf(eLogArea area, char* log_text, va_list args) 00170 { 00171 char text_buffer[256]; 00172 00173 int idx = getLogListIndex(area); 00174 00175 if(idx >=0) 00176 { 00177 //Lock the stdio mutex 00178 stdio_mutex.lock(); 00179 00180 //Print log area prefix 00181 pStdOut->printf("%s(0x%02x) ", log_list[idx].prefix, osThreadGetId()); 00182 00183 //Print formated log text 00184 vsprintf(text_buffer, log_text, args); 00185 pStdOut->printf(text_buffer); 00186 00187 //New line characters 00188 pStdOut->printf("\n\r"); 00189 00190 //Unlock the stdio mutex 00191 stdio_mutex.unlock(); 00192 } 00193 else 00194 { 00195 log(LOGGER, ERROR, "Attempt to log to an un-registered log area!"); //This doesn't cause log recursion issues... 00196 } 00197 } 00198 //***********End private member functions************ 00199 00200 00201 00202
Generated on Mon Jul 18 2022 02:31:04 by
1.7.2