update Logger
This commit is contained in:
parent
0eceb235ca
commit
d76a4de248
|
|
@ -19,15 +19,11 @@
|
|||
// THE SOFTWARE.
|
||||
|
||||
#include "AsyncTask.h"
|
||||
#include "../common/closure.hpp"
|
||||
#include "../platform/Application.h"
|
||||
#include <thread>
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
//
|
||||
// AsyncTask
|
||||
//
|
||||
|
||||
AsyncTask::AsyncTask()
|
||||
{
|
||||
|
|
@ -75,39 +71,18 @@ namespace kiwano
|
|||
func_mutex_.unlock();
|
||||
}
|
||||
|
||||
if (thread_cb_)
|
||||
{
|
||||
AsyncTaskThread::Instance().PerformTaskCallback(MakeClosure(this, &AsyncTask::Complete));
|
||||
}
|
||||
Application::PreformInMainThread(MakeClosure(this, &AsyncTask::Complete));
|
||||
}
|
||||
|
||||
void AsyncTask::Complete()
|
||||
{
|
||||
thread_cb_();
|
||||
if (thread_cb_)
|
||||
{
|
||||
thread_cb_();
|
||||
}
|
||||
|
||||
// Release this object
|
||||
Release();
|
||||
}
|
||||
|
||||
//
|
||||
// AsyncTaskThread
|
||||
//
|
||||
|
||||
void AsyncTaskThread::SetupComponent(Application* app)
|
||||
{
|
||||
app_ = app;
|
||||
}
|
||||
|
||||
void AsyncTaskThread::DestroyComponent()
|
||||
{
|
||||
}
|
||||
|
||||
void AsyncTaskThread::PerformTaskCallback(Closure<void()> func)
|
||||
{
|
||||
if (app_)
|
||||
{
|
||||
app_->PreformFunctionInMainThread(func);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,9 +20,7 @@
|
|||
|
||||
#pragma once
|
||||
#include "Object.h"
|
||||
#include "Component.h"
|
||||
#include "../common/Closure.hpp"
|
||||
#include "../common/Singleton.hpp"
|
||||
#include "../common/closure.hpp"
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
|
||||
|
|
@ -63,21 +61,4 @@ namespace kiwano
|
|||
AsyncTaskCallback thread_cb_;
|
||||
std::mutex func_mutex_;
|
||||
};
|
||||
|
||||
class AsyncTaskThread
|
||||
: public Singleton<AsyncTaskThread>
|
||||
, public Component
|
||||
{
|
||||
KGE_DECLARE_SINGLETON(AsyncTaskThread);
|
||||
|
||||
public:
|
||||
virtual void SetupComponent(Application*);
|
||||
|
||||
virtual void DestroyComponent();
|
||||
|
||||
void PerformTaskCallback(Closure<void()> func);
|
||||
|
||||
private:
|
||||
Application* app_;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,105 @@
|
|||
|
||||
#include "logs.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
namespace
|
||||
{
|
||||
std::streambuf* cin_buffer, * cout_buffer, * cerr_buffer;
|
||||
std::fstream console_input, console_output, console_error;
|
||||
|
||||
std::wstreambuf* wcin_buffer, * wcout_buffer, * wcerr_buffer;
|
||||
std::wfstream wconsole_input, wconsole_output, wconsole_error;
|
||||
|
||||
void RedirectStdIO()
|
||||
{
|
||||
cin_buffer = std::cin.rdbuf();
|
||||
cout_buffer = std::cout.rdbuf();
|
||||
cerr_buffer = std::cerr.rdbuf();
|
||||
wcin_buffer = std::wcin.rdbuf();
|
||||
wcout_buffer = std::wcout.rdbuf();
|
||||
wcerr_buffer = std::wcerr.rdbuf();
|
||||
|
||||
console_input.open("CONIN$", std::ios::in);
|
||||
console_output.open("CONOUT$", std::ios::out);
|
||||
console_error.open("CONOUT$", std::ios::out);
|
||||
wconsole_input.open("CONIN$", std::ios::in);
|
||||
wconsole_output.open("CONOUT$", std::ios::out);
|
||||
wconsole_error.open("CONOUT$", std::ios::out);
|
||||
|
||||
FILE* dummy;
|
||||
freopen_s(&dummy, "CONOUT$", "w+t", stdout);
|
||||
freopen_s(&dummy, "CONIN$", "r+t", stdin);
|
||||
freopen_s(&dummy, "CONOUT$", "w+t", stderr);
|
||||
(void)dummy;
|
||||
|
||||
std::cin.rdbuf(console_input.rdbuf());
|
||||
std::cout.rdbuf(console_output.rdbuf());
|
||||
std::cerr.rdbuf(console_error.rdbuf());
|
||||
std::wcin.rdbuf(wconsole_input.rdbuf());
|
||||
std::wcout.rdbuf(wconsole_output.rdbuf());
|
||||
std::wcerr.rdbuf(wconsole_error.rdbuf());
|
||||
}
|
||||
|
||||
void ResetStdIO()
|
||||
{
|
||||
console_input.close();
|
||||
console_output.close();
|
||||
console_error.close();
|
||||
wconsole_input.close();
|
||||
wconsole_output.close();
|
||||
wconsole_error.close();
|
||||
|
||||
std::cin.rdbuf(cin_buffer);
|
||||
std::cout.rdbuf(cout_buffer);
|
||||
std::cerr.rdbuf(cerr_buffer);
|
||||
std::wcin.rdbuf(wcin_buffer);
|
||||
std::wcout.rdbuf(wcout_buffer);
|
||||
std::wcerr.rdbuf(wcerr_buffer);
|
||||
|
||||
fclose(stdout);
|
||||
fclose(stdin);
|
||||
fclose(stderr);
|
||||
|
||||
cin_buffer = nullptr;
|
||||
cout_buffer = nullptr;
|
||||
cerr_buffer = nullptr;
|
||||
wcin_buffer = nullptr;
|
||||
wcout_buffer = nullptr;
|
||||
wcerr_buffer = nullptr;
|
||||
}
|
||||
|
||||
HWND allocated_console = nullptr;
|
||||
|
||||
HWND AllocateConsole()
|
||||
{
|
||||
if (::AllocConsole())
|
||||
{
|
||||
allocated_console = ::GetConsoleWindow();
|
||||
|
||||
if (allocated_console)
|
||||
{
|
||||
RedirectStdIO();
|
||||
}
|
||||
}
|
||||
return allocated_console;
|
||||
}
|
||||
|
||||
void FreeAllocatedConsole()
|
||||
{
|
||||
if (allocated_console)
|
||||
{
|
||||
ResetStdIO();
|
||||
::FreeConsole();
|
||||
allocated_console = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
HWND GetAllocatedConsole()
|
||||
{
|
||||
return allocated_console;
|
||||
}
|
||||
}
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
|
|
@ -78,6 +177,11 @@ namespace kiwano
|
|||
ResetOutputStream();
|
||||
}
|
||||
|
||||
Logger::~Logger()
|
||||
{
|
||||
FreeAllocatedConsole();
|
||||
}
|
||||
|
||||
void Logger::ResetOutputStream()
|
||||
{
|
||||
bool has_console = ::GetConsoleWindow() != nullptr;
|
||||
|
|
@ -200,4 +304,46 @@ namespace kiwano
|
|||
return out;
|
||||
}
|
||||
|
||||
void Logger::ShowConsole(bool show)
|
||||
{
|
||||
HWND current_console = ::GetConsoleWindow();
|
||||
if (show)
|
||||
{
|
||||
if (current_console)
|
||||
{
|
||||
::ShowWindow(current_console, SW_SHOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
HWND console = ::AllocateConsole();
|
||||
if (!console)
|
||||
{
|
||||
KGE_WARNING_LOG(L"AllocConsole failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
// disable the close button of console
|
||||
HMENU hmenu = ::GetSystemMenu(console, FALSE);
|
||||
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (current_console)
|
||||
{
|
||||
if (current_console == GetAllocatedConsole())
|
||||
{
|
||||
FreeAllocatedConsole();
|
||||
}
|
||||
else
|
||||
{
|
||||
::ShowWindow(current_console, SW_HIDE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ResetOutputStream();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,8 +49,13 @@ namespace kiwano
|
|||
KGE_DECLARE_SINGLETON(Logger);
|
||||
|
||||
public:
|
||||
// 显示或关闭控制台
|
||||
void ShowConsole(bool show);
|
||||
|
||||
// 启用 Logger
|
||||
void Enable();
|
||||
|
||||
// 禁用 Logger
|
||||
void Disable();
|
||||
|
||||
void Printf(const wchar_t* format, ...);
|
||||
|
|
@ -94,6 +99,8 @@ namespace kiwano
|
|||
private:
|
||||
Logger();
|
||||
|
||||
~Logger();
|
||||
|
||||
void Outputf(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, va_list args) const;
|
||||
|
||||
std::wstring MakeOutputStringf(const wchar_t* prompt, const wchar_t* format, va_list args) const;
|
||||
|
|
|
|||
|
|
@ -244,16 +244,13 @@ namespace kiwano
|
|||
namespace network
|
||||
{
|
||||
HttpClient::HttpClient()
|
||||
: app_(nullptr)
|
||||
, timeout_for_connect_(30000 /* 30 seconds */)
|
||||
: timeout_for_connect_(30000 /* 30 seconds */)
|
||||
, timeout_for_read_(60000 /* 60 seconds */)
|
||||
{
|
||||
}
|
||||
|
||||
void HttpClient::SetupComponent(Application * app)
|
||||
{
|
||||
app_ = app;
|
||||
|
||||
::curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
std::thread thread(MakeClosure(this, &HttpClient::NetworkThread));
|
||||
|
|
@ -299,10 +296,7 @@ namespace kiwano
|
|||
response_queue_.push(response);
|
||||
response_mutex_.unlock();
|
||||
|
||||
if (app_)
|
||||
{
|
||||
app_->PreformFunctionInMainThread(MakeClosure(this, &HttpClient::DispatchResponseCallback));
|
||||
}
|
||||
Application::PreformInMainThread(MakeClosure(this, &HttpClient::DispatchResponseCallback));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -314,7 +308,6 @@ namespace kiwano
|
|||
std::string response_header;
|
||||
std::string response_data;
|
||||
|
||||
|
||||
std::string url = convert_to_utf8(request->GetUrl());
|
||||
std::string data = convert_to_utf8(request->GetData());
|
||||
|
||||
|
|
|
|||
|
|
@ -85,8 +85,6 @@ namespace kiwano
|
|||
void DispatchResponseCallback();
|
||||
|
||||
private:
|
||||
Application* app_;
|
||||
|
||||
Duration timeout_for_connect_;
|
||||
Duration timeout_for_read_;
|
||||
|
||||
|
|
|
|||
|
|
@ -23,113 +23,24 @@
|
|||
#include "../base/logs.h"
|
||||
#include "../base/input.h"
|
||||
#include "../base/Event.hpp"
|
||||
#include "../base/AsyncTask.h"
|
||||
#include "../renderer/render.h"
|
||||
#include "../2d/Scene.h"
|
||||
#include "../2d/DebugNode.h"
|
||||
#include "../2d/Transition.h"
|
||||
#include <windowsx.h>
|
||||
#include <imm.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <windowsx.h> // GET_X_LPARAM, GET_Y_LPARAM
|
||||
#include <imm.h> // ImmAssociateContext
|
||||
#include <mutex> // std::mutex
|
||||
|
||||
#pragma comment(lib, "imm32.lib")
|
||||
|
||||
namespace
|
||||
namespace kiwano
|
||||
{
|
||||
std::streambuf *cin_buffer, *cout_buffer, *cerr_buffer;
|
||||
std::fstream console_input, console_output, console_error;
|
||||
using FunctionToPerform = Closure<void()>;
|
||||
|
||||
std::wstreambuf *wcin_buffer, *wcout_buffer, *wcerr_buffer;
|
||||
std::wfstream wconsole_input, wconsole_output, wconsole_error;
|
||||
|
||||
void RedirectStdIO()
|
||||
namespace
|
||||
{
|
||||
cin_buffer = std::cin.rdbuf();
|
||||
cout_buffer = std::cout.rdbuf();
|
||||
cerr_buffer = std::cerr.rdbuf();
|
||||
wcin_buffer = std::wcin.rdbuf();
|
||||
wcout_buffer = std::wcout.rdbuf();
|
||||
wcerr_buffer = std::wcerr.rdbuf();
|
||||
|
||||
console_input.open("CONIN$", std::ios::in);
|
||||
console_output.open("CONOUT$", std::ios::out);
|
||||
console_error.open("CONOUT$", std::ios::out);
|
||||
wconsole_input.open("CONIN$", std::ios::in);
|
||||
wconsole_output.open("CONOUT$", std::ios::out);
|
||||
wconsole_error.open("CONOUT$", std::ios::out);
|
||||
|
||||
FILE* dummy;
|
||||
freopen_s(&dummy, "CONOUT$", "w+t", stdout);
|
||||
freopen_s(&dummy, "CONIN$", "r+t", stdin);
|
||||
freopen_s(&dummy, "CONOUT$", "w+t", stderr);
|
||||
(void)dummy;
|
||||
|
||||
std::cin.rdbuf(console_input.rdbuf());
|
||||
std::cout.rdbuf(console_output.rdbuf());
|
||||
std::cerr.rdbuf(console_error.rdbuf());
|
||||
std::wcin.rdbuf(wconsole_input.rdbuf());
|
||||
std::wcout.rdbuf(wconsole_output.rdbuf());
|
||||
std::wcerr.rdbuf(wconsole_error.rdbuf());
|
||||
}
|
||||
|
||||
void ResetStdIO()
|
||||
{
|
||||
console_input.close();
|
||||
console_output.close();
|
||||
console_error.close();
|
||||
wconsole_input.close();
|
||||
wconsole_output.close();
|
||||
wconsole_error.close();
|
||||
|
||||
std::cin.rdbuf(cin_buffer);
|
||||
std::cout.rdbuf(cout_buffer);
|
||||
std::cerr.rdbuf(cerr_buffer);
|
||||
std::wcin.rdbuf(wcin_buffer);
|
||||
std::wcout.rdbuf(wcout_buffer);
|
||||
std::wcerr.rdbuf(wcerr_buffer);
|
||||
|
||||
fclose(stdout);
|
||||
fclose(stdin);
|
||||
fclose(stderr);
|
||||
|
||||
cin_buffer = nullptr;
|
||||
cout_buffer = nullptr;
|
||||
cerr_buffer = nullptr;
|
||||
wcin_buffer = nullptr;
|
||||
wcout_buffer = nullptr;
|
||||
wcerr_buffer = nullptr;
|
||||
}
|
||||
|
||||
HWND allocated_console = nullptr;
|
||||
|
||||
HWND AllocateConsole()
|
||||
{
|
||||
if (::AllocConsole())
|
||||
{
|
||||
allocated_console = ::GetConsoleWindow();
|
||||
|
||||
if (allocated_console)
|
||||
{
|
||||
RedirectStdIO();
|
||||
}
|
||||
}
|
||||
return allocated_console;
|
||||
}
|
||||
|
||||
void FreeAllocatedConsole()
|
||||
{
|
||||
if (allocated_console)
|
||||
{
|
||||
ResetStdIO();
|
||||
::FreeConsole();
|
||||
allocated_console = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
HWND GetAllocatedConsole()
|
||||
{
|
||||
return allocated_console;
|
||||
std::mutex perform_mutex_;
|
||||
Queue<FunctionToPerform> functions_to_perform_;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,21 +52,20 @@ namespace kiwano
|
|||
, main_window_(nullptr)
|
||||
, time_scale_(1.f)
|
||||
{
|
||||
::CoInitialize(nullptr);
|
||||
ThrowIfFailed(
|
||||
::CoInitialize(nullptr)
|
||||
);
|
||||
|
||||
main_window_ = new Window;
|
||||
|
||||
Use(&Renderer::Instance());
|
||||
Use(&Input::Instance());
|
||||
Use(&AsyncTaskThread::Instance());
|
||||
}
|
||||
|
||||
Application::~Application()
|
||||
{
|
||||
Destroy();
|
||||
|
||||
FreeAllocatedConsole();
|
||||
|
||||
::CoUninitialize();
|
||||
}
|
||||
|
||||
|
|
@ -414,54 +324,12 @@ namespace kiwano
|
|||
);
|
||||
}
|
||||
|
||||
void Application::PreformFunctionInMainThread(Closure<void()> function)
|
||||
void Application::PreformInMainThread(Closure<void()> function)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(perform_mutex_);
|
||||
functions_to_perform_.push(function);
|
||||
}
|
||||
|
||||
void Application::ShowConsole(bool show)
|
||||
{
|
||||
HWND current_console = ::GetConsoleWindow();
|
||||
if (show)
|
||||
{
|
||||
if (current_console)
|
||||
{
|
||||
::ShowWindow(current_console, SW_SHOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
HWND console = ::AllocateConsole();
|
||||
if (!console)
|
||||
{
|
||||
KGE_WARNING_LOG(L"AllocConsole failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
// disable the close button of console
|
||||
HMENU hmenu = ::GetSystemMenu(console, FALSE);
|
||||
::RemoveMenu(hmenu, SC_CLOSE, MF_BYCOMMAND);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (current_console)
|
||||
{
|
||||
if (current_console == GetAllocatedConsole())
|
||||
{
|
||||
FreeAllocatedConsole();
|
||||
}
|
||||
else
|
||||
{
|
||||
::ShowWindow(current_console, SW_HIDE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Logger::Instance().ResetOutputStream();
|
||||
}
|
||||
|
||||
LRESULT CALLBACK Application::WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
||||
{
|
||||
Application * app = reinterpret_cast<Application*>(
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@
|
|||
#include "../base/time.h"
|
||||
#include "../base/window.h"
|
||||
#include "../base/Component.h"
|
||||
#include <mutex>
|
||||
|
||||
namespace kiwano
|
||||
{
|
||||
|
|
@ -133,15 +132,10 @@ namespace kiwano
|
|||
|
||||
// 在 Kiwano 主线程中执行函数
|
||||
// 当在其他线程调用 Kiwano 函数时使用
|
||||
void PreformFunctionInMainThread(
|
||||
static void PreformInMainThread(
|
||||
Closure<void()> function
|
||||
);
|
||||
|
||||
// ÏÔʾ¿ØÖÆÌ¨
|
||||
static void ShowConsole(
|
||||
bool show
|
||||
);
|
||||
|
||||
protected:
|
||||
void Render();
|
||||
|
||||
|
|
@ -161,8 +155,5 @@ namespace kiwano
|
|||
|
||||
Window* main_window_;
|
||||
Array<Component*> components_;
|
||||
|
||||
std::mutex perform_mutex_;
|
||||
Queue<Closure<void()>> functions_to_perform_;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,13 +33,13 @@ public:
|
|||
void OnEnter() override
|
||||
{
|
||||
// 进入场景时打开控制台
|
||||
Application::ShowConsole(true);
|
||||
Logger::Instance().ShowConsole(true);
|
||||
}
|
||||
|
||||
void OnExit() override
|
||||
{
|
||||
// 退出场景时关闭控制台
|
||||
Application::ShowConsole(false);
|
||||
Logger::Instance().ShowConsole(false);
|
||||
}
|
||||
|
||||
void OnKeyDown(Event const& e)
|
||||
|
|
|
|||
Loading…
Reference in New Issue