From fa2d75af98fe8ea7f9fef8d82808578269e47359 Mon Sep 17 00:00:00 2001 From: XargonWan Date: Sat, 30 Nov 2024 10:00:20 +0900 Subject: [PATCH] Feat/logger (#9) * When RetroDECK is defined the logs are written in the RetroDECK format * LOG: writing to retrodeck.log if exists and read the log level from retrodeck.cfg * Added a script to build locally * LOG: def + include fixes * LOGS: fixed log folder --- developer_toolbox/build_locally.sh | 48 +------------------------- es-core/src/Log.cpp | 55 ++++++++++++++++++++++++++++++ es-core/src/Log.h | 4 +++ 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/developer_toolbox/build_locally.sh b/developer_toolbox/build_locally.sh index 00781c4e1..2eb08072c 100755 --- a/developer_toolbox/build_locally.sh +++ b/developer_toolbox/build_locally.sh @@ -2,50 +2,4 @@ # WARNING: run this script from the project root folder, not from here!! -# Check if script is running with elevated privileges -if [ "$EUID" -ne 0 ]; then - echo "The build might fail without some superuser permissions, please run me with sudo. Continue without sudo? [y/N]" - read -r continue_without_sudo - if [[ "$continue_without_sudo" != "y" ]]; then - exit 1 - fi -fi - -git submodule update --init --recursive - -export GITHUB_WORKSPACE="." -export FOLDER="es-de-build" - -chmod a+rwx -R "$FOLDER" - -# Initialize the Flatpak repo -ostree init --mode=archive-z2 --repo=${GITHUB_WORKSPACE}/retrodeck-repo - -automation_tools/update_es-de_manifest.sh - -cp net.retrodeck.es-de.appdata.xml net.retrodeck.es-de.appdata.xml.bak -cp net.retrodeck.es-de.yml net.retrodeck.es-de.yml.bak - -automation_tools/install_dependencies.sh -#automation_tools/cooker_build_id.sh -automation_tools/pre_build_automation.sh -#automation_tools/cooker_flatpak_portal_add.sh -# THIS SCRIPT IS BROKEN HENCE DISABLED FTM -# automation_tools/appdata_management.sh -#automation_tools/flatpak_build_download_only.sh -#automation_tools/flatpak_build_only.sh -#automation_tools/flatpak_build_bundle.sh - -flatpak-builder --user --force-clean \ - --install-deps-from=flathub \ - --install-deps-from=flathub-beta \ - --repo="${GITHUB_WORKSPACE}/retrodeck-repo" \ - "${GITHUB_WORKSPACE}/${FOLDER}" \ - net.retrodeck.es-de.yml - -flatpak build-bundle "${GITHUB_WORKSPACE}/retrodeck-repo" "$GITHUB_WORKSPACE/RetroDECK-ES-DE.flatpak" net.retrodeck.es-de - -rm -f net.retrodeck.es-de.appdata.xml -rm -f net.retrodeck.es-de.yml -cp net.retrodeck.es-de.appdata.xml.bak net.retrodeck.es-de.appdata.xml -cp net.retrodeck.es-de.yml.bak net.retrodeck.es-de.yml \ No newline at end of file +cmake -DRETRODECK=on -DCMAKE_INSTALL_PREFIX=/app . && make && make install \ No newline at end of file diff --git a/es-core/src/Log.cpp b/es-core/src/Log.cpp index 948973cde..cadaab9a8 100644 --- a/es-core/src/Log.cpp +++ b/es-core/src/Log.cpp @@ -11,6 +11,10 @@ #include "Settings.h" #include "utils/StringUtil.h" +#if defined(RETRODECK) +#include +#endif + LogLevel Log::getReportingLevel() { std::unique_lock lock {sLogMutex}; @@ -25,10 +29,25 @@ void Log::setReportingLevel(LogLevel level) void Log::init() { +#if defined(RETRODECK) + // Check for the rd_logs_folder environment variable + //const char* logFolder = std::getenv("rd_logs_folder"); + const char* logFolder = "/var/config/retrodeck/logs"; + if (logFolder && std::strlen(logFolder) > 0) + { + sLogPath = std::string(logFolder) + "/retrodeck.log"; + } + else + { + // Default to the existing location if rd_logs_folder is not defined + sLogPath = Utils::FileSystem::getAppDataDirectory() + "/retrodeck.log"; + } +#else if (Settings::getInstance()->getBool("LegacyAppDataDirectory")) sLogPath = Utils::FileSystem::getAppDataDirectory() + "/es_log.txt"; else sLogPath = Utils::FileSystem::getAppDataDirectory() + "/logs/es_log.txt"; +#endif Utils::FileSystem::removeFile(sLogPath + ".bak"); // Rename the previous log file. @@ -71,9 +90,20 @@ std::ostringstream& Log::get(LogLevel level) localtime_r(&t, &tm); #endif std::unique_lock lock {sLogMutex}; + +#if defined(RETRODECK) + // Convert log level to uppercase for RetroDECK + std::string levelUpper = mLogLevelMap[level]; + std::transform(levelUpper.begin(), levelUpper.end(), levelUpper.begin(), ::toupper); + + mOutStringStream << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") + << "] [" << levelUpper << "] [ES-DE] "; +#else mOutStringStream << std::put_time(&tm, "%b %d %H:%M:%S ") << mLogLevelMap[level] << (level == LogLevel::LogInfo || level == LogLevel::LogWarning ? ": " : ": "); +#endif + mMessageLevel = level; return mOutStringStream; @@ -123,4 +153,29 @@ Log::~Log() if (mMessageLevel == LogError || sReportingLevel >= LogDebug) std::cerr << mOutStringStream.str(); #endif + +#if defined(RETRODECK) + // Always write logs to the terminal as well when RetroDECK is defined + std::cout << mOutStringStream.str(); +#endif } + +// RetroDECK specific function +#if defined(RETRODECK) +void Log::setReportingLevelFromEnv() +{ + // Check for the logging_level environment variable + const char* logLevelEnv = std::getenv("logging_level"); + std::string logLevel = logLevelEnv ? logLevelEnv : "info"; + + // Map string to LogLevel + if (logLevel == "debug") + sReportingLevel = LogDebug; + else if (logLevel == "warning") + sReportingLevel = LogWarning; + else if (logLevel == "error") + sReportingLevel = LogError; + else + sReportingLevel = LogInfo; // Default is Info +} +#endif \ No newline at end of file diff --git a/es-core/src/Log.h b/es-core/src/Log.h index 586afe9bb..8c63b3122 100644 --- a/es-core/src/Log.h +++ b/es-core/src/Log.h @@ -53,6 +53,10 @@ public: static void flush(); static void close(); + #if defined(RETRODECK) + static void setReportingLevelFromEnv(); + #endif + protected: std::ostringstream mOutStringStream;