Fix console logging

This commit is contained in:
momo5502 2018-12-27 17:11:52 +01:00
parent 1764ff9699
commit 071dbfa5fc
5 changed files with 78 additions and 20 deletions

View File

@ -10,7 +10,7 @@
__declspec(thread) char tls_data[TLS_PAYLOAD_SIZE];
void exit_hook(const int code)
DECLSPEC_NORETURN void WINAPI exit_hook(const int code)
{
module_loader::pre_destroy();
exit(code);
@ -39,7 +39,7 @@ void verify_tls()
int main()
{
FARPROC entry_point = nullptr;
FARPROC entry_point;
try
{

View File

@ -1,6 +1,7 @@
#include <std_include.hpp>
#include "loader/module_loader.hpp"
#include "game/game.hpp"
#include "scheduler.hpp"
class console final : public module
{
@ -13,12 +14,13 @@ public:
_dup2(this->handles_[1], 1);
_dup2(this->handles_[1], 2);
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
//setvbuf(stdout, nullptr, _IONBF, 0);
//setvbuf(stderr, nullptr, _IONBF, 0);
}
void post_start() override
{
scheduler::on_frame(std::bind(&console::log_messages, this));
this->console_runner_ = std::thread(std::bind(&console::runner, this));
}
@ -46,7 +48,6 @@ public:
std::lock_guard _(this->mutex_);
this->console_initialized_ = true;
this->replay_messages();
}
private:
@ -59,26 +60,31 @@ private:
int handles_[2]{};
void replay_messages()
void log_messages()
{
if (!this->console_initialized_) return;
while (!this->message_queue_.empty())
while (this->console_initialized_ && !this->message_queue_.empty())
{
this->push_message(this->message_queue_.front());
this->message_queue_.pop();
std::queue<std::string> message_queue_copy;
{
std::lock_guard _(this->mutex_);
message_queue_copy = this->message_queue_;
this->message_queue_ = {};
}
while (!message_queue_copy.empty())
{
log_message(message_queue_copy.front());
message_queue_copy.pop();
}
}
fflush(stdout);
fflush(stderr);
}
void push_message(const std::string& message)
static void log_message(const std::string& message)
{
if (!this->console_initialized_)
{
std::lock_guard _(this->mutex_);
this->message_queue_.push(message);
return;
}
game::native::Conbuf_AppendText(message.data());
}
@ -91,7 +97,8 @@ private:
const auto len = _read(this->handles_[0], buffer, sizeof(buffer));
if (len > 0)
{
this->push_message(std::string(buffer, len));
std::lock_guard _(this->mutex_);
this->message_queue_.push(std::string(buffer, len));
}
else
{

34
src/module/scheduler.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <std_include.hpp>
#include "scheduler.hpp"
std::mutex scheduler::mutex_;
std::vector<std::function<void()>> scheduler::callbacks_;
void scheduler::on_frame(const std::function<void()>& callback)
{
std::lock_guard _(mutex_);
callbacks_.push_back(callback);
}
void scheduler::execute()
{
std::vector<std::function<void()>> callbacks_copy;
{
std::lock_guard _(mutex_);
callbacks_copy = callbacks_;
}
for (const auto& callback : callbacks_copy)
{
callback();
}
}
void scheduler::pre_destroy()
{
std::lock_guard _(mutex_);
callbacks_.clear();
}
REGISTER_MODULE(scheduler);

15
src/module/scheduler.hpp Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include "loader/module_loader.hpp"
class scheduler final : public module
{
public:
static void on_frame(const std::function<void()>& callback);
static void execute();
void pre_destroy() override;
private:
static std::mutex mutex_;
static std::vector<std::function<void()>> callbacks_;
};

View File

@ -1,5 +1,6 @@
#include <std_include.hpp>
#include "steam/steam.hpp"
#include "module/scheduler.hpp"
namespace steam
{
@ -125,6 +126,7 @@ namespace steam
void SteamAPI_RunCallbacks()
{
callbacks::run_callbacks();
scheduler::execute();
}
void SteamAPI_Shutdown()