#include <glwebtools/internal/glwebtools_default_config.h>
#include <glwebtools/os/glwebtools_console.h>
#include <glwebtools/internal/glwebtools_macro.h>
#include <glwebtools/internal/glwebtools_memory.h>
#ifdef ANDROID
#include <android/log.h>
#else
#if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY==WINAPI_FAMILY_APP) || (WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP))
#include <Windows.h>
#include <string>
#else
#include <stdio.h>
#endif
#endif
namespace glwebtools
{
#if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY==WINAPI_FAMILY_APP) || (WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP))
namespace
{
bool s2ws(const char* input, std::wstring& output)
{
const int wchars_num = MultiByteToWideChar( CP_UTF8, 0, input, -1, NULL, 0);
output.clear();
output.resize(wchars_num);
return SUCCEEDED(MultiByteToWideChar( CP_UTF8, 0, input, -1, &output[0], wchars_num));
}
bool s2ws(const std::string& input, std::wstring& output)
{
return s2ws(input.c_str(), output);
}
}
#endif
//statics
ConsoleImplInterface* Console::s_impl = 0;
Console* Console::s_instance = 0;
unsigned int ConsoleImplInterface::s_logLevel = GLWEBTOOLS_CONSOLE_DEFAULT_LEVEL;
void Console::SetConsoleImpl(ConsoleImplInterface* externalConsole)
{
s_impl = externalConsole;
}
void Console::SetLogLevel(unsigned int level)
{
glwebtools::ConsoleImplInterface::s_logLevel = level;
}
void Console::Print(unsigned int level, const char* format, ...)
{
#if GLWEBTOOLS_ENABLE_CONSOLE
va_list argptr;
va_start (argptr,format);
if(s_impl) //custom console
{
s_impl->Print(level, format, argptr);
}
else
{
_Print(level, format, argptr);
}
va_end(argptr);
#endif
}
Console::Console()
{
#ifdef GLWEBTOOLS_DEBUG
GLWEBTOOLS_ASSERT(false);
GLWEBTOOLS_LOG_INFO("%s", "glwebtools::Console is a purely static class do not instanciate");
#endif
}
Console::~Console()
{
}
void Console::_Print(unsigned int level, const char* format, va_list arguments)
{
if(level <= glwebtools::ConsoleImplInterface::s_logLevel)
{
#ifdef ANDROID
#if _DEBUG
__android_log_vprint(ANDROID_LOG_FATAL-level, "GLWT", format, arguments);
#endif
#else
#if defined(WINAPI_FAMILY) && ((WINAPI_FAMILY==WINAPI_FAMILY_APP) || (WINAPI_FAMILY==WINAPI_FAMILY_PHONE_APP))
static const wchar_t* LEVEL_STR[6] = { L"", L"FATAL", L"MAJOR", L"MINOR", L"INFO", L"VERBOSE" };
wchar_t wbuffer[1024];
swprintf_s(wbuffer, 1024, L"[GLWEBTOOLS_%s] ", level <= 5 ? LEVEL_STR[level] : LEVEL_STR[5]);
OutputDebugStringW(wbuffer);
char buffer[1024];
std::wstring wstr;
vsnprintf_s(buffer, 1024, _TRUNCATE, format, arguments);
s2ws(buffer, wstr);
OutputDebugStringW(wstr.c_str());
OutputDebugStringW(L"\n");
#else
static const char* LEVEL_STR[6] = { "", "FATAL", "MAJOR", "MINOR", "INFO", "VERBOSE" };
printf("[GLWEBTOOLS_%s] ", level <= 5 ? LEVEL_STR[level] : LEVEL_STR[5]);
vprintf(format, arguments);
printf("\n");
#endif
#endif
}
}
}