65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
// IACore-OSS; The Core Library for All IA Open Source Projects
|
|
// Copyright (C) 2025 IAS (ias@iasoft.dev)
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#include <IACore/Logger.hpp>
|
|
#include <IACore/FileOps.hpp>
|
|
|
|
namespace IACore
|
|
{
|
|
Logger::ELogLevel Logger::s_logLevel{Logger::ELogLevel::WARN};
|
|
HRTimePoint Logger::s_startTime{};
|
|
std::ofstream Logger::s_logFile{};
|
|
|
|
VOID Logger::Initialize()
|
|
{
|
|
s_startTime = HRClock::now();
|
|
}
|
|
|
|
VOID Logger::Terminate()
|
|
{
|
|
if (s_logFile.is_open())
|
|
{
|
|
s_logFile.flush();
|
|
s_logFile.close();
|
|
}
|
|
}
|
|
|
|
BOOL Logger::EnableLoggingToDisk(IN PCCHAR filePath)
|
|
{
|
|
if (s_logFile.is_open())
|
|
return true;
|
|
s_logFile.open(filePath);
|
|
return s_logFile.is_open();
|
|
}
|
|
|
|
VOID Logger::SetLogLevel(IN ELogLevel logLevel)
|
|
{
|
|
s_logLevel = logLevel;
|
|
}
|
|
|
|
VOID Logger::LogInternal(IN PCCHAR prefix, IN PCCHAR tag, IN String &&msg)
|
|
{
|
|
std::chrono::duration<double> elapsed = HRClock::now() - s_startTime;
|
|
double timestamp = elapsed.count();
|
|
const auto outLine = std::format("[{:>8.3f}]: [{}]: {}", timestamp, tag, msg);
|
|
std::cout << prefix << outLine << "\033[39m\n";
|
|
if (s_logFile)
|
|
{
|
|
s_logFile.write(outLine.data(), outLine.size());
|
|
s_logFile.put('\n');
|
|
}
|
|
}
|
|
} // namespace IACore
|