trace.h 1.04 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
#include <git2/trace.h>
13
#include "str.h"
Edward Thomson committed
14 15 16

struct git_trace_data {
	git_trace_level_t level;
17
	git_trace_cb callback;
Edward Thomson committed
18 19 20 21
};

extern struct git_trace_data git_trace__data;

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

30
	git_str_vprintf(&message, fmt, ap);
nulltoken committed
31

32
	callback(level, git_str_cstr(&message));
Edward Thomson committed
33

34
	git_str_dispose(&message);
Edward Thomson committed
35 36
}

37
#define git_trace_level()	(git_trace__data.level)
38 39 40 41

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

45
		va_start(ap, fmt);
46
		git_trace__write_fmt(level, fmt, ap);
47 48 49
		va_end(ap);
	}
}
50

Edward Thomson committed
51
#endif