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 Haichao.chc |
17 | * \date Oct 2020 |
18 | * \brief Encapsulation of aitheta2 index logger |
19 | */ |
20 | |
21 | #pragma once |
22 | |
23 | #include <ailego/io/file.h> |
24 | #include <aitheta2/index_factory.h> |
25 | #include <gflags/gflags.h> |
26 | #include "common/error_code.h" |
27 | |
28 | namespace proxima { |
29 | namespace be { |
30 | |
31 | class LogUtil { |
32 | public: |
33 | static int Init(const std::string &log_dir, const std::string &log_file, |
34 | int log_level, const std::string &logger_type) { |
35 | if (log_dir.empty() || log_file.empty()) { |
36 | return ErrorCode_InvalidArgument; |
37 | } |
38 | |
39 | if (!ailego::File::IsExist(log_dir)) { |
40 | ailego::File::MakePath(log_dir); |
41 | } |
42 | |
43 | auto logger = aitheta2::IndexFactory::CreateLogger(logger_type); |
44 | if (!logger) { |
45 | LOG_FATAL("Invalid logger_type[%s]" , logger_type.c_str()); |
46 | return ErrorCode_InvalidArgument; |
47 | } |
48 | |
49 | aitheta2::IndexParams params; |
50 | params.set("proxima.file.logger.log_dir" , log_dir); |
51 | params.set("proxima.file.logger.log_file" , log_file); |
52 | params.set("proxima.file.logger.path" , log_dir + "/" + log_file); |
53 | std::string program_name = ailego::File::BaseName(gflags::GetArgv0()); |
54 | params.set("proxima.program.program_name" , program_name); |
55 | |
56 | int ret = logger->init(params); |
57 | if (ret != 0) { |
58 | return ret; |
59 | } |
60 | |
61 | aitheta2::IndexLoggerBroker::SetLevel(log_level); |
62 | aitheta2::IndexLoggerBroker::Register(logger); |
63 | return 0; |
64 | } |
65 | |
66 | static void Shutdown() { |
67 | aitheta2::IndexLoggerBroker::Unregister(); |
68 | } |
69 | }; |
70 | |
71 | } // namespace be |
72 | } // end namespace proxima |
73 | |