From cdc8ed5d51d0ff1355df43acfa6ba61bbfd7ef86 Mon Sep 17 00:00:00 2001 From: cathery Date: Wed, 4 Dec 2019 20:29:53 +0300 Subject: [PATCH] Reimplement WirteToLog to grealy reduce filesize --- .../source/Controllers/XboxOneAdapter.cpp | 4 +- .../source/Controllers/XboxOneController.cpp | 2 +- source/log.c | 49 +++++++++++++++++++ source/log.h | 47 ++++-------------- source/main.cpp | 3 +- source/mainLoop.cpp | 14 +++--- 6 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 source/log.c diff --git a/ControllerUSB/source/Controllers/XboxOneAdapter.cpp b/ControllerUSB/source/Controllers/XboxOneAdapter.cpp index 3ce38e7..ccda7e9 100644 --- a/ControllerUSB/source/Controllers/XboxOneAdapter.cpp +++ b/ControllerUSB/source/Controllers/XboxOneAdapter.cpp @@ -126,7 +126,7 @@ Result XboxOneAdapter::SendInitBytes() ControlWrite(m_interface, MT_FCE_PDMA_GLOBAL_CONF, 0x44); ControlWrite(m_interface, MT_FCE_SKIP_FS, 0x03); - WriteToLog("firmware path: ", firmwarePath); + WriteToLog("firmware path: %s", firmwarePath); if (!firmwarePath || *firmwarePath == '\0') { WriteToLog("But the string is empty!"); @@ -145,7 +145,7 @@ Result XboxOneAdapter::SendInitBytes() fs.close(); - WriteToLog("writing ", firmware.size(), " bytes..."); + WriteToLog("writing %lu bytes...", firmware.size()); FwHeader *header = reinterpret_cast(firmware.data()); diff --git a/ControllerUSB/source/Controllers/XboxOneController.cpp b/ControllerUSB/source/Controllers/XboxOneController.cpp index b83dfdb..f909730 100644 --- a/ControllerUSB/source/Controllers/XboxOneController.cpp +++ b/ControllerUSB/source/Controllers/XboxOneController.cpp @@ -203,7 +203,7 @@ Result XboxOneController::SendInitBytes() if (R_FAILED(rc)) break; else - WriteToLog("Send a specific init packet ", i, " for controller v", vendor, " p", product); + WriteToLog("Sent a specific init packet %i for controller v%u p%u", i, vendor, product); } return rc; } diff --git a/source/log.c b/source/log.c new file mode 100644 index 0000000..a13b3a2 --- /dev/null +++ b/source/log.c @@ -0,0 +1,49 @@ +#include "switch.h" +#include "log.h" +#include "configFile.h" +#include +#include +#include + +static Mutex g_PrintMutex = 0; + +void WriteToLog(const char *fmt, ...) +{ + mutexLock(&g_PrintMutex); + +#ifdef __APPLET__ + va_list va; + va_start(va, fmt); + vprintf(fmt, va); + printf("\n"); + va_end(va); + +#else + + time_t unixTime = time(NULL); + struct tm *tStruct = localtime((const time_t *)&unixTime); + + FILE *fp = fopen(CONFIG_PATH "log.txt", "a"); + + //Print time + fprintf(fp, "%04i-%02i-%02i %02i:%02i:%02i: ", (tStruct->tm_year + 1900), tStruct->tm_mon, tStruct->tm_mday, tStruct->tm_hour, tStruct->tm_min, tStruct->tm_sec); + + //Print the actual text + va_list va; + va_start(va, fmt); + vfprintf(fp, fmt, va); + va_end(va); + + fprintf(fp, "\n"); + fclose(fp); +#endif + + mutexUnlock(&g_PrintMutex); +} + +void LockedUpdateConsole() +{ + mutexLock(&g_PrintMutex); + consoleUpdate(NULL); + mutexUnlock(&g_PrintMutex); +} \ No newline at end of file diff --git a/source/log.h b/source/log.h index 51124a2..647c0df 100644 --- a/source/log.h +++ b/source/log.h @@ -1,41 +1,14 @@ #pragma once -#include -#include -#include -#include "configFile.h" -template -void WriteToLog(T &&... text) +#ifdef __cplusplus +extern "C" { -#ifdef __APPLET__ - - std::stringstream ss; - ((ss << text), ...); - printf(ss.str().c_str()); - printf("\n"); - -#else - - using namespace std; - - time_t unixTime = time(NULL); - struct tm *time = localtime((const time_t *)&unixTime); - - fstream fs; - fs.open(CONFIG_PATH "log.txt", fstream::app); - - //Print time - fs << setfill('0'); - fs << setw(4) << (time->tm_year + 1900) - << "-" << setw(2) << time->tm_mon - << "-" << setw(2) << time->tm_mday - << " " << setw(2) << time->tm_hour - << ":" << setw(2) << time->tm_min - << ":" << setw(2) << time->tm_sec << ": "; - //Print the actual text - ((fs << text), ...); - fs << "\n"; - fs.close(); - #endif -} \ No newline at end of file + + void WriteToLog(const char *fmt, ...) __attribute__((format(printf, 1, 2))); + + void LockedUpdateConsole(); + +#ifdef __cplusplus +} +#endif diff --git a/source/main.cpp b/source/main.cpp index 4f9aaf4..a42a051 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -90,7 +90,8 @@ extern "C" void __libnx_exception_handler(ThreadExceptionDump *ctx) { - WriteToLog("Sysmodule crashed with error 0x", std::hex, ctx->error_desc); + WriteToLog("Sysmodule crashed with error 0x%x", ctx->error_desc); + LockedUpdateConsole(); } } diff --git a/source/mainLoop.cpp b/source/mainLoop.cpp index d724b19..1a85ec9 100644 --- a/source/mainLoop.cpp +++ b/source/mainLoop.cpp @@ -44,7 +44,7 @@ Result CallInitHandler(std::unique_ptr &controllerPtr) } else { - WriteToLog("Error creating interface with error ", rc); + WriteToLog("Error creating interface with error 0x%x", rc); return rc; } } @@ -192,8 +192,7 @@ Result mainLoop() const uint16_t dependencies[] = {PscPmModuleId_Usb}; rc = pscmGetPmModule(&pscModule, static_cast(126), dependencies, sizeof(dependencies) / sizeof(uint16_t), true); - WriteToLog("Get module result: 0x", std::hex, rc); - //Waiter pscModuleWaiter = waiterForEvent(&pscModule.event); + WriteToLog("Get module result: 0x%x", rc); bool pscLoopRunning = true; bool shouldSleep = false; @@ -210,8 +209,7 @@ Result mainLoop() rc = OpenEvents(); if (R_FAILED(rc)) - WriteToLog("Failed to open events: ", rc); - + WriteToLog("Failed to open events: 0x%x", rc); controllerInterfaces.reserve(10); while (appletMainLoop()) @@ -227,7 +225,7 @@ Result mainLoop() { u64 kHeld = hidKeysDown(static_cast(i)); if (kHeld != 0) - WriteToLog("Player ", i + 1, ": ", kHeld); + WriteToLog("Player %i: %lu", i + 1, kHeld); } if (kDown & KEY_B) @@ -350,7 +348,7 @@ Result mainLoop() if (!found_flag) { - WriteToLog("Erasing controller! ", (*it)->GetController()->GetType()); + WriteToLog("Erasing controller! %i", (*it)->GetController()->GetType()); controllerInterfaces.erase(it--); WriteToLog("Controller erased!"); } @@ -377,7 +375,7 @@ Result mainLoop() controllerInterfaces.clear(); } #ifdef __APPLET__ - consoleUpdate(nullptr); + LockedUpdateConsole(); #else svcSleepThread(1e+7L); #endif