62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <functional>
|
|
#include <fstream>
|
|
#include <mutex>
|
|
|
|
#include "ddkit/utils/Logger.hpp"
|
|
|
|
namespace ddkit {
|
|
namespace utils {
|
|
namespace log {
|
|
namespace sinks {
|
|
|
|
// writes all lines to std::ostream after applying ANSI color codes
|
|
class OStreamSink : public Sink {
|
|
|
|
public:
|
|
OStreamSink(const base::IConfig& config, std::ostream& stream);
|
|
void write(Level level, const std::string& prefix, const std::string& line) override;
|
|
|
|
private:
|
|
std::mutex m_writeMutex;
|
|
bool m_doColorize;
|
|
std::ostream& m_stream;
|
|
static const Registrar<OStreamSink> s_registrarCerr;
|
|
static const Registrar<OStreamSink> s_registrarCout;
|
|
};
|
|
|
|
// writes all lines to logfile
|
|
class FileSink : public Sink {
|
|
|
|
public:
|
|
FileSink(const base::IConfig& config);
|
|
void write(Level level, const std::string& prefix, const std::string& line) override;
|
|
|
|
private:
|
|
void openFile();
|
|
void rotateLog();
|
|
void incrementFileRecursive(size_t count); // moves m_file<count> to m_file<count+1>
|
|
|
|
// log rotate settings
|
|
bool m_rotateOnStartup;
|
|
size_t m_rotateOnFileSize;
|
|
size_t m_rotateOnLineCount;
|
|
size_t m_rotateKeptFileCount;
|
|
|
|
size_t m_currentLinesInFile;
|
|
|
|
std::mutex m_writeMutex;
|
|
std::mutex m_logRotateMutex;
|
|
std::string m_filename;
|
|
std::ofstream m_fh;
|
|
static const Registrar<FileSink> s_registrar;
|
|
};
|
|
|
|
} // namespace sinks
|
|
} // namespace log
|
|
} // namespace utils
|
|
} // namespace ddkit
|