/* * Copyright (C) 2022-2024 Volodymyr Atamanenko * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ #include "utils/logger.h" #include #include #include #include #define COLOR_RED "\x1B[38;5;196m" #define COLOR_PINK "\x1B[38;5;212m" #define COLOR_ORANGE "\x1B[38;5;202m" #define COLOR_BLUE "\x1B[38;5;32m" #define COLOR_GREEN "\x1B[32m" #define COLOR_CYAN "\x1B[36m" #define COLOR_END "\033[0m" static SceKernelLwMutexWork _log_mutex; static atomic_bool _log_mutex_ready = ATOMIC_VAR_INIT(false); // Buffer A is used to adjust the format string. static char buffer_a[2048]; // Buffer B is used to compile the final log using the updated format string. static char buffer_b[2048]; void _log_print(int t, const char* fmt, ...) { #ifdef PERFORMANCE_BUILD /* Runtime informational/debug console output is expensive on Vita. */ if (t == LT_DEBUG || t == LT_INFO || t == LT_SUCCESS || t == LT_WAIT) return; #endif if (!atomic_load_explicit(&_log_mutex_ready, memory_order_relaxed)) { int ret = sceKernelCreateLwMutex(&_log_mutex, "log_lock", 0, 0, NULL); if (ret < 0) { sceClibPrintf("Error: failed to create log mutex: 0x%x\n", ret); return; } atomic_store_explicit(&_log_mutex_ready, true, memory_order_relaxed); } sceKernelLockLwMutex(&_log_mutex, 1, NULL); switch (t) { case LT_DEBUG: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s• debug%s %s\n", COLOR_PINK, COLOR_END, fmt); break; case LT_INFO: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %sℹ info%s %s\n", COLOR_BLUE, COLOR_END, fmt); break; case LT_WARN: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s⚠ warning%s %s\n", COLOR_ORANGE, COLOR_END, fmt); break; case LT_ERROR: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s⨯ error%s %s\n", COLOR_RED, COLOR_END, fmt); break; case LT_FATAL: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s! fatal%s %s\n", COLOR_RED, COLOR_END, fmt); break; case LT_SUCCESS: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s! success%s %s\n", COLOR_GREEN, COLOR_END, fmt); break; case LT_WAIT: sceClibSnprintf(buffer_a, sizeof(buffer_a), " %s… waiting%s %s\n", COLOR_CYAN, COLOR_END, fmt); break; default: if (atomic_load_explicit(&_log_mutex_ready, memory_order_relaxed)) { sceKernelUnlockLwMutex(&_log_mutex, 1); } return; } va_list list; va_start(list, fmt); sceClibVsnprintf(buffer_b, sizeof(buffer_b), buffer_a, list); va_end(list); sceClibPrintf(buffer_b); if (atomic_load_explicit(&_log_mutex_ready, memory_order_relaxed)) { sceKernelUnlockLwMutex(&_log_mutex, 1); } }