1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-05 10:48:46 +00:00

Reimplement WirteToLog to grealy reduce filesize

This commit is contained in:
cathery 2019-12-04 20:29:53 +03:00
parent ac6eff74bc
commit cdc8ed5d51
6 changed files with 70 additions and 49 deletions

View File

@ -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<FwHeader *>(firmware.data());

View File

@ -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;
}

49
source/log.c Normal file
View File

@ -0,0 +1,49 @@
#include "switch.h"
#include "log.h"
#include "configFile.h"
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
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);
}

View File

@ -1,41 +1,14 @@
#pragma once
#include <iomanip>
#include <fstream>
#include <iostream>
#include "configFile.h"
template <typename... T>
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
}
void WriteToLog(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
void LockedUpdateConsole();
#ifdef __cplusplus
}
#endif

View File

@ -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();
}
}

View File

@ -44,7 +44,7 @@ Result CallInitHandler(std::unique_ptr<IController> &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<PscPmModuleId>(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<HidControllerID>(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