1
0
Fork 0

Logging: Various logging improvements

* Uses PopWait to reduce the amount of busy waiting if there aren't many
new logs
* Opens the log file as shared on windows, letting other programs read
the logs, but not write to them while citra is running
* Flushes the logs to disk if a log >= error arrives
This commit is contained in:
James Rowe 2018-02-23 00:33:44 -07:00 committed by Daniel Lim Wee Soong
parent 9fdc89a456
commit 87bc5266ef
4 changed files with 29 additions and 11 deletions

View File

@ -803,8 +803,8 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
IOFile::IOFile() {} IOFile::IOFile() {}
IOFile::IOFile(const std::string& filename, const char openmode[]) { IOFile::IOFile(const std::string& filename, const char openmode[], int flags) {
Open(filename, openmode); Open(filename, openmode, flags);
} }
IOFile::~IOFile() { IOFile::~IOFile() {
@ -825,11 +825,16 @@ void IOFile::Swap(IOFile& other) {
std::swap(m_good, other.m_good); std::swap(m_good, other.m_good);
} }
bool IOFile::Open(const std::string& filename, const char openmode[]) { bool IOFile::Open(const std::string& filename, const char openmode[], int flags) {
Close(); Close();
#ifdef _WIN32 #ifdef _WIN32
if (flags != 0) {
m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(),
Common::UTF8ToUTF16W(openmode).c_str(), flags);
} else {
_wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(),
Common::UTF8ToUTF16W(openmode).c_str()); Common::UTF8ToUTF16W(openmode).c_str());
}
#else #else
m_file = fopen(filename.c_str(), openmode); m_file = fopen(filename.c_str(), openmode);
#endif #endif

View File

@ -156,7 +156,8 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
class IOFile : public NonCopyable { class IOFile : public NonCopyable {
public: public:
IOFile(); IOFile();
IOFile(const std::string& filename, const char openmode[]); /// Opens the file. flags is for windows shared file settings and are ignored on other oses
IOFile(const std::string& filename, const char openmode[], int flags = 0);
~IOFile(); ~IOFile();
@ -165,7 +166,7 @@ public:
void Swap(IOFile& other); void Swap(IOFile& other);
bool Open(const std::string& filename, const char openmode[]); bool Open(const std::string& filename, const char openmode[], int flags = 0);
bool Close(); bool Close();
template <typename T> template <typename T>

View File

@ -9,6 +9,11 @@
#include <future> #include <future>
#include <memory> #include <memory>
#include <thread> #include <thread>
#ifdef _WIN32
#include <share.h> // For _SH_DENYWR
#else
#define _SH_DENYWR 0
#endif
#include "common/assert.h" #include "common/assert.h"
#include "common/common_funcs.h" // snprintf compatibility define #include "common/common_funcs.h" // snprintf compatibility define
#include "common/logging/backend.h" #include "common/logging/backend.h"
@ -68,8 +73,7 @@ private:
using namespace std::chrono_literals; using namespace std::chrono_literals;
Entry entry; Entry entry;
while (running) { while (running) {
if (!message_queue.Pop(entry)) { if (!message_queue.PopWait(entry)) {
std::this_thread::sleep_for(1ms);
continue; continue;
} }
for (const auto& backend : backends) { for (const auto& backend : backends) {
@ -97,11 +101,18 @@ void ColorConsoleBackend::Write(const Entry& entry) {
PrintColoredMessage(entry); PrintColoredMessage(entry);
} }
// _SH_DENYWR allows read only access to the file for other programs.
// It is #defined to 0 on other platforms
FileBackend::FileBackend(const std::string& filename) : file(filename, "w", _SH_DENYWR) {}
void FileBackend::Write(const Entry& entry) { void FileBackend::Write(const Entry& entry) {
if (!file.IsOpen()) { if (!file.IsOpen()) {
return; return;
} }
file.WriteString(FormatLogMessage(entry) + '\n'); file.WriteString(FormatLogMessage(entry) + '\n');
if (entry.log_level >= Level::Error) {
file.Flush();
}
} }
/// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this. /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.

View File

@ -78,11 +78,12 @@ public:
}; };
/** /**
* Backend that writes to a file passed into the constructor * Backend that writes to a file passed into the constructor. If a log level is error or higher, it
* will flush immediately after writing
*/ */
class FileBackend : public Backend { class FileBackend : public Backend {
public: public:
explicit FileBackend(const std::string& filename) : file(filename, "w") {} explicit FileBackend(const std::string& filename);
const char* GetName() const override { const char* GetName() const override {
return "file"; return "file";