trace.h 1.28 KB
Newer Older
Edward Thomson committed
1 2 3 4 5 6 7 8 9
/*
 * 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__

10 11
#include "common.h"

Edward Thomson committed
12 13 14 15 16 17 18
#include <git2/trace.h>
#include "buffer.h"

#ifdef GIT_TRACE

struct git_trace_data {
	git_trace_level_t level;
19
	git_trace_cb callback;
Edward Thomson committed
20 21 22 23
};

extern struct git_trace_data git_trace__data;

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

Edward Thomson committed
32
	git_buf_vprintf(&message, fmt, ap);
nulltoken committed
33

Edward Thomson committed
34 35
	callback(level, git_buf_cstr(&message));

36
	git_buf_dispose(&message);
Edward Thomson committed
37 38
}

39
#define git_trace_level()	(git_trace__data.level)
40 41 42 43

GIT_INLINE(void) git_trace(git_trace_level_t level, const char *fmt, ...)
{
	if (git_trace__data.level >= level &&
44
	    git_trace__data.callback != NULL) {
45
		va_list ap;
Edward Thomson committed
46

47
		va_start(ap, fmt);
48
		git_trace__write_fmt(level, fmt, ap);
49 50 51
		va_end(ap);
	}
}
52

Edward Thomson committed
53 54
#else

55 56 57 58 59 60 61 62
GIT_INLINE(void) git_trace__null(
	git_trace_level_t level,
	const char *fmt, ...)
{
	GIT_UNUSED(level);
	GIT_UNUSED(fmt);
}

63 64
#define git_trace_level()	((git_trace_level_t)0)
#define git_trace		git_trace__null
Edward Thomson committed
65 66 67 68

#endif

#endif