Changeset ae2c925 in mainline for uspace/srv/logger/logs.c


Ignore:
Timestamp:
2012-08-17T11:47:03Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
52c4264
Parents:
90dc458
Message:

Refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/logger/logs.c

    r90dc458 rae2c925  
    4242
    4343
    44 static logger_log_t *find_log_by_name_and_parent_no_list_lock_and_acquire(const char *name, logger_log_t *parent)
     44static logger_log_t *find_log_by_name_and_parent_no_list_lock(const char *name, logger_log_t *parent)
    4545{
    4646        list_foreach(log_list, it) {
    4747                logger_log_t *log = list_get_instance(it, logger_log_t, link);
    48                 if ((parent == log->parent) && (str_cmp(log->name, name) == 0)) {
    49                         fibril_mutex_lock(&log->guard);
     48                if ((parent == log->parent) && (str_cmp(log->name, name) == 0))
    5049                        return log;
    51                 }
    5250        }
    5351
     
    7169}
    7270
    73 int find_or_create_log_and_acquire(const char *name, sysarg_t parent_id, logger_log_t **log_out)
    74 {
    75         int rc;
    76         logger_log_t *result = NULL;
    77         logger_log_t *parent = (logger_log_t *) parent_id;
    78 
    79         fibril_mutex_lock(&log_list_guard);
    80 
    81         result = find_log_by_name_and_parent_no_list_lock_and_acquire(name, parent);
    82         if (result != NULL) {
    83                 rc = EOK;
    84                 goto leave;
    85         }
    86 
    87         result = calloc(1, sizeof(logger_log_t));
    88         if (result == NULL) {
    89                 rc = ENOMEM;
    90                 goto leave;
    91         }
    92 
    93         result->logged_level = LOG_LEVEL_USE_DEFAULT;
     71static logger_log_t *create_log_no_locking(const char *name, logger_log_t *parent)
     72{
     73        logger_log_t *result = calloc(1, sizeof(logger_log_t));
     74        if (result == NULL)
     75                return NULL;
     76
    9477        result->name = str_dup(name);
     78        if (result->name == NULL)
     79                goto error;
     80
     81        /*
     82         * Notice that we create new dest as the last
     83         * operation that can fail and thus there is no code
     84         * to deallocate dest.
     85         */
    9586        if (parent == NULL) {
    9687                result->full_name = str_dup(name);
    97                 rc = create_dest(name, &result->dest);
     88                if (result->full_name == NULL)
     89                        goto error;
     90                int rc = create_dest(name, &result->dest);
    9891                if (rc != EOK)
    99                         goto error_result_allocated;
     92                        goto error;
    10093        } else {
    101                 rc = asprintf(&result->full_name, "%s/%s",
     94                int rc = asprintf(&result->full_name, "%s/%s",
    10295                    parent->full_name, name);
    10396                if (rc < 0)
    104                         goto error_result_allocated;
     97                        goto error;
    10598                result->dest = parent->dest;
    10699        }
     100
     101        /* Following initializations cannot fail. */
     102        result->logged_level = LOG_LEVEL_USE_DEFAULT;
     103        fibril_mutex_initialize(&result->guard);
     104        link_initialize(&result->link);
    107105        result->parent = parent;
    108         fibril_mutex_initialize(&result->guard);
    109 
    110         link_initialize(&result->link);
    111 
    112         fibril_mutex_lock(&result->guard);
    113 
    114         list_append(&result->link, &log_list);
    115 
    116 
    117 leave:
    118         fibril_mutex_unlock(&log_list_guard);
    119 
    120         if (rc == EOK) {
    121                 assert(fibril_mutex_is_locked(&result->guard));
    122                 *log_out = result;
    123         }
    124 
    125         return rc;
    126 
    127 error_result_allocated:
     106
     107        return result;
     108
     109error:
    128110        free(result->name);
    129111        free(result->full_name);
    130112        free(result);
    131 
    132         fibril_mutex_unlock(&log_list_guard);
    133 
    134         return rc;
     113        return NULL;
     114
     115}
     116
     117logger_log_t *find_or_create_log_and_acquire(const char *name, sysarg_t parent_id)
     118{
     119        logger_log_t *result = NULL;
     120        logger_log_t *parent = (logger_log_t *) parent_id;
     121
     122        fibril_mutex_lock(&log_list_guard);
     123
     124        result = find_log_by_name_and_parent_no_list_lock(name, parent);
     125        if (result == NULL) {
     126                result = create_log_no_locking(name, parent);
     127                if (result == NULL)
     128                        goto leave;
     129        }
     130
     131        fibril_mutex_lock(&result->guard);
     132
     133        list_append(&result->link, &log_list);
     134
     135leave:
     136        fibril_mutex_unlock(&log_list_guard);
     137
     138        return result;
    135139}
    136140
Note: See TracChangeset for help on using the changeset viewer.