LogUtil.cpp 4.5 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
starlord 已提交
12 13
#include "utils/LogUtil.h"

14
#include <libgen.h>
15
#include <cctype>
S
starlord 已提交
16
#include <string>
17

18 19 20 21 22
#include <yaml-cpp/yaml.h>

#include "config/Config.h"
#include "utils/Log.h"

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

H
Heisenberg 已提交
26 27 28 29 30 31 32
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;
33
}  // namespace
H
Heisenberg 已提交
34 35

// TODO(yzb) : change the easylogging library to get the log level from parameter rather than filename
S
starlord 已提交
36
void
S
starlord 已提交
37 38 39 40 41
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 已提交
42 43 44

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

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

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

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

94 95 96 97 98 99
void
LogConfigInFile(const std::string& path) {
    // TODO(yhz): Check if file exists
    auto node = YAML::LoadFile(path);
    YAML::Emitter out;
    out << node;
100
    LOG_SERVER_INFO_ << "\n\n"
101 102 103 104 105 106 107 108 109
                     << std::string(15, '*') << "Config in file" << std::string(15, '*') << "\n\n"
                     << out.c_str();
}

void
LogConfigInMem() {
    auto& config = Config::GetInstance();
    std::string config_str;
    config.GetConfigJsonStr(config_str, 3);
110
    LOG_SERVER_INFO_ << "\n\n"
111 112 113 114 115 116 117 118 119
                     << std::string(15, '*') << "Config in memory" << std::string(15, '*') << "\n\n"
                     << config_str;
}

void
LogCpuInfo() {
    /*CPU information*/
    std::fstream fcpu("/proc/cpuinfo", std::ios::in);
    if (!fcpu.is_open()) {
120
        LOG_SERVER_WARNING_ << "Cannot obtain CPU information. Open file /proc/cpuinfo fail: " << strerror(errno);
121 122 123 124 125 126 127 128 129
        return;
    }
    std::stringstream cpu_info_ss;
    cpu_info_ss << fcpu.rdbuf();
    fcpu.close();
    std::string cpu_info = cpu_info_ss.str();

    auto processor_pos = cpu_info.rfind("processor");
    if (std::string::npos == processor_pos) {
130
        LOG_SERVER_WARNING_ << "Cannot obtain CPU information. No sub string \'processor\'";
131 132 133 134
        return;
    }

    auto sub_str = cpu_info.substr(processor_pos);
135
    LOG_SERVER_INFO_ << "\n\n" << std::string(15, '*') << "CPU" << std::string(15, '*') << "\n\n" << sub_str;
136 137
}

S
starlord 已提交
138 139
}  // namespace server
}  // namespace milvus