From d76a4de248c6995377a66df84e3889692e7c918c Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Wed, 24 Apr 2019 13:33:19 +0800 Subject: [PATCH] update Logger --- Kiwano/base/AsyncTask.cpp | 35 +------ Kiwano/base/AsyncTask.h | 21 +---- Kiwano/base/logs.cpp | 146 ++++++++++++++++++++++++++++++ Kiwano/base/logs.h | 7 ++ Kiwano/network/HttpClient.cpp | 11 +-- Kiwano/network/HttpClient.h | 2 - Kiwano/platform/Application.cpp | 156 +++----------------------------- Kiwano/platform/Application.h | 11 +-- samples/Samples/Demo5.h | 4 +- 9 files changed, 176 insertions(+), 217 deletions(-) diff --git a/Kiwano/base/AsyncTask.cpp b/Kiwano/base/AsyncTask.cpp index a07a2b2f..bb18d686 100644 --- a/Kiwano/base/AsyncTask.cpp +++ b/Kiwano/base/AsyncTask.cpp @@ -19,15 +19,11 @@ // THE SOFTWARE. #include "AsyncTask.h" -#include "../common/closure.hpp" #include "../platform/Application.h" #include 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 func) - { - if (app_) - { - app_->PreformFunctionInMainThread(func); - } - } - } diff --git a/Kiwano/base/AsyncTask.h b/Kiwano/base/AsyncTask.h index 66348b61..ac637197 100644 --- a/Kiwano/base/AsyncTask.h +++ b/Kiwano/base/AsyncTask.h @@ -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 #include @@ -63,21 +61,4 @@ namespace kiwano AsyncTaskCallback thread_cb_; std::mutex func_mutex_; }; - - class AsyncTaskThread - : public Singleton - , public Component - { - KGE_DECLARE_SINGLETON(AsyncTaskThread); - - public: - virtual void SetupComponent(Application*); - - virtual void DestroyComponent(); - - void PerformTaskCallback(Closure func); - - private: - Application* app_; - }; } diff --git a/Kiwano/base/logs.cpp b/Kiwano/base/logs.cpp index 7c7cb4bb..a8f2d257 100644 --- a/Kiwano/base/logs.cpp +++ b/Kiwano/base/logs.cpp @@ -20,6 +20,105 @@ #include "logs.h" #include +#include + +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(); + } + } diff --git a/Kiwano/base/logs.h b/Kiwano/base/logs.h index 9e486a41..95729974 100644 --- a/Kiwano/base/logs.h +++ b/Kiwano/base/logs.h @@ -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; diff --git a/Kiwano/network/HttpClient.cpp b/Kiwano/network/HttpClient.cpp index 7fa21bc3..364457b0 100644 --- a/Kiwano/network/HttpClient.cpp +++ b/Kiwano/network/HttpClient.cpp @@ -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()); diff --git a/Kiwano/network/HttpClient.h b/Kiwano/network/HttpClient.h index 2171ed06..0cb6ab10 100644 --- a/Kiwano/network/HttpClient.h +++ b/Kiwano/network/HttpClient.h @@ -85,8 +85,6 @@ namespace kiwano void DispatchResponseCallback(); private: - Application* app_; - Duration timeout_for_connect_; Duration timeout_for_read_; diff --git a/Kiwano/platform/Application.cpp b/Kiwano/platform/Application.cpp index 38db9e7e..5cd1e20b 100644 --- a/Kiwano/platform/Application.cpp +++ b/Kiwano/platform/Application.cpp @@ -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 -#include -#include -#include +#include // GET_X_LPARAM, GET_Y_LPARAM +#include // ImmAssociateContext +#include // 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; - 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 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 function) + void Application::PreformInMainThread(Closure function) { std::lock_guard 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( diff --git a/Kiwano/platform/Application.h b/Kiwano/platform/Application.h index ae7f0a7d..aa3dcdfa 100644 --- a/Kiwano/platform/Application.h +++ b/Kiwano/platform/Application.h @@ -23,7 +23,6 @@ #include "../base/time.h" #include "../base/window.h" #include "../base/Component.h" -#include namespace kiwano { @@ -133,15 +132,10 @@ namespace kiwano // 在 Kiwano 主线程中执行函数 // 当在其他线程调用 Kiwano 函数时使用 - void PreformFunctionInMainThread( + static void PreformInMainThread( Closure function ); - // 显示控制台 - static void ShowConsole( - bool show - ); - protected: void Render(); @@ -161,8 +155,5 @@ namespace kiwano Window* main_window_; Array components_; - - std::mutex perform_mutex_; - Queue> functions_to_perform_; }; } diff --git a/samples/Samples/Demo5.h b/samples/Samples/Demo5.h index 79353a30..d64114bf 100644 --- a/samples/Samples/Demo5.h +++ b/samples/Samples/Demo5.h @@ -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)