add: logs module can be disabled now
This commit is contained in:
parent
b98e958213
commit
9bd216dc92
|
|
@ -0,0 +1,155 @@
|
||||||
|
// Copyright (c) 2016-2018 Easy2D - Nomango
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
// THE SOFTWARE.
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "logs.h"
|
||||||
|
#include <ctime>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
namespace easy2d
|
||||||
|
{
|
||||||
|
namespace logs
|
||||||
|
{
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
bool enabled = true;
|
||||||
|
|
||||||
|
void Out(std::ostream& stream, const char* output)
|
||||||
|
{
|
||||||
|
stream << output;
|
||||||
|
::OutputDebugStringA(output);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 ");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Output(std::ostream& stream, const char* prompt, const char* format, va_list args)
|
||||||
|
{
|
||||||
|
if (!enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OutputLine(std::ostream& stream, const char* prompt, const char* format, va_list args)
|
||||||
|
{
|
||||||
|
if (!enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Output(stream, prompt, format, args);
|
||||||
|
Out(stream, "\r\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void easy2d::logs::Enable()
|
||||||
|
{
|
||||||
|
enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void easy2d::logs::Disable()
|
||||||
|
{
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Print(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
Output(std::cout, "", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Println(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
OutputLine(std::cout, "", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Warning(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
Output(std::cerr, "Warning: ", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Warningln(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
OutputLine(std::cerr, "Warning: ", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Error(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
Output(std::cerr, "Error: ", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Errorln(const char* format, ...)
|
||||||
|
{
|
||||||
|
va_list args = nullptr;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
OutputLine(std::cerr, "Error: ", format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Errorln(HRESULT hr)
|
||||||
|
{
|
||||||
|
Errorln("failure with HRESULT of %08X", hr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Errorln(HRESULT hr, const char* output)
|
||||||
|
{
|
||||||
|
Errorln("failure with HRESULT of %08X: %s", hr, output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
109
core/base/logs.h
109
core/base/logs.h
|
|
@ -20,9 +20,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include <ctime>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
@ -38,112 +36,25 @@ namespace easy2d
|
||||||
{
|
{
|
||||||
namespace logs
|
namespace logs
|
||||||
{
|
{
|
||||||
namespace
|
void Enable();
|
||||||
{
|
|
||||||
inline void Out(std::ostream& stream, const char* output)
|
|
||||||
{
|
|
||||||
stream << output;
|
|
||||||
::OutputDebugStringA(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void OutPrefix(std::stringstream& ss)
|
void Disable();
|
||||||
{
|
|
||||||
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)
|
void Print(const char* format, ...);
|
||||||
{
|
|
||||||
size_t len = ::_vscprintf(format, args) + 1;
|
|
||||||
char* buffer = new char[len];
|
|
||||||
::_vsnprintf_s(buffer, len, len, format, args);
|
|
||||||
|
|
||||||
std::stringstream ss;
|
void Println(const char* format, ...);
|
||||||
OutPrefix(ss);
|
|
||||||
ss << prompt << buffer;
|
|
||||||
Out(stream, ss.str().c_str());
|
|
||||||
|
|
||||||
delete[] buffer;
|
void Warning(const char* format, ...);
|
||||||
}
|
|
||||||
|
|
||||||
inline void OutputLine(std::ostream& stream, const char* prompt, const char* format, va_list args)
|
void Warningln(const char* format, ...);
|
||||||
{
|
|
||||||
Output(stream, prompt, format, args);
|
|
||||||
Out(stream, "\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Print(const char* format, ...)
|
void Error(const char* format, ...);
|
||||||
{
|
|
||||||
va_list args = nullptr;
|
|
||||||
va_start(args, format);
|
|
||||||
|
|
||||||
Output(std::cout, "", format, args);
|
void Errorln(const char* format, ...);
|
||||||
|
|
||||||
va_end(args);
|
void Errorln(HRESULT hr);
|
||||||
}
|
|
||||||
|
|
||||||
inline void Println(const char* format, ...)
|
void Errorln(HRESULT hr, const char* output);
|
||||||
{
|
|
||||||
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)
|
inline void ThrowIfFailed(HRESULT hr)
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,7 @@
|
||||||
<ClCompile Include="..\..\core\base\Image.cpp" />
|
<ClCompile Include="..\..\core\base\Image.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Input.cpp" />
|
<ClCompile Include="..\..\core\base\Input.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\modules.cpp" />
|
<ClCompile Include="..\..\core\base\modules.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Music.cpp" />
|
<ClCompile Include="..\..\core\base\Music.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -288,5 +288,8 @@
|
||||||
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
||||||
<Filter>base</Filter>
|
<Filter>base</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp">
|
||||||
|
<Filter>base</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -88,6 +88,7 @@
|
||||||
<ClCompile Include="..\..\core\base\Image.cpp" />
|
<ClCompile Include="..\..\core\base\Image.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Input.cpp" />
|
<ClCompile Include="..\..\core\base\Input.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\modules.cpp" />
|
<ClCompile Include="..\..\core\base\modules.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Music.cpp" />
|
<ClCompile Include="..\..\core\base\Music.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -288,5 +288,8 @@
|
||||||
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
||||||
<Filter>base</Filter>
|
<Filter>base</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp">
|
||||||
|
<Filter>base</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
@ -88,6 +88,7 @@
|
||||||
<ClCompile Include="..\..\core\base\Image.cpp" />
|
<ClCompile Include="..\..\core\base\Image.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Input.cpp" />
|
<ClCompile Include="..\..\core\base\Input.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
<ClCompile Include="..\..\core\base\KeyEvent.cpp" />
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\modules.cpp" />
|
<ClCompile Include="..\..\core\base\modules.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
<ClCompile Include="..\..\core\base\MouseEvent.cpp" />
|
||||||
<ClCompile Include="..\..\core\base\Music.cpp" />
|
<ClCompile Include="..\..\core\base\Music.cpp" />
|
||||||
|
|
|
||||||
|
|
@ -288,5 +288,8 @@
|
||||||
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
<ClCompile Include="..\..\core\base\ActionManager.cpp">
|
||||||
<Filter>base</Filter>
|
<Filter>base</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\core\base\logs.cpp">
|
||||||
|
<Filter>base</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
Loading…
Reference in New Issue