Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

output format #2

Open
sametsan opened this issue Jan 14, 2021 · 1 comment
Open

output format #2

sametsan opened this issue Jan 14, 2021 · 1 comment

Comments

@sametsan
Copy link

Hello

This library is awesome. But it doesnt write funciton for output. can you add function name ?

Thanks.

@pymumu
Copy link
Owner

pymumu commented Jan 15, 2021

static int my_format_func(char *buff, int maxlen, struct tlog_loginfo *info, void *userptr, const char *format, va_list ap)
{
    int len = 0;
    int total_len = 0;
    struct tlog_time *tm = &info->time;
    void* unused __attribute__ ((unused));

    unused = userptr;

    /* format prefix */
    len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d][%s] ", 
        tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000, 
        tlog_get_level_string(info->level), info->file, info->line, info->func);

    if (len < 0 || len >= maxlen) {
        return -1;
    }
    buff += len;
    total_len += len;
    maxlen -= len;

    /* format log message */
    len = vsnprintf(buff, maxlen, format, ap);
    if (len < 0 || len == maxlen) {
        return -1;
    }
    buff += len;
    total_len += len;

    /* return total length */
    return total_len;
}

tlog_reg_format_func(my_format_func);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants