mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-13 21:40:11 +00:00
Merge branch 'compile-times' into 'master'
Improve compile time a bit See merge request OpenMW/openmw!1985
This commit is contained in:
commit
f687827f98
@ -8,6 +8,8 @@
|
|||||||
#include <components/debug/debugging.hpp>
|
#include <components/debug/debugging.hpp>
|
||||||
#include <components/settings/settings.hpp>
|
#include <components/settings/settings.hpp>
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#ifndef BT_NO_PROFILE
|
#ifndef BT_NO_PROFILE
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@ -103,9 +105,10 @@ namespace MWGui
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<char> DebugWindow::sLogCircularBuffer;
|
static std::vector<char> sLogCircularBuffer;
|
||||||
int64_t DebugWindow::sLogStartIndex = 0;
|
static std::mutex sBufferMutex;
|
||||||
int64_t DebugWindow::sLogEndIndex = 0;
|
static int64_t sLogStartIndex;
|
||||||
|
static int64_t sLogEndIndex;
|
||||||
|
|
||||||
void DebugWindow::startLogRecording()
|
void DebugWindow::startLogRecording()
|
||||||
{
|
{
|
||||||
@ -125,6 +128,7 @@ namespace MWGui
|
|||||||
default: color = "#FFFFFF";
|
default: color = "#FFFFFF";
|
||||||
}
|
}
|
||||||
bool bufferOverflow = false;
|
bool bufferOverflow = false;
|
||||||
|
std::lock_guard lock(sBufferMutex);
|
||||||
const int64_t bufSize = sLogCircularBuffer.size();
|
const int64_t bufSize = sLogCircularBuffer.size();
|
||||||
auto addChar = [&](char c)
|
auto addChar = [&](char c)
|
||||||
{
|
{
|
||||||
@ -153,6 +157,8 @@ namespace MWGui
|
|||||||
|
|
||||||
void DebugWindow::updateLogView()
|
void DebugWindow::updateLogView()
|
||||||
{
|
{
|
||||||
|
std::lock_guard lock(sBufferMutex);
|
||||||
|
|
||||||
if (!mLogView || sLogCircularBuffer.empty() || sLogStartIndex == sLogEndIndex)
|
if (!mLogView || sLogCircularBuffer.empty() || sLogStartIndex == sLogEndIndex)
|
||||||
return;
|
return;
|
||||||
if (mLogView->isTextSelection())
|
if (mLogView->isTextSelection())
|
||||||
@ -161,7 +167,6 @@ namespace MWGui
|
|||||||
std::string addition;
|
std::string addition;
|
||||||
const int64_t bufSize = sLogCircularBuffer.size();
|
const int64_t bufSize = sLogCircularBuffer.size();
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock = Log::lock();
|
|
||||||
if (sLogStartIndex < sLogEndIndex)
|
if (sLogStartIndex < sLogEndIndex)
|
||||||
addition = std::string(sLogCircularBuffer.data() + sLogStartIndex, sLogEndIndex - sLogStartIndex);
|
addition = std::string(sLogCircularBuffer.data() + sLogStartIndex, sLogEndIndex - sLogStartIndex);
|
||||||
else
|
else
|
||||||
@ -212,4 +217,4 @@ namespace MWGui
|
|||||||
else
|
else
|
||||||
updateBulletProfile();
|
updateBulletProfile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,7 @@ namespace MWGui
|
|||||||
void updateBulletProfile();
|
void updateBulletProfile();
|
||||||
|
|
||||||
MyGUI::TabControl* mTabControl;
|
MyGUI::TabControl* mTabControl;
|
||||||
|
|
||||||
MyGUI::EditBox* mLogView;
|
MyGUI::EditBox* mLogView;
|
||||||
|
|
||||||
static std::vector<char> sLogCircularBuffer;
|
|
||||||
static int64_t sLogStartIndex;
|
|
||||||
static int64_t sLogEndIndex;
|
|
||||||
|
|
||||||
MyGUI::EditBox* mBulletProfilerEdit;
|
MyGUI::EditBox* mBulletProfilerEdit;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include <components/compiler/extensions.hpp>
|
#include <components/compiler/extensions.hpp>
|
||||||
#include <components/compiler/opcodes.hpp>
|
#include <components/compiler/opcodes.hpp>
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include <SDL_messagebox.h>
|
#include <SDL_messagebox.h>
|
||||||
|
|
||||||
|
@ -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,79 +68,177 @@ 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
|
||||||
{
|
{
|
||||||
if (size <= 0)
|
public:
|
||||||
|
DebugOutputBase()
|
||||||
|
{
|
||||||
|
if (CurrentDebugLevel == NoLevel)
|
||||||
|
fillCurrentDebugLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual std::streamsize write(const char* str, std::streamsize size)
|
||||||
|
{
|
||||||
|
if (size <= 0)
|
||||||
|
return size;
|
||||||
|
std::string_view msg{ str, size_t(size) };
|
||||||
|
|
||||||
|
// Skip debug level marker
|
||||||
|
Level level = getLevelMarker(str);
|
||||||
|
if (level != NoLevel)
|
||||||
|
msg = msg.substr(1);
|
||||||
|
|
||||||
|
char prefix[32];
|
||||||
|
int prefixSize;
|
||||||
|
{
|
||||||
|
prefix[0] = '[';
|
||||||
|
const auto now = std::chrono::system_clock::now();
|
||||||
|
const auto time = std::chrono::system_clock::to_time_t(now);
|
||||||
|
prefixSize = std::strftime(prefix + 1, sizeof(prefix) - 1, "%T", std::localtime(&time)) + 1;
|
||||||
|
char levelLetter = " EWIVD*"[int(level)];
|
||||||
|
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||||
|
prefixSize += snprintf(prefix + prefixSize, sizeof(prefix) - prefixSize,
|
||||||
|
".%03u %c] ", static_cast<unsigned>(ms % 1000), levelLetter);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!msg.empty())
|
||||||
|
{
|
||||||
|
if (msg[0] == 0)
|
||||||
|
break;
|
||||||
|
size_t lineSize = 1;
|
||||||
|
while (lineSize < msg.size() && msg[lineSize - 1] != '\n')
|
||||||
|
lineSize++;
|
||||||
|
writeImpl(prefix, prefixSize, level);
|
||||||
|
writeImpl(msg.data(), lineSize, level);
|
||||||
|
if (logListener)
|
||||||
|
logListener(level, std::string_view(prefix, prefixSize), std::string_view(msg.data(), lineSize));
|
||||||
|
msg = msg.substr(lineSize);
|
||||||
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
std::string_view msg{str, size_t(size)};
|
|
||||||
|
|
||||||
// Skip debug level marker
|
|
||||||
Level level = getLevelMarker(str);
|
|
||||||
if (level != NoLevel)
|
|
||||||
msg = msg.substr(1);
|
|
||||||
|
|
||||||
char prefix[32];
|
|
||||||
int prefixSize;
|
|
||||||
{
|
|
||||||
prefix[0] = '[';
|
|
||||||
const auto now = std::chrono::system_clock::now();
|
|
||||||
const auto time = std::chrono::system_clock::to_time_t(now);
|
|
||||||
prefixSize = std::strftime(prefix + 1, sizeof(prefix) - 1, "%T", std::localtime(&time)) + 1;
|
|
||||||
char levelLetter = " EWIVD*"[int(level)];
|
|
||||||
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
|
||||||
prefixSize += snprintf(prefix + prefixSize, sizeof(prefix) - prefixSize,
|
|
||||||
".%03u %c] ", static_cast<unsigned>(ms % 1000), levelLetter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!msg.empty())
|
virtual ~DebugOutputBase() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
static Level getLevelMarker(const char* str)
|
||||||
{
|
{
|
||||||
if (msg[0] == 0)
|
if (unsigned(*str) <= unsigned(Marker))
|
||||||
break;
|
{
|
||||||
size_t lineSize = 1;
|
return Level(*str);
|
||||||
while (lineSize < msg.size() && msg[lineSize - 1] != '\n')
|
}
|
||||||
lineSize++;
|
|
||||||
writeImpl(prefix, prefixSize, level);
|
return NoLevel;
|
||||||
writeImpl(msg.data(), lineSize, level);
|
|
||||||
if (logListener)
|
|
||||||
logListener(level, std::string_view(prefix, prefixSize), std::string_view(msg.data(), lineSize));
|
|
||||||
msg = msg.substr(lineSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
static void fillCurrentDebugLevel()
|
||||||
}
|
{
|
||||||
|
const char* env = getenv("OPENMW_DEBUG_LEVEL");
|
||||||
|
if (env)
|
||||||
|
{
|
||||||
|
std::string value(env);
|
||||||
|
if (value == "ERROR")
|
||||||
|
CurrentDebugLevel = Error;
|
||||||
|
else if (value == "WARNING")
|
||||||
|
CurrentDebugLevel = Warning;
|
||||||
|
else if (value == "INFO")
|
||||||
|
CurrentDebugLevel = Info;
|
||||||
|
else if (value == "VERBOSE")
|
||||||
|
CurrentDebugLevel = Verbose;
|
||||||
|
else if (value == "DEBUG")
|
||||||
|
CurrentDebugLevel = Debug;
|
||||||
|
|
||||||
Level DebugOutputBase::getLevelMarker(const char *str)
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
{
|
{
|
||||||
if (unsigned(*str) <= unsigned(Marker))
|
public:
|
||||||
|
std::streamsize writeImpl(const char* str, std::streamsize size, Level debugLevel)
|
||||||
{
|
{
|
||||||
return Level(*str);
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NoLevel;
|
virtual ~DebugOutput() = default;
|
||||||
}
|
};
|
||||||
|
#else
|
||||||
|
|
||||||
void DebugOutputBase::fillCurrentDebugLevel()
|
class Tee : public DebugOutputBase
|
||||||
{
|
{
|
||||||
const char* env = getenv("OPENMW_DEBUG_LEVEL");
|
public:
|
||||||
if (env)
|
Tee(std::ostream& stream, std::ostream& stream2)
|
||||||
|
: out(stream), out2(stream2)
|
||||||
{
|
{
|
||||||
std::string value(env);
|
// TODO: check which stream is stderr?
|
||||||
if (value == "ERROR")
|
mUseColor = useColoredOutput();
|
||||||
CurrentDebugLevel = Error;
|
|
||||||
else if (value == "WARNING")
|
|
||||||
CurrentDebugLevel = Warning;
|
|
||||||
else if (value == "INFO")
|
|
||||||
CurrentDebugLevel = Info;
|
|
||||||
else if (value == "VERBOSE")
|
|
||||||
CurrentDebugLevel = Verbose;
|
|
||||||
else if (value == "DEBUG")
|
|
||||||
CurrentDebugLevel = Debug;
|
|
||||||
|
|
||||||
return;
|
mColors[Error] = Red;
|
||||||
|
mColors[Warning] = Yellow;
|
||||||
|
mColors[Info] = Reset;
|
||||||
|
mColors[Verbose] = DarkGray;
|
||||||
|
mColors[Debug] = DarkGray;
|
||||||
|
mColors[NoLevel] = Reset;
|
||||||
}
|
}
|
||||||
|
|
||||||
CurrentDebugLevel = Verbose;
|
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
|
||||||
|
@ -1,8 +1,36 @@
|
|||||||
#include "debuglog.hpp"
|
#include "debuglog.hpp"
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
namespace Debug
|
namespace Debug
|
||||||
{
|
{
|
||||||
Level CurrentDebugLevel = Level::NoLevel;
|
Level CurrentDebugLevel = Level::NoLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::mutex Log::sLock;
|
static std::mutex sLock;
|
||||||
|
|
||||||
|
Log::Log(Debug::Level level)
|
||||||
|
: mShouldLog(level <= Debug::CurrentDebugLevel)
|
||||||
|
{
|
||||||
|
// No need to hold the lock if there will be no logging anyway
|
||||||
|
if (!mShouldLog)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Locks a global lock while the object is alive
|
||||||
|
sLock.lock();
|
||||||
|
|
||||||
|
// If the app has no logging system enabled, log level is not specified.
|
||||||
|
// Show all messages without marker - we just use the plain cout in this case.
|
||||||
|
if (Debug::CurrentDebugLevel == Debug::NoLevel)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::cout << static_cast<unsigned char>(level);
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::~Log()
|
||||||
|
{
|
||||||
|
if (!mShouldLog)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
|
sLock.unlock();
|
||||||
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#ifndef DEBUG_LOG_H
|
#ifndef DEBUG_LOG_H
|
||||||
#define DEBUG_LOG_H
|
#define DEBUG_LOG_H
|
||||||
|
|
||||||
#include <mutex>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace Debug
|
namespace Debug
|
||||||
@ -23,27 +22,9 @@ namespace Debug
|
|||||||
|
|
||||||
class Log
|
class Log
|
||||||
{
|
{
|
||||||
static std::mutex sLock;
|
|
||||||
|
|
||||||
std::unique_lock<std::mutex> mLock;
|
|
||||||
public:
|
public:
|
||||||
explicit Log(Debug::Level level)
|
explicit Log(Debug::Level level);
|
||||||
: mShouldLog(level <= Debug::CurrentDebugLevel)
|
~Log();
|
||||||
{
|
|
||||||
// No need to hold the lock if there will be no logging anyway
|
|
||||||
if (!mShouldLog)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Locks a global lock while the object is alive
|
|
||||||
mLock = lock();
|
|
||||||
|
|
||||||
// If the app has no logging system enabled, log level is not specified.
|
|
||||||
// Show all messages without marker - we just use the plain cout in this case.
|
|
||||||
if (Debug::CurrentDebugLevel == Debug::NoLevel)
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::cout << static_cast<unsigned char>(level);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Perfect forwarding wrappers to give the chain of objects to cout
|
// Perfect forwarding wrappers to give the chain of objects to cout
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -55,14 +36,6 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Log()
|
|
||||||
{
|
|
||||||
if (mShouldLog)
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::unique_lock<std::mutex> lock() { return std::unique_lock<std::mutex>(sLock); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const bool mShouldLog;
|
const bool mShouldLog;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user