Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Log.cpp 2.09 KiB
/********************************************************************************
 * Copyright (c) 2023 CEA-List
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 ********************************************************************************/

#include "aidge/utils/Log.hpp"
#include "aidge/utils/ErrorHandling.hpp"

#include <fmt/color.h>
#include <fmt/chrono.h>

Aidge::Log::Level Aidge::Log::mConsoleLevel = Info;
Aidge::Log::Level Aidge::Log::mFileLevel = Debug;
std::string Aidge::Log::mFileName = "aidge.log";
std::unique_ptr<FILE, decltype(&std::fclose)> Aidge::Log::mFile {nullptr, nullptr};

void Aidge::Log::log(Level level, const std::string& msg) {
    if (level >= mConsoleLevel) {
        // Apply log level style only for console.
        // Styles that were already applied to msg with fmt are kept also in 
        // the log file.
        const auto modifier
            = (level == Debug) ? fmt::fg(fmt::color::gray)
            : (level == Notice) ? fmt::fg(fmt::color::light_yellow)
            : (level == Warn) ? fmt::fg(fmt::color::orange)
            : (level == Error) ? fmt::fg(fmt::color::red)
            : (level == Fatal) ? fmt::bg(fmt::color::red)
            : fmt::text_style();

        fmt::println("{}", fmt::styled(msg, modifier));
    }

    if (level >= mFileLevel && !mFileName.empty()) {
        if (!mFile) {
            initFile(mFileName);
        }

        fmt::println(mFile.get(), msg);
    }
}

void Aidge::Log::initFile(const std::string& fileName) {
    mFile = std::unique_ptr<FILE, decltype(&std::fclose)>(std::fopen(fileName.c_str(), "a"), &std::fclose);

    if (!mFile) {
        mFileName.clear(); // prevents AIDGE_THROW_OR_ABORT() to try to log into file
        AIDGE_THROW_OR_ABORT(std::runtime_error,
            "Could not create log file: {}", fileName);
    }

    const std::time_t t = std::time(nullptr);
    fmt::println(mFile.get(), "###### {:%Y-%m-%d %H:%M:%S} ######", fmt::localtime(t));
}