From 298f686729e778818d84665c064f8621bc7338dc Mon Sep 17 00:00:00 2001 From: Haibo Date: Thu, 15 Nov 2018 17:59:18 +0800 Subject: [PATCH] add: log tool --- core/base/ActionCombined.cpp | 5 +- core/base/ActionFiniteTime.h | 11 +-- core/base/ActionManager.cpp | 4 +- core/base/Animation.cpp | 5 +- core/base/Canvas.cpp | 1 + core/base/Game.cpp | 3 +- core/base/Image.cpp | 8 +-- core/base/Input.cpp | 4 ++ core/base/IntrusivePtr.hpp | 8 +-- core/base/Music.cpp | 14 ++-- core/base/Node.cpp | 11 +-- core/base/Resource.cpp | 8 +-- core/base/Text.cpp | 1 + core/base/Transition.cpp | 1 + core/base/audio.cpp | 5 ++ core/base/base.h | 12 ---- core/base/logs.h | 127 +++++++++++++++++++++++++++++++---- core/base/macros.h | 15 ----- core/base/modules.cpp | 5 ++ core/base/render.cpp | 5 ++ core/base/time.cpp | 55 +++++++-------- core/base/time.h | 17 ++--- core/base/window.cpp | 13 ++-- core/utils/Transcoder.cpp | 4 +- 24 files changed, 221 insertions(+), 121 deletions(-) diff --git a/core/base/ActionCombined.cpp b/core/base/ActionCombined.cpp index 2155ad3d..f1a4c1a3 100644 --- a/core/base/ActionCombined.cpp +++ b/core/base/ActionCombined.cpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #include "ActionCombined.h" -#include "base.h" +#include "logs.h" namespace easy2d { @@ -32,7 +32,8 @@ namespace easy2d , times_(0) , total_times_(times) { - E2D_WARNING_IF(!action, "Loop NULL pointer exception!"); + if (!action) + logs::Warningln("Loop action contains a null action"); action_ = action; } diff --git a/core/base/ActionFiniteTime.h b/core/base/ActionFiniteTime.h index 9256764a..c051f7fb 100644 --- a/core/base/ActionFiniteTime.h +++ b/core/base/ActionFiniteTime.h @@ -20,6 +20,7 @@ #pragma once #include "Action.hpp" +#include "logs.h" namespace easy2d { @@ -101,7 +102,7 @@ namespace easy2d // 获取该动作的倒转 virtual spAction Reverse() const override { - E2D_WARNING("Reverse() not supported in MoveTo"); + logs::Errorln("Reverse() not supported in MoveTo"); return nullptr; } @@ -170,7 +171,7 @@ namespace easy2d // 获取该动作的倒转 virtual spAction Reverse() const override { - E2D_WARNING("Reverse() not supported in JumpTo"); + logs::Errorln("Reverse() not supported in JumpTo"); return nullptr; } @@ -246,7 +247,7 @@ namespace easy2d // 获取该动作的倒转 virtual spAction Reverse() const override { - E2D_WARNING("Reverse() not supported in ScaleTo"); + logs::Errorln("Reverse() not supported in ScaleTo"); return nullptr; } @@ -309,7 +310,7 @@ namespace easy2d // 获取该动作的倒转 virtual spAction Reverse() const override { - E2D_WARNING("Reverse() not supported in OpacityTo"); + logs::Errorln("Reverse() not supported in OpacityTo"); return nullptr; } @@ -399,7 +400,7 @@ namespace easy2d // 获取该动作的倒转 virtual spAction Reverse() const override { - E2D_WARNING("Reverse() not supported in RotateTo"); + logs::Errorln("Reverse() not supported in RotateTo"); return nullptr; } diff --git a/core/base/ActionManager.cpp b/core/base/ActionManager.cpp index a157ec5b..ab94f7d8 100644 --- a/core/base/ActionManager.cpp +++ b/core/base/ActionManager.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include "ActionManager.h" +#include "logs.h" namespace easy2d { @@ -56,7 +57,8 @@ namespace easy2d void ActionManager::RunAction(spAction const& action) { - E2D_WARNING_IF(!action, "Action NULL pointer exception!"); + if (!action) + logs::Warningln("Node::RunAction failed, action is nullptr"); if (action) { diff --git a/core/base/Animation.cpp b/core/base/Animation.cpp index 7b887cd2..63a74ece 100644 --- a/core/base/Animation.cpp +++ b/core/base/Animation.cpp @@ -21,6 +21,7 @@ #include "Animation.h" #include "Image.h" #include "Sprite.h" +#include "logs.h" namespace easy2d { @@ -169,7 +170,9 @@ namespace easy2d void Animation::Add(spImage const& frame) { - E2D_WARNING_IF(!frame, "Animation::Add failed, frame Is nullptr."); + if (!frame) + logs::Warningln("Animation::Add failed, frame is nullptr."); + if (frame) { frames_.push_back(frame); diff --git a/core/base/Canvas.cpp b/core/base/Canvas.cpp index 07fb32f4..282b4f17 100644 --- a/core/base/Canvas.cpp +++ b/core/base/Canvas.cpp @@ -21,6 +21,7 @@ #include "base.h" #include "Canvas.h" #include "render.h" +#include "logs.h" namespace easy2d { diff --git a/core/base/Game.cpp b/core/base/Game.cpp index 4d68c404..1186602d 100644 --- a/core/base/Game.cpp +++ b/core/base/Game.cpp @@ -24,6 +24,7 @@ #include "Image.h" #include "../utils/Player.h" #include "../math/Matrix.hpp" +#include "logs.h" #include "render.h" #include "input.h" #include "audio.h" @@ -170,7 +171,7 @@ namespace easy2d { if (!scene) { - E2D_WARNING("Next scene is null pointer!"); + logs::Warningln("Game::EnterScene failed, scene is nullptr"); return false; } diff --git a/core/base/Image.cpp b/core/base/Image.cpp index 46a74f26..1aac4fa0 100644 --- a/core/base/Image.cpp +++ b/core/base/Image.cpp @@ -72,7 +72,7 @@ namespace easy2d HRESULT hr = devices::Graphics::Instance()->CreateBitmapFromResource(res, &bitmap); if (FAILED(hr)) { - logs::Trace(L"Load Image from resource failed!", hr); + logs::Errorln(hr, "Load Image from resource failed!"); return false; } this->SetBitmap(bitmap); @@ -81,12 +81,10 @@ namespace easy2d bool Image::Load(const String & file_name) { - E2D_WARNING_IF(file_name.empty(), "Image Load failed! Invalid file name."); - File image_file; if (!image_file.Open(file_name)) { - E2D_WARNING("Image file not found!"); + logs::Warningln("Image file '%s' not found!", file_name.c_str()); return false; } @@ -98,7 +96,7 @@ namespace easy2d HRESULT hr = devices::Graphics::Instance()->CreateBitmapFromFile(image_file_path, &bitmap); if (FAILED(hr)) { - logs::Trace(L"Load Image from file failed!", hr); + logs::Errorln(hr, "Load Image from file failed!"); return false; } this->SetBitmap(bitmap); diff --git a/core/base/Input.cpp b/core/base/Input.cpp index 0deef824..9feb497a 100644 --- a/core/base/Input.cpp +++ b/core/base/Input.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include "input.h" +#include "logs.h" #include namespace easy2d @@ -34,6 +35,7 @@ namespace easy2d InputDevice::~InputDevice() { + E2D_LOG("Destroying input device"); } void InputDevice::Init(bool debug) @@ -41,6 +43,8 @@ namespace easy2d if (initialized) return; + E2D_LOG("Initing input device"); + initialized = true; } diff --git a/core/base/IntrusivePtr.hpp b/core/base/IntrusivePtr.hpp index 220e2211..0c70ac5e 100644 --- a/core/base/IntrusivePtr.hpp +++ b/core/base/IntrusivePtr.hpp @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include "macros.h" +#include namespace easy2d { @@ -66,20 +66,16 @@ namespace easy2d inline void Swap(IntrusivePtr& other) { - ::std::swap(ptr_, other.ptr_); + std::swap(ptr_, other.ptr_); } inline ElemType* operator ->() const { - E2D_WARNING_IF(!ptr_ || ptr_->GetRefCount() <= 0, - "Invalid pointer!"); return ptr_; } inline ElemType& operator *() const { - E2D_WARNING_IF(!ptr_ || ptr_->GetRefCount() <= 0, - "Invalid pointer!"); return *ptr_; } diff --git a/core/base/Music.cpp b/core/base/Music.cpp index 3a413abb..29664922 100644 --- a/core/base/Music.cpp +++ b/core/base/Music.cpp @@ -71,7 +71,7 @@ namespace easy2d File music_file; if (!music_file.Open(file_path)) { - logs::Trace(L"Media file not found."); + logs::Warningln("Media file '%s' not found", file_path.c_str()); return false; } @@ -83,7 +83,7 @@ namespace easy2d HRESULT hr = transcoder.LoadMediaFile(music_file_path.c_str(), &wave_data_, &size_); if (FAILED(hr)) { - logs::Trace(L"Load media from file failed.", hr); + logs::Errorln(hr, "Load media from file failed"); return false; } @@ -95,7 +95,7 @@ namespace easy2d delete[] wave_data_; wave_data_ = nullptr; } - logs::Trace(L"Create source voice error", hr); + logs::Errorln(hr, "Create source voice failed"); return false; } @@ -115,7 +115,7 @@ namespace easy2d if (FAILED(hr)) { - logs::Trace(L"Load media from resource failed.", hr); + logs::Errorln(hr, "Load media from resource failed"); return false; } @@ -127,7 +127,7 @@ namespace easy2d delete[] wave_data_; wave_data_ = nullptr; } - logs::Trace(L"Create source voice error", hr); + logs::Errorln(hr, "Create source voice error"); return false; } @@ -139,7 +139,7 @@ namespace easy2d { if (!opened_) { - logs::Trace(L"Music must be opened first!"); + logs::Errorln("Music must be opened first!"); return false; } @@ -156,7 +156,7 @@ namespace easy2d HRESULT hr = voice_.Play(wave_data_, size_, static_cast(loop_count)); if (FAILED(hr)) { - logs::Trace(L"Submitting source buffer error", hr); + logs::Errorln(hr, "Submitting source buffer error"); } playing_ = SUCCEEDED(hr); diff --git a/core/base/Node.cpp b/core/base/Node.cpp index fab80bb4..9d0bf9cd 100644 --- a/core/base/Node.cpp +++ b/core/base/Node.cpp @@ -23,6 +23,7 @@ #include "Task.h" #include "Action.hpp" #include "render.h" +#include "logs.h" namespace easy2d { @@ -528,20 +529,21 @@ namespace easy2d void Node::AddChild(spNode const& child, int order) { - E2D_WARNING_IF(!child, "Node::AddChild NULL pointer exception."); + if (!child) + logs::Warningln("Node::AddChild failed, child is nullptr"); if (child) { if (child->parent_) { - throw std::runtime_error("节点已有父节点, 不能再添加到其他节点"); + throw std::logic_error("节点已有父节点, 不能再添加到其他节点"); } for (Node * parent = this; parent; parent = parent->GetParent().Get()) { if (child == parent) { - throw std::runtime_error("一个节点不能同时是另一个节点的父节点和子节点"); + throw std::logic_error("一个节点不能同时是另一个节点的父节点和子节点"); } } @@ -622,7 +624,8 @@ namespace easy2d bool Node::RemoveChild(spNode const& child) { - E2D_WARNING_IF(!child, "Node::RemoveChildren NULL pointer exception."); + if (!child) + logs::Warningln("Node::RemoveChild failed, child is nullptr"); if (children_.empty()) { diff --git a/core/base/Resource.cpp b/core/base/Resource.cpp index 120424df..e362f50c 100644 --- a/core/base/Resource.cpp +++ b/core/base/Resource.cpp @@ -55,28 +55,28 @@ namespace easy2d res_info = FindResourceW(nullptr, name_, type_); if (res_info == nullptr) { - logs::Trace(L"FindResource"); + logs::Errorln("FindResource"); return false; } res_data = LoadResource(nullptr, res_info); if (res_data == nullptr) { - logs::Trace(L"LoadResource"); + logs::Errorln("LoadResource"); return false; } (*buffer).buffer_size = SizeofResource(nullptr, res_info); if ((*buffer).buffer_size == 0) { - logs::Trace(L"SizeofResource"); + logs::Errorln("SizeofResource"); return false; } (*buffer).buffer = LockResource(res_data); if ((*buffer).buffer == nullptr) { - logs::Trace(L"LockResource"); + logs::Errorln("LockResource"); return false; } return true; diff --git a/core/base/Text.cpp b/core/base/Text.cpp index 5bc25a15..df4df087 100644 --- a/core/base/Text.cpp +++ b/core/base/Text.cpp @@ -21,6 +21,7 @@ #include "Text.h" #include "render.h" #include "base.h" +#include "logs.h" namespace easy2d { diff --git a/core/base/Transition.cpp b/core/base/Transition.cpp index 922eb4b1..4b4eaad5 100644 --- a/core/base/Transition.cpp +++ b/core/base/Transition.cpp @@ -23,6 +23,7 @@ #include "Scene.h" #include "window.h" #include "render.h" +#include "logs.h" namespace easy2d { diff --git a/core/base/audio.cpp b/core/base/audio.cpp index 9e127089..674d4bcd 100644 --- a/core/base/audio.cpp +++ b/core/base/audio.cpp @@ -21,6 +21,7 @@ #include "audio.h" #include "base.h" #include "modules.h" +#include "logs.h" #include #include #include @@ -170,6 +171,8 @@ namespace easy2d AudioDevice::~AudioDevice() { + E2D_LOG("Destroying audio device"); + ClearVoiceCache(); if (mastering_voice_) @@ -190,6 +193,8 @@ namespace easy2d if (initialized) return; + E2D_LOG("Initing audio device"); + ThrowIfFailed( modules::MediaFoundation.MFStartup(MF_VERSION, MFSTARTUP_FULL) ); diff --git a/core/base/base.h b/core/base/base.h index b577377f..ad4b47b8 100644 --- a/core/base/base.h +++ b/core/base/base.h @@ -22,7 +22,6 @@ #include "BaseTypes.h" #include "IntrusivePtr.hpp" #include "RefCounter.hpp" -#include #ifndef E2D_DECLARE_SMART_PTR @@ -97,15 +96,4 @@ namespace easy2d p = nullptr; } } - - inline void ThrowIfFailed(HRESULT hr) - { - if (FAILED(hr)) - { - static char s_str[64] = {}; - ::sprintf_s(s_str, "[easy2d] Failure with HRESULT of %08X", static_cast(hr)); - ::OutputDebugStringA(s_str); - throw std::runtime_error(s_str); - } - } } diff --git a/core/base/logs.h b/core/base/logs.h index 65a9d537..fce3d466 100644 --- a/core/base/logs.h +++ b/core/base/logs.h @@ -20,6 +20,19 @@ #pragma once #include "macros.h" +#include +#include +#include +#include +#include + +#ifndef E2D_LOG +# if defined(DEBUG) || defined(_DEBUG) +# define E2D_LOG(format, ...) easy2d::logs::Println(format, ##__VA_ARGS__) +# else +# define E2D_LOG ((void)0) +# endif +#endif namespace easy2d { @@ -27,30 +40,118 @@ namespace easy2d { namespace { - inline void OutputDebugStringExW(LPCWSTR pszOutput, ...) + inline void Out(std::ostream& stream, const char* output) { - va_list args = NULL; - va_start(args, pszOutput); + stream << output; + ::OutputDebugStringA(output); + } - size_t nLen = ::_vscwprintf(pszOutput, args) + 1; - wchar_t* psBuffer = new wchar_t[nLen]; - ::_vsnwprintf_s(psBuffer, nLen, nLen, pszOutput, args); + inline void OutPrefix(std::stringstream& ss) + { + std::time_t unix = ::time(NULL); + struct tm tmbuf; + localtime_s(&tmbuf, &unix); + ss << std::put_time(&tmbuf, "[easy2d] %H:%M:%S "); + } - va_end(args); + inline void Output(std::ostream& stream, const char* prompt, const char* format, va_list args) + { + size_t len = ::_vscprintf(format, args) + 1; + char* buffer = new char[len]; + ::_vsnprintf_s(buffer, len, len, format, args); - ::OutputDebugStringW(psBuffer); - delete[] psBuffer; + std::stringstream ss; + OutPrefix(ss); + ss << prompt << buffer; + Out(stream, ss.str().c_str()); + + delete[] buffer; + } + + inline void OutputLine(std::ostream& stream, const char* prompt, const char* format, va_list args) + { + Output(stream, prompt, format, args); + Out(stream, "\r\n"); } } - inline void Trace(LPCWSTR output) + inline void Print(const char* format, ...) { - OutputDebugStringExW(L"[easy2d] Error: %s\r\n", output); + va_list args = nullptr; + va_start(args, format); + + Output(std::cout, "", format, args); + + va_end(args); } - inline void Trace(LPCWSTR output, HRESULT hr) + inline void Println(const char* format, ...) { - OutputDebugStringExW(L"[easy2d] Failure with HRESULT of %08X: %s\r\n", output, static_cast(hr)); + va_list args = nullptr; + va_start(args, format); + + OutputLine(std::cout, "", format, args); + + va_end(args); + } + + inline void Warning(const char* format, ...) + { + va_list args = nullptr; + va_start(args, format); + + Output(std::cerr, "Warning: ", format, args); + + va_end(args); + } + + inline void Warningln(const char* format, ...) + { + va_list args = nullptr; + va_start(args, format); + + OutputLine(std::cerr, "Warning: ", format, args); + + va_end(args); + } + + inline void Error(const char* format, ...) + { + va_list args = nullptr; + va_start(args, format); + + Output(std::cerr, "Error: ", format, args); + + va_end(args); + } + + inline void Errorln(const char* format, ...) + { + va_list args = nullptr; + va_start(args, format); + + OutputLine(std::cerr, "Error: ", format, args); + + va_end(args); + } + + inline void Errorln(HRESULT hr) + { + Errorln("failure with HRESULT of %08X", hr); + } + + inline void Errorln(HRESULT hr, const char* output) + { + Errorln("failure with HRESULT of %08X: %s", hr, output); + } + } + + inline void ThrowIfFailed(HRESULT hr) + { + if (FAILED(hr)) + { + logs::Errorln(hr); + throw std::runtime_error("Fatal error"); } } } diff --git a/core/base/macros.h b/core/base/macros.h index 90b564d1..b6a8659c 100644 --- a/core/base/macros.h +++ b/core/base/macros.h @@ -80,7 +80,6 @@ #include #include #include -#include #if VS_VER >= VS_2015 @@ -96,17 +95,3 @@ private: \ Class(const Class &) = delete; \ Class & operator= (const Class &) = delete - - -#if defined( DEBUG ) || defined( _DEBUG ) -# define E2D_WARNING(msg) do { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0) -#else -# define E2D_WARNING(msg) ((void)0) -#endif - - -#if defined( DEBUG ) || defined( _DEBUG ) -# define E2D_WARNING_IF(exp, msg) do { if (exp) { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } } while(0) -#else -# define E2D_WARNING_IF(exp, msg) ((void)0) -#endif diff --git a/core/base/modules.cpp b/core/base/modules.cpp index fa8b8ef3..c579c7c9 100644 --- a/core/base/modules.cpp +++ b/core/base/modules.cpp @@ -19,6 +19,7 @@ // THE SOFTWARE. #include "modules.h" +#include "logs.h" namespace easy2d { @@ -44,6 +45,8 @@ namespace easy2d if (initialize_count > 1) return; + E2D_LOG("Initing modules"); + const auto xaudio2_dll_names = { L"xaudio2_9.dll", @@ -98,6 +101,8 @@ namespace easy2d if (initialize_count > 0) return; + E2D_LOG("Destroying modules"); + SafeFreeLibrary(XAudio2.instance); SafeFreeLibrary(MediaFoundation.mfplat); SafeFreeLibrary(MediaFoundation.mfreadwrite); diff --git a/core/base/render.cpp b/core/base/render.cpp index 0a5f522e..2709d20a 100644 --- a/core/base/render.cpp +++ b/core/base/render.cpp @@ -21,6 +21,7 @@ #include "render.h" #include "time.h" #include "base.h" +#include "logs.h" #include "modules.h" #include "Image.h" @@ -44,6 +45,8 @@ namespace easy2d GraphicsDevice::~GraphicsDevice() { + E2D_LOG("Destroying graphics device"); + ClearImageCache(); SafeRelease(fps_text_format_); @@ -69,6 +72,8 @@ namespace easy2d if (initialized) return; + E2D_LOG("Initing graphics device"); + D2D1_FACTORY_OPTIONS options{ debug ? D2D1_DEBUG_LEVEL_INFORMATION : D2D1_DEBUG_LEVEL_NONE }; ThrowIfFailed( D2D1CreateFactory( diff --git a/core/base/time.cpp b/core/base/time.cpp index 3f9b9eb3..9bd94b47 100644 --- a/core/base/time.cpp +++ b/core/base/time.cpp @@ -19,15 +19,13 @@ // THE SOFTWARE. #include "time.h" -#include +#include "logs.h" #include namespace easy2d { namespace time { - using namespace std::chrono; - //------------------------------------------------------- // TimePoint //------------------------------------------------------- @@ -36,51 +34,46 @@ namespace easy2d { } - TimePoint::TimePoint(const Duration& dur_since_epoch) - : dur_since_epoch_(dur_since_epoch) - { - } - - TimePoint::TimePoint(int64_t dur_since_epoch) - : dur_since_epoch_(dur_since_epoch) + TimePoint::TimePoint(const Duration& dur) + : dur(dur) { } TimePoint::TimePoint(const TimePoint & other) - : dur_since_epoch_(other.dur_since_epoch_) + : dur(other.dur) { } TimePoint::TimePoint(TimePoint && other) - : dur_since_epoch_(std::move(other.dur_since_epoch_)) + : dur(std::move(other.dur)) { } const TimePoint TimePoint::operator+(const Duration & dur) const { - return TimePoint(dur_since_epoch_ + dur); + return TimePoint(dur + dur); } const TimePoint TimePoint::operator-(const Duration & dur) const { - return TimePoint(dur_since_epoch_ - dur); + return TimePoint(dur - dur); } TimePoint & TimePoint::operator+=(const Duration & other) { - dur_since_epoch_ += other; + dur += other; return (*this); } TimePoint & TimePoint::operator-=(const Duration &other) { - dur_since_epoch_ -= other; + dur -= other; return (*this); } const Duration TimePoint::operator-(const TimePoint & other) const { - return dur_since_epoch_ - other.dur_since_epoch_; + return dur - other.dur; } TimePoint& TimePoint::operator=(const TimePoint & other) E2D_NOEXCEPT @@ -88,7 +81,7 @@ namespace easy2d if (this == &other) return *this; - dur_since_epoch_ = other.dur_since_epoch_; + dur = other.dur; return *this; } @@ -97,7 +90,7 @@ namespace easy2d if (this == &other) return *this; - dur_since_epoch_ = std::move(other.dur_since_epoch_); + dur = std::move(other.dur); return *this; } @@ -443,11 +436,19 @@ namespace easy2d TimePoint easy2d::time::Now() { - return TimePoint( - static_cast( - duration_cast(steady_clock::now().time_since_epoch()).count() - ) - ); + static LARGE_INTEGER freq = {}; + if (freq.QuadPart == 0LL) + { + if (QueryPerformanceFrequency(&freq) == 0) + throw std::runtime_error("QueryPerformanceFrequency not supported: " + std::to_string(GetLastError())); + } + + LARGE_INTEGER count; + QueryPerformanceCounter(&count); + + const long long whole = (count.QuadPart / freq.QuadPart) * 1000LL; + const long long part = (count.QuadPart % freq.QuadPart) * 1000LL / freq.QuadPart; + return TimePoint{ Duration{ whole + part } }; } Duration easy2d::time::ParseDuration(const std::wstring & str) @@ -459,7 +460,7 @@ namespace easy2d if (!std::regex_match(str, duration_regex)) { - E2D_WARNING("Time::Duration::Parse: invalid duration"); + logs::Errorln("time::ParseDuration failed, invalid duration"); return Duration(); } @@ -490,7 +491,7 @@ namespace easy2d if (num_str.empty() || num_str == L".") { - E2D_WARNING("Time::Duration::Parse: invalid duration"); + logs::Errorln("time::ParseDuration failed, invalid duration"); return Duration(); } @@ -509,7 +510,7 @@ namespace easy2d if (unit_map.find(unit_str) == unit_map.end()) { - E2D_WARNING("Time::Duration::Parse: invalid duration"); + logs::Errorln("time::ParseDuration failed, invalid duration"); return Duration(); } diff --git a/core/base/time.h b/core/base/time.h index 8082f650..efaf49c3 100644 --- a/core/base/time.h +++ b/core/base/time.h @@ -138,6 +138,8 @@ namespace easy2d // auto duration = t2 - t1; // 获取两时间相差的毫秒数: // int ms = duration.Milliseconds(); + // 注: 由于该时间点基于系统启动时间开始计算, 所以无法格式化该时间, + // 也无法获得该时间的 Unix 时间戳 // class TimePoint { @@ -145,11 +147,7 @@ namespace easy2d TimePoint(); explicit TimePoint( - const Duration& dur_since_epoch - ); - - explicit TimePoint( - int64_t dur_since_epoch + const Duration& ); TimePoint( @@ -160,13 +158,8 @@ namespace easy2d TimePoint&& other ); - inline Duration const& SinceEpoch() const { return dur_since_epoch_; } - - // 获取时间戳 - inline time_t GetTimeStamp() const { return static_cast(dur_since_epoch_.Seconds()); } - // 是否是零时 - inline bool IsZero() const { return dur_since_epoch_.IsZero(); } + inline bool IsZero() const { return dur.IsZero(); } const TimePoint operator + (const Duration &) const; const TimePoint operator - (const Duration &) const; @@ -180,7 +173,7 @@ namespace easy2d TimePoint& operator = (TimePoint &&) E2D_NOEXCEPT; private: - Duration dur_since_epoch_; + Duration dur; }; // 获取当前时间 diff --git a/core/base/window.cpp b/core/base/window.cpp index fafc6687..bec8e369 100644 --- a/core/base/window.cpp +++ b/core/base/window.cpp @@ -20,6 +20,7 @@ #include "window.h" #include "render.h" +#include "logs.h" #include "Game.h" #include "KeyEvent.h" #include "MouseEvent.h" @@ -51,6 +52,8 @@ namespace easy2d WindowImpl::~WindowImpl() { + E2D_LOG("Destroying window"); + if (handle) ::DestroyWindow(handle); } @@ -60,6 +63,8 @@ namespace easy2d if (initialized) return; + E2D_LOG("Initing window"); + HINSTANCE hinstance = GetModuleHandle(nullptr); WNDCLASSEX wcex = { 0 }; wcex.cbSize = sizeof(WNDCLASSEX); @@ -112,7 +117,7 @@ namespace easy2d if (handle == nullptr) { ::UnregisterClass(REGISTER_CLASS, hinstance); - throw std::runtime_error("Create window failed"); + throw std::runtime_error("Create window failed!"); } // 禁用输入法 @@ -240,13 +245,13 @@ namespace easy2d static_cast(math::Ceil(height * scale_y)) }; - // 计算合适的窗口大小 ::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL); width = static_cast(rect.right - rect.left); height = static_cast(rect.bottom - rect.top); - // 当输入的窗口大小比分辨率大时,给出警告 - E2D_WARNING_IF(max_width < width || max_height < height, "The window Is larger than screen!"); + if (max_width < width || max_height < height) + logs::Warningln("The window is larger than screen!"); + width = std::min(width, max_width); height = std::min(height, max_height); diff --git a/core/utils/Transcoder.cpp b/core/utils/Transcoder.cpp index befd6d53..3a1ea09f 100644 --- a/core/utils/Transcoder.cpp +++ b/core/utils/Transcoder.cpp @@ -86,7 +86,7 @@ namespace easy2d if (stream == nullptr) { - logs::Trace(L"SHCreateMemStream"); + logs::Errorln("SHCreateMemStream"); return E_OUTOFMEMORY; } @@ -207,7 +207,7 @@ namespace easy2d if (data == nullptr) { - logs::Trace(L"Low memory"); + logs::Errorln("Low memory"); hr = E_OUTOFMEMORY; } else