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.
Log.cpp
00001 #include "Log.h" 00002 #include "Clock.h" 00003 00004 Log::Log(Serial* serial) 00005 : serial(serial) 00006 { 00007 this->wasLineBreak = true; 00008 } 00009 00010 Log::~Log() 00011 { 00012 delete this->serial; 00013 } 00014 00015 Serial* Log::getSerial() const 00016 { 00017 return this->serial; 00018 } 00019 00020 void Log::setSerial(Serial* serial) 00021 { 00022 if (this->serial != NULL) { 00023 delete this->serial; 00024 } 00025 this->serial = serial; 00026 } 00027 00028 Log& Log::print(const char* str) 00029 { 00030 this->printInfo(); 00031 this->serial->puts(str); 00032 return *this; 00033 } 00034 00035 Log& Log::print(const string& str) 00036 { 00037 return this->print(str.c_str()); 00038 } 00039 00040 Log& Log::print(bool value) 00041 { 00042 if (value) { 00043 return this->print("true"); 00044 } else { 00045 return this->print("false"); 00046 } 00047 } 00048 00049 Log& Log::print(unsigned char num, uint8_t base) 00050 { 00051 return this->print((unsigned int)num, base); 00052 } 00053 00054 Log& Log::print(unsigned int num, uint8_t base) 00055 { 00056 if (base == 10) { 00057 if (this->serial != NULL) { 00058 this->printInfo(); 00059 this->serial->printf("%u", num); 00060 } 00061 } else if (base == 16) { 00062 if (this->serial != NULL) { 00063 this->printInfo(); 00064 this->serial->printf("%X", num); 00065 } 00066 } else { 00067 return this->print(num); 00068 } 00069 return *this; 00070 } 00071 00072 Log& Log::print(unsigned long num, uint8_t base) 00073 { 00074 if (base == 10) { 00075 if (this->serial != NULL) { 00076 this->printInfo(); 00077 this->serial->printf("%lu", num); 00078 } 00079 } else if (base == 16) { 00080 if (this->serial != NULL) { 00081 this->printInfo(); 00082 this->serial->printf("%lX", num); 00083 } 00084 } else { 00085 return this->print(num); 00086 } 00087 return *this; 00088 } 00089 00090 Log& Log::print(unsigned long long num, uint8_t base) 00091 { 00092 if (base == 10) { 00093 if (this->serial != NULL) { 00094 this->printInfo(); 00095 this->serial->printf("%lu", num); 00096 } 00097 } else if (base == 16) { 00098 if (this->serial != NULL) { 00099 this->printInfo(); 00100 this->serial->printf("%lX", num); 00101 } 00102 } else { 00103 return this->print(num); 00104 } 00105 return *this; 00106 } 00107 00108 Log& Log::print(char c) 00109 { 00110 if (this->serial != NULL) { 00111 this->serial->putc(c); 00112 } 00113 return *this; 00114 } 00115 00116 Log& Log::print(int num) 00117 { 00118 if (this->serial != NULL) { 00119 this->printInfo(); 00120 this->serial->printf("%d", num); 00121 } 00122 return *this; 00123 } 00124 00125 Log& Log::print(long num) 00126 { 00127 if (this->serial != NULL) { 00128 this->printInfo(); 00129 this->serial->printf("%ld", num); 00130 } 00131 return *this; 00132 } 00133 00134 Log& Log::print(double num, uint8_t decimals) 00135 { 00136 if (this->serial != NULL) { 00137 this->printInfo(); 00138 00139 char format[10]; 00140 snprintf(format, sizeof format, "%%.%df", decimals); 00141 this->serial->printf(format, num); 00142 } 00143 return *this; 00144 } 00145 00146 Log& Log::printf(const char* fmt, ...) 00147 { 00148 if (this->serial != NULL) { 00149 this->printInfo(); 00150 00151 va_list args; 00152 va_start(args, fmt); 00153 this->serial->vprintf(fmt, args); 00154 va_end(args); 00155 } 00156 return *this; 00157 } 00158 00159 Log& Log::ln(uint8_t quantity) 00160 { 00161 if (this->serial != NULL) { 00162 for (uint8_t i = 0; i < quantity; i++) { 00163 this->serial->puts("\r\n"); 00164 } 00165 } 00166 this->wasLineBreak = true; 00167 return *this; 00168 } 00169 00170 Log& Log::flush() 00171 { 00172 fflush(stdout); 00173 return *this; 00174 } 00175 00176 void Log::printInfo() 00177 { 00178 if (this->wasLineBreak) { 00179 this->wasLineBreak = false; 00180 this->print(Clock::getInstance()->getTimeAsAString()) 00181 .print("; ram: ").print(" "); 00182 } 00183 }
Generated on Fri Aug 19 2022 07:33:34 by
1.7.2