Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 20dd67e in mainline


Ignore:
Timestamp:
2012-08-17T13:40:22Z (9 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master
Children:
920d0fc
Parents:
267f235
Message:

USB logging uses standard logging

Location:
uspace/lib/usb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/include/usb/debug.h

    r267f235 r20dd67e  
    3838#include <inttypes.h>
    3939#include <usb/usb.h>
     40#include <io/log.h>
    4041#include <assert.h>
    4142
     
    4344    const uint8_t *, size_t);
    4445
    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
    8152
    8253/** Default log level. */
     
    8758#endif
    8859
    89 void usb_log_enable(usb_log_level_t, const char *);
     60void usb_log_enable(log_level_t , const char *);
    9061
    91 void usb_log_printf(usb_log_level_t, const char *, ...)
     62void usb_log_printf(log_level_t, const char *, ...)
    9263        PRINTF_ATTRIBUTE(2, 3);
    9364
  • uspace/lib/usb/src/debug.c

    r267f235 r20dd67e  
    4141#include <usb/debug.h>
    4242
    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 
    5643/** Enable logging.
    5744 *
     
    5946 * @param message_prefix Prefix for each printed message.
    6047 */
    61 void usb_log_enable(usb_log_level_t level, const char *message_prefix)
     48void usb_log_enable(log_level_t level, const char *message_prefix)
    6249{
    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         }
    7650        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         }
    9851}
    9952
     
    10356 * @param format Formatting directive.
    10457 */
    105 void usb_log_printf(usb_log_level_t level, const char *format, ...)
     58void usb_log_printf(log_level_t level, const char *format, ...)
    10659{
    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 
    11960        va_list args;
    120 
    121         /*
    122          * Serialize access to log files.
    123          * Print to screen only messages with higher level than the one
    124          * 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         }
    15061
    15162        va_start(args, format);
    15263        log_msgv(level, format, args);
    15364        va_end(args);
    154 
    155         fibril_mutex_unlock(&log_serializer);
    15665}
    15766
Note: See TracChangeset for help on using the changeset viewer.