LogUtil.cpp 4.0 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "LogUtil.h"
G
groot 已提交
7
#include "server/ServerConfig.h"
8
#include "easylogging++.h"
G
groot 已提交
9

G
groot 已提交
10
#include <ctype.h>
G
groot 已提交
11

12 13 14
#include <string>
#include <libgen.h>

H
Heisenberg 已提交
15

G
groot 已提交
16
namespace zilliz {
J
jinhai 已提交
17
namespace milvus {
G
groot 已提交
18 19
namespace server {

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

    el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
H
Heisenberg 已提交
121
    el::Helpers::installPreRollOutCallback(RolloutHandler);
122
    el::Loggers::addFlag(el::LoggingFlag::DisableApplicationAbortOnFatalLog);
G
groot 已提交
123 124 125 126 127
    return 0;
}


}   // server
J
jinhai 已提交
128
}   // milvus
G
groot 已提交
129
}   // zilliz