/*
 * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved.
 * 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
 *
 *     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.
 */

#ifndef LOG_H
#define LOG_H

#include <mutex>
#include <sstream>
#include <string>
#include <vector>

namespace MatrixAILog {
// log level
enum LogLevels {
    LOG_LEVEL_DEBUG = 0,
    LOG_LEVEL_INFO = 1,
    LOG_LEVEL_WARN = 2,
    LOG_LEVEL_ERROR = 3,
    LOG_LEVEL_FATAL = 4,
    LOG_LEVEL_NONE
};

class Log {
public:
    Log(std::string file, std::string function, int line, uint32_t level);
    ~Log();
    std::ostringstream &Stream();
    // log switch, turn on and off both screen and file log of special level.
    static void LogDebugOn();
    static void LogInfoOn();
    static void LogWarnOn();
    static void LogErrorOn();
    static void LogFatalOn();
    static void LogAllOn();
    static void LogAllOff();
    static void SetLogLevel(const std::string &log_level);
    static void SetLogFile(const std::string &log_file, const std::string &logfile_bakpath);
    static void SetLogFile(const std::string &log_file);

private:
    std::ostringstream ss_;
    uint32_t myLevel_;
    std::string date_;
    std::string file_;
    std::string function_;
    int line_;

    static uint32_t logLevel;
    static std::vector<std::string> levelString;
    static std::mutex mutex;
    static std::string logFile;
    static std::string logFileBak;
    static std::string logFileBakPath;
};
} // namespace MatrixAILog

#define LogDebug MatrixAILog::Log(__FILE__, __FUNCTION__, __LINE__, MatrixAILog::LOG_LEVEL_DEBUG).Stream()
//#define LogInfo std::cout
#define LogInfo MatrixAILog::Log(__FILE__, __FUNCTION__, __LINE__, MatrixAILog::LOG_LEVEL_INFO).Stream()
#define LogWarn MatrixAILog::Log(__FILE__, __FUNCTION__, __LINE__, MatrixAILog::LOG_LEVEL_WARN).Stream()
#define LogError MatrixAILog::Log(__FILE__, __FUNCTION__, __LINE__, MatrixAILog::LOG_LEVEL_ERROR).Stream()
#define LogFatal MatrixAILog::Log(__FILE__, __FUNCTION__, __LINE__, MatrixAILog::LOG_LEVEL_FATAL).Stream()
#define LOG(security) MatrixAILog::LOG_##security.Stream()

#endif