[Feat] (All): Implement init, log and other related components.

This commit is contained in:
BZLZHH
2025-07-11 18:09:45 +08:00
parent 0eed6f17a2
commit b76c23429f
6 changed files with 279 additions and 2 deletions
+4 -2
View File
@@ -15,8 +15,10 @@ set(PROFILING OFF)
find_library(GLSLANG_LIB glslang PATHS ${CMAKE_SOURCE_DIR}/libraries/arm64-v8a/)
add_library(${CMAKE_PROJECT_NAME} SHARED
)
MobileGL/Init.cpp
MobileGL/MG_Util/Debug/Log.cpp
"MobileGL/Includes.h" "MobileGL/MG_Util/Debug/Log.cpp" "MobileGL/MG_Util/Debug/Log.h" "MobileGL/Config.h")
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/library/arm64-v8a/libglslang.a
+13
View File
@@ -0,0 +1,13 @@
#pragma once
#define MOBILEGL_LOG_ACTIVE_LEVEL MOBILEGL_LOG_LEVEL_DEBUG
#define MOBILEGL_LOG_ENABLE_CONSOLE 0
#define MOBILEGL_LOG_ENABLE_FILE 1
#define MOBILEGL_LOG_ENABLE_ANDROID 1
#ifdef __ANDROID__
#define MOBILEGL_LOG_FILE_PATH "/sdcard/MG/mobilegl_log.txt"
#elif
#define MOBILEGL_LOG_FILE_PATH ""
#endif
+60
View File
@@ -0,0 +1,60 @@
#pragma once
#ifdef __ANDROID__
#define __ANDROID_API__ 26 // fix pthread_getname_np not defined
#endif
#include <cstring>
#include <iostream>
#include <cstdio>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <map>
#include <vector>
#include <cctype>
#include <stdexcept>
#include <atomic>
#include <regex>
#include <strstream>
#include <algorithm>
#include <array>
#include <random>
#include <optional>
#include <unordered_map>
#include <queue>
#include <format>
#include <memory>
#include <glslang/Public/ShaderLang.h>
#include <glslang/Include/Types.h>
#include <glslang/Public/ShaderLang.h>
#include <spirv_cross/spirv_cross_c.h>
#include <glslang/SPIRV/GlslangToSpv.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <ankerl/unordered_dense.h>
#include <GLES3/gl32.h>
#ifdef __ANDROID__
#include <android/log.h>
#include <pthread.h>
#include <vulkan/vulkan_android.h>
#include <android/native_window.h>
#include <dlfcn.h>
#include <unistd.h>
#include <vulkan/vulkan.h>
#elif defined(_WIN32)
#include <Windows.h>
#include <windows.h>
#include <processthreadsapi.h>
#else
#include <pthread.h>
#endif
#include <EGL/egl.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include "MG_Util/Debug/Log.h"
#include "Config.h"
+42
View File
@@ -0,0 +1,42 @@
#include "Includes.h"
namespace MobileGL {
void MG_Initialize() {
MG_Util::Debug::InitFile();
MGLOG_I("MobileGL Initializing...");
}
void MG_Destroy() {
MGLOG_I("MobileGL Closing...");
MG_Util::Debug::Close();
}
#if defined(__linux__) || defined(__APPLE__)
__attribute__((constructor)) static void AutoInit() {
MG_Initialize();
}
__attribute__((destructor)) static void AutoDestroy() {
MG_Destroy();
}
#endif
#ifdef _WIN32
BOOL WINAPI DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MG_Initialize();
break;
case DLL_PROCESS_DETACH:
MG_Destroy();
break;
}
return TRUE;
}
#endif
}
+104
View File
@@ -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
}
}
}
+56
View File
@@ -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();
}
}
}