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

Changeset 2bf781a in mainline for uspace/lib/c/generic/io/log.c


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.