Skip to content

Commit

Permalink
Feat/logger (#9)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
XargonWan authored Nov 30, 2024
1 parent c562a21 commit fa2d75a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
48 changes: 1 addition & 47 deletions developer_toolbox/build_locally.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
cmake -DRETRODECK=on -DCMAKE_INSTALL_PREFIX=/app . && make && make install
55 changes: 55 additions & 0 deletions es-core/src/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include "Settings.h"
#include "utils/StringUtil.h"

#if defined(RETRODECK)
#include <algorithm>
#endif

LogLevel Log::getReportingLevel()
{
std::unique_lock<std::mutex> lock {sLogMutex};
Expand All @@ -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.
Expand Down Expand Up @@ -71,9 +90,20 @@ std::ostringstream& Log::get(LogLevel level)
localtime_r(&t, &tm);
#endif
std::unique_lock<std::mutex> 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;
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions es-core/src/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class Log
static void flush();
static void close();

#if defined(RETRODECK)
static void setReportingLevelFromEnv();
#endif

protected:
std::ostringstream mOutStringStream;

Expand Down

0 comments on commit fa2d75a

Please sign in to comment.