trace.h 1.12 KB
Newer Older
Edward Thomson committed
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
/*
 * 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 <git2/trace.h>
#include "buffer.h"

#ifdef GIT_TRACE

struct git_trace_data {
	git_trace_level_t level;
	git_trace_callback callback;
};

extern struct git_trace_data git_trace__data;

GIT_INLINE(void) git_trace__write_fmt(
	git_trace_level_t level,
	const char *fmt, ...)
{
nulltoken committed
26
	git_trace_callback callback = git_trace__data.callback;
Edward Thomson committed
27 28
	git_buf message = GIT_BUF_INIT;
	va_list ap;
nulltoken committed
29

Edward Thomson committed
30 31 32
	va_start(ap, fmt);
	git_buf_vprintf(&message, fmt, ap);
	va_end(ap);
nulltoken committed
33

Edward Thomson committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	callback(level, git_buf_cstr(&message));

	git_buf_free(&message);
}

#define git_trace_level()		(git_trace__data.level)
#define git_trace(l, ...)		{ \
									if (git_trace__data.level >= l && \
										git_trace__data.callback != NULL) { \
										git_trace__write_fmt(l, __VA_ARGS__); \
									} \
								}

#else

#define git_trace_level()		((void)0)
#define git_trace(lvl, ...)		((void)0)

#endif

#endif