mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-09-11 05:38:31 +09:00
[Feat] (All): Implement init, log and other related components.
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#include "../../Includes.h"
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_Util::Debug {
|
||||
static FILE* s_logFile = nullptr;
|
||||
static std::mutex s_mutex;
|
||||
|
||||
constexpr char* GetOSName() {
|
||||
#if defined(_WIN32)
|
||||
return "Windows";
|
||||
#elif defined(__ANDROID__)
|
||||
return "Android";
|
||||
#elif defined(__APPLE__)
|
||||
return "macOS";
|
||||
#elif defined(__linux__)
|
||||
return "Linux";
|
||||
#else
|
||||
return "UnknownOS";
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string GetThreadName() {
|
||||
char buffer[64] = { 0 };
|
||||
#if defined(_WIN32)
|
||||
PWSTR desc = nullptr;
|
||||
if (SUCCEEDED(GetThreadDescription(GetCurrentThread(), &desc))) {
|
||||
WideCharToMultiByte(CP_UTF8, 0, desc, -1, buffer, sizeof(buffer), nullptr, nullptr);
|
||||
LocalFree(desc);
|
||||
}
|
||||
#elif defined(__ANDROID__) || defined(__linux__)
|
||||
pthread_getname_np(pthread_self(), buffer, sizeof(buffer));
|
||||
#elif defined(__APPLE__)
|
||||
pthread_getname_np(buffer, sizeof(buffer));
|
||||
#endif
|
||||
return buffer[0] ? buffer : "UnknownThread";
|
||||
}
|
||||
|
||||
std::string GetCurrentTime() {
|
||||
using namespace std::chrono;
|
||||
auto now = system_clock::now();
|
||||
std::time_t tt = system_clock::to_time_t(now);
|
||||
struct tm tm {};
|
||||
#if defined(_WIN32)
|
||||
localtime_s(&tm, &tt);
|
||||
#else
|
||||
localtime_r(&tt, &tm);
|
||||
#endif
|
||||
char buf[16];
|
||||
std::strftime(buf, sizeof(buf), "%H:%M:%S", &tm);
|
||||
return buf;
|
||||
}
|
||||
|
||||
void InitFile() {
|
||||
#if MOBILEGL_LOG_ENABLE_FILE
|
||||
if (!s_logFile && MOBILEGL_LOG_FILE_PATH && *MOBILEGL_LOG_FILE_PATH) {
|
||||
s_logFile = std::fopen(MOBILEGL_LOG_FILE_PATH, "w");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void WriteToFile(const char* msg) {
|
||||
#if MOBILEGL_LOG_ENABLE_FILE
|
||||
if (!s_logFile) InitFile();
|
||||
if (s_logFile) {
|
||||
std::fputs(msg, s_logFile);
|
||||
std::fflush(s_logFile);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void Log(const char* levelTag, android_LogPriority androidLogLevel, const char* fmt, ...) {
|
||||
std::lock_guard<std::mutex> lock(s_mutex);
|
||||
|
||||
std::string header = "[" + GetCurrentTime() + "] [" + GetOSName() + " " + GetThreadName() + "/" + levelTag + "]: ";
|
||||
|
||||
char buffer[1024];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
int n = std::vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||
std::string out = header + std::string(buffer, n) + "\n";
|
||||
|
||||
#if MOBILEGL_LOG_ENABLE_CONSOLE
|
||||
std::fwrite(out.c_str(), 1, out.size(), stdout);
|
||||
#endif
|
||||
|
||||
#if MOBILEGL_LOG_ENABLE_ANDROID && defined(__ANDROID__)
|
||||
__android_log_print(androidLogLevel, "MobileGL", "%s", out.c_str());
|
||||
#endif
|
||||
|
||||
WriteToFile(out.c_str());
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void Close() {
|
||||
#if MOBILEGL_LOG_ENABLE_FILE
|
||||
if (s_logFile) {
|
||||
std::fclose(s_logFile);
|
||||
s_logFile = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#define MOBILEGL_LOG_LEVEL_DEBUG 0
|
||||
#define MOBILEGL_LOG_LEVEL_WARN 1
|
||||
#define MOBILEGL_LOG_LEVEL_ERROR 2
|
||||
#define MOBILEGL_LOG_LEVEL_INFO 3
|
||||
#define MOBILEGL_LOG_LEVEL_FATAL 4
|
||||
|
||||
#define MOBILEGL_LOG_INTERNAL(levelTag, androidLogLevel, fmt, ...) \
|
||||
do { \
|
||||
MobileGL::MG_Util::Debug::Log(levelTag, androidLogLevel, fmt, ##__VA_ARGS__); \
|
||||
} while(0)
|
||||
|
||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_DEBUG
|
||||
# define MGLOG_D(fmt, ...) MOBILEGL_LOG_INTERNAL("DEBUG", ANDROID_LOG_DEBUG, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define MGLOG_D(fmt, ...) { }
|
||||
#endif
|
||||
|
||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_INFO
|
||||
# define MGLOG_I(fmt, ...) MOBILEGL_LOG_INTERNAL("INFO", ANDROID_LOG_INFO, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define MGLOG_I(fmt, ...) { }
|
||||
#endif
|
||||
|
||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_WARN
|
||||
# define MGLOG_W(fmt, ...) MOBILEGL_LOG_INTERNAL("WARN", ANDROID_LOG_WARN, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define MGLOG_W(fmt, ...) { }
|
||||
#endif
|
||||
|
||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_ERROR
|
||||
# define MGLOG_E(fmt, ...) MOBILEGL_LOG_INTERNAL("ERROR", ANDROID_LOG_ERROR, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define MGLOG_E(fmt, ...) { }
|
||||
#endif
|
||||
|
||||
#if MOBILEGL_LOG_ACTIVE_LEVEL <= MOBILEGL_LOG_LEVEL_FATAL
|
||||
# define MGLOG_F(fmt, ...) MOBILEGL_LOG_INTERNAL("FATAL", ANDROID_LOG_FATAL, fmt, ##__VA_ARGS__)
|
||||
#else
|
||||
# define MGLOG_F(fmt, ...) { }
|
||||
#endif
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_Util {
|
||||
namespace Debug {
|
||||
void Log(const char* levelTag, android_LogPriority androidLogLevel, const char* fmt, ...);
|
||||
void InitFile();
|
||||
void WriteToFile(const char* msg);
|
||||
std::string GetCurrentTime();
|
||||
constexpr char* GetOSName();
|
||||
std::string GetThreadName();
|
||||
void Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user