Changeset 20dd67e in mainline
- Timestamp:
- 2012-08-17T13:40:22Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 920d0fc
- Parents:
- 267f235
- Location:
- uspace/lib/usb
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/include/usb/debug.h
r267f235 r20dd67e 38 38 #include <inttypes.h> 39 39 #include <usb/usb.h> 40 #include <io/log.h> 40 41 #include <assert.h> 41 42 … … 43 44 const uint8_t *, size_t); 44 45 45 /** Logging level. */ 46 typedef enum { 47 /** Fatal, unrecoverable, error. 48 * Such error prevents the driver from working at all. 49 */ 50 USB_LOG_LEVEL_FATAL, 51 52 /** Serious but recoverable error 53 * Shall be used for errors fatal for single device but not for 54 * driver itself. 55 */ 56 USB_LOG_LEVEL_ERROR, 57 58 /** Warning. 59 * Problems from which the driver is able to recover gracefully. 60 */ 61 USB_LOG_LEVEL_WARNING, 62 63 /** Information message. 64 * This should be the last level that is printed by default to 65 * the screen. 66 * Typical usage is to inform that new device was found and what 67 * are its capabilities. 68 * Do not use for repetitive actions (such as device polling). 69 */ 70 USB_LOG_LEVEL_INFO, 71 72 /** Debugging message. */ 73 USB_LOG_LEVEL_DEBUG, 74 75 /** More detailed debugging message. */ 76 USB_LOG_LEVEL_DEBUG2, 77 78 /** Terminating constant for logging levels. */ 79 USB_LOG_LEVEL_MAX 80 } usb_log_level_t; 46 #define USB_LOG_LEVEL_FATAL LVL_FATAL 47 #define USB_LOG_LEVEL_ERROR LVL_ERROR 48 #define USB_LOG_LEVEL_WARNING LVL_WARN 49 #define USB_LOG_LEVEL_INFO LVL_NOTE 50 #define USB_LOG_LEVEL_DEBUG LVL_DEBUG 51 #define USB_LOG_LEVEL_DEBUG2 LVL_DEBUG2 81 52 82 53 /** Default log level. */ … … 87 58 #endif 88 59 89 void usb_log_enable( usb_log_level_t, const char *);60 void usb_log_enable(log_level_t , const char *); 90 61 91 void usb_log_printf( usb_log_level_t, const char *, ...)62 void usb_log_printf(log_level_t, const char *, ...) 92 63 PRINTF_ATTRIBUTE(2, 3); 93 64 -
uspace/lib/usb/src/debug.c
r267f235 r20dd67e 41 41 #include <usb/debug.h> 42 42 43 /** Level of logging messages. */44 static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;45 46 /** Prefix for logging messages. */47 static const char *log_prefix = "usb";48 49 /** Serialization mutex for logging functions. */50 static FIBRIL_MUTEX_INITIALIZE(log_serializer);51 52 /** File where to store the log. */53 static FILE *log_stream = NULL;54 55 56 43 /** Enable logging. 57 44 * … … 59 46 * @param message_prefix Prefix for each printed message. 60 47 */ 61 void usb_log_enable( usb_log_level_t level, const char *message_prefix)48 void usb_log_enable(log_level_t level, const char *message_prefix) 62 49 { 63 log_prefix = message_prefix;64 log_level = level;65 if (log_stream == NULL) {66 char *fname;67 int rc = asprintf(&fname, "/log/%s", message_prefix);68 if (rc > 0) {69 log_stream = fopen(fname, "w");70 if (log_stream != NULL)71 setvbuf(log_stream, NULL, _IOFBF, BUFSIZ);72 73 free(fname);74 }75 }76 50 log_init(message_prefix); 77 }78 79 /** Get log level name prefix.80 *81 * @param level Log level.82 * @return String prefix for the message.83 */84 static const char *log_level_name(usb_log_level_t level)85 {86 switch (level) {87 case USB_LOG_LEVEL_FATAL:88 return " FATAL";89 case USB_LOG_LEVEL_ERROR:90 return " ERROR";91 case USB_LOG_LEVEL_WARNING:92 return " WARN";93 case USB_LOG_LEVEL_INFO:94 return " info";95 default:96 return "";97 }98 51 } 99 52 … … 103 56 * @param format Formatting directive. 104 57 */ 105 void usb_log_printf( usb_log_level_t level, const char *format, ...)58 void usb_log_printf(log_level_t level, const char *format, ...) 106 59 { 107 FILE *screen_stream = NULL;108 switch (level) {109 case USB_LOG_LEVEL_FATAL:110 case USB_LOG_LEVEL_ERROR:111 screen_stream = stderr;112 break;113 default:114 screen_stream = stdout;115 break;116 }117 assert(screen_stream != NULL);118 119 60 va_list args; 120 121 /*122 * Serialize access to log files.123 * Print to screen only messages with higher level than the one124 * specified during logging initialization.125 * Print also to file, to it print one more (lower) level as well.126 */127 fibril_mutex_lock(&log_serializer);128 129 const char *level_name = log_level_name(level);130 131 if ((log_stream != NULL) && (level <= log_level + 1)) {132 va_start(args, format);133 134 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);135 vfprintf(log_stream, format, args);136 fflush(log_stream);137 138 va_end(args);139 }140 141 if (level <= log_level) {142 va_start(args, format);143 144 fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name);145 vfprintf(screen_stream, format, args);146 fflush(screen_stream);147 148 va_end(args);149 }150 61 151 62 va_start(args, format); 152 63 log_msgv(level, format, args); 153 64 va_end(args); 154 155 fibril_mutex_unlock(&log_serializer);156 65 } 157 66
Note:
See TracChangeset
for help on using the changeset viewer.