1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 116 117 118 119 120 121 122 123 124 125
|
/***********************************************************************
* flogger.cpp - The FINAL CUT text logger *
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2020-2022 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
* the License, or (at your option) any later version. *
* *
* FINAL CUT 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <array>
#include <string>
#include "final/util/flogger.h"
namespace finalcut
{
//----------------------------------------------------------------------
// class FLogger
//----------------------------------------------------------------------
// constructors and destructor
//----------------------------------------------------------------------
FLogger::~FLogger() noexcept = default; // destructor
// private methods of FLogger
//----------------------------------------------------------------------
void FLogger::newlineReplace ( std::string& str
, const std::string& replace_str ) const
{
std::size_t pos{0};
std::size_t npos{std::string::npos};
while ( (pos = str.find('\n', pos)) != npos
&& pos + 1 < str.length() )
{
str.replace(pos, 1, replace_str);
pos += replace_str.length();
}
}
//----------------------------------------------------------------------
auto FLogger::getTimeString() const -> std::string
{
std::array<char, 100> str{};
const auto& now = std::chrono::system_clock::now();
const auto& t = std::chrono::system_clock::to_time_t(now);
// Print RFC 2822 date
struct tm time{};
localtime_r (&t, &time);
std::strftime (str.data(), str.size(), "%a, %d %b %Y %T %z", &time);
return {str.data()};
}
//----------------------------------------------------------------------
auto FLogger::getEOL() const -> std::string
{
if ( getEnding() == LineEnding::LF )
return "\n";
if ( getEnding() == LineEnding::CR )
return "\r";
if ( getEnding() == LineEnding::CRLF )
return "\r\n";
return "";
}
//----------------------------------------------------------------------
void FLogger::printLogLine (const std::string& msg)
{
const auto& log_level = [this] ()
{
switch ( getLevel() )
{
case LogLevel::Info:
return std::string("INFO");
case LogLevel::Warn:
return std::string("WARNING");
case LogLevel::Error:
return std::string("ERROR");
case LogLevel::Debug:
return std::string("DEBUG");
}
return std::string("");
}();
const auto& prefix = [this, &log_level] ()
{
if ( timestamp )
return getTimeString() + " [" + log_level + "] ";
return "[" + log_level + "] ";
}();
std::string message{msg};
const auto& eol = getEOL();
const auto replace_str = eol + prefix;
newlineReplace (message, replace_str);
std::lock_guard<std::mutex> lock_guard(output_mutex);
output << prefix << message << eol;
}
} // namespace finalcut
|