add: log tool

This commit is contained in:
Haibo 2018-11-15 17:59:18 +08:00 committed by Nomango
parent f2be349841
commit 298f686729
24 changed files with 221 additions and 121 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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)
{

View File

@ -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);

View File

@ -21,6 +21,7 @@
#include "base.h"
#include "Canvas.h"
#include "render.h"
#include "logs.h"
namespace easy2d
{

View File

@ -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;
}

View File

@ -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);

View File

@ -19,6 +19,7 @@
// THE SOFTWARE.
#include "input.h"
#include "logs.h"
#include <cstring>
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;
}

View File

@ -19,7 +19,7 @@
// THE SOFTWARE.
#pragma once
#include "macros.h"
#include <utility>
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_;
}

View File

@ -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<UINT32>(loop_count));
if (FAILED(hr))
{
logs::Trace(L"Submitting source buffer error", hr);
logs::Errorln(hr, "Submitting source buffer error");
}
playing_ = SUCCEEDED(hr);

View File

@ -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())
{

View File

@ -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;

View File

@ -21,6 +21,7 @@
#include "Text.h"
#include "render.h"
#include "base.h"
#include "logs.h"
namespace easy2d
{

View File

@ -23,6 +23,7 @@
#include "Scene.h"
#include "window.h"
#include "render.h"
#include "logs.h"
namespace easy2d
{

View File

@ -21,6 +21,7 @@
#include "audio.h"
#include "base.h"
#include "modules.h"
#include "logs.h"
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
@ -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)
);

View File

@ -22,7 +22,6 @@
#include "BaseTypes.h"
#include "IntrusivePtr.hpp"
#include "RefCounter.hpp"
#include <stdexcept>
#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<unsigned int>(hr));
::OutputDebugStringA(s_str);
throw std::runtime_error(s_str);
}
}
}

View File

@ -20,6 +20,19 @@
#pragma once
#include "macros.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#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 ");
}
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);
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 Print(const char* format, ...)
{
va_list args = nullptr;
va_start(args, format);
Output(std::cout, "", format, args);
va_end(args);
::OutputDebugStringW(psBuffer);
delete[] psBuffer;
}
}
inline void Trace(LPCWSTR output)
inline void Println(const char* format, ...)
{
OutputDebugStringExW(L"[easy2d] Error: %s\r\n", output);
va_list args = nullptr;
va_start(args, format);
OutputLine(std::cout, "", format, args);
va_end(args);
}
inline void Trace(LPCWSTR output, HRESULT hr)
inline void Warning(const char* format, ...)
{
OutputDebugStringExW(L"[easy2d] Failure with HRESULT of %08X: %s\r\n", output, static_cast<unsigned int>(hr));
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");
}
}
}

View File

@ -80,7 +80,6 @@
#include <list>
#include <vector>
#include <algorithm>
#include <memory>
#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

View File

@ -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);

View File

@ -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(

View File

@ -19,15 +19,13 @@
// THE SOFTWARE.
#include "time.h"
#include <chrono>
#include "logs.h"
#include <regex>
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<int64_t>(
duration_cast<milliseconds>(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();
}

View File

@ -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<time_t>(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;
};
// 获取当前时间

View File

@ -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<LONG>(math::Ceil(height * scale_y))
};
// 计算合适的窗口大小
::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL);
width = static_cast<int>(rect.right - rect.left);
height = static_cast<int>(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);

View File

@ -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