mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-19 17:41:33 +00:00
Move implementation details of debugging.hpp to cpp, reduce includes
This commit is contained in:
parent
e6fcb8bd2d
commit
14bf9af056
2 changed files with 156 additions and 167 deletions
|
@ -7,12 +7,14 @@
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
#include <components/crashcatcher/crashcatcher.hpp>
|
#include <components/crashcatcher/crashcatcher.hpp>
|
||||||
|
#include <components/files/configurationmanager.hpp>
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <components/crashcatcher/windows_crashcatcher.hpp>
|
#include <components/crashcatcher/windows_crashcatcher.hpp>
|
||||||
#include <components/windows.hpp>
|
#include <components/windows.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <SDL_messagebox.h>
|
||||||
|
|
||||||
namespace Debug
|
namespace Debug
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -66,7 +68,16 @@ namespace Debug
|
||||||
static LogListener logListener;
|
static LogListener logListener;
|
||||||
void setLogListener(LogListener listener) { logListener = std::move(listener); }
|
void setLogListener(LogListener listener) { logListener = std::move(listener); }
|
||||||
|
|
||||||
std::streamsize DebugOutputBase::write(const char *str, std::streamsize size)
|
class DebugOutputBase : public boost::iostreams::sink
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DebugOutputBase()
|
||||||
|
{
|
||||||
|
if (CurrentDebugLevel == NoLevel)
|
||||||
|
fillCurrentDebugLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual std::streamsize write(const char* str, std::streamsize size)
|
||||||
{
|
{
|
||||||
if (size <= 0)
|
if (size <= 0)
|
||||||
return size;
|
return size;
|
||||||
|
@ -107,7 +118,10 @@ namespace Debug
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
Level DebugOutputBase::getLevelMarker(const char *str)
|
virtual ~DebugOutputBase() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static Level getLevelMarker(const char* str)
|
||||||
{
|
{
|
||||||
if (unsigned(*str) <= unsigned(Marker))
|
if (unsigned(*str) <= unsigned(Marker))
|
||||||
{
|
{
|
||||||
|
@ -117,7 +131,7 @@ namespace Debug
|
||||||
return NoLevel;
|
return NoLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugOutputBase::fillCurrentDebugLevel()
|
static void fillCurrentDebugLevel()
|
||||||
{
|
{
|
||||||
const char* env = getenv("OPENMW_DEBUG_LEVEL");
|
const char* env = getenv("OPENMW_DEBUG_LEVEL");
|
||||||
if (env)
|
if (env)
|
||||||
|
@ -139,6 +153,92 @@ namespace Debug
|
||||||
|
|
||||||
CurrentDebugLevel = Verbose;
|
CurrentDebugLevel = Verbose;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel)
|
||||||
|
{
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined _WIN32 && defined _DEBUG
|
||||||
|
class DebugOutput : public DebugOutputBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel)
|
||||||
|
{
|
||||||
|
// Make a copy for null termination
|
||||||
|
std::string tmp(str, static_cast<unsigned int>(size));
|
||||||
|
// Write string to Visual Studio Debug output
|
||||||
|
OutputDebugString(tmp.c_str());
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~DebugOutput() = default;
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
|
||||||
|
class Tee : public DebugOutputBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Tee(std::ostream& stream, std::ostream& stream2)
|
||||||
|
: out(stream), out2(stream2)
|
||||||
|
{
|
||||||
|
// TODO: check which stream is stderr?
|
||||||
|
mUseColor = useColoredOutput();
|
||||||
|
|
||||||
|
mColors[Error] = Red;
|
||||||
|
mColors[Warning] = Yellow;
|
||||||
|
mColors[Info] = Reset;
|
||||||
|
mColors[Verbose] = DarkGray;
|
||||||
|
mColors[Debug] = DarkGray;
|
||||||
|
mColors[NoLevel] = Reset;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel) override
|
||||||
|
{
|
||||||
|
out.write(str, size);
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
if (mUseColor)
|
||||||
|
{
|
||||||
|
out2 << "\033[0;" << mColors[debugLevel] << "m";
|
||||||
|
out2.write(str, size);
|
||||||
|
out2 << "\033[0;" << Reset << "m";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out2.write(str, size);
|
||||||
|
}
|
||||||
|
out2.flush();
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~Tee() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
static bool useColoredOutput()
|
||||||
|
{
|
||||||
|
// Note: cmd.exe in Win10 should support ANSI colors, but in its own way.
|
||||||
|
#if defined(_WIN32)
|
||||||
|
return 0;
|
||||||
|
#else
|
||||||
|
char* term = getenv("TERM");
|
||||||
|
bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr));
|
||||||
|
|
||||||
|
return useColor;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& out;
|
||||||
|
std::ostream& out2;
|
||||||
|
bool mUseColor;
|
||||||
|
|
||||||
|
std::map<Level, int> mColors;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::unique_ptr<std::ostream> rawStdout = nullptr;
|
static std::unique_ptr<std::ostream> rawStdout = nullptr;
|
||||||
|
|
|
@ -4,17 +4,10 @@
|
||||||
#include <boost/filesystem/fstream.hpp>
|
#include <boost/filesystem/fstream.hpp>
|
||||||
#include <boost/iostreams/stream.hpp>
|
#include <boost/iostreams/stream.hpp>
|
||||||
|
|
||||||
#include <components/files/configurationmanager.hpp>
|
|
||||||
#include <components/misc/guarded.hpp>
|
#include <components/misc/guarded.hpp>
|
||||||
|
|
||||||
#include <SDL_messagebox.h>
|
|
||||||
|
|
||||||
#include "debuglog.hpp"
|
#include "debuglog.hpp"
|
||||||
|
|
||||||
#if defined _WIN32 && defined _DEBUG
|
|
||||||
#include <components/windows.hpp>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Debug
|
namespace Debug
|
||||||
{
|
{
|
||||||
// ANSI colors for terminal
|
// ANSI colors for terminal
|
||||||
|
@ -26,116 +19,12 @@ namespace Debug
|
||||||
Yellow = 93
|
Yellow = 93
|
||||||
};
|
};
|
||||||
|
|
||||||
class DebugOutputBase : public boost::iostreams::sink
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DebugOutputBase()
|
|
||||||
{
|
|
||||||
if (CurrentDebugLevel == NoLevel)
|
|
||||||
fillCurrentDebugLevel();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual std::streamsize write(const char *str, std::streamsize size);
|
|
||||||
|
|
||||||
virtual ~DebugOutputBase() = default;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
static Level getLevelMarker(const char *str);
|
|
||||||
|
|
||||||
static void fillCurrentDebugLevel();
|
|
||||||
|
|
||||||
virtual std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
|
|
||||||
{
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
bool attachParentConsole();
|
bool attachParentConsole();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined _WIN32 && defined _DEBUG
|
|
||||||
class DebugOutput : public DebugOutputBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel)
|
|
||||||
{
|
|
||||||
// Make a copy for null termination
|
|
||||||
std::string tmp (str, static_cast<unsigned int>(size));
|
|
||||||
// Write string to Visual Studio Debug output
|
|
||||||
OutputDebugString (tmp.c_str ());
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~DebugOutput() {}
|
|
||||||
};
|
|
||||||
#else
|
|
||||||
|
|
||||||
class Tee : public DebugOutputBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Tee(std::ostream &stream, std::ostream &stream2)
|
|
||||||
: out(stream), out2(stream2)
|
|
||||||
{
|
|
||||||
// TODO: check which stream is stderr?
|
|
||||||
mUseColor = useColoredOutput();
|
|
||||||
|
|
||||||
mColors[Error] = Red;
|
|
||||||
mColors[Warning] = Yellow;
|
|
||||||
mColors[Info] = Reset;
|
|
||||||
mColors[Verbose] = DarkGray;
|
|
||||||
mColors[Debug] = DarkGray;
|
|
||||||
mColors[NoLevel] = Reset;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::streamsize writeImpl(const char *str, std::streamsize size, Level debugLevel) override
|
|
||||||
{
|
|
||||||
out.write (str, size);
|
|
||||||
out.flush();
|
|
||||||
|
|
||||||
if(mUseColor)
|
|
||||||
{
|
|
||||||
out2 << "\033[0;" << mColors[debugLevel] << "m";
|
|
||||||
out2.write (str, size);
|
|
||||||
out2 << "\033[0;" << Reset << "m";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
out2.write(str, size);
|
|
||||||
}
|
|
||||||
out2.flush();
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~Tee() {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
static bool useColoredOutput()
|
|
||||||
{
|
|
||||||
// Note: cmd.exe in Win10 should support ANSI colors, but in its own way.
|
|
||||||
#if defined(_WIN32)
|
|
||||||
return 0;
|
|
||||||
#else
|
|
||||||
char *term = getenv("TERM");
|
|
||||||
bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr));
|
|
||||||
|
|
||||||
return useColor;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream &out;
|
|
||||||
std::ostream &out2;
|
|
||||||
bool mUseColor;
|
|
||||||
|
|
||||||
std::map<Level, int> mColors;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
using LogListener = std::function<void(Debug::Level, std::string_view prefix, std::string_view msg)>;
|
using LogListener = std::function<void(Debug::Level, std::string_view prefix, std::string_view msg)>;
|
||||||
void setLogListener(LogListener);
|
void setLogListener(LogListener);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can be used to print messages without timestamps
|
// Can be used to print messages without timestamps
|
||||||
|
|
Loading…
Reference in a new issue