Skip to content

Commit

Permalink
add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
mortie committed Nov 12, 2020
1 parent 027bab0 commit a16931a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum log_importance {
LOG_ERROR = 1,
LOG_INFO = 2,
LOG_DEBUG = 3,
LOG_TRACE = 4,
LOG_IMPORTANCE_LAST,
};

Expand All @@ -24,6 +25,8 @@ void swaylock_log_init(enum log_importance verbosity);
void _swaylock_log(enum log_importance verbosity, const char *format, ...)
_ATTRIB_PRINTF(2, 3);

void _swaylock_trace(const char *file, int line, const char *func);

const char *_swaylock_strip_path(const char *filepath);

#define swaylock_log(verb, fmt, ...) \
Expand All @@ -33,4 +36,7 @@ const char *_swaylock_strip_path(const char *filepath);
#define swaylock_log_errno(verb, fmt, ...) \
swaylock_log(verb, fmt ": %s", ##__VA_ARGS__, strerror(errno))

#define swaylock_trace() \
_swaylock_trace(__FILE__, __LINE__, __func__)

#endif
14 changes: 13 additions & 1 deletion log.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static const char *verbosity_colors[] = {
[LOG_ERROR ] = "\x1B[1;31m",
[LOG_INFO ] = "\x1B[1;34m",
[LOG_DEBUG ] = "\x1B[1;30m",
[LOG_TRACE ] = "\x1B[1;32m",
};

void swaylock_log_init(enum log_importance verbosity) {
Expand Down Expand Up @@ -58,11 +59,22 @@ void _swaylock_log(enum log_importance verbosity, const char *fmt, ...) {
va_end(args);
}

// This is mainly here for performance.
// Don't want to do _swaylock_strip_path every event if we're not tracing.
void _swaylock_trace(const char *file, int line, const char *func) {
if (LOG_TRACE > log_importance) {
return;
}

_swaylock_log(LOG_TRACE, "[%s:%d]: trace: %s",
_swaylock_strip_path(file), line, func);
}

const char *_swaylock_strip_path(const char *filepath) {
if (*filepath == '.') {
while (*filepath == '.' || *filepath == '/') {
++filepath;
}
}
return filepath;
}
}

0 comments on commit a16931a

Please sign in to comment.