Skip to content

Commit b63ad95

Browse files
authored
Merge pull request libgit2#5309 from libgit2/ethomson/trace
Improve trace support in tests
2 parents 0e5243b + b7f70bc commit b63ad95

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ OPTION(BUILD_EXAMPLES "Build library usage example apps" OFF)
5050
OPTION(BUILD_FUZZERS "Build the fuzz targets" OFF)
5151
OPTION(TAGS "Generate tags" OFF)
5252
OPTION(PROFILE "Generate profiling information" OFF)
53-
OPTION(ENABLE_TRACE "Enables tracing support" OFF)
53+
OPTION(ENABLE_TRACE "Enables tracing support" ON)
5454
OPTION(LIBGIT2_FILENAME "Name of the produced binary" OFF)
5555
OPTION(USE_SSH "Link with libssh2 to enable SSH support" ON)
5656
OPTION(USE_HTTPS "Enable HTTPS support. Can be set to a specific backend" ON)

tests/clar_libgit2_trace.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,42 @@ struct method {
1010
void (*close)(void);
1111
};
1212

13+
static const char *message_prefix(git_trace_level_t level)
14+
{
15+
switch (level) {
16+
case GIT_TRACE_NONE:
17+
return "[NONE]: ";
18+
case GIT_TRACE_FATAL:
19+
return "[FATAL]: ";
20+
case GIT_TRACE_ERROR:
21+
return "[ERROR]: ";
22+
case GIT_TRACE_WARN:
23+
return "[WARN]: ";
24+
case GIT_TRACE_INFO:
25+
return "[INFO]: ";
26+
case GIT_TRACE_DEBUG:
27+
return "[DEBUG]: ";
28+
case GIT_TRACE_TRACE:
29+
return "[TRACE]: ";
30+
default:
31+
return "[?????]: ";
32+
}
33+
}
1334

1435
#if defined(GIT_TRACE)
1536
static void _git_trace_cb__printf(git_trace_level_t level, const char *msg)
1637
{
17-
/* TODO Use level to print a per-message prefix. */
18-
GIT_UNUSED(level);
19-
20-
printf("%s\n", msg);
38+
printf("%s%s\n", message_prefix(level), msg);
2139
}
2240

2341
#if defined(GIT_WIN32)
2442
static void _git_trace_cb__debug(git_trace_level_t level, const char *msg)
2543
{
26-
/* TODO Use level to print a per-message prefix. */
27-
GIT_UNUSED(level);
28-
44+
OutputDebugString(message_prefix(level));
2945
OutputDebugString(msg);
3046
OutputDebugString("\n");
3147

32-
printf("%s\n", msg);
48+
printf("%s%s\n", message_prefix(level), msg);
3349
}
3450
#else
3551
#define _git_trace_cb__debug _git_trace_cb__printf
@@ -55,7 +71,7 @@ static struct method s_methods[] = {
5571
static int s_trace_loaded = 0;
5672
static int s_trace_level = GIT_TRACE_NONE;
5773
static struct method *s_trace_method = NULL;
58-
74+
static int s_trace_tests = 0;
5975

6076
static int set_method(const char *name)
6177
{
@@ -101,6 +117,7 @@ static void _load_trace_params(void)
101117
{
102118
char *sz_level;
103119
char *sz_method;
120+
char *sz_tests;
104121

105122
s_trace_loaded = 1;
106123

@@ -117,6 +134,10 @@ static void _load_trace_params(void)
117134
sz_method = cl_getenv("CLAR_TRACE_METHOD");
118135
if (set_method(sz_method) < 0)
119136
set_method(NULL);
137+
138+
sz_tests = cl_getenv("CLAR_TRACE_TESTS");
139+
if (sz_tests != NULL)
140+
s_trace_tests = 1;
120141
}
121142

122143
#define HR "================================================================"
@@ -139,6 +160,9 @@ void _cl_trace_cb__event_handler(
139160
{
140161
GIT_UNUSED(payload);
141162

163+
if (!s_trace_tests)
164+
return;
165+
142166
switch (ev) {
143167
case CL_TRACE__SUITE_BEGIN:
144168
git_trace(GIT_TRACE_TRACE, "\n\n%s\n%s: Begin Suite", HR, suite_name);

0 commit comments

Comments
 (0)