xref: /memcached-1.4.29/logger.h (revision c7fbcceb)
1 /* logging functions */
2 #ifndef LOGGER_H
3 #define LOGGER_H
4 
5 #include "bipbuffer.h"
6 
7 /* TODO: starttime tunable */
8 #define LOGGER_BUF_SIZE 1024 * 64
9 #define LOGGER_WATCHER_BUF_SIZE 1024 * 256
10 #define LOGGER_ENTRY_MAX_SIZE 2048
11 #define GET_LOGGER() ((logger *) pthread_getspecific(logger_key));
12 
13 enum log_entry_type {
14     LOGGER_ASCII_CMD = 0,
15     LOGGER_EVICTION,
16     LOGGER_ITEM_GET,
17     LOGGER_ITEM_STORE
18 };
19 
20 enum log_entry_subtype {
21     LOGGER_TEXT_ENTRY = 0,
22     LOGGER_EVICTION_ENTRY,
23     LOGGER_ITEM_GET_ENTRY,
24     LOGGER_ITEM_STORE_ENTRY
25 };
26 
27 enum logger_ret_type {
28     LOGGER_RET_OK = 0,
29     LOGGER_RET_NOSPACE,
30     LOGGER_RET_ERR
31 };
32 
33 enum logger_parse_entry_ret {
34     LOGGER_PARSE_ENTRY_OK = 0,
35     LOGGER_PARSE_ENTRY_FULLBUF,
36     LOGGER_PARSE_ENTRY_FAILED
37 };
38 
39 typedef const struct {
40     enum log_entry_subtype subtype;
41     int reqlen;
42     uint16_t eflags;
43     char *format;
44 } entry_details;
45 
46 /* log entry intermediary structures */
47 struct logentry_eviction {
48     long long int exptime;
49     uint32_t latime;
50     uint16_t it_flags;
51     uint8_t nkey;
52     char key[];
53 };
54 
55 struct logentry_item_get {
56     uint8_t was_found;
57     uint8_t nkey;
58     char key[];
59 };
60 
61 struct logentry_item_store {
62     int status;
63     int cmd;
64     uint8_t nkey;
65     char key[];
66 };
67 
68 /* end intermediary structures */
69 
70 typedef struct _logentry {
71     enum log_entry_subtype event;
72     uint16_t eflags;
73     uint64_t gid;
74     struct timeval tv; /* not monotonic! */
75     int size;
76     union {
77         void *entry; /* probably an item */
78         char end;
79     } data[];
80 } logentry;
81 
82 #define LOG_SYSEVENTS  (1<<1) /* threads start/stop/working */
83 #define LOG_FETCHERS   (1<<2) /* get/gets/etc */
84 #define LOG_MUTATIONS  (1<<3) /* set/append/incr/etc */
85 #define LOG_SYSERRORS  (1<<4) /* malloc/etc errors */
86 #define LOG_CONNEVENTS (1<<5) /* new client, closed, etc */
87 #define LOG_EVICTIONS  (1<<6) /* defailts of evicted items */
88 #define LOG_STRICT     (1<<7) /* block worker instead of drop */
89 #define LOG_RAWCMDS    (1<<9) /* raw ascii commands */
90 
91 typedef struct _logger {
92     struct _logger *prev;
93     struct _logger *next;
94     pthread_mutex_t mutex; /* guard for this + *buf */
95     uint64_t written; /* entries written to the buffer */
96     uint64_t dropped; /* entries dropped */
97     uint64_t blocked; /* times blocked instead of dropped */
98     uint16_t fetcher_ratio; /* log one out of every N fetches */
99     uint16_t mutation_ratio; /* log one out of every N mutations */
100     uint16_t eflags; /* flags this logger should log */
101     bipbuf_t *buf;
102     const entry_details *entry_map;
103 } logger;
104 
105 enum logger_watcher_type {
106     LOGGER_WATCHER_STDERR = 0,
107     LOGGER_WATCHER_CLIENT = 1
108 };
109 
110 typedef struct  {
111     void *c; /* original connection structure. still with source thread attached */
112     int sfd; /* client fd */
113     int id; /* id number for watcher list */
114     uint64_t skipped; /* lines skipped since last successful print */
115     bool failed_flush; /* recently failed to write out (EAGAIN), wait before retry */
116     enum logger_watcher_type t; /* stderr, client, syslog, etc */
117     uint16_t eflags; /* flags we are interested in */
118     bipbuf_t *buf; /* per-watcher output buffer */
119 } logger_watcher;
120 
121 
122 struct logger_stats {
123     uint64_t worker_dropped;
124     uint64_t worker_written;
125     uint64_t watcher_skipped;
126     uint64_t watcher_sent;
127 };
128 
129 extern pthread_key_t logger_key;
130 
131 /* public functions */
132 
133 void logger_init(void);
134 logger *logger_create(void);
135 
136 #define LOGGER_LOG(l, flag, type, ...) \
137     do { \
138         logger *myl = l; \
139         if (l == NULL) \
140             myl = GET_LOGGER(); \
141         if (myl->eflags & flag) \
142             logger_log(myl, type, __VA_ARGS__); \
143     } while (0)
144 
145 enum logger_ret_type logger_log(logger *l, const enum log_entry_type event, const void *entry, ...);
146 
147 enum logger_add_watcher_ret {
148     LOGGER_ADD_WATCHER_TOO_MANY = 0,
149     LOGGER_ADD_WATCHER_OK,
150     LOGGER_ADD_WATCHER_FAILED
151 };
152 
153 enum logger_add_watcher_ret logger_add_watcher(void *c, const int sfd, uint16_t f);
154 
155 #endif
156