]> git.proxmox.com Git - libgit2.git/blob - src/trace.h
a233aa2257c3518f98c7eac699a3b644eb5b4efc
[libgit2.git] / src / trace.h
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_trace_h__
8 #define INCLUDE_trace_h__
9
10 #include "common.h"
11
12 #include <git2/trace.h>
13 #include "buffer.h"
14
15 #ifdef GIT_TRACE
16
17 struct git_trace_data {
18 git_trace_level_t level;
19 git_trace_cb callback;
20 };
21
22 extern struct git_trace_data git_trace__data;
23
24 GIT_INLINE(void) git_trace__write_fmt(
25 git_trace_level_t level,
26 const char *fmt,
27 va_list ap)
28 {
29 git_trace_cb callback = git_trace__data.callback;
30 git_buf message = GIT_BUF_INIT;
31
32 git_buf_vprintf(&message, fmt, ap);
33
34 callback(level, git_buf_cstr(&message));
35
36 git_buf_dispose(&message);
37 }
38
39 #define git_trace_level() (git_trace__data.level)
40
41 GIT_INLINE(void) git_trace(git_trace_level_t level, const char *fmt, ...)
42 {
43 if (git_trace__data.level >= level &&
44 git_trace__data.callback != NULL) {
45 va_list ap;
46
47 va_start(ap, fmt);
48 git_trace__write_fmt(level, fmt, ap);
49 va_end(ap);
50 }
51 }
52
53 #else
54
55 GIT_INLINE(void) git_trace__null(
56 git_trace_level_t level,
57 const char *fmt, ...)
58 {
59 GIT_UNUSED(level);
60 GIT_UNUSED(fmt);
61 }
62
63 #define git_trace_level() ((git_trace_level_t)0)
64 #define git_trace git_trace__null
65
66 #endif
67
68 #endif