mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-28 21:07:59 +03:00
replace use of boost::iostreams::sink and tee
This commit is contained in:
parent
05555947c3
commit
31e90fb478
1 changed files with 61 additions and 17 deletions
|
@ -5,8 +5,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <unistd.h>
|
||||||
#include <boost/iostreams/stream.hpp>
|
|
||||||
|
|
||||||
#include <components/crashcatcher/crashcatcher.hpp>
|
#include <components/crashcatcher/crashcatcher.hpp>
|
||||||
#include <components/files/conversion.hpp>
|
#include <components/files/conversion.hpp>
|
||||||
|
@ -77,7 +76,7 @@ namespace Debug
|
||||||
logListener = std::move(listener);
|
logListener = std::move(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
class DebugOutputBase : public boost::iostreams::sink
|
class DebugOutputBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DebugOutputBase()
|
DebugOutputBase()
|
||||||
|
@ -175,6 +174,58 @@ namespace Debug
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class DebugOutputBuffer : public std::streambuf
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DebugOutputBuffer(std::unique_ptr<DebugOutputBase> sink)
|
||||||
|
: m_sink(std::move(sink))
|
||||||
|
{}
|
||||||
|
|
||||||
|
std::streamsize xsputn(const char* s, std::streamsize n) override
|
||||||
|
{
|
||||||
|
return m_sink->write(s, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int overflow(int c = EOF) override
|
||||||
|
{
|
||||||
|
if (c != EOF)
|
||||||
|
{
|
||||||
|
char z = c;
|
||||||
|
if (m_sink->write(&z, 1) != 1)
|
||||||
|
return EOF;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sync() override
|
||||||
|
{
|
||||||
|
// This is up to you to decide when to sync, or if you need to.
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<DebugOutputBase> m_sink;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DebugOutputStream : public std::ostream
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DebugOutputStream(std::unique_ptr<DebugOutputBase> sink)
|
||||||
|
: std::ostream(new DebugOutputBuffer(std::move(sink)))
|
||||||
|
{}
|
||||||
|
|
||||||
|
void open(std::unique_ptr<DebugOutputBase> new_sink)
|
||||||
|
{
|
||||||
|
delete rdbuf();
|
||||||
|
rdbuf(new DebugOutputBuffer(std::move(new_sink)));
|
||||||
|
}
|
||||||
|
|
||||||
|
~DebugOutputStream()
|
||||||
|
{
|
||||||
|
delete rdbuf();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#if defined _WIN32 && defined _DEBUG
|
#if defined _WIN32 && defined _DEBUG
|
||||||
class DebugOutput : public DebugOutputBase
|
class DebugOutput : public DebugOutputBase
|
||||||
{
|
{
|
||||||
|
@ -261,13 +312,6 @@ static std::unique_ptr<std::ostream> rawStderr = nullptr;
|
||||||
static std::unique_ptr<std::mutex> rawStderrMutex = nullptr;
|
static std::unique_ptr<std::mutex> rawStderrMutex = nullptr;
|
||||||
static std::ofstream logfile;
|
static std::ofstream logfile;
|
||||||
|
|
||||||
#if defined(_WIN32) && defined(_DEBUG)
|
|
||||||
static boost::iostreams::stream_buffer<Debug::DebugOutput> sb;
|
|
||||||
#else
|
|
||||||
static boost::iostreams::stream_buffer<Debug::Tee> coutsb;
|
|
||||||
static boost::iostreams::stream_buffer<Debug::Tee> cerrsb;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
std::ostream& getRawStdout()
|
std::ostream& getRawStdout()
|
||||||
{
|
{
|
||||||
return rawStdout ? *rawStdout : std::cout;
|
return rawStdout ? *rawStdout : std::cout;
|
||||||
|
@ -288,18 +332,18 @@ void setupLogging(const std::filesystem::path& logDir, std::string_view appName,
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) && defined(_DEBUG)
|
#if defined(_WIN32) && defined(_DEBUG)
|
||||||
// Redirect cout and cerr to VS debug output when running in debug mode
|
// Redirect cout and cerr to VS debug output when running in debug mode
|
||||||
sb.open(Debug::DebugOutput());
|
static Debug::DebugOutputStream sb(std::make_unique<Debug::DebugOutput>());
|
||||||
std::cout.rdbuf(&sb);
|
std::cout.rdbuf(sb.rdbuf());
|
||||||
std::cerr.rdbuf(&sb);
|
std::cerr.rdbuf(sb.rdbuf());
|
||||||
#else
|
#else
|
||||||
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
|
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
|
||||||
logfile.open(logDir / logName, mode);
|
logfile.open(logDir / logName, mode);
|
||||||
|
|
||||||
coutsb.open(Debug::Tee(logfile, *rawStdout));
|
static Debug::DebugOutputStream coutsb(std::make_unique<Debug::Tee>(logfile, *rawStdout));
|
||||||
cerrsb.open(Debug::Tee(logfile, *rawStderr));
|
static Debug::DebugOutputStream cerrsb(std::make_unique<Debug::Tee>(logfile, *rawStderr));
|
||||||
|
|
||||||
std::cout.rdbuf(&coutsb);
|
std::cout.rdbuf(coutsb.rdbuf());
|
||||||
std::cerr.rdbuf(&cerrsb);
|
std::cerr.rdbuf(cerrsb.rdbuf());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue