forked from naver/arcus-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension_loggers.c
82 lines (68 loc) · 2.14 KB
/
extension_loggers.c
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
75
76
77
78
79
80
81
82
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <stdarg.h>
#include <stdio.h>
#include <memcached/extension.h>
#include <memcached/extension_loggers.h>
#include <memcached/engine.h>
static EXTENSION_LOG_LEVEL current_log_level = EXTENSION_LOG_WARNING;
SERVER_HANDLE_V1 *sapi;
static const char *stderror_get_name(void) {
return "standard error";
}
static void stderror_logger_log(EXTENSION_LOG_LEVEL severity,
const void* client_cookie,
const char *fmt, ...)
{
if (severity >= current_log_level) {
(void)client_cookie;
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
}
}
static EXTENSION_LOGGER_DESCRIPTOR stderror_logger_descriptor = {
.get_name = stderror_get_name,
.log = stderror_logger_log
};
static void on_log_level(const void *cookie,
ENGINE_EVENT_TYPE type,
const void *event_data,
const void *cb_data) {
if (sapi != NULL) {
current_log_level = sapi->log->get_level();
}
}
EXTENSION_ERROR_CODE memcached_initialize_stderr_logger(GET_SERVER_API get_server_api) {
sapi = get_server_api();
if (sapi == NULL) {
return EXTENSION_FATAL;
}
current_log_level = sapi->log->get_level();
sapi->callback->register_callback(NULL, ON_LOG_LEVEL,
on_log_level, NULL);
return EXTENSION_SUCCESS;
}
static const char *null_get_name(void) {
return "/dev/null";
}
static void null_logger_log(EXTENSION_LOG_LEVEL severity,
const void* client_cookie,
const char *fmt, ...)
{
(void)severity;
(void)client_cookie;
(void)fmt;
/* EMPTY */
}
static EXTENSION_LOGGER_DESCRIPTOR null_logger_descriptor = {
.get_name = null_get_name,
.log = null_logger_log
};
EXTENSION_LOGGER_DESCRIPTOR* get_null_logger(void) {
return &null_logger_descriptor;
}
EXTENSION_LOGGER_DESCRIPTOR* get_stderr_logger(void) {
return &stderror_logger_descriptor;
}