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.h
00001 #ifndef LOG_H 00002 #define LOG_H 00003 00004 #include "mbed.h" 00005 00006 #include "stdarg.h" 00007 #include "stdio.h" 00008 00009 /** 00010 \file log.h 00011 \brief Logging utilities 00012 00013 This file contains logging utilities 00014 */ 00015 00016 #define LOG_BUFSIZE 4096 00017 00018 /// Enumeration used to enable/disable various serial lines in the logger 00019 typedef enum { 00020 LOG_USB = 0, 00021 LOG_SER_9_10, 00022 LOG_SER_13_14, 00023 LOG_SER_28_27, 00024 LOG_MAX 00025 } SerialLines; 00026 00027 /// Combination serial/file logger 00028 class Log { 00029 private: 00030 // Accomodate all 3 serial ports and USB 00031 Serial *m_serial[4]; 00032 bool m_enable[4]; 00033 00034 // File log 00035 LocalFileSystem local; 00036 00037 public: 00038 /// Constructor 00039 inline Log() : local("local") 00040 { 00041 // Write to file 00042 FILE *logfile = fopen("/local/NetTool.log", "w"); 00043 fputs("NetTool - Welcome!", logfile); 00044 fclose(logfile); 00045 00046 // Set up the serial classes 00047 m_serial[LOG_USB] = new Serial(USBTX,USBRX); 00048 m_serial[LOG_SER_9_10] = new Serial(p9,p10); 00049 m_serial[LOG_SER_13_14] = new Serial(p13,p14); 00050 m_serial[LOG_SER_28_27] = new Serial(p28,p27); 00051 00052 // Disable logging to all of them by default 00053 for (int idx = 0; idx < LOG_MAX; ++idx) 00054 { 00055 m_enable[idx] = false; 00056 } 00057 } 00058 00059 /// Enable logging to the given serial line 00060 inline void enable(SerialLines idx) 00061 { 00062 m_enable[idx] = true; 00063 } 00064 00065 /// Disable logging to the given serial line 00066 inline void disable(SerialLines idx) 00067 { 00068 m_enable[idx] = false; 00069 } 00070 00071 /// This can log messages up to 4095 characters and has printf semantics 00072 /// All log messages include an implicit \r\n at the end 00073 inline bool printf(char *format, ...) 00074 { 00075 static char buffer[LOG_BUFSIZE]; 00076 static int count; 00077 static va_list va; 00078 00079 va_start(va, format); 00080 count = vsnprintf(buffer, LOG_BUFSIZE, format, va); 00081 00082 // Ensure that the log message fit 00083 if (count > LOG_BUFSIZE-1) 00084 { 00085 // Log an error message if it didn't 00086 Log::printf("Log message too long: %4d", count); 00087 } 00088 else 00089 { 00090 // Write all characters from the message 00091 puts(buffer); 00092 // Send the EOM (\r\n) 00093 puts("\r\n"); 00094 } 00095 va_end(va); 00096 return true; 00097 } // printf 00098 00099 /// Put the given string (no implicit \r\n) on all enabled serial lines and to the logfile 00100 bool puts(const char *str) 00101 { 00102 // Write to file 00103 FILE *logfile = fopen("/local/NetTool.log", "a"); 00104 fputs(str, logfile); 00105 fclose(logfile); 00106 00107 // Write all characters from the message 00108 while (*str) 00109 { 00110 // Write to all serial devices 00111 for (int s = 0; s < LOG_MAX; ++s) 00112 { 00113 // Only write if enabled 00114 if (m_enable[s]) 00115 s[*m_serial].putc(*str++); 00116 } 00117 } 00118 return true; 00119 } 00120 00121 }; 00122 00123 #endif
Generated on Tue Jul 12 2022 11:59:40 by
1.7.2