vasprintf 实现和 va_list

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

char *vasprintf(const char *fmt, va_list ap)
{
    unsigned int first, second;
    char *p;
    va_list aq;

    va_copy(aq, ap);
    first = vsnprintf(NULL, 0, fmt, aq);
    va_end(aq);

    p = malloc(first + 1);
    if (!p)
        return NULL;

    second = vsnprintf(p, first + 1, fmt, ap);
    if(first != second)
        printf("different return values (%u and %u) from vsnprintf(\"%s\", ...)",
            first, second, fmt);

    return p;
}


void debug_helper(const char *TAG, const char *fmt, va_list args)
{
    char *str = vasprintf(fmt, args);
    if(str)
    {
        printf("%s: %s\n", TAG, str);
        free(str);
    }
}

void debug(const char *TAG, const char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);

    debug_helper(TAG, fmt, args);

    va_end(args);
}

#define DEBUG_ERROR(...) debug("ERROR", __VA_ARGS__)

int main(int argc, char const *argv[])
{
    DEBUG_ERROR("Something wrong!");
    
    const char *TAG = __FUNCTION__;
    debug(TAG, "Some information");

    return 0;
}


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容