1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Jiliang.ljl
17 * \date Mar 2021
18 * \brief Implementation of syslog
19 */
20
21#include <syslog.h>
22#include <ailego/io/file.h>
23#include <aitheta2/index_factory.h>
24
25namespace proxima {
26namespace be {
27
28class SysLogger : public aitheta2::IndexLogger {
29 public:
30 SysLogger() = default;
31
32 ~SysLogger() {
33 this->cleanup();
34 }
35
36 public:
37 int init(const aitheta2::IndexParams &params) override {
38 program_name_ = params.get_as_string("proxima.program.program_name");
39 // openlog require first argument to be accessible forever
40 openlog(program_name_.c_str(), LOG_PID, LOG_DAEMON);
41 return 0;
42 }
43
44 int cleanup() override {
45 closelog();
46 program_name_.clear();
47 return 0;
48 }
49
50 void log(int level, const char *file, int line, const char *format,
51 va_list args) override {
52 static int log_level_mapping[] = {LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERR,
53 LOG_CRIT};
54 char buf[2048];
55 vsnprintf(buf, sizeof(buf), format, args);
56 syslog(log_level_mapping[level], "%s:%d %s", file, line, buf);
57 }
58
59 private:
60 std::string program_name_;
61};
62
63INDEX_FACTORY_REGISTER_LOGGER(SysLogger);
64
65} // namespace be
66} // end namespace proxima
67