optional tracing
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5a0043f..7f2e293 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,7 @@ OPTION( BUILD_CLAR "Build Tests using the Clar suite" ON )
OPTION( BUILD_EXAMPLES "Build library usage example apps" OFF )
OPTION( TAGS "Generate tags" OFF )
OPTION( PROFILE "Generate profiling information" OFF )
+OPTION( ENABLE_TRACE "Enables tracing support" OFF )
IF(MSVC)
# This option is only availalbe when building with MSVC. By default,
# libgit2 is build using the stdcall calling convention, as that's what
@@ -105,6 +106,11 @@ ELSE()
FILE(GLOB SRC_SHA1 src/hash/hash_generic.c)
ENDIF()
+# Enable tracing
+IF (ENABLE_TRACE STREQUAL "ON")
+ ADD_DEFINITIONS(-DGIT_TRACE)
+ENDIF()
+
# Include POSIX regex when it is required
IF(WIN32 OR AMIGA)
INCLUDE_DIRECTORIES(deps/regex)
diff --git a/include/git2/trace.h b/include/git2/trace.h
new file mode 100644
index 0000000..7409b03
--- /dev/null
+++ b/include/git2/trace.h
@@ -0,0 +1,68 @@
+/*
+ * 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_git_trace_h__
+#define INCLUDE_git_trace_h__
+
+#include "common.h"
+#include "types.h"
+
+/**
+ * @file git2/trace.h
+ * @brief Git tracing configuration routines
+ * @defgroup git_trace Git tracing configuration routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/**
+ * Available tracing levels. When tracing is set to a particular level,
+ * callers will be provided tracing at the given level and all lower levels.
+ */
+typedef enum {
+ /** No tracing will be performed. */
+ GIT_TRACE_NONE = 0,
+
+ /** Severe errors that may impact the program's execution */
+ GIT_TRACE_FATAL = 1,
+
+ /** Errors that do not impact the program's execution */
+ GIT_TRACE_ERROR = 2,
+
+ /** Warnings that suggest abnormal data */
+ GIT_TRACE_WARN = 3,
+
+ /** Informational messages about program execution */
+ GIT_TRACE_INFO = 4,
+
+ /** Detailed data that allows for debugging */
+ GIT_TRACE_DEBUG = 5,
+
+ /** Exceptionally detailed debugging data */
+ GIT_TRACE_TRACE = 6
+} git_trace_level_t;
+
+/**
+ * An instance for a tracing function
+ */
+typedef void (*git_trace_callback)(git_trace_level_t level, const char *msg);
+
+/**
+ * Sets the system tracing configuration to the specified level with the
+ * specified callback. When system events occur at a level equal to, or
+ * lower than, the given level they will be reported to the given callback.
+ *
+ * @param level Level to set tracing to
+ * @param cb Function to call with trace data
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_trace_set(git_trace_level_t level, git_trace_callback cb);
+
+/** @} */
+GIT_END_DECL
+#endif
+
diff --git a/src/trace.c b/src/trace.c
new file mode 100644
index 0000000..159ac91
--- /dev/null
+++ b/src/trace.c
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+
+#include "buffer.h"
+#include "common.h"
+#include "global.h"
+#include "trace.h"
+#include "git2/trace.h"
+
+#ifdef GIT_TRACE
+
+struct git_trace_data git_trace__data = {0};
+
+#endif
+
+int git_trace_set(git_trace_level_t level, git_trace_callback callback)
+{
+#ifdef GIT_TRACE
+ assert(level == 0 || callback != NULL);
+
+ git_trace__data.level = level;
+ git_trace__data.callback = callback;
+ GIT_MEMORY_BARRIER;
+
+ return 0;
+#else
+ GIT_UNUSED(level);
+ GIT_UNUSED(callback);
+
+ giterr_set(GITERR_INVALID,
+ "This version of libgit2 was not built with tracing.");
+ return -1;
+#endif
+}
+
diff --git a/src/trace.h b/src/trace.h
new file mode 100644
index 0000000..f4bdff8
--- /dev/null
+++ b/src/trace.h
@@ -0,0 +1,56 @@
+/*
+ * 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 <stdarg.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, ...)
+{
+ git_trace_callback callback = git_trace__data.callback;
+ git_buf message = GIT_BUF_INIT;
+ va_list ap;
+
+ va_start(ap, fmt);
+ git_buf_vprintf(&message, fmt, ap);
+ va_end(ap);
+
+ 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
diff --git a/tests-clar/trace/trace.c b/tests-clar/trace/trace.c
new file mode 100644
index 0000000..712fe62
--- /dev/null
+++ b/tests-clar/trace/trace.c
@@ -0,0 +1,86 @@
+#include "clar_libgit2.h"
+#include "trace.h"
+
+static int written = 0;
+
+static void trace_callback(git_trace_level_t level, const char *message)
+{
+ cl_assert(strcmp(message, "Hello world!") == 0);
+
+ written = 1;
+}
+
+void test_trace_trace__initialize(void)
+{
+ git_trace_set(GIT_TRACE_INFO, trace_callback);
+ written = 0;
+}
+
+void test_trace_trace__cleanup(void)
+{
+ git_trace_set(GIT_TRACE_NONE, NULL);
+}
+
+void test_trace_trace__sets(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(git_trace_level() == GIT_TRACE_INFO);
+#endif
+}
+
+void test_trace_trace__can_reset(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(git_trace_level() == GIT_TRACE_INFO);
+ cl_git_pass(git_trace_set(GIT_TRACE_ERROR, trace_callback));
+
+ cl_assert(written == 0);
+ git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
+ cl_assert(written == 0);
+
+ git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
+ cl_assert(written == 1);
+#endif
+}
+
+void test_trace_trace__can_unset(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(git_trace_level() == GIT_TRACE_INFO);
+ cl_git_pass(git_trace_set(GIT_TRACE_NONE, NULL));
+
+ cl_assert(git_trace_level() == GIT_TRACE_NONE);
+
+ cl_assert(written == 0);
+ git_trace(GIT_TRACE_FATAL, "Hello %s!", "world");
+ cl_assert(written == 0);
+#endif
+}
+
+void test_trace_trace__skips_higher_level(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(written == 0);
+ git_trace(GIT_TRACE_DEBUG, "Hello %s!", "world");
+ cl_assert(written == 0);
+#endif
+}
+
+void test_trace_trace__writes(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(written == 0);
+ git_trace(GIT_TRACE_INFO, "Hello %s!", "world");
+ cl_assert(written == 1);
+#endif
+}
+
+void test_trace_trace__writes_lower_level(void)
+{
+#ifdef GIT_TRACE
+ cl_assert(written == 0);
+ git_trace(GIT_TRACE_ERROR, "Hello %s!", "world");
+ cl_assert(written == 1);
+#endif
+}
+