Simple multipurpose logger
Fork of LogIt by
loggers/serial_logger.cpp@4:9c5143891c8a, 2016-12-14 (annotated)
- Committer:
- Nico De Witte
- Date:
- Wed Dec 14 18:06:34 2016 +0100
- Revision:
- 4:9c5143891c8a
- Child:
- 7:eb101b1726a5
Refactor LogIt to SerialLogger and also add Logable interface and NullLogger.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nico De Witte |
4:9c5143891c8a | 1 | #include "serial_logger.h" |
Nico De Witte |
4:9c5143891c8a | 2 | #include <stdarg.h> |
Nico De Witte |
4:9c5143891c8a | 3 | #include <stdio.h> |
Nico De Witte |
4:9c5143891c8a | 4 | |
Nico De Witte |
4:9c5143891c8a | 5 | namespace LogIt { |
Nico De Witte |
4:9c5143891c8a | 6 | |
Nico De Witte |
4:9c5143891c8a | 7 | SerialLogger::SerialLogger(Serial* serial) |
Nico De Witte |
4:9c5143891c8a | 8 | { |
Nico De Witte |
4:9c5143891c8a | 9 | this->serial = serial; |
Nico De Witte |
4:9c5143891c8a | 10 | } |
Nico De Witte |
4:9c5143891c8a | 11 | |
Nico De Witte |
4:9c5143891c8a | 12 | void SerialLogger::emergency(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 13 | { |
Nico De Witte |
4:9c5143891c8a | 14 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 15 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 16 | log(EMERGENCY, message, args); |
Nico De Witte |
4:9c5143891c8a | 17 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 18 | } |
Nico De Witte |
4:9c5143891c8a | 19 | |
Nico De Witte |
4:9c5143891c8a | 20 | void SerialLogger::alert(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 21 | { |
Nico De Witte |
4:9c5143891c8a | 22 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 23 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 24 | log(ALERT, message, args); |
Nico De Witte |
4:9c5143891c8a | 25 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 26 | } |
Nico De Witte |
4:9c5143891c8a | 27 | |
Nico De Witte |
4:9c5143891c8a | 28 | void SerialLogger::critical(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 29 | { |
Nico De Witte |
4:9c5143891c8a | 30 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 31 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 32 | log(CRITICAL, message, args); |
Nico De Witte |
4:9c5143891c8a | 33 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 34 | } |
Nico De Witte |
4:9c5143891c8a | 35 | |
Nico De Witte |
4:9c5143891c8a | 36 | void SerialLogger::error(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 37 | { |
Nico De Witte |
4:9c5143891c8a | 38 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 39 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 40 | log(ERROR, message, args); |
Nico De Witte |
4:9c5143891c8a | 41 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 42 | } |
Nico De Witte |
4:9c5143891c8a | 43 | |
Nico De Witte |
4:9c5143891c8a | 44 | void SerialLogger::warning(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 45 | { |
Nico De Witte |
4:9c5143891c8a | 46 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 47 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 48 | log(WARNING, message, args); |
Nico De Witte |
4:9c5143891c8a | 49 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 50 | } |
Nico De Witte |
4:9c5143891c8a | 51 | |
Nico De Witte |
4:9c5143891c8a | 52 | void SerialLogger::notice(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 53 | { |
Nico De Witte |
4:9c5143891c8a | 54 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 55 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 56 | log(NOTICE, message, args); |
Nico De Witte |
4:9c5143891c8a | 57 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 58 | } |
Nico De Witte |
4:9c5143891c8a | 59 | |
Nico De Witte |
4:9c5143891c8a | 60 | void SerialLogger::info(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 61 | { |
Nico De Witte |
4:9c5143891c8a | 62 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 63 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 64 | log(INFO, message, args); |
Nico De Witte |
4:9c5143891c8a | 65 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 66 | } |
Nico De Witte |
4:9c5143891c8a | 67 | |
Nico De Witte |
4:9c5143891c8a | 68 | void SerialLogger::debug(const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 69 | { |
Nico De Witte |
4:9c5143891c8a | 70 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 71 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 72 | log(DEBUG, message, args); |
Nico De Witte |
4:9c5143891c8a | 73 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 74 | } |
Nico De Witte |
4:9c5143891c8a | 75 | |
Nico De Witte |
4:9c5143891c8a | 76 | void SerialLogger::log(Level level, const char * message, ...) |
Nico De Witte |
4:9c5143891c8a | 77 | { |
Nico De Witte |
4:9c5143891c8a | 78 | va_list args; |
Nico De Witte |
4:9c5143891c8a | 79 | va_start (args, message); |
Nico De Witte |
4:9c5143891c8a | 80 | log(level, message, args); |
Nico De Witte |
4:9c5143891c8a | 81 | va_end (args); |
Nico De Witte |
4:9c5143891c8a | 82 | } |
Nico De Witte |
4:9c5143891c8a | 83 | |
Nico De Witte |
4:9c5143891c8a | 84 | void SerialLogger::log(Level level, const char * message, va_list args) |
Nico De Witte |
4:9c5143891c8a | 85 | { |
Nico De Witte |
4:9c5143891c8a | 86 | if(level > this->level){ |
Nico De Witte |
4:9c5143891c8a | 87 | return; |
Nico De Witte |
4:9c5143891c8a | 88 | } |
Nico De Witte |
4:9c5143891c8a | 89 | |
Nico De Witte |
4:9c5143891c8a | 90 | char levelString[16]; |
Nico De Witte |
4:9c5143891c8a | 91 | switch(level) |
Nico De Witte |
4:9c5143891c8a | 92 | { |
Nico De Witte |
4:9c5143891c8a | 93 | case EMERGENCY: |
Nico De Witte |
4:9c5143891c8a | 94 | strcpy(levelString, "EMERGENCY"); |
Nico De Witte |
4:9c5143891c8a | 95 | break; |
Nico De Witte |
4:9c5143891c8a | 96 | case ALERT: |
Nico De Witte |
4:9c5143891c8a | 97 | strcpy(levelString, "ALERT"); |
Nico De Witte |
4:9c5143891c8a | 98 | break; |
Nico De Witte |
4:9c5143891c8a | 99 | case CRITICAL: |
Nico De Witte |
4:9c5143891c8a | 100 | strcpy(levelString, "CRITICAL"); |
Nico De Witte |
4:9c5143891c8a | 101 | break; |
Nico De Witte |
4:9c5143891c8a | 102 | case ERROR: |
Nico De Witte |
4:9c5143891c8a | 103 | strcpy(levelString, "ERROR"); |
Nico De Witte |
4:9c5143891c8a | 104 | break; |
Nico De Witte |
4:9c5143891c8a | 105 | case WARNING: |
Nico De Witte |
4:9c5143891c8a | 106 | strcpy(levelString, "WARNING"); |
Nico De Witte |
4:9c5143891c8a | 107 | break; |
Nico De Witte |
4:9c5143891c8a | 108 | case NOTICE: |
Nico De Witte |
4:9c5143891c8a | 109 | strcpy(levelString, "NOTICE"); |
Nico De Witte |
4:9c5143891c8a | 110 | break; |
Nico De Witte |
4:9c5143891c8a | 111 | case INFO: |
Nico De Witte |
4:9c5143891c8a | 112 | strcpy(levelString, "INFO"); |
Nico De Witte |
4:9c5143891c8a | 113 | break; |
Nico De Witte |
4:9c5143891c8a | 114 | case DEBUG: |
Nico De Witte |
4:9c5143891c8a | 115 | strcpy(levelString, "DEBUG"); |
Nico De Witte |
4:9c5143891c8a | 116 | break; |
Nico De Witte |
4:9c5143891c8a | 117 | default: |
Nico De Witte |
4:9c5143891c8a | 118 | strcpy(levelString, "DEBUG"); |
Nico De Witte |
4:9c5143891c8a | 119 | break; |
Nico De Witte |
4:9c5143891c8a | 120 | } |
Nico De Witte |
4:9c5143891c8a | 121 | |
Nico De Witte |
4:9c5143891c8a | 122 | char buffer[256]; |
Nico De Witte |
4:9c5143891c8a | 123 | vsprintf (buffer, message, args); |
Nico De Witte |
4:9c5143891c8a | 124 | serial->printf("%9s: %s\r\n", levelString, buffer); |
Nico De Witte |
4:9c5143891c8a | 125 | } |
Nico De Witte |
4:9c5143891c8a | 126 | |
Nico De Witte |
4:9c5143891c8a | 127 | void SerialLogger::setLevel(Level level) |
Nico De Witte |
4:9c5143891c8a | 128 | { |
Nico De Witte |
4:9c5143891c8a | 129 | this->level = level; |
Nico De Witte |
4:9c5143891c8a | 130 | } |
Nico De Witte |
4:9c5143891c8a | 131 | |
Nico De Witte |
4:9c5143891c8a | 132 | }; |