Exemplo n.º 1
0
static void __log_init(void)
{
    int type = _log_type;
    const char *ident = _log_ident;
    if (_is_log_init) {
        return;
    }
    log_check_env();
#ifdef LOG_VERBOSE_ENABLE
    UPDATE_LOG_PREFIX(_log_prefix, LOG_VERBOSE_BIT);
#endif
    UPDATE_LOG_PREFIX(_log_prefix, LOG_FUNCLINE_BIT);

#ifdef LOG_IO_OPS
    _log_use_io = 1;
#endif
    if (_log_use_io) {
        _log_handle = &log_io_ops;
    } else {
        _log_handle = &log_fio_ops;
    }

    if (CHECK_LOG_PREFIX(_log_prefix, LOG_VERBOSE_BIT)) {
        memset(_proc_name, 0, sizeof(_proc_name));
        char *proc = get_proc_name();
        if (proc) {
            memset(_log_name, 0, sizeof(_log_name));
            strncpy(_proc_name, proc, strlen(proc));
            free(proc);
        }
    }
    if (type == 0) {
    } else {
        _log_output = type;
    }
    switch (_log_output) {
    case LOG_STDERR:
        _log_driver = &log_stderr_driver;
        break;
    case LOG_FILE:
        _log_driver = &log_file_driver;
        break;
    case LOG_RSYSLOG:
        _log_driver = &log_rsys_driver;
        break;
    default:
        fprintf(stderr, "unsupport log type!\n");
        break;
    }
    _log_driver->init(ident);
    _is_log_init = 1;
    pthread_mutex_init(&_log_mutex, NULL);
    return;
}
Exemplo n.º 2
0
static void log_check_env(void)
{
    _log_level = LOG_LEVEL_DEFAULT;
    const char *levelstr = level_str(getenv(LOG_LEVEL_ENV));
    const char *outputstr = output_str(getenv(LOG_OUTPUT_ENV));
    const char *timestr = time_str(getenv(LOG_TIMESTAMP_ENV));
    int level = atoi(levelstr);
    int output = atoi(outputstr);
    int timestamp = atoi(timestr);

    switch (level) {
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
        _log_level = level;
        break;
    case 0:
        if (is_str_equal(levelstr, "error")) {
            _log_level = LOG_ERR;
        } else if (is_str_equal(levelstr, "warn")) {
            _log_level = LOG_WARNING;
        } else if (is_str_equal(levelstr, "notice")) {
            _log_level = LOG_NOTICE;
        } else if (is_str_equal(levelstr, "info")) {
            _log_level = LOG_INFO;
        } else if (is_str_equal(levelstr, "debug")) {
            _log_level = LOG_DEBUG;
        } else if (is_str_equal(levelstr, "verbose")) {
            _log_level = LOG_VERB;
        }
        break;
    default:
        break;
    }
    switch (output) {
    case 1:
    case 2:
    case 3:
    case 4:
        _log_output = output;
        break;
    case 0:
        if (is_str_equal(outputstr, "stderr")) {
            _log_output = LOG_STDERR;
        } else if (is_str_equal(outputstr, "file")) {
            _log_output = LOG_FILE;
        } else if (is_str_equal(outputstr, "rsyslog")) {
            _log_output = LOG_RSYSLOG;
        }
        break;
    default:
        break;
    }
    switch (timestamp) {
    case 1:
        UPDATE_LOG_PREFIX(_log_prefix, LOG_TIMESTAMP_BIT);
        break;
    case 0:
        if (is_str_equal(timestr, "y") ||
            is_str_equal(timestr, "yes") ||
            is_str_equal(timestr, "true")) {
             UPDATE_LOG_PREFIX(_log_prefix, LOG_TIMESTAMP_BIT);
        }
        break;
    default:
        break;
    }
    if (_log_level == LOG_DEBUG) {
        UPDATE_LOG_PREFIX(_log_prefix, LOG_FUNCLINE_BIT);
    }
    if (_log_level == LOG_VERB) {
        UPDATE_LOG_PREFIX(_log_prefix, LOG_VERBOSE_BIT);
    }
}