Changeset 6028ec8 in mainline


Ignore:
Timestamp:
2011-02-01T11:28:09Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0b31409
Parents:
632ed68
Message:

Add generic logging functions

See ticket #52.

Location:
uspace/lib/usb
Files:
2 edited

Legend:

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

    r632ed68 r6028ec8  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2010-2011 Vojtech Horky
    33 * All rights reserved.
    44 *
     
    3737#include <stdio.h>
    3838#include <usb/usb.h>
     39#include <assert.h>
    3940
    4041void usb_dprintf(const char *tag, int level, const char *format, ...);
     
    4445    const uint8_t *, size_t);
    4546
     47/** Logging level. */
     48typedef enum {
     49    USB_LOG_LEVEL_FATAL,
     50    USB_LOG_LEVEL_ERROR,
     51    USB_LOG_LEVEL_WARNING,
     52    USB_LOG_LEVEL_INFO,
     53    USB_LOG_LEVEL_DEBUG,
     54    USB_LOG_LEVEL_DEBUG2
     55} usb_log_level_t;
     56
     57
     58void usb_log_enable(usb_log_level_t, const char *);
     59
     60void usb_log_printf(usb_log_level_t, const char *, ...);
     61
     62#define usb_log_fatal(format, ...) \
     63        usb_log_printf(USB_LOG_LEVEL_FATAL, format, ##__VA_ARGS__)
     64
     65#define usb_log_error(format, ...) \
     66        usb_log_printf(USB_LOG_LEVEL_ERROR, format, ##__VA_ARGS__)
     67
     68#define usb_log_warning(format, ...) \
     69        usb_log_printf(USB_LOG_LEVEL_WARNING, format, ##__VA_ARGS__)
     70
     71#define usb_log_info(format, ...) \
     72        usb_log_printf(USB_LOG_LEVEL_INFO, format, ##__VA_ARGS__)
     73
     74#define usb_log_debug(format, ...) \
     75        usb_log_printf(USB_LOG_LEVEL_DEBUG, format, ##__VA_ARGS__)
     76
     77#define usb_log_debug2(format, ...) \
     78        usb_log_printf(USB_LOG_LEVEL_DEBUG2, format, ##__VA_ARGS__)
     79
     80
     81
    4682#endif
    4783/**
  • uspace/lib/usb/src/debug.c

    r632ed68 r6028ec8  
    11/*
    2  * Copyright (c) 2010 Vojtech Horky
     2 * Copyright (c) 2010-2011 Vojtech Horky
    33 * All rights reserved.
    44 *
     
    6161static FIBRIL_MUTEX_INITIALIZE(tag_list_guard);
    6262
     63/** Level of logging messages. */
     64static usb_log_level_t log_level = USB_LOG_LEVEL_WARNING;
     65/** Prefix for logging messages. */
     66static const char *log_prefix = "usb";
     67/** Serialization mutex for logging functions. */
     68static FIBRIL_MUTEX_INITIALIZE(log_serializer);
     69
    6370/** Find or create new tag with given name.
    6471 *
     
    155162}
    156163
     164/** Enable logging.
     165 *
     166 * @param level Maximal enabled level (including this one).
     167 * @param message_prefix Prefix for each printed message.
     168 */
     169void usb_log_enable(usb_log_level_t level, const char *message_prefix)
     170{
     171        log_prefix = message_prefix;
     172        log_level = level;
     173}
     174
     175
     176static const char *log_level_name(usb_log_level_t level)
     177{
     178        switch (level) {
     179                case USB_LOG_LEVEL_FATAL:
     180                        return " FATAL";
     181                case USB_LOG_LEVEL_ERROR:
     182                        return " ERROR";
     183                case USB_LOG_LEVEL_WARNING:
     184                        return " WARN";
     185                case USB_LOG_LEVEL_INFO:
     186                        return " info";
     187                default:
     188                        return "";
     189        }
     190}
     191
     192/** Print logging message.
     193 *
     194 * @param level Verbosity level of the message.
     195 * @param format Formatting directive.
     196 */
     197void usb_log_printf(usb_log_level_t level, const char *format, ...)
     198{
     199        if (level > log_level) {
     200                return;
     201        }
     202
     203        FILE *stream = NULL;
     204        switch (level) {
     205                case USB_LOG_LEVEL_FATAL:
     206                case USB_LOG_LEVEL_ERROR:
     207                        stream = stderr;
     208                        break;
     209                default:
     210                        stream = stdout;
     211                        break;
     212        }
     213        assert(stream != NULL);
     214
     215        va_list args;
     216        va_start(args, format);
     217
     218        fibril_mutex_lock(&log_serializer);
     219        fprintf(stream, "[%s]%s: ", log_prefix, log_level_name(level));
     220        vfprintf(stream, format, args);
     221        fibril_mutex_unlock(&log_serializer);
     222
     223        va_end(args);
     224}
    157225
    158226/**
Note: See TracChangeset for help on using the changeset viewer.