Commit b63ad95888d867d8931b66b789cf86b89542ca0c

Patrick Steinhardt 2019-11-28T13:19:50

Merge pull request #5309 from libgit2/ethomson/trace Improve trace support in tests

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);