Changes in uspace/lib/usb/src/debug.c [aa389fd:bb95594] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/src/debug.c
raa389fd rbb95594 1 1 /* 2 * Copyright (c) 2010 -2011Vojtech Horky2 * Copyright (c) 2010 Vojtech Horky 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libusb 29 /** @addtogroup libusb usb 30 30 * @{ 31 31 */ … … 60 60 /** Mutex guard for the list of all tags. */ 61 61 static FIBRIL_MUTEX_INITIALIZE(tag_list_guard); 62 63 /** Level of logging messages. */64 static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;65 /** Prefix for logging messages. */66 static const char *log_prefix = "usb";67 /** Serialization mutex for logging functions. */68 static FIBRIL_MUTEX_INITIALIZE(log_serializer);69 static FILE *log_stream = NULL;70 62 71 63 /** Find or create new tag with given name. … … 163 155 } 164 156 165 /** Enable logging.166 *167 * @param level Maximal enabled level (including this one).168 * @param message_prefix Prefix for each printed message.169 */170 void usb_log_enable(usb_log_level_t level, const char *message_prefix)171 {172 log_prefix = message_prefix;173 log_level = level;174 if (log_stream == NULL) {175 char *fname;176 int rc = asprintf(&fname, "/log/%s", message_prefix);177 if (rc > 0) {178 log_stream = fopen(fname, "w");179 free(fname);180 }181 }182 }183 184 185 static const char *log_level_name(usb_log_level_t level)186 {187 switch (level) {188 case USB_LOG_LEVEL_FATAL:189 return " FATAL";190 case USB_LOG_LEVEL_ERROR:191 return " ERROR";192 case USB_LOG_LEVEL_WARNING:193 return " WARN";194 case USB_LOG_LEVEL_INFO:195 return " info";196 default:197 return "";198 }199 }200 201 /** Print logging message.202 *203 * @param level Verbosity level of the message.204 * @param format Formatting directive.205 */206 void usb_log_printf(usb_log_level_t level, const char *format, ...)207 {208 FILE *stream = NULL;209 switch (level) {210 case USB_LOG_LEVEL_FATAL:211 case USB_LOG_LEVEL_ERROR:212 stream = stderr;213 break;214 default:215 stream = stdout;216 break;217 }218 assert(stream != NULL);219 220 va_list args;221 va_start(args, format);222 223 /*224 * Serialize access to log files.225 * Always print to log file, to screen print only when the enabled226 * log level is high enough.227 */228 fibril_mutex_lock(&log_serializer);229 230 const char *level_name = log_level_name(level);231 232 if (log_stream != NULL) {233 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);234 vfprintf(log_stream, format, args);235 }236 237 if (level <= log_level) {238 fprintf(stream, "[%s]%s: ", log_prefix, level_name);239 vfprintf(stream, format, args);240 }241 242 fibril_mutex_unlock(&log_serializer);243 244 va_end(args);245 }246 157 247 158 /**
Note:
See TracChangeset
for help on using the changeset viewer.