This is a low-level network debugging utility that utilizes raw packet i/o to construct and deconstruct tcp, udp, ipv4, arp, and icmp packets over ethernet.
util/log.h@8:1c1f6ce348c6, 2010-10-12 (annotated)
- Committer:
- etherealflaim
- Date:
- Tue Oct 12 06:48:59 2010 +0000
- Revision:
- 8:1c1f6ce348c6
- Parent:
- 6:66c4cd9073aa
Documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
etherealflaim | 0:d494b853ce97 | 1 | #ifndef LOG_H |
etherealflaim | 0:d494b853ce97 | 2 | #define LOG_H |
etherealflaim | 0:d494b853ce97 | 3 | |
etherealflaim | 0:d494b853ce97 | 4 | #include "mbed.h" |
etherealflaim | 0:d494b853ce97 | 5 | |
etherealflaim | 0:d494b853ce97 | 6 | #include "stdarg.h" |
etherealflaim | 0:d494b853ce97 | 7 | #include "stdio.h" |
etherealflaim | 0:d494b853ce97 | 8 | |
etherealflaim | 6:66c4cd9073aa | 9 | /** |
etherealflaim | 6:66c4cd9073aa | 10 | \file log.h |
etherealflaim | 6:66c4cd9073aa | 11 | \brief Logging utilities |
etherealflaim | 6:66c4cd9073aa | 12 | |
etherealflaim | 6:66c4cd9073aa | 13 | This file contains logging utilities |
etherealflaim | 6:66c4cd9073aa | 14 | */ |
etherealflaim | 6:66c4cd9073aa | 15 | |
etherealflaim | 0:d494b853ce97 | 16 | #define LOG_BUFSIZE 4096 |
etherealflaim | 0:d494b853ce97 | 17 | |
etherealflaim | 4:88fc7fa58931 | 18 | /// Enumeration used to enable/disable various serial lines in the logger |
etherealflaim | 0:d494b853ce97 | 19 | typedef enum { |
etherealflaim | 0:d494b853ce97 | 20 | LOG_USB = 0, |
etherealflaim | 0:d494b853ce97 | 21 | LOG_SER_9_10, |
etherealflaim | 0:d494b853ce97 | 22 | LOG_SER_13_14, |
etherealflaim | 0:d494b853ce97 | 23 | LOG_SER_28_27, |
etherealflaim | 0:d494b853ce97 | 24 | LOG_MAX |
etherealflaim | 0:d494b853ce97 | 25 | } SerialLines; |
etherealflaim | 0:d494b853ce97 | 26 | |
etherealflaim | 4:88fc7fa58931 | 27 | /// Combination serial/file logger |
etherealflaim | 0:d494b853ce97 | 28 | class Log { |
etherealflaim | 0:d494b853ce97 | 29 | private: |
etherealflaim | 0:d494b853ce97 | 30 | // Accomodate all 3 serial ports and USB |
etherealflaim | 0:d494b853ce97 | 31 | Serial *m_serial[4]; |
etherealflaim | 0:d494b853ce97 | 32 | bool m_enable[4]; |
etherealflaim | 0:d494b853ce97 | 33 | |
etherealflaim | 0:d494b853ce97 | 34 | // File log |
etherealflaim | 0:d494b853ce97 | 35 | LocalFileSystem local; |
etherealflaim | 0:d494b853ce97 | 36 | |
etherealflaim | 0:d494b853ce97 | 37 | public: |
etherealflaim | 4:88fc7fa58931 | 38 | /// Constructor |
etherealflaim | 0:d494b853ce97 | 39 | inline Log() : local("local") |
etherealflaim | 0:d494b853ce97 | 40 | { |
etherealflaim | 0:d494b853ce97 | 41 | // Write to file |
etherealflaim | 0:d494b853ce97 | 42 | FILE *logfile = fopen("/local/NetTool.log", "w"); |
etherealflaim | 0:d494b853ce97 | 43 | fputs("NetTool - Welcome!", logfile); |
etherealflaim | 0:d494b853ce97 | 44 | fclose(logfile); |
etherealflaim | 0:d494b853ce97 | 45 | |
etherealflaim | 0:d494b853ce97 | 46 | // Set up the serial classes |
etherealflaim | 0:d494b853ce97 | 47 | m_serial[LOG_USB] = new Serial(USBTX,USBRX); |
etherealflaim | 0:d494b853ce97 | 48 | m_serial[LOG_SER_9_10] = new Serial(p9,p10); |
etherealflaim | 0:d494b853ce97 | 49 | m_serial[LOG_SER_13_14] = new Serial(p13,p14); |
etherealflaim | 0:d494b853ce97 | 50 | m_serial[LOG_SER_28_27] = new Serial(p28,p27); |
etherealflaim | 0:d494b853ce97 | 51 | |
etherealflaim | 0:d494b853ce97 | 52 | // Disable logging to all of them by default |
etherealflaim | 0:d494b853ce97 | 53 | for (int idx = 0; idx < LOG_MAX; ++idx) |
etherealflaim | 0:d494b853ce97 | 54 | { |
etherealflaim | 0:d494b853ce97 | 55 | m_enable[idx] = false; |
etherealflaim | 0:d494b853ce97 | 56 | } |
etherealflaim | 0:d494b853ce97 | 57 | } |
etherealflaim | 0:d494b853ce97 | 58 | |
etherealflaim | 4:88fc7fa58931 | 59 | /// Enable logging to the given serial line |
etherealflaim | 0:d494b853ce97 | 60 | inline void enable(SerialLines idx) |
etherealflaim | 0:d494b853ce97 | 61 | { |
etherealflaim | 0:d494b853ce97 | 62 | m_enable[idx] = true; |
etherealflaim | 0:d494b853ce97 | 63 | } |
etherealflaim | 0:d494b853ce97 | 64 | |
etherealflaim | 4:88fc7fa58931 | 65 | /// Disable logging to the given serial line |
etherealflaim | 0:d494b853ce97 | 66 | inline void disable(SerialLines idx) |
etherealflaim | 0:d494b853ce97 | 67 | { |
etherealflaim | 0:d494b853ce97 | 68 | m_enable[idx] = false; |
etherealflaim | 0:d494b853ce97 | 69 | } |
etherealflaim | 0:d494b853ce97 | 70 | |
etherealflaim | 4:88fc7fa58931 | 71 | /// This can log messages up to 4095 characters and has printf semantics |
etherealflaim | 4:88fc7fa58931 | 72 | /// All log messages include an implicit \r\n at the end |
etherealflaim | 0:d494b853ce97 | 73 | inline bool printf(char *format, ...) |
etherealflaim | 0:d494b853ce97 | 74 | { |
etherealflaim | 0:d494b853ce97 | 75 | static char buffer[LOG_BUFSIZE]; |
etherealflaim | 0:d494b853ce97 | 76 | static int count; |
etherealflaim | 0:d494b853ce97 | 77 | static va_list va; |
etherealflaim | 0:d494b853ce97 | 78 | |
etherealflaim | 0:d494b853ce97 | 79 | va_start(va, format); |
etherealflaim | 0:d494b853ce97 | 80 | count = vsnprintf(buffer, LOG_BUFSIZE, format, va); |
etherealflaim | 0:d494b853ce97 | 81 | |
etherealflaim | 0:d494b853ce97 | 82 | // Ensure that the log message fit |
etherealflaim | 0:d494b853ce97 | 83 | if (count > LOG_BUFSIZE-1) |
etherealflaim | 0:d494b853ce97 | 84 | { |
etherealflaim | 0:d494b853ce97 | 85 | // Log an error message if it didn't |
etherealflaim | 0:d494b853ce97 | 86 | Log::printf("Log message too long: %4d", count); |
etherealflaim | 0:d494b853ce97 | 87 | } |
etherealflaim | 0:d494b853ce97 | 88 | else |
etherealflaim | 0:d494b853ce97 | 89 | { |
etherealflaim | 0:d494b853ce97 | 90 | // Write all characters from the message |
etherealflaim | 0:d494b853ce97 | 91 | puts(buffer); |
etherealflaim | 0:d494b853ce97 | 92 | // Send the EOM (\r\n) |
etherealflaim | 0:d494b853ce97 | 93 | puts("\r\n"); |
etherealflaim | 0:d494b853ce97 | 94 | } |
etherealflaim | 0:d494b853ce97 | 95 | va_end(va); |
etherealflaim | 0:d494b853ce97 | 96 | return true; |
etherealflaim | 0:d494b853ce97 | 97 | } // printf |
etherealflaim | 0:d494b853ce97 | 98 | |
etherealflaim | 4:88fc7fa58931 | 99 | /// Put the given string (no implicit \r\n) on all enabled serial lines and to the logfile |
etherealflaim | 0:d494b853ce97 | 100 | bool puts(const char *str) |
etherealflaim | 0:d494b853ce97 | 101 | { |
etherealflaim | 0:d494b853ce97 | 102 | // Write to file |
etherealflaim | 0:d494b853ce97 | 103 | FILE *logfile = fopen("/local/NetTool.log", "a"); |
etherealflaim | 0:d494b853ce97 | 104 | fputs(str, logfile); |
etherealflaim | 0:d494b853ce97 | 105 | fclose(logfile); |
etherealflaim | 0:d494b853ce97 | 106 | |
etherealflaim | 0:d494b853ce97 | 107 | // Write all characters from the message |
etherealflaim | 0:d494b853ce97 | 108 | while (*str) |
etherealflaim | 0:d494b853ce97 | 109 | { |
etherealflaim | 0:d494b853ce97 | 110 | // Write to all serial devices |
etherealflaim | 0:d494b853ce97 | 111 | for (int s = 0; s < LOG_MAX; ++s) |
etherealflaim | 0:d494b853ce97 | 112 | { |
etherealflaim | 0:d494b853ce97 | 113 | // Only write if enabled |
etherealflaim | 0:d494b853ce97 | 114 | if (m_enable[s]) |
etherealflaim | 0:d494b853ce97 | 115 | s[*m_serial].putc(*str++); |
etherealflaim | 0:d494b853ce97 | 116 | } |
etherealflaim | 0:d494b853ce97 | 117 | } |
etherealflaim | 0:d494b853ce97 | 118 | return true; |
etherealflaim | 0:d494b853ce97 | 119 | } |
etherealflaim | 0:d494b853ce97 | 120 | |
etherealflaim | 0:d494b853ce97 | 121 | }; |
etherealflaim | 0:d494b853ce97 | 122 | |
etherealflaim | 0:d494b853ce97 | 123 | #endif |