LogUtil.cpp 4.4 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"
20
#include "easylogging++.h"
G
groot 已提交
21

G
groot 已提交
22
#include <ctype.h>
G
groot 已提交
23

24 25 26
#include <string>
#include <libgen.h>

H
Heisenberg 已提交
27

G
groot 已提交
28
namespace zilliz {
J
jinhai 已提交
29
namespace milvus {
G
groot 已提交
30 31
namespace server {

H
Heisenberg 已提交
32 33 34 35 36 37 38 39 40 41
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
42
void RolloutHandler(const char *filename, std::size_t size, el::Level level) {
H
Heisenberg 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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 已提交
63
    if (level == el::Level::Global) {
H
Heisenberg 已提交
64 65
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
66
    } else if (level == el::Level::Debug) {
H
Heisenberg 已提交
67 68
        s.append("." + std::to_string(++debug_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
69
    } else if (level == el::Level::Warning) {
H
Heisenberg 已提交
70 71
        s.append("." + std::to_string(++warning_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
72
    } else if (level == el::Level::Trace) {
H
Heisenberg 已提交
73 74
        s.append("." + std::to_string(++trace_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
75
    } else if (level == el::Level::Error) {
H
Heisenberg 已提交
76 77
        s.append("." + std::to_string(++error_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
78
    } else if (level == el::Level::Fatal) {
H
Heisenberg 已提交
79 80
        s.append("." + std::to_string(++fatal_idx));
        ret = rename(m.c_str(), s.c_str());
H
Heisenberg 已提交
81
    } else {
H
Heisenberg 已提交
82 83 84 85 86 87
        s.append("." + std::to_string(++global_idx));
        ret = rename(m.c_str(), s.c_str());
    }
}

int32_t InitLog(const std::string &log_config_file) {
G
groot 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#if 0
    ServerConfig &config = ServerConfig::GetInstance();
    ConfigNode log_config = config.GetConfig(CONFIG_LOG);
    const std::map<std::string, ConfigNode>& settings = log_config.GetChildren();

    std::string str_config;
    for(auto iter : settings) {
        str_config += "* ";
        str_config += iter.first;
        str_config += ":";
        str_config.append("\n");

        auto sub_configs = iter.second.GetConfig();
        for(auto it_sub : sub_configs) {
            str_config += "    ";
            str_config += it_sub.first;
            str_config += " = ";
            std::string temp = it_sub.first;
            std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
            bool is_text = (temp == "format" || temp == "filename");
            if(is_text){
                str_config += "\"";
            }
            str_config += it_sub.second;
            if(is_text){
                str_config += "\"";
            }
            str_config.append("\n");
        }
    }

    el::Configurations conf;
    conf.parseFromText(str_config);
#else
G
groot 已提交
122
    el::Configurations conf(log_config_file);
G
groot 已提交
123 124
#endif
    el::Loggers::reconfigureAllLoggers(conf);
125 126

    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
H
Heisenberg 已提交
127
    el::Helpers::installPreRollOutCallback(RolloutHandler);
128
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
G
groot 已提交
129 130 131 132 133
    return 0;
}


}   // server
J
jinhai 已提交
134
}   // milvus
G
groot 已提交
135
}   // zilliz