This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Removed static qualifier from tracef() to fix potential multithreading bug; - Added log levels.
- Loading branch information
Showing
9 changed files
with
121 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,97 @@ | ||
#include <inttypes.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
#include <unistd.h> /* getpid */ | ||
#include <string.h> /* strstr, strlen */ | ||
|
||
#include "tracing.h" | ||
#include "assert.h" /* assert */ | ||
|
||
static inline void noopTracerEmit(struct raft_tracer *t, | ||
const char *file, | ||
int line, | ||
const char *message) | ||
static inline void noopTracerEmit(struct raft_tracer *t UNUSED, | ||
const char *file UNUSED, | ||
unsigned int line UNUSED, | ||
const char *func UNUSED, | ||
unsigned int level UNUSED, | ||
const char *message UNUSED) | ||
{ | ||
(void)t; | ||
(void)file; | ||
(void)line; | ||
(void)message; | ||
} | ||
struct raft_tracer NoopTracer = {.impl = NULL, | ||
.enabled = false, | ||
.emit = noopTracerEmit}; | ||
|
||
static inline const char *tracer__short_file_name(const char *fname) | ||
{ | ||
static const char top_src_dir[] = "raft/"; | ||
const char *p; | ||
|
||
p = strstr(fname, top_src_dir); | ||
return p != NULL ? p + strlen(top_src_dir) : fname; | ||
} | ||
|
||
static inline const char *tracer__trace_level_name(unsigned int level) | ||
{ | ||
|
||
static const char *levels[] = { | ||
"NONE", "ALWAYS", "FATAL", "ERROR", "WARN", | ||
"NOTICE", "INFO", "DEBUG", "CALL" | ||
}; | ||
|
||
return level < ARRAY_SIZE(levels) ? levels[level] : levels[0]; | ||
} | ||
|
||
static pid_t tracer__pid_cached; | ||
|
||
static inline void tracer__emit(const char *file, unsigned int line, | ||
const char *func, unsigned int level, | ||
const char *message) | ||
{ | ||
struct timespec ts = {0}; | ||
struct tm tm; | ||
/* Use stack pointer instead of explicit thread id */ | ||
uint64_t sp = (uint64_t) &sp; | ||
|
||
clock_gettime(CLOCK_REALTIME, &ts); | ||
gmtime_r(&ts.tv_sec, &tm); | ||
|
||
fprintf(stderr, | ||
"LIBRAFT[%5.5u] %04d-%02d-%02dT%02d:%02d:%02d.%09lu " | ||
"%6.6x %-7s %-20s %s:%-3i %s\n", | ||
tracer__pid_cached, | ||
|
||
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, | ||
tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec, | ||
|
||
(unsigned)(sp & 0xffff), | ||
tracer__trace_level_name(level), | ||
func, tracer__short_file_name(file), | ||
line, message); | ||
} | ||
|
||
static inline void stderrTracerEmit(struct raft_tracer *t, | ||
const char *file, | ||
int line, | ||
unsigned int line, | ||
const char *func, | ||
unsigned int level, | ||
const char *message) | ||
{ | ||
(void)t; | ||
struct timespec ts = {0}; | ||
/* ignore errors */ | ||
clock_gettime(CLOCK_REALTIME, &ts); | ||
int64_t ns = ts.tv_sec * 1000000000 + ts.tv_nsec; | ||
fprintf(stderr, "LIBRAFT %" PRId64 " %s:%d %s\n", ns, file, line, | ||
message); | ||
assert(t->level < TRACE_NR); | ||
if (level <= t->level) | ||
tracer__emit(file, line, func, level, message); | ||
} | ||
struct raft_tracer StderrTracer = {.impl = NULL, | ||
.enabled = false, | ||
.emit = stderrTracerEmit}; | ||
|
||
void raft_tracer_maybe_enable(struct raft_tracer *tracer, bool enabled) | ||
{ | ||
if (getenv(LIBRAFT_TRACE) != NULL) { | ||
const char *trace_level = getenv(LIBRAFT_TRACE); | ||
|
||
if (trace_level != NULL) { | ||
tracer__pid_cached = getpid(); | ||
tracer->enabled = enabled; | ||
|
||
tracer->level = (unsigned) atoi(trace_level); | ||
tracer->level = tracer->level < TRACE_NR ? tracer->level : TRACE_NONE; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters