Merge pull request #5309 from libgit2/ethomson/trace Improve trace support in tests
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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3c44775..8765a97 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,7 +50,7 @@ OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF)
OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF)
OPTION(TAGS "Generate tags" OFF)
OPTION(PROFILE "Generate profiling information" OFF)
-OPTION(ENABLE_TRACE "Enables tracing support" OFF)
+OPTION(ENABLE_TRACE "Enables tracing support" ON)
OPTION(LIBGIT2_FILENAME "Name of the produced binary" OFF)
OPTION(USE_SSH "Link with libssh2 to enable SSH support" ON)
OPTION(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON)
diff --git a/tests/clar_libgit2_trace.c b/tests/clar_libgit2_trace.c
index aaeeb78..83c83b9 100644
--- a/tests/clar_libgit2_trace.c
+++ b/tests/clar_libgit2_trace.c
@@ -10,26 +10,42 @@ struct method {
void (*close)(void);
};
+static const char *message_prefix(git_trace_level_t level)
+{
+ switch (level) {
+ case GIT_TRACE_NONE:
+ return "[NONE]: ";
+ case GIT_TRACE_FATAL:
+ return "[FATAL]: ";
+ case GIT_TRACE_ERROR:
+ return "[ERROR]: ";
+ case GIT_TRACE_WARN:
+ return "[WARN]: ";
+ case GIT_TRACE_INFO:
+ return "[INFO]: ";
+ case GIT_TRACE_DEBUG:
+ return "[DEBUG]: ";
+ case GIT_TRACE_TRACE:
+ return "[TRACE]: ";
+ default:
+ return "[?????]: ";
+ }
+}
#if defined(GIT_TRACE)
static void _git_trace_cb__printf(git_trace_level_t level, const char *msg)
{
- /* TODO Use level to print a per-message prefix. */
- GIT_UNUSED(level);
-
- printf("%s\n", msg);
+ printf("%s%s\n", message_prefix(level), msg);
}
#if defined(GIT_WIN32)
static void _git_trace_cb__debug(git_trace_level_t level, const char *msg)
{
- /* TODO Use level to print a per-message prefix. */
- GIT_UNUSED(level);
-
+ OutputDebugString(message_prefix(level));
OutputDebugString(msg);
OutputDebugString("\n");
- printf("%s\n", msg);
+ printf("%s%s\n", message_prefix(level), msg);
}
#else
#define _git_trace_cb__debug _git_trace_cb__printf
@@ -55,7 +71,7 @@ static struct method s_methods[] = {
static int s_trace_loaded = 0;
static int s_trace_level = GIT_TRACE_NONE;
static struct method *s_trace_method = NULL;
-
+static int s_trace_tests = 0;
static int set_method(const char *name)
{
@@ -101,6 +117,7 @@ static void _load_trace_params(void)
{
char *sz_level;
char *sz_method;
+ char *sz_tests;
s_trace_loaded = 1;
@@ -117,6 +134,10 @@ static void _load_trace_params(void)
sz_method = cl_getenv("CLAR_TRACE_METHOD");
if (set_method(sz_method) < 0)
set_method(NULL);
+
+ sz_tests = cl_getenv("CLAR_TRACE_TESTS");
+ if (sz_tests != NULL)
+ s_trace_tests = 1;
}
#define HR "================================================================"
@@ -139,6 +160,9 @@ void _cl_trace_cb__event_handler(
{
GIT_UNUSED(payload);
+ if (!s_trace_tests)
+ return;
+
switch (ev) {
case CL_TRACE__SUITE_BEGIN:
git_trace(GIT_TRACE_TRACE, "\n\n%s\n%s: Begin Suite", HR, suite_name);