LogUtil.cpp 3.3 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

S
starlord 已提交
18 19
#include "utils/LogUtil.h"

G
groot 已提交
20
#include <ctype.h>
21
#include <libgen.h>
S
starlord 已提交
22
#include <string>
23

J
jinhai 已提交
24
namespace milvus {
G
groot 已提交
25 26
namespace server {

H
Heisenberg 已提交
27 28 29 30 31 32 33
namespace {
static int global_idx = 0;
static int debug_idx = 0;
static int warning_idx = 0;
static int trace_idx = 0;
static int error_idx = 0;
static int fatal_idx = 0;
34
}  // namespace
H
Heisenberg 已提交
35 36

// TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename
S
starlord 已提交
37
void
S
starlord 已提交
38 39 40 41 42
RolloutHandler(const char* filename, std::size_t size, el::Level level) {
    char* dirc = strdup(filename);
    char* basec = strdup(filename);
    char* dir = dirname(dirc);
    char* base = basename(basec);
H
Heisenberg 已提交
43 44 45

    std::string s(base);
    std::stringstream ss;
S
starlord 已提交
46 47
    std::string list[] = {"\\", " ", "\'", "\"", "*", "\?", "{", "}", ";", "<",
                          ">",  "|", "^",  "&",  "$", "#",  "!", "`", "~"};
H
Heisenberg 已提交
48 49 50 51 52 53 54 55 56 57 58
    std::string::size_type position;
    for (auto substr : list) {
        position = 0;
        while ((position = s.find_first_of(substr, position)) != std::string::npos) {
            s.insert(position, "\\");
            position += 2;
        }
    }
    int ret;
    std::string m(std::string(dir) + "/" + s);
    s = m;
H
Heisenberg 已提交
59
    if (level == el::Level::Global) {
H
Heisenberg 已提交
60 61
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
62
    } else if (level == el::Level::Debug) {
H
Heisenberg 已提交
63 64
        s.append("." + std::to_string(++debug_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
65
    } else if (level == el::Level::Warning) {
H
Heisenberg 已提交
66 67
        s.append("." + std::to_string(++warning_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
68
    } else if (level == el::Level::Trace) {
H
Heisenberg 已提交
69 70
        s.append("." + std::to_string(++trace_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
71
    } else if (level == el::Level::Error) {
H
Heisenberg 已提交
72 73
        s.append("." + std::to_string(++error_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
74
    } else if (level == el::Level::Fatal) {
H
Heisenberg 已提交
75 76
        s.append("." + std::to_string(++fatal_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
77
    } else {
H
Heisenberg 已提交
78 79 80 81 82
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
    }
}

S
starlord 已提交
83
Status
S
starlord 已提交
84
InitLog(const std::string& log_config_file) {
G
groot 已提交
85
    el::Configurations conf(log_config_file);
G
groot 已提交
86
    el::Loggers::reconfigureAllLoggers(conf);
87 88

    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
H
Heisenberg 已提交
89
    el::Helpers::installPreRollOutCallback(RolloutHandler);
90
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
S
starlord 已提交
91 92

    return Status::OK();
G
groot 已提交
93 94
}

S
starlord 已提交
95 96
}  // namespace server
}  // namespace milvus