Changes in uspace/lib/usb/src/debug.c [a9d85df:acdb5bac] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/usb/src/debug.c
ra9d85df racdb5bac 40 40 #include <ddf/log.h> 41 41 #include <usb/debug.h> 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 /** Enable logging.57 *58 * @param level Maximal enabled level (including this one).59 * @param message_prefix Prefix for each printed message.60 */61 void usb_log_enable(usb_log_level_t level, const char *message_prefix)62 {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 }77 78 /** Get log level name prefix.79 *80 * @param level Log level.81 * @return String prefix for the message.82 */83 static const char *log_level_name(usb_log_level_t level)84 {85 switch (level) {86 case USB_LOG_LEVEL_FATAL:87 return " FATAL";88 case USB_LOG_LEVEL_ERROR:89 return " ERROR";90 case USB_LOG_LEVEL_WARNING:91 return " WARN";92 case USB_LOG_LEVEL_INFO:93 return " info";94 default:95 return "";96 }97 }98 99 /** Print logging message.100 *101 * @param level Verbosity level of the message.102 * @param format Formatting directive.103 */104 void usb_log_printf(usb_log_level_t level, const char *format, ...)105 {106 FILE *screen_stream = NULL;107 switch (level) {108 case USB_LOG_LEVEL_FATAL:109 case USB_LOG_LEVEL_ERROR:110 screen_stream = stderr;111 break;112 default:113 screen_stream = stdout;114 break;115 }116 assert(screen_stream != NULL);117 118 va_list args;119 120 /*121 * Serialize access to log files.122 * Print to screen only messages with higher level than the one123 * specified during logging initialization.124 * Print also to file, to it print one more (lower) level as well.125 */126 fibril_mutex_lock(&log_serializer);127 128 const char *level_name = log_level_name(level);129 130 if ((log_stream != NULL) && (level <= log_level + 1)) {131 va_start(args, format);132 133 fprintf(log_stream, "[%s]%s: ", log_prefix, level_name);134 vfprintf(log_stream, format, args);135 fflush(log_stream);136 137 va_end(args);138 }139 140 if (level <= log_level) {141 va_start(args, format);142 143 fprintf(screen_stream, "[%s]%s: ", log_prefix, level_name);144 vfprintf(screen_stream, format, args);145 fflush(screen_stream);146 147 va_end(args);148 }149 150 fibril_mutex_unlock(&log_serializer);151 }152 153 42 154 43 #define REMAINDER_STR_FMT " (%zu)..." … … 195 84 * Remove previous string. 196 85 */ 197 bzero(buffer_dump[buffer_dump_index], BUFFER_DUMP_LEN);86 memset(buffer_dump[buffer_dump_index], 0, BUFFER_DUMP_LEN); 198 87 199 88 /* Do the actual dump. */
Note:
See TracChangeset
for help on using the changeset viewer.