-
Notifications
You must be signed in to change notification settings - Fork 55
/
syslog_logger.c
105 lines (93 loc) · 2.98 KB
/
syslog_logger.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2010-2014 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include <memcached/extension.h>
#include <memcached/engine.h>
#include "protocol_extension.h"
static EXTENSION_LOG_LEVEL current_log_level = EXTENSION_LOG_WARNING;
SERVER_HANDLE_V1 *sapi;
static const char *get_name(void) {
return "syslog";
}
static int map_priority(EXTENSION_LOG_LEVEL severity)
{
int priority;
switch (severity) {
case EXTENSION_LOG_WARNING:
priority = LOG_WARNING;
break;
case EXTENSION_LOG_INFO:
priority = LOG_NOTICE;
break;
case EXTENSION_LOG_DETAIL:
/****
priority = LOG_INFO;
break;
****/
case EXTENSION_LOG_DEBUG:
default:
priority = LOG_DEBUG;
}
return priority;
}
static void logger_log(EXTENSION_LOG_LEVEL severity,
const void* client_cookie,
const char *fmt, ...)
{
(void)client_cookie;
if (severity >= current_log_level) {
char buffer[2048];
va_list ap;
va_start(ap, fmt);
int len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap);
if (len != -1) {
syslog(map_priority(severity), "%s", buffer);
}
}
}
static EXTENSION_LOGGER_DESCRIPTOR descriptor = {
.get_name = get_name,
.log = 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();
setlogmask(LOG_UPTO(map_priority(current_log_level)));
}
}
MEMCACHED_PUBLIC_API
EXTENSION_ERROR_CODE memcached_extensions_initialize(const char *config,
GET_SERVER_API get_server_api) {
sapi = get_server_api();
if (sapi == NULL) {
return EXTENSION_FATAL;
}
current_log_level = sapi->log->get_level();
openlog("memcached", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_DAEMON);
setlogmask(LOG_UPTO(map_priority(current_log_level)));
if (!sapi->extension->register_extension(EXTENSION_LOGGER, &descriptor)) {
return EXTENSION_FATAL;
}
sapi->callback->register_callback(NULL, ON_LOG_LEVEL, on_log_level, NULL);
return EXTENSION_SUCCESS;
}