Beispiel #1
0
int logger_write(klogger_t* logger, int level, const char* format, ...) {
    char buffer[64] = {0};
    static const char* logger_level_name[] = { 0, "VERB", "INFO", "WARN", "ERRO", "FATA" };
    va_list va_ptr_file;
    va_list va_ptr_console;
    verify(logger);
    verify(format);
    if (logger->level > level) {
        /* 日志等级不足 */
        return error_ok;
    }
    va_start(va_ptr_file, format);
    va_start(va_ptr_console, format);
    time_get_string(buffer, sizeof(buffer));
    if (logger->mode & logger_mode_file) {
        /* 写入日志文件 */
        lock_lock(logger->lock);
        fprintf(logger->fd, "[%s][%s]", logger_level_name[level], buffer);
        vfprintf(logger->fd, format, va_ptr_file);
        fprintf(logger->fd, "\n");
        if (logger->mode & logger_mode_flush) {
            /* 立即写入 */
            fflush(logger->fd);
        }
        lock_unlock(logger->lock);
    }
    if (logger->mode & logger_mode_console) {
        /* 写入stderr */
        lock_lock(logger->lock);
        if (level == logger_level_verbose) {
            set_console_blue();
        } else if (level == logger_level_information) {
            set_console_white();
        } else if (level == logger_level_warning) {
            set_console_green();
        } else if (level == logger_level_error) {
            set_console_red();
        } else if (level == logger_level_fatal) {
            set_console_yellow();
        }
        fprintf(stderr, "[%s][%s]", logger_level_name[level], buffer);
        vfprintf(stderr, format, va_ptr_console);
        fprintf(stderr, "\n");
        if (logger->mode & logger_mode_flush) {
            /* 立即写入 */
            fflush(stderr);
        }
        lock_unlock(logger->lock);
    }
    set_console_white();
    va_end(va_ptr_file);
    va_end(va_ptr_console);
    return error_ok;
}
Beispiel #2
0
int logger_write(logger_t* logger, logger_level_e level, const char* format, ...) {
    char buffer[64] = {0};
    int  bytes = 0;
    static const char* logger_level_name[] = { 0, "VERB", "INFO", "WARN", "ERRO", "FATA" };
    va_list arg_ptr;
    assert(logger);
    assert(format);
    if (logger->level > level) {
        /* 日志等级不足 */
        return error_ok;
    }
    va_start(arg_ptr, format);
    time_get_string(buffer, sizeof(buffer));
    if (logger->mode & logger_mode_file) {
        /* 写入日志文件 */
        lock_lock(logger->lock);
        fprintf(logger->fd, "[%s][%s]", logger_level_name[level], buffer);
        bytes = vfprintf(logger->fd, format, arg_ptr);
        if (bytes <= 0) {
            lock_unlock(logger->lock);
            return error_logger_write;
        }
        fprintf(logger->fd, "\n");
        if (logger->mode & logger_mode_flush) {
            /* 立即写入 */
            fflush(logger->fd);
        }
        lock_unlock(logger->lock);
    }
    if (logger->mode & logger_mode_console) {
        /* 写入stderr */
        lock_lock(logger->lock);
        fprintf(stderr, "[%s][%s]", logger_level_name[level], buffer);
        vfprintf(stderr, format, arg_ptr);
        fprintf(stderr, "\n");
        if (logger->mode & logger_mode_flush) {
            /* 立即写入 */
            fflush(stderr);
        }
        lock_unlock(logger->lock);
    }
    va_end(arg_ptr);  
    return error_ok;
}