finalcut/src/include/final/flogger.h

173 lines
5.5 KiB
C
Raw Normal View History

2020-05-13 23:47:14 +02:00
/***********************************************************************
* flogger.h - The FINAL CUT text logger *
* *
* This file is part of the FINAL CUT widget toolkit *
2020-05-13 23:47:14 +02:00
* *
* Copyright 2020 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 *
2020-05-13 23:47:14 +02:00
* 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 *
2020-05-13 23:47:14 +02:00
* 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/>. *
***********************************************************************/
2020-05-24 02:15:43 +02:00
/* Inheritance diagram
*
2020-05-13 23:47:14 +02:00
*
2020-05-24 02:15:43 +02:00
*
* std::stringbuf
*
*
*
*
* FLog
*
*
*
*
* FLogger
*
2020-05-13 23:47:14 +02:00
*/
#ifndef FLOGGER_H
#define FLOGGER_H
#if !defined (USE_FINAL_H) && !defined (COMPILE_FINAL_CUT)
#error "Only <final/final.h> can be included directly."
#endif
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
#include <iomanip>
#include "final/flog.h"
namespace finalcut
{
//----------------------------------------------------------------------
// class FLogger
//----------------------------------------------------------------------
class FLogger : public FLog
{
public:
// Constructor
FLogger() = default;
2020-05-13 23:47:14 +02:00
// Destructor
~FLogger() noexcept override;
2020-05-13 23:47:14 +02:00
// Methods
FString getClassName() const override;
2020-05-13 23:47:14 +02:00
void info (const std::string&) override;
void warn (const std::string&) override;
void error (const std::string&) override;
void debug (const std::string&) override;
2020-06-07 18:21:59 +02:00
void flush() override;
2020-05-13 23:47:14 +02:00
void setOutputStream (const std::ostream&) override;
void setLineEnding (LineEnding) override;
void enableTimestamp() override;
void disableTimestamp() override;
private:
// Methods
2020-07-12 15:25:21 +02:00
void newlineReplace (std::string&, const std::string&) const;
std::string getTimeString() const;
2020-10-11 09:14:52 +02:00
std::string getEOL();
2020-05-13 23:47:14 +02:00
void printLogLine (const std::string&);
// Data member
bool timestamp{false};
std::ostream output{std::cerr.rdbuf()};
};
// FLogger inline functions
//----------------------------------------------------------------------
inline FString FLogger::getClassName() const
2020-05-13 23:47:14 +02:00
{ return "FLogger"; }
//----------------------------------------------------------------------
inline void FLogger::info (const std::string& msg)
{
2020-10-11 09:14:52 +02:00
std::lock_guard<std::mutex> lock_guard(getMutex());
2020-12-06 02:11:54 +01:00
setLevel() = LogLevel::Info;
2020-05-13 23:47:14 +02:00
printLogLine (msg);
}
//----------------------------------------------------------------------
inline void FLogger::warn (const std::string& msg)
{
2020-10-11 09:14:52 +02:00
std::lock_guard<std::mutex> lock_guard(getMutex());
2020-12-06 02:11:54 +01:00
setLevel() = LogLevel::Warn;
2020-05-13 23:47:14 +02:00
printLogLine (msg);
}
//----------------------------------------------------------------------
inline void FLogger::error (const std::string& msg)
{
2020-10-11 09:14:52 +02:00
std::lock_guard<std::mutex> lock_guard(getMutex());
2020-12-06 02:11:54 +01:00
setLevel() = LogLevel::Error;
2020-05-13 23:47:14 +02:00
printLogLine (msg);
}
//----------------------------------------------------------------------
inline void FLogger::debug (const std::string& msg)
{
2020-10-11 09:14:52 +02:00
std::lock_guard<std::mutex> lock_guard(getMutex());
2020-12-06 02:11:54 +01:00
setLevel() = LogLevel::Debug;
2020-05-13 23:47:14 +02:00
printLogLine (msg);
}
2020-06-07 18:21:59 +02:00
//----------------------------------------------------------------------
inline void FLogger::flush()
2020-10-11 09:14:52 +02:00
{
std::lock_guard<std::mutex> lock_guard(getMutex());
output.flush();
}
2020-06-07 18:21:59 +02:00
2020-05-13 23:47:14 +02:00
//----------------------------------------------------------------------
inline void FLogger::setOutputStream (const std::ostream& os)
2020-10-11 09:14:52 +02:00
{
std::lock_guard<std::mutex> lock_guard(getMutex());
output.rdbuf(os.rdbuf());
}
2020-05-13 23:47:14 +02:00
//----------------------------------------------------------------------
inline void FLogger::setLineEnding (LineEnding eol)
2020-10-11 09:14:52 +02:00
{
std::lock_guard<std::mutex> lock_guard(getMutex());
setEnding() = eol;
}
2020-05-13 23:47:14 +02:00
//----------------------------------------------------------------------
inline void FLogger::enableTimestamp()
2020-10-11 09:14:52 +02:00
{
std::lock_guard<std::mutex> lock_guard(getMutex());
timestamp = true;
}
2020-05-13 23:47:14 +02:00
//----------------------------------------------------------------------
inline void FLogger::disableTimestamp()
2020-10-11 09:14:52 +02:00
{
std::lock_guard<std::mutex> lock_guard(getMutex());
timestamp = false;
}
2020-05-13 23:47:14 +02:00
} // namespace finalcut
#endif // FLOGGER_H