-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcountlog.h
More file actions
74 lines (61 loc) · 1.95 KB
/
countlog.h
File metadata and controls
74 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef __COUNTLOG_H__
#define __COUNTLOG_H__
# include <pthread.h>
enum event_level { trace=10, debug=20, info = 30, warn = 40, error = 50, fatal = 60 };
enum event_arg_type { STRING=1, UNSIGNED_LONG=2 };
struct event_arg {
enum event_arg_type Type;
const char *Val_string;
unsigned long Val_ulong;
};
struct event_arg cl_str(const char *val) {
struct event_arg _ = { STRING, val, strlen(val) };
return _;
}
struct event_arg cl_ulong(unsigned long val) {
struct event_arg _ = { UNSIGNED_LONG, 0, val };
return _;
}
typedef void (*countlog0_pfn_t)(pid_t, int, struct ch_span);
static countlog0_pfn_t countlog0_func = NULL;
typedef void (*countlog1_pfn_t)(pid_t, int, struct ch_span, struct ch_span, struct event_arg);
static countlog1_pfn_t countlog1_func = NULL;
void load_koala_so_countlog(void *koala_so_handle) {
countlog0_func = (countlog0_pfn_t) dlsym(koala_so_handle, "countlog0");
countlog1_func = (countlog1_pfn_t) dlsym(koala_so_handle, "countlog1");
}
static __thread pid_t _thread_id = 0;
static pid_t get_thread_id() {
if (_thread_id == 0) {
#ifdef __APPLE__
uint64_t tid;
pthread_threadid_np(NULL, &tid);
_thread_id = (pid_t)tid;
#else
_thread_id = syscall(__NR_gettid);
#endif
}
return _thread_id;
}
static void countlog0(enum event_level level, const char *event) {
if (countlog0_func == NULL) {
return;
}
struct ch_span event_span;
event_span.Ptr = event;
event_span.Len = strlen(event);
countlog0_func(get_thread_id(), level, event_span);
}
static void countlog1(enum event_level level, const char *event, const char *k1, struct event_arg v1) {
if (countlog1_func == NULL) {
return;
}
struct ch_span event_span;
event_span.Ptr = event;
event_span.Len = strlen(event);
struct ch_span k1_span;
k1_span.Ptr = k1;
k1_span.Len = strlen(k1);
countlog1_func(get_thread_id(), level, event_span, k1_span, v1);
}
#endif