Changeset 9b415c9 in mainline


Ignore:
Timestamp:
2011-03-30T21:19:57Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fc51296
Parents:
0418050
Message:

Add a simple logging module to C library and use it in devman. Only set
devman to display error messages by default. Based on devman_logging patch
by Vojtech Horky. Message levels inspired by USB team's logging facility.

Location:
uspace
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/Makefile

    r0418050 r9b415c9  
    7676        generic/io/io.c \
    7777        generic/io/printf.c \
     78        generic/io/log.c \
    7879        generic/io/klog.c \
    7980        generic/io/snprintf.c \
  • uspace/srv/devman/devman.c

    r0418050 r9b415c9  
    3434#include <fcntl.h>
    3535#include <sys/stat.h>
     36#include <io/log.h>
    3637#include <ipc/driver.h>
    3738#include <ipc/devman.h>
     
    146147        fibril_mutex_unlock(&drivers_list->drivers_mutex);
    147148
    148         printf(NAME": the '%s' driver was added to the list of available "
     149        log_msg(LVL_NOTE, "Driver `%s' was added to the list of available "
    149150            "drivers.\n", drv->name);
    150151}
     
    237238bool read_match_ids(const char *conf_path, match_id_list_t *ids)
    238239{
    239         printf(NAME ": read_match_ids conf_path = %s.\n", conf_path);
     240        log_msg(LVL_DEBUG, "read_match_ids(conf_path=\"%s\")\n", conf_path);
    240241       
    241242        bool suc = false;
     
    247248        fd = open(conf_path, O_RDONLY);
    248249        if (fd < 0) {
    249                 printf(NAME ": unable to open %s\n", conf_path);
     250                log_msg(LVL_ERROR, "Unable to open `%s' for reading: %s.\n",
     251                    conf_path, str_error(fd));
    250252                goto cleanup;
    251253        }
     
    255257        lseek(fd, 0, SEEK_SET);
    256258        if (len == 0) {
    257                 printf(NAME ": configuration file '%s' is empty.\n", conf_path);
     259                log_msg(LVL_ERROR, "Configuration file '%s' is empty.\n",
     260                    conf_path);
    258261                goto cleanup;
    259262        }
     
    261264        buf = malloc(len + 1);
    262265        if (buf == NULL) {
    263                 printf(NAME ": memory allocation failed when parsing file "
     266                log_msg(LVL_ERROR, "Memory allocation failed when parsing file "
    264267                    "'%s'.\n", conf_path);
    265268                goto cleanup;
     
    268271        ssize_t read_bytes = safe_read(fd, buf, len);
    269272        if (read_bytes <= 0) {
    270                 printf(NAME ": unable to read file '%s'.\n", conf_path);
     273                log_msg(LVL_ERROR, "Unable to read file '%s'.\n", conf_path);
    271274                goto cleanup;
    272275        }
     
    306309bool get_driver_info(const char *base_path, const char *name, driver_t *drv)
    307310{
    308         printf(NAME ": get_driver_info base_path = %s, name = %s.\n",
     311        log_msg(LVL_DEBUG, "get_driver_info(base_path=\"%s\", name=\"%s\")\n",
    309312            base_path, name);
    310313       
     
    338341        struct stat s;
    339342        if (stat(drv->binary_path, &s) == ENOENT) { /* FIXME!! */
    340                 printf(NAME ": driver not found at path %s.", drv->binary_path);
     343                log_msg(LVL_ERROR, "Driver not found at path `%s'.",
     344                    drv->binary_path);
    341345                goto cleanup;
    342346        }
     
    365369int lookup_available_drivers(driver_list_t *drivers_list, const char *dir_path)
    366370{
    367         printf(NAME ": lookup_available_drivers, dir = %s \n", dir_path);
     371        log_msg(LVL_DEBUG, "lookup_available_drivers(dir=\"%s\")\n", dir_path);
    368372       
    369373        int drv_cnt = 0;
     
    399403        dev_node_t *dev;
    400404       
    401         printf(NAME ": create_root_nodes\n");
     405        log_msg(LVL_DEBUG, "create_root_nodes()\n");
    402406       
    403407        fibril_rwlock_write_lock(&tree->rwlock);
     
    484488void attach_driver(dev_node_t *dev, driver_t *drv)
    485489{
    486         printf(NAME ": attach_driver %s to device %s\n",
    487             drv->name, dev->pfun->pathname);
     490        log_msg(LVL_DEBUG, "attach_driver(dev=\"%s\",drv=\"%s\")\n",
     491            dev->pfun->pathname, drv->name);
    488492       
    489493        fibril_mutex_lock(&drv->driver_mutex);
     
    507511        assert(fibril_mutex_is_locked(&drv->driver_mutex));
    508512       
    509         printf(NAME ": start_driver '%s'\n", drv->name);
     513        log_msg(LVL_DEBUG, "start_driver(drv=\"%s\")\n", drv->name);
    510514       
    511515        rc = task_spawnl(NULL, drv->binary_path, drv->binary_path, NULL);
    512516        if (rc != EOK) {
    513                 printf(NAME ": error spawning %s (%s)\n",
    514                     drv->name, str_error(rc));
     517                log_msg(LVL_ERROR, "Spawning driver `%s' (%s) failed: %s.\n",
     518                    drv->name, drv->binary_path, str_error(rc));
    515519                return false;
    516520        }
     
    574578        int phone;
    575579
    576         printf(NAME ": pass_devices_to_driver(`%s')\n", driver->name);
     580        log_msg(LVL_DEBUG, "pass_devices_to_driver(driver=\"%s\")\n",
     581            driver->name);
    577582
    578583        fibril_mutex_lock(&driver->driver_mutex);
     
    641646         * immediately and possibly started here as well.
    642647         */
    643         printf(NAME ": driver %s goes into running state.\n", driver->name);
     648        log_msg(LVL_DEBUG, "Driver `%s' enters running state.\n", driver->name);
    644649        driver->state = DRIVER_RUNNING;
    645650
     
    658663void initialize_running_driver(driver_t *driver, dev_tree_t *tree)
    659664{
    660         printf(NAME ": initialize_running_driver (`%s')\n", driver->name);
     665        log_msg(LVL_DEBUG, "initialize_running_driver(driver=\"%s\")\n",
     666            driver->name);
    661667       
    662668        /*
     
    748754         * access any structures that would affect driver_t.
    749755         */
    750         printf(NAME ": add_device (driver `%s', device `%s')\n", drv->name,
    751             dev->pfun->name);
     756        log_msg(LVL_DEBUG, "add_device(drv=\"%s\", dev=\"%s\")\n",
     757            drv->name, dev->pfun->name);
    752758       
    753759        sysarg_t rc;
     
    810816        driver_t *drv = find_best_match_driver(drivers_list, dev);
    811817        if (drv == NULL) {
    812                 printf(NAME ": no driver found for device '%s'.\n",
     818                log_msg(LVL_ERROR, "No driver found for device `%s'.\n",
    813819                    dev->pfun->pathname);
    814820                return false;
     
    848854bool init_device_tree(dev_tree_t *tree, driver_list_t *drivers_list)
    849855{
    850         printf(NAME ": init_device_tree.\n");
     856        log_msg(LVL_DEBUG, "init_device_tree()\n");
    851857       
    852858        tree->current_handle = 0;
     
    10271033        fun->pathname = (char *) malloc(pathsize);
    10281034        if (fun->pathname == NULL) {
    1029                 printf(NAME ": failed to allocate device path.\n");
     1035                log_msg(LVL_ERROR, "Failed to allocate device path.\n");
    10301036                return false;
    10311037        }
     
    10581064        assert(fibril_rwlock_is_write_locked(&tree->rwlock));
    10591065       
     1066        log_msg(LVL_DEBUG, "insert_dev_node(dev=%p, pfun=%p [\"%s\"])\n",
     1067            dev, pfun, pfun->pathname);
     1068
    10601069        /* Add the node to the handle-to-node map. */
    10611070        dev->handle = ++tree->current_handle;
     
    10641073
    10651074        /* Add the node to the list of its parent's children. */
    1066         printf("insert_dev_node: dev=%p, dev->pfun := %p\n", dev, pfun);
    10671075        dev->pfun = pfun;
    10681076        pfun->child = dev;
  • uspace/srv/devman/main.c

    r0418050 r9b415c9  
    4343#include <stdio.h>
    4444#include <errno.h>
     45#include <str_error.h>
    4546#include <bool.h>
    4647#include <fibril_synch.h>
     
    5152#include <sys/stat.h>
    5253#include <ctype.h>
     54#include <io/log.h>
    5355#include <ipc/devman.h>
    5456#include <ipc/driver.h>
     
    7173        driver_t *driver = NULL;
    7274
    73         printf(NAME ": devman_driver_register \n");
     75        log_msg(LVL_DEBUG, "devman_driver_register\n");
    7476       
    7577        iid = async_get_call(&icall);
     
    8890        }
    8991
    90         printf(NAME ": the %s driver is trying to register by the service.\n",
     92        log_msg(LVL_DEBUG, "The `%s' driver is trying to register.\n",
    9193            drv_name);
    9294       
     
    9597       
    9698        if (driver == NULL) {
    97                 printf(NAME ": no driver named %s was found.\n", drv_name);
     99                log_msg(LVL_ERROR, "No driver named `%s' was found.\n", drv_name);
    98100                free(drv_name);
    99101                drv_name = NULL;
     
    106108       
    107109        /* Create connection to the driver. */
    108         printf(NAME ":  creating connection to the %s driver.\n", driver->name);
     110        log_msg(LVL_DEBUG, "Creating connection to the `%s' driver.\n",
     111            driver->name);
    109112        ipc_call_t call;
    110113        ipc_callid_t callid = async_get_call(&call);
     
    118121        set_driver_phone(driver, IPC_GET_ARG5(call));
    119122       
    120         printf(NAME ": the %s driver was successfully registered as running.\n",
     123        log_msg(LVL_NOTE,
     124            "The `%s' driver was successfully registered as running.\n",
    121125            driver->name);
    122126       
     
    142146        callid = async_get_call(&call);
    143147        if (DEVMAN_ADD_MATCH_ID != IPC_GET_IMETHOD(call)) {
    144                 printf(NAME ": ERROR: devman_receive_match_id - invalid "
    145                     "protocol.\n");
     148                log_msg(LVL_ERROR,
     149                    "Invalid protocol when trying to receive match id.\n");
    146150                async_answer_0(callid, EINVAL);
    147151                delete_match_id(match_id);
     
    150154       
    151155        if (match_id == NULL) {
    152                 printf(NAME ": ERROR: devman_receive_match_id - failed to "
    153                     "allocate match id.\n");
     156                log_msg(LVL_ERROR, "Failed to allocate match id.\n");
    154157                async_answer_0(callid, ENOMEM);
    155158                return ENOMEM;
     
    165168        if (rc != EOK) {
    166169                delete_match_id(match_id);
    167                 printf(NAME ": devman_receive_match_id - failed to receive "
    168                     "match id string.\n");
     170                log_msg(LVL_ERROR, "Failed to receive match id string: %s.\n",
     171                    str_error(rc));
    169172                return rc;
    170173        }
     
    172175        list_append(&match_id->link, &match_ids->ids);
    173176       
    174         printf(NAME ": received match id '%s', score = %d \n",
     177        log_msg(LVL_DEBUG, "Received match id `%s', score %d.\n",
    175178            match_id->id, match_id->score);
    176179        return rc;
     
    228231        if (ftype != fun_inner && ftype != fun_exposed) {
    229232                /* Unknown function type */
    230                 printf(NAME ": Error, unknown function type provided by driver!\n");
     233                log_msg(LVL_ERROR,
     234                    "Unknown function type %d provided by driver.\n",
     235                    (int) ftype);
    231236
    232237                fibril_rwlock_write_unlock(&tree->rwlock);
     
    265270        fibril_rwlock_write_unlock(&tree->rwlock);
    266271       
    267         printf(NAME ": devman_add_function %s\n", fun->pathname);
     272        log_msg(LVL_DEBUG, "devman_add_function(fun=\"%s\")\n", fun->pathname);
    268273       
    269274        devman_receive_match_ids(match_count, &fun->match_ids);
     
    347352        devmap_register_class_dev(class_info);
    348353       
    349         printf(NAME ": function'%s' added to class '%s', class name '%s' was "
    350             "asigned to it\n", fun->pathname, class_name, class_info->dev_name);
     354        log_msg(LVL_NOTE, "Function `%s' added to class `%s' as `%s'.\n",
     355            fun->pathname, class_name, class_info->dev_name);
    351356
    352357        async_answer_0(callid, EOK);
     
    363368       
    364369        initialize_running_driver(driver, &device_tree);
    365         printf(NAME ": the %s driver was successfully initialized. \n",
     370        log_msg(LVL_DEBUG, "The `%s` driver was successfully initialized.\n",
    366371            driver->name);
    367372        return 0;
     
    385390        fid_t fid = fibril_create(init_running_drv, driver);
    386391        if (fid == 0) {
    387                 printf(NAME ": Error creating fibril for the initialization of "
    388                     "the newly registered running driver.\n");
     392                log_msg(LVL_ERROR, "Failed to create initialization fibril " \
     393                    "for driver `%s' .\n", driver->name);
    389394                return;
    390395        }
     
    484489         */
    485490        if (dev == NULL) {
    486                 printf(NAME ": devman_forward error - no device or function with "
    487                     "handle %" PRIun " was found.\n", handle);
     491                log_msg(LVL_ERROR, "IPC forwarding failed - no device or "
     492                    "function with handle %" PRIun " was found.\n", handle);
    488493                async_answer_0(iid, ENOENT);
    489494                return;
     
    491496
    492497        if (fun == NULL && !drv_to_parent) {
    493                 printf(NAME ": devman_forward error - cannot connect to "
    494                     "handle %" PRIun ", refers to a device.\n", handle);
     498                log_msg(LVL_ERROR, NAME ": devman_forward error - cannot "
     499                    "connect to handle %" PRIun ", refers to a device.\n",
     500                    handle);
    495501                async_answer_0(iid, ENOENT);
    496502                return;
     
    513519       
    514520        if (driver == NULL) {
    515                 printf(NAME ": devman_forward error - the device is not in %" PRIun
    516                     " usable state.\n", handle);
     521                log_msg(LVL_ERROR, "IPC forwarding refused - " \
     522                    "the device %" PRIun " is not in usable state.\n", handle);
    517523                async_answer_0(iid, ENOENT);
    518524                return;
     
    526532       
    527533        if (driver->phone <= 0) {
    528                 printf(NAME ": devman_forward: cound not forward to driver %s ",
    529                     driver->name);
    530                 printf("the driver's phone is %" PRIun ").\n", driver->phone);
     534                log_msg(LVL_ERROR,
     535                    "Could not forward to driver `%s' (phone is %d).\n",
     536                    driver->name, (int) driver->phone);
    531537                async_answer_0(iid, EINVAL);
    532538                return;
     
    534540
    535541        if (fun != NULL) {
    536                 printf(NAME ": devman_forward: forward connection to function %s to "
    537                     "driver %s.\n", fun->pathname, driver->name);
     542                log_msg(LVL_DEBUG,
     543                    "Forwarding request for `%s' function to driver `%s'.\n",
     544                    fun->pathname, driver->name);
    538545        } else {
    539                 printf(NAME ": devman_forward: forward connection to device %s to "
    540                     "driver %s.\n", dev->pfun->pathname, driver->name);
     546                log_msg(LVL_DEBUG,
     547                    "Forwarding request for `%s' device to driver `%s'.\n",
     548                    dev->pfun->pathname, driver->name);
    541549        }
    542550
     
    570578        async_forward_fast(iid, dev->drv->phone, DRIVER_CLIENT, fun->handle, 0,
    571579            IPC_FF_NONE);
    572         printf(NAME ": devman_connection_devmapper: forwarded connection to "
    573             "device %s to driver %s.\n", fun->pathname, dev->drv->name);
     580        log_msg(LVL_DEBUG,
     581            "Forwarding devmapper request for `%s' function to driver `%s'.\n",
     582            fun->pathname, dev->drv->name);
    574583}
    575584
     
    606615static bool devman_init(void)
    607616{
    608         printf(NAME ": devman_init - looking for available drivers.\n");
     617        log_msg(LVL_DEBUG, "devman_init - looking for available drivers.\n");
    609618       
    610619        /* Initialize list of available drivers. */
     
    612621        if (lookup_available_drivers(&drivers_list,
    613622            DRIVER_DEFAULT_STORE) == 0) {
    614                 printf(NAME " no drivers found.");
     623                log_msg(LVL_FATAL, "no drivers found.");
    615624                return false;
    616625        }
    617626
    618         printf(NAME ": devman_init - list of drivers has been initialized.\n");
     627        log_msg(LVL_DEBUG, "devman_init - list of drivers has been initialized.\n");
    619628
    620629        /* Create root device node. */
    621630        if (!init_device_tree(&device_tree, &drivers_list)) {
    622                 printf(NAME " failed to initialize device tree.");
     631                log_msg(LVL_FATAL, "Failed to initialize device tree.");
    623632                return false;
    624633        }
     
    641650        printf(NAME ": HelenOS Device Manager\n");
    642651
     652        if (log_init(NAME, LVL_ERROR) != EOK) {
     653                printf(NAME ": Error initializing logging subsystem.\n");
     654                return -1;
     655        }
     656
    643657        if (!devman_init()) {
    644                 printf(NAME ": Error while initializing service\n");
     658                log_msg(LVL_ERROR, "Error while initializing service.\n");
    645659                return -1;
    646660        }
     
    650664
    651665        /* Register device manager at naming service. */
    652         if (service_register(SERVICE_DEVMAN) != EOK)
     666        if (service_register(SERVICE_DEVMAN) != EOK) {
     667                log_msg(LVL_ERROR, "Failed registering as a service.\n");
    653668                return -1;
    654 
    655         printf(NAME ": Accepting connections\n");
     669        }
     670
     671        printf(NAME ": Accepting connections.\n");
    656672        async_manager();
    657673
Note: See TracChangeset for help on using the changeset viewer.