Changes in uspace/lib/usb/src/debug.c [bb95594:ef7f043] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/src/debug.c
rbb95594 ref7f043 1 1 /* 2 * Copyright (c) 2010 Vojtech Horky2 * Copyright (c) 2010-2011 Vojtech Horky 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup libusb usb29 /** @addtogroup libusb 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; 62 70 63 71 /** Find or create new tag with given name. … … 155 163 } 156 164 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 *screen_stream = NULL; 209 switch (level) { 210 case USB_LOG_LEVEL_FATAL: 211 case USB_LOG_LEVEL_ERROR: 212 screen_stream = stderr; 213 break; 214 default: 215 screen_stream = stdout; 216 break; 217 } 218 assert(screen_stream != NULL); 219 220 va_list args; 221 222 /* 223 * Serialize access to log files. 224 * Always print to log file, to screen print only when the enabled 225 * log level is high enough. 226 */ 227 fibril_mutex_lock(&log_serializer); 228 229 const char *level_name = log_level_name(level); 230 231 if (log_stream != NULL) { 232 va_start(args, format); 233 234 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name); 235 vfprintf(log_stream, format, args); 236 fflush(log_stream); 237 238 va_end(args); 239 } 240 241 if (level <= log_level) { 242 va_start(args, format); 243 244 fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name); 245 vfprintf(screen_stream, format, args); 246 fflush(screen_stream); 247 248 va_end(args); 249 } 250 251 fibril_mutex_unlock(&log_serializer); 252 } 157 253 158 254 /**
Note:
See TracChangeset
for help on using the changeset viewer.