Skip to content

Commit

Permalink
[feat] support color log
Browse files Browse the repository at this point in the history
  • Loading branch information
jyf111 committed Aug 12, 2023
1 parent c2149bc commit 184d632
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
1 change: 0 additions & 1 deletion eBPF_Supermarket/User_Function_Tracer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ Examples:
### TODO
- simplify c++ symbols
- more tests
- colorful log
- multithread
15 changes: 14 additions & 1 deletion eBPF_Supermarket/User_Function_Tracer/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@

#include "log.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int debug;

void log_color(const char* color) {
char* term = getenv("TERM");
if (isatty(fileno(stdout)) && !(term && !strcmp(term, "dumb"))) {
LOG("%s", color);
}
}

void log_char(char c, int cnt) {
while (cnt > 0) {
LOG("%c", c);
Expand All @@ -33,8 +44,10 @@ void log_tid(int tid) { LOG("%6d", tid); }

void log_cpuid(int cpuid) { LOG("%4d", cpuid); }

void log_split() { LOG(" | "); }

void log_time(size_t ns) {
static char *units[] = {
static char* units[] = {
"ns", "us", "ms", " s", " m", " h",
};
static size_t limit[] = {
Expand Down
19 changes: 18 additions & 1 deletion eBPF_Supermarket/User_Function_Tracer/src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
#include <stdio.h>

/**
* @brief 输出cnt个字符c
* @brief 设置输出的颜色
* @param[in] color 颜色控制符
*/
void log_color(const char* color);

/**
* @brief 输出连续cnt个字符c
* @param[in] c 字符
* @param[in] cnt 个数
*/
Expand All @@ -46,6 +52,12 @@ void log_tid(int tid);
*/
void log_cpuid(int cpuid);

/**
* @brief 输出分隔符
* @details 分隔符为" | "
*/
void log_split();

/**
* @brief 输出时间及其单位
* @param[in] ns 时间(单位纳秒)
Expand Down Expand Up @@ -86,4 +98,9 @@ extern int debug;
fprintf(stderr, fmt, ##__VA_ARGS__); \
} while (0)

#define TERM_RED "\033[0;31m"
#define TERM_GREEN "\033[0;32m"
#define TERM_GRAY "\033[0;90m"
#define TERM_NC "\033[0m"

#endif // UTRACE_LOG_H
26 changes: 15 additions & 11 deletions eBPF_Supermarket/User_Function_Tracer/src/utrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,28 @@ static int handle_event(void *ctx, void *data, size_t data_sz) {
// return 0;
if (status == 0 && r->ustack_sz == pre_ustack_sz) {
log_cpuid(r->cpu_id);
LOG(" | ");
log_split();
log_tid(r->tid);
LOG(" | ");
log_split();
log_time(r->duration_ns);
LOG(" | ");
log_split();
log_char(' ', 2 * r->ustack_sz - 2);
LOG("%s();\n", stack_func[r->ustack_sz]);
status = 1;
pre_ustack_sz = r->ustack_sz;
} else if (status == 1 && r->ustack_sz == pre_ustack_sz - 1) {
log_cpuid(r->cpu_id);
LOG(" | ");
log_split();
log_tid(r->tid);
LOG(" | ");
log_split();
log_time(r->duration_ns);
LOG(" | ");
log_split();
log_char(' ', 2 * r->ustack_sz - 2);
LOG("} /* %s */\n", stack_func[r->ustack_sz]);
LOG("} ");
log_color(TERM_GRAY);
LOG("/* %s */", stack_func[r->ustack_sz]);
log_color(TERM_NC);
LOG("\n");
status = 1;
pre_ustack_sz = r->ustack_sz;
}
Expand All @@ -213,11 +217,11 @@ static int handle_event(void *ctx, void *data, size_t data_sz) {
if (status == 1 && r->ustack_sz != pre_ustack_sz) return 0;
if (status == 0) {
log_cpuid(r->cpu_id);
LOG(" | ");
log_split();
log_tid(r->tid);
LOG(" |");
log_char(' ', 12);
LOG(" | ");
log_split();
log_char(' ', 11);
log_split();
log_char(' ', 2 * r->ustack_sz - 4);
LOG("%s() {\n", stack_func[r->ustack_sz - 1]);
}
Expand Down

0 comments on commit 184d632

Please sign in to comment.