Changeset 90dc458 in mainline


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

Create log files lazily

Location:
uspace/srv/logger
Files:
4 edited

Legend:

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

    r5239e17 r90dc458  
    3535 */
    3636#include <errno.h>
     37#include <str_error.h>
    3738#include <sysinfo.h>
    3839#include <str.h>
     
    6162                return;
    6263
    63         logger_log_t *log = find_or_create_log_and_acquire(key, 0);
    64         if (log == NULL)
     64        logger_log_t *log;
     65        rc = find_or_create_log_and_acquire(key, 0, &log);
     66        if (rc != EOK)
    6567                return;
    6668
  • uspace/srv/logger/logger.h

    r5239e17 r90dc458  
    5151
    5252typedef struct {
     53        fibril_mutex_t guard;
     54        char *filename;
    5355        FILE *logfile;
    5456} logger_dest_t;
     
    6769
    6870logger_log_t *find_log_by_name_and_acquire(const char *name);
    69 logger_log_t *find_or_create_log_and_acquire(const char *name, sysarg_t parent);
     71int find_or_create_log_and_acquire(const char *, sysarg_t, logger_log_t **);
    7072logger_log_t *find_log_by_id_and_acquire(sysarg_t);
    7173bool shall_log_message(logger_log_t *, log_level_t);
    7274void log_release(logger_log_t *);
     75void write_to_log(logger_log_t *, log_level_t, const char *);
    7376
    7477log_level_t get_default_logging_level(void);
  • uspace/srv/logger/logs.c

    r5239e17 r90dc458  
    4242
    4343
    44 static logger_log_t *find_log_by_name_and_parent_no_lock(const char *name, logger_log_t *parent)
     44static logger_log_t *find_log_by_name_and_parent_no_list_lock_and_acquire(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))
     48                if ((parent == log->parent) && (str_cmp(log->name, name) == 0)) {
     49                        fibril_mutex_lock(&log->guard);
    4950                        return log;
     51                }
    5052        }
    5153
     
    5860        if (result == NULL)
    5961                return ENOMEM;
    60         char *logfilename;
    61         int rc = asprintf(&logfilename, "/log/%s", name);
     62        int rc = asprintf(&result->filename, "/log/%s", name);
    6263        if (rc < 0) {
    6364                free(result);
    6465                return ENOMEM;
    6566        }
    66         result->logfile = fopen(logfilename, "a");
    67         free(logfilename);
    68         if (result->logfile == NULL) {
    69                 free(result);
    70                 return ENOMEM;
    71         }
     67        result->logfile = NULL;
     68        fibril_mutex_initialize(&result->guard);
    7269        *dest = result;
    7370        return EOK;
    7471}
    7572
    76 logger_log_t *find_or_create_log_and_acquire(const char *name, sysarg_t parent_id)
    77 {
     73int find_or_create_log_and_acquire(const char *name, sysarg_t parent_id, logger_log_t **log_out)
     74{
     75        int rc;
    7876        logger_log_t *result = NULL;
    7977        logger_log_t *parent = (logger_log_t *) parent_id;
     
    8179        fibril_mutex_lock(&log_list_guard);
    8280
    83         result = find_log_by_name_and_parent_no_lock(name, parent);
    84         if (result != NULL)
     81        result = find_log_by_name_and_parent_no_list_lock_and_acquire(name, parent);
     82        if (result != NULL) {
     83                rc = EOK;
    8584                goto leave;
     85        }
    8686
    8787        result = calloc(1, sizeof(logger_log_t));
    88         if (result == NULL)
     88        if (result == NULL) {
     89                rc = ENOMEM;
    8990                goto leave;
     91        }
    9092
    9193        result->logged_level = LOG_LEVEL_USE_DEFAULT;
     
    9395        if (parent == NULL) {
    9496                result->full_name = str_dup(name);
    95                 int rc = create_dest(name, &result->dest);
     97                rc = create_dest(name, &result->dest);
    9698                if (rc != EOK)
    9799                        goto error_result_allocated;
    98100        } else {
    99                 int rc = asprintf(&result->full_name, "%s/%s",
     101                rc = asprintf(&result->full_name, "%s/%s",
    100102                    parent->full_name, name);
    101103                if (rc < 0)
     
    116118        fibril_mutex_unlock(&log_list_guard);
    117119
    118         return result;
     120        if (rc == EOK) {
     121                assert(fibril_mutex_is_locked(&result->guard));
     122                *log_out = result;
     123        }
     124
     125        return rc;
    119126
    120127error_result_allocated:
     
    125132        fibril_mutex_unlock(&log_list_guard);
    126133
    127         return NULL;
     134        return rc;
    128135}
    129136
     
    190197}
    191198
     199void write_to_log(logger_log_t *log, log_level_t level, const char *message)
     200{
     201        assert(fibril_mutex_is_locked(&log->guard));
     202        assert(log->dest != NULL);
     203        fibril_mutex_lock(&log->dest->guard);
     204        if (log->dest->logfile == NULL)
     205                log->dest->logfile = fopen(log->dest->filename, "a");
     206
     207        if (log->dest->logfile != NULL) {
     208                fprintf(log->dest->logfile, "[%s] %s: %s\n",
     209                    log->full_name, log_level_str(level),
     210                    (const char *) message);
     211                fflush(log->dest->logfile);
     212        }
     213
     214        fibril_mutex_unlock(&log->dest->guard);
     215}
     216
    192217/**
    193218 * @}
  • uspace/srv/logger/writer.c

    r5239e17 r90dc458  
    5656                return NULL;
    5757
    58         logger_log_t *log = find_or_create_log_and_acquire(name, parent);
     58        logger_log_t *log = NULL;
     59        rc = find_or_create_log_and_acquire(name, parent, &log);
     60        if (rc)
    5961
    6062        free(name);
     
    8284            log->full_name, log_level_str(level),
    8385            (const char *) message);
    84         fprintf(log->dest->logfile, "[%s] %s: %s\n",
    85             log->full_name, log_level_str(level),
    86             (const char *) message);
    87         fflush(log->dest->logfile);
     86        write_to_log(log, level, message);
    8887
    8988        rc = EOK;
Note: See TracChangeset for help on using the changeset viewer.