Hash :
950a7f76
Author :
Date :
2021-07-17T16:21:05
Variadic arguments aren't in C89 This refactors this to a wrapper function, as well as changing the existing API have a va_list and ... version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_trace_h__
#define INCLUDE_trace_h__
#include "common.h"
#include <git2/trace.h>
#include "buffer.h"
#ifdef GIT_TRACE
struct git_trace_data {
git_trace_level_t level;
git_trace_cb callback;
};
extern struct git_trace_data git_trace__data;
GIT_INLINE(void) git_trace__vwrite_fmt(
git_trace_level_t level,
const char *fmt, va_list ap)
{
git_trace_cb callback = git_trace__data.callback;
git_buf message = GIT_BUF_INIT;
git_buf_vprintf(&message, fmt, ap);
callback(level, git_buf_cstr(&message));
git_buf_dispose(&message);
}
GIT_INLINE(void) git_trace__write_fmt(
git_trace_level_t level,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
git_trace__vwrite_fmt(level, fmt, ap);
va_end(ap);
}
#define git_trace_level() (git_trace__data.level)
/* Varadic macros are a C99 feature */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
#define git_trace(l, ...) { \
if (git_trace__data.level >= l && \
git_trace__data.callback != NULL) { \
git_trace__write_fmt(l, __VA_ARGS__); \
} \
}
#else
GIT_INLINE(void) git_trace(git_trace_level_t level, const char *fmt, ...)
{
if (git_trace__data.level >= level &&
git_trace__data.callback != NULL) {
va_list ap;
va_start(ap, fmt);
git_trace__vwrite_fmt(level, fmt, ap);
va_end(ap);
}
}
#endif
#else
GIT_INLINE(void) git_trace__null(
git_trace_level_t level,
const char *fmt, ...)
{
GIT_UNUSED(level);
GIT_UNUSED(fmt);
}
#define git_trace_level() ((git_trace_level_t)0)
#define git_trace git_trace__null
#endif
#endif