예제 #1
0
SOL_API void
sol_log_print_function_file(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
{
    FILE *fp = data;
    const char *name = domain->name ? domain->name : "";
    char level_str[4] = { 0 };
    size_t len;
    int errno_bkp = errno;

    sol_log_level_to_str(message_level, level_str, sizeof(level_str));

    if (_main_pid != getpid())
        fprintf(fp, "P:%u ", getpid());
#ifdef _POSIX_THREADS
    if (_main_thread != pthread_self())
        fprintf(fp, "T%" PRIu64 " ", (uint64_t)(uintptr_t)pthread_self());
#endif

    if (_show_file && _show_function && _show_line)
        fprintf(fp, "%s:%s %s:%d %s() ", level_str, name, file, line, function);
    else {
        fprintf(fp, "%s:%s ", level_str, name);

        if (_show_file)
            fputs(file, fp);
        if (_show_file && _show_line)
            fputc(':', fp);
        if (_show_line)
            fprintf(fp, "%d", line);

        if (_show_file || _show_line)
            fputc(' ', fp);

        if (_show_function)
            fprintf(fp, "%s() ", function);
    }

    errno = errno_bkp;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
    vfprintf(fp, format, args);
#pragma GCC diagnostic pop

    len = strlen(format);
    if (len > 0 && format[len - 1] != '\n')
        fputc('\n', fp);
    fflush(fp);
}
예제 #2
0
SOL_API void
sol_log_print_function_file(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
{
    FILE *fp = data;
    const char *name = domain->name ? domain->name : "";
    char level_str[4] = { 0 };
    size_t len;
    int errno_bkp = errno;

    sol_log_level_to_str(message_level, level_str, sizeof(level_str));

    if (_main_pid != getpid())
        fprintf(fp, "P:%u ", getpid());
#ifdef PTHREAD
    if (_main_thread != pthread_self())
        fprintf(fp, "T:%lu ", pthread_self());
#endif

    if (_show_file && _show_function && _show_line)
        fprintf(fp, "%s:%s %s:%d %s() ", level_str, name, file, line, function);
    else {
        fprintf(fp, "%s:%s ", level_str, name);

        if (_show_file)
            fputs(file, fp);
        if (_show_file && _show_line)
            fputc(':', fp);
        if (_show_line)
            fprintf(fp, "%d", line);

        if (_show_file || _show_line)
            fputc(' ', fp);

        if (_show_function)
            fprintf(fp, "%s() ", function);
    }

    errno = errno_bkp;
    vfprintf(fp, format, args);

    len = strlen(format);
    if (len > 0 && format[len - 1] != '\n')
        fputc('\n', fp);
    fflush(fp);
}
예제 #3
0
void
sol_log_impl_print_function_stderr(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
{
    const char *name = domain->name ? domain->name : "";
    char level_str[4];
    size_t len;
    int errno_bkp = errno;

    sol_log_level_to_str(message_level, level_str, sizeof(level_str));

    if (_show_file && _show_function && _show_line) {
        printf("%s:%s %s:%d %s() ",
            level_str, name, file, line, function);
    } else {
        printf("%s:%s ", level_str, name);

        if (_show_file)
            printf("%s", file);
        if (_show_file && _show_line)
            printf(":");
        if (_show_line)
            printf("%d", line);

        if (_show_file || _show_line)
            printf(" ");

        if (_show_function)
            printf("%s() ", function);
    }

    errno = errno_bkp;

    vprintf(format, args);

    len = strlen(format);
    if (len > 0 && format[len - 1] != '\n')
        printf("\n");
}
예제 #4
0
void
sol_log_impl_print_function_stderr(void *data, const struct sol_log_domain *domain, uint8_t message_level, const char *file, const char *function, int line, const char *format, va_list args)
{
    const char *name = domain->name ? domain->name : "";
    char level_str[4] = { 0 };
    size_t len;
    int errno_bkp = errno;

    sol_log_level_to_str(message_level, level_str, sizeof(level_str));

    if (_main_pid != getpid())
        fprintf(stderr, "P%u ", getpid());
#ifdef PTHREAD
    if (_main_thread != pthread_self())
        fprintf(stderr, "T%" PRIu64 " ", (uint64_t)(uintptr_t)pthread_self());
#endif

    if (_show_file && _show_function && _show_line) {
        if (!_show_colors) {
            fprintf(stderr, "%s:%s %s:%d %s() ",
                level_str, name, file, line, function);
        } else {
            const char *level_color = sol_log_get_level_color(message_level);
            const char *reset_color = SOL_LOG_COLOR_RESET;
            const char *address_color = SOL_LOG_COLOR_HIGH;
            fprintf(stderr, "%s%s%s:%s%s%s %s%s:%d %s()%s ",
                level_color, level_str, reset_color,
                domain->color ? domain->color : "", name, reset_color,
                address_color, file, line, function, reset_color);
        }
    } else {
        const char *level_color = "", *reset_color = "", *address_color = "",
        *domain_color = "";

        if (_show_colors) {
            level_color = sol_log_get_level_color(message_level);
            reset_color = SOL_LOG_COLOR_RESET;
            address_color = SOL_LOG_COLOR_HIGH;
            domain_color = domain->color ? domain->color : "";
        }

        fprintf(stderr, "%s%s%s:%s%s%s ",
            level_color, level_str, reset_color,
            domain_color, name, reset_color);

        if (_show_file || _show_line || _show_function)
            fputs(address_color, stderr);

        if (_show_file)
            fputs(file, stderr);
        if (_show_file && _show_line)
            fputc(':', stderr);
        if (_show_line)
            fprintf(stderr, "%d", line);

        if (_show_file || _show_line)
            fputc(' ', stderr);

        if (_show_function)
            fprintf(stderr, "%s() ", function);

        if (_show_file || _show_line || _show_function)
            fputs(reset_color, stderr);
    }

    errno = errno_bkp;
    vfprintf(stderr, format, args);

    len = strlen(format);
    if (len > 0 && format[len - 1] != '\n')
        fputc('\n', stderr);
    fflush(stderr);
}