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

Changeset 2bf781a in mainline


Ignore:
Timestamp:
2012-08-16T21:50:52Z (9 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master
Children:
f50a756
Parents:
cba45af
Message:

Pretend logs form a tree hierarchy

Location:
uspace
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/stdio/logger2.c

    rcba45af r2bf781a  
    3636const char *test_logger2(void)
    3737{
    38         log_t log_alpha = log_create("alpha");
    39         log_t log_bravo = log_create("bravo");
     38        log_t log_alpha = log_create("alpha", LOG_DEFAULT);
     39        log_t log_bravo = log_create("bravo", log_alpha);
    4040
    41         TPRINTF("Alpha context is %" PRIlogctx ".\n", log_alpha);
    42         TPRINTF("Bravo context is %" PRIlogctx ".\n", log_bravo);
     41        TPRINTF("Alpha is %" PRIlogctx ".\n", log_alpha);
     42        TPRINTF("Bravo is %" PRIlogctx ".\n", log_bravo);
    4343
    4444        while (true) {
     
    5454                            (int) level, log_level_str(level));
    5555                        log_log_msg(log_bravo, level,
    56                             "Printing level %d (%s) into bravo log.",
     56                            "Printing level %d (%s) into bravo sub-log.",
    5757                            (int) level, log_level_str(level));
    5858                        async_usleep(1000 * 100);
  • uspace/lib/c/generic/io/log.c

    rcba45af r2bf781a  
    4343#include <ns.h>
    4444
     45typedef struct {
     46        char *name;
     47        sysarg_t top_log_id;
     48        sysarg_t log_id;
     49} log_info_t;
     50
     51static log_info_t default_log = {
     52        .name = NULL,
     53        .top_log_id = 0,
     54        .log_id = 0
     55};
     56
     57static sysarg_t default_top_log_id;
     58
    4559/** Log messages are printed under this name. */
    4660static const char *log_prog_name;
     
    6276#define MESSAGE_BUFFER_SIZE 4096
    6377
    64 static sysarg_t toplog_id;
    6578
    6679static int logger_register(async_sess_t *session, const char *prog_name)
     
    8699                return reg_msg_rc;
    87100
    88         toplog_id = IPC_GET_ARG1(answer);
     101        default_top_log_id = IPC_GET_ARG1(answer);
     102        default_log.top_log_id = default_top_log_id;
    89103
    90104        return EOK;
     
    97111                return ENOMEM;
    98112        }
     113        log_info_t *log_info = ctx != 0 ? (log_info_t *) ctx : &default_log;
    99114
    100115        aid_t reg_msg = async_send_3(exchange, LOGGER_WRITER_MESSAGE,
    101             toplog_id, ctx, level, NULL);
     116            log_info->top_log_id, log_info->log_id, level, NULL);
    102117        int rc = async_data_write_start(exchange, message, str_size(message));
    103118        sysarg_t reg_msg_rc;
     
    184199 * This function always returns a valid context.
    185200 */
    186 log_t log_create(const char *name)
    187 {
     201log_t log_create(const char *name, log_t parent)
     202{
     203        log_info_t *info = malloc(sizeof(log_info_t));
     204        if (info == NULL)
     205                return LOG_DEFAULT;
     206
     207        if (parent == LOG_DEFAULT) {
     208                info->name = str_dup(name);
     209                if (info->name == NULL) {
     210                        free(info);
     211                        return LOG_DEFAULT;
     212                }
     213                info->top_log_id = default_top_log_id;
     214        } else {
     215                log_info_t *parent_info = (log_info_t *) parent;
     216                int rc = asprintf(&info->name, "%s/%s",
     217                    parent_info->name, name);
     218                if (rc < 0) {
     219                        free(info);
     220                        return LOG_DEFAULT;
     221                }
     222                info->top_log_id = parent_info->top_log_id;
     223        }
     224
    188225        async_exch_t *exchange = async_exchange_begin(logger_session);
    189226        if (exchange == NULL)
    190                 return LOG_DEFAULT;
     227                goto error;
    191228
    192229        ipc_call_t answer;
    193         aid_t reg_msg = async_send_1(exchange, LOGGER_WRITER_CREATE_SUB_LOG, toplog_id, &answer);
    194         int rc = async_data_write_start(exchange, name, str_size(name));
     230        aid_t reg_msg = async_send_1(exchange, LOGGER_WRITER_CREATE_SUB_LOG,
     231            info->top_log_id, &answer);
     232        int rc = async_data_write_start(exchange, info->name, str_size(info->name));
    195233        sysarg_t reg_msg_rc;
    196234        async_wait_for(reg_msg, &reg_msg_rc);
     
    199237
    200238        if ((rc != EOK) || (reg_msg_rc != EOK))
    201                 return LOG_DEFAULT;
    202 
    203         return IPC_GET_ARG1(answer);
     239                goto error;
     240
     241        info->log_id = IPC_GET_ARG1(answer);
     242        return (sysarg_t) info;
     243
     244error:
     245        free(info->name);
     246        free(info);
     247        return LOG_DEFAULT;
    204248}
    205249
  • uspace/lib/c/include/io/log.h

    rcba45af r2bf781a  
    5959
    6060extern int log_init(const char *, log_level_t);
    61 
    62 extern log_t log_create(const char *);
     61extern log_t log_create(const char *, log_t);
    6362
    6463#define log_msg(level, format, ...) \
Note: See TracChangeset for help on using the changeset viewer.