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.

G
groot 已提交
18
#include "LogUtil.h"
G
groot 已提交
19
#include "server/ServerConfig.h"
G
groot 已提交
20

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

H
Heisenberg 已提交
25

G
groot 已提交
26
namespace zilliz {
J
jinhai 已提交
27
namespace milvus {
G
groot 已提交
28 29
namespace server {

H
Heisenberg 已提交
30 31 32 33 34 35 36 37 38 39
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;
}

// TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename
40
void RolloutHandler(const char *filename, std::size_t size, el::Level level) {
H
Heisenberg 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    char *dirc = strdup(filename);
    char *basec = strdup(filename);
    char *dir = dirname(dirc);
    char *base = basename(basec);

    std::string s(base);
    std::stringstream ss;
    std::string
        list[] = {"\\", " ", "\'", "\"", "*", "\?", "{", "}", ";", "<", ">", "|", "^", "&", "$", "#", "!", "`", "~"};
    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 已提交
61
    if (level == el::Level::Global) {
H
Heisenberg 已提交
62 63
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
64
    } else if (level == el::Level::Debug) {
H
Heisenberg 已提交
65 66
        s.append("." + std::to_string(++debug_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
67
    } else if (level == el::Level::Warning) {
H
Heisenberg 已提交
68 69
        s.append("." + std::to_string(++warning_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
70
    } else if (level == el::Level::Trace) {
H
Heisenberg 已提交
71 72
        s.append("." + std::to_string(++trace_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
73
    } else if (level == el::Level::Error) {
H
Heisenberg 已提交
74 75
        s.append("." + std::to_string(++error_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
76
    } else if (level == el::Level::Fatal) {
H
Heisenberg 已提交
77 78
        s.append("." + std::to_string(++fatal_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
79
    } else {
H
Heisenberg 已提交
80 81 82 83 84
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
    }
}

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

    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
H
Heisenberg 已提交
90
    el::Helpers::installPreRollOutCallback(RolloutHandler);
91
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
G
groot 已提交
92 93

    return Status::OK();
G
groot 已提交
94 95 96 97
}


}   // server
J
jinhai 已提交
98
}   // milvus
G
groot 已提交
99
}   // zilliz