Logging cleanup (#69)

* Mac OS X 10.6 & More C++03 Support

* Fix SDL2 options.txt loading for C++03

* Output/Logging Overhaul
* Added StandardOut class
* Renamed LOGX macros to LOG_X
* Removed LogMsg macros in favor of LOG_X
* Added console window for debug Windows builds

* Updated Xcode Project
+ StandardOut.hpp
+ StandardOut.cpp

* StandardOut_windows
* Replaced the Windows #ifdefs in StandardOut with StandardOut_windows

---------

Co-authored-by: Brent Da Mage <BrentDaMage@users.noreply.github.com>
This commit is contained in:
Brent
2023-08-28 02:55:41 -05:00
committed by GitHub
parent f7915a1dab
commit 5c1ea03747
43 changed files with 326 additions and 207 deletions

View File

@@ -0,0 +1,50 @@
#include <iostream>
#include <stdarg.h>
#include "StandardOut.hpp"
#include "Util.hpp"
StandardOut* StandardOut::m_singleton = nullptr;
StandardOut* const StandardOut::singleton()
{
return m_singleton;
}
StandardOut::StandardOut()
{
// Stick with the first output handle we get
if (!m_singleton)
m_singleton = this;
}
StandardOut::~StandardOut()
{
if (m_singleton == this)
m_singleton = nullptr;
}
void StandardOut::print(const char* const str)
{
std::cout << str << std::endl;
}
void StandardOut::print(std::string str)
{
print(str.c_str());
}
void StandardOut::vprintf(const char* const fmt, va_list argPtr)
{
print(Util::vformat(fmt, argPtr));
}
void StandardOut::printf(const char* const fmt, ...)
{
va_list argList;
va_start(argList, fmt);
vprintf(fmt, argList);
va_end(argList);
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include <string>
class StandardOut
{
private:
static StandardOut* m_singleton;
public:
static StandardOut* const singleton();
StandardOut();
~StandardOut();
virtual void print(const char* const str);
virtual void print(std::string str);
virtual void vprintf(const char* const fmt, va_list argPtr);
virtual void printf(const char* const fmt, ...);
};
#ifdef _DEBUG
#define LOG(...) StandardOut::singleton()->printf(__VA_ARGS__)
#ifdef PLATFORM_ANDROID
#define LOG_I(...) __android_log_print(ANDROID_LOG_INFO, "MinecraftPE", __VA_ARGS__)
#define LOG_W(...) __android_log_print(ANDROID_LOG_WARN, "MinecraftPE", __VA_ARGS__)
#define LOG_E(...) __android_log_print(ANDROID_LOG_ERROR, "MinecraftPE", __VA_ARGS__)
#else
#define LOG_I(...) LOG("[Info]: " __VA_ARGS__)
#define LOG_W(...) LOG("[WARN]: " __VA_ARGS__)
#define LOG_E(...) LOG("[ERROR]: " __VA_ARGS__)
#endif
#else
#define LOG(...)
#define LOG_I(...)
#define LOG_W(...)
#define LOG_E(...)
#endif

View File

@@ -6,6 +6,7 @@
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#include <stdarg.h>
#include "Util.hpp"
std::string Util::stringTrim(const std::string& str, const std::string& filter, bool a4, bool a5)
@@ -43,3 +44,23 @@ std::string Util::stringTrim(const std::string& str)
return stringTrim(str, " \t\n\r", true, true);
}
std::string Util::vformat(const char *fmt, va_list argPtr)
{
char str[1024];
vsnprintf(str, sizeof(str), fmt, argPtr);
return std::string(str);
}
std::string Util::format(const char *fmt, ...)
{
std::string str;
va_list argList;
va_start(argList, fmt);
str = vformat(fmt, argList);
va_end(argList);
return str;
}

View File

@@ -20,6 +20,9 @@ public:
static std::string stringTrim(const std::string &, const std::string &, bool, bool);
static std::string stringTrim(const std::string &);
static std::string vformat(const char* fmt, va_list argPtr);
static std::string format(const char* fmt, ...);
template<typename T>
static bool remove(std::vector<T>& vec, const T& t)
{

View File

@@ -83,10 +83,11 @@ void closedir(DIR* dir);
#endif
#include "compat/KeyCodes.hpp"
#include "../../compat/KeyCodes.hpp"
#include "StandardOut.hpp"
// options:
#include "GameMods.hpp"
#include "../../GameMods.hpp"
// don't know where to declare these:
@@ -612,17 +613,6 @@ uint8_t* ZlibDeflateToMemory(uint8_t* pInput, size_t sizeBytes, size_t *compress
uint8_t* ZlibDeflateToMemoryLvl(uint8_t* pInput, size_t sizeBytes, size_t* compressedSizeOut, int level);
// things that we added:
#ifndef ORIGINAL_CODE
void LogMsg(const char* fmt, ...); // not part of actual Minecraft (they use printf), but useful
void LogMsgNoCR(const char* fmt, ...); // not part of actual Minecraft (they use printf), but useful
#ifdef printf
#error "printf is defined. Why?"
#endif
#define printf LogMsgNoCR
#define puts(str) LogMsg("%s", str);
#ifdef _WIN32
@@ -636,30 +626,3 @@ void SetInstance(HINSTANCE hinst);
void SetHWND(HWND hwnd);
#endif
#else
#define LogMsg(...)
#define LogMsgNoCR(...)
#endif
#ifdef MC_DEBUG
#ifdef PLATFORM_ANDROID
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "MinecraftPE", __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, "MinecraftPE", __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "MinecraftPE", __VA_ARGS__)
#else
#define LOGI(...) printf("Info: " __VA_ARGS__)
#define LOGW(...) printf("WARN: " __VA_ARGS__)
#define LOGE(...) printf("ERROR: " __VA_ARGS__)
#endif
#else
#define LOGI(...) printf(__VA_ARGS__)
#define LOGW(...) printf(__VA_ARGS__)
#define LOGE(...) printf(__VA_ARGS__)
#endif