Skip to content

Commit

Permalink
Rewrite logger to use static buffer and printf-style syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
IrneRacoonovich committed Jan 31, 2024
1 parent dcd6752 commit 6da34a6
Show file tree
Hide file tree
Showing 17 changed files with 216 additions and 223 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ NO_ICON := 1
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE

CFLAGS := -g -Wall -Os -ffunction-sections \
CFLAGS := -g -Wall -Werror=format -Os -ffunction-sections \
$(ARCH) $(DEFINES)

CFLAGS += $(INCLUDE) -D__SWITCH__ -DAPP_VERSION="\"$(APP_VERSION)\""
Expand Down
2 changes: 1 addition & 1 deletion lib/libtesla/include/tesla.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace tsl {

ss << std::hex << hexString.substr(5,2);
ss >> b;
// logMessage("rgb = " + std::to_string(r) + ", " + std::to_string(g) + ", " + std::to_string(b) + ", ");
// log("rgb = " + std::to_string(r) + ", " + std::to_string(g) + ", " + std::to_string(b) + ", ");
return tsl::Color(r, g, b, 0xF);
}
union {
Expand Down
2 changes: 1 addition & 1 deletion source/FanSliderOverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FanSliderOverlay : public tsl::Gui {
~FanSliderOverlay() {}

virtual tsl::elm::Element* createUI() override {
// logMessage ("FanSliderOverlay");
// log ("FanSliderOverlay");

size_t fifthSlashPos = filePath.find('/', filePath.find('/', filePath.find('/', filePath.find('/') + 1) + 1) + 1);
bool hasHelp = false;
Expand Down
2 changes: 1 addition & 1 deletion source/HelpOverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HelpOverlay : public tsl::Gui {
~HelpOverlay() {}

virtual tsl::elm::Element* createUI() override {
// logMessage ("HelpOverlay");
// log ("HelpOverlay");

std::pair<std::string, int> textDataPair;
constexpr int lineHeight = 20; // Adjust the line height as needed
Expand Down
4 changes: 2 additions & 2 deletions source/IniSection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ void updateIniData(IniSectionInput& iniData, const IniSectionInput& updates, boo
if (remove) {
for (const auto& [section, kvPairs] : updates) {
for (const auto& [key, value] : kvPairs) {
//logMessage(iniData[section][key]);
//log(iniData[section][key]);
iniData[section].erase(key);
}
}
} else {
for (const auto& [section, kvPairs] : updates) {
for (const auto& [key, value] : kvPairs) {
//logMessage(iniData[section][key]);
//log(iniData[section][key]);
iniData[section][key] = value;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/JsonInfoOverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class JsonInfoOverlay : public tsl::Gui {
~JsonInfoOverlay() {}

virtual tsl::elm::Element* createUI() override {
// logMessage ("JsonInfoOverlay");
// log ("JsonInfoOverlay");

std::pair<std::string, int> textDataPair;
constexpr int lineHeight = 20; // Adjust the line height as needed
Expand Down
2 changes: 1 addition & 1 deletion source/KipInfoOverlay.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KipInfoOverlay : public tsl::Gui {
~KipInfoOverlay() {}

virtual tsl::elm::Element* createUI() override {
// logMessage ("KipInfoOverlay");
// log ("KipInfoOverlay");

std::pair<std::string, int> textDataPair;
constexpr int lineHeight = 20; // Adjust the line height as needed
Expand Down
41 changes: 28 additions & 13 deletions source/debug_funcs.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
#pragma once
#include <algorithm>
#include <cstdarg>
#include <cstdio>
#include <time.h>
#include <chrono>

void logMessage(const std::string& message) {
std::time_t currentTime = std::time(nullptr);
std::string logEntry = std::asctime(std::localtime(&currentTime));
std::size_t lastNonNewline = logEntry.find_last_not_of("\r\n");
if (lastNonNewline != std::string::npos) {
logEntry.erase(lastNonNewline + 1);
}
logEntry = "[" + logEntry + "] " + message + "\n";
const char* logPath = "sdmc:/config/uberhand/log.txt";

char logTimeBuffer[std::size("yyyy-mm-dd hh:mm:ss")]{ 0 };

FILE* file = fopen("sdmc:/config/uberhand/log.txt", "a");
if (file != nullptr) {
fputs(logEntry.c_str(), file);
fclose(file);
__attribute__((__format__(__printf__, 1, 2)))
void log(const char* format, ...) noexcept {
FILE* logFile = fopen(logPath, "a");
if (logFile == nullptr) {
fprintf(stderr, "Failed to open log file: %s", logPath);
return;
}
}
u64 currentTime;
timeGetCurrentTime(TimeType_NetworkSystemClock, &currentTime);
tm* localTime = std::localtime((std::time_t*)(&currentTime));
std::strftime(logTimeBuffer, sizeof(logTimeBuffer), "%F %T", localTime);

fputc('[', logFile);
fputs(logTimeBuffer, logFile);
fputc(']', logFile);
fputc(' ', logFile);
va_list args;
va_start(args, format);
vfprintf(logFile, format, args);
va_end(args);
fputc('\n', logFile);
fclose(logFile);
}
24 changes: 12 additions & 12 deletions source/download_funcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination) {
std::string filename = url.substr(lastSlash + 1);
destination += filename;
} else {
logMessage(std::string("Invalid URL: ") + url);
log("Invalid URL: %s", url.c_str());
return false;
}
} else {
Expand All @@ -98,18 +98,18 @@ bool downloadFile(const std::string& url, const std::string& toDestination) {
} else {
// Failed initialization, increment retry count and try again
retryCount++;
logMessage("Error initializing curl. Retrying...");
log("Error initializing curl. Retrying...");
}
}
if (!curl) {
// Failed to initialize curl after multiple attempts
logMessage("Error initializing curl after multiple retries.");
log("Error initializing curl after multiple retries.");
return false;
}

FILE* file = fopen(destination.c_str(), "wb");
if (!file) {
logMessage(std::string("Error opening file: ") + destination);
log("Error opening file: %s", destination.c_str());
return false;
}

Expand All @@ -128,7 +128,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination) {

CURLcode result = curl_easy_perform(curl);
if (result != CURLE_OK) {
logMessage(std::string("Error downloading file: ") + curl_easy_strerror(result));
log("Error downloading file: %s", curl_easy_strerror(result));
curl_easy_cleanup(curl);
fclose(file);
// Delete the file if nothing was written to it
Expand All @@ -140,7 +140,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination) {
// Check if the file is empty
long fileSize = ftell(file);
if (fileSize == 0) {
logMessage(std::string("Error downloading file: Empty file"));
log("Error downloading file: Empty file");
std::remove(destination.c_str());
fclose(file);
return false;
Expand All @@ -153,7 +153,7 @@ bool downloadFile(const std::string& url, const std::string& toDestination) {
bool unzipFile(const std::string& zipFilePath, const std::string& toDestination) {
ZZIP_DIR* dir = zzip_dir_open(zipFilePath.c_str(), nullptr);
if (!dir) {
logMessage(std::string("Error opening zip file: ") + zipFilePath);
log("Error opening file: %s; error: %s", zipFilePath.c_str(), zzip_strerror_of(dir));
return false;
}

Expand All @@ -180,12 +180,12 @@ bool unzipFile(const std::string& zipFilePath, const std::string& toDestination)
}

if (isDirectory(directoryPath)) {
//logMessage("directoryPath: success");
//log("directoryPath: success");
} else {
logMessage("directoryPath: failure");
log("directoryPath: failure");
}

//logMessage(std::string("directoryPath: ") + directoryPath);
//log(std::string("directoryPath: ") + directoryPath);

ZZIP_FILE* file = zzip_file_open(dir, entry.d_name, 0);
if (file) {
Expand All @@ -201,13 +201,13 @@ bool unzipFile(const std::string& zipFilePath, const std::string& toDestination)

fclose(outputFile);
} else {
logMessage(std::string("Error opening output file: ") + extractedFilePath);
log("Error opening output file: %s", extractedFilePath.c_str());
success = false;
}

zzip_file_close(file);
} else {
logMessage(std::string("Error opening file in zip: ") + fileName);
log("Error opening file in zip: %s", entry.d_name);
success = false;
}
}
Expand Down
50 changes: 25 additions & 25 deletions source/get_funcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ std::vector<std::string> getFilesListByWildcard(const std::string& pathPattern)
dirPath = pathPattern + "/";
}

//logMessage("dirPath: " + dirPath);
//logMessage("wildcard: " + wildcard);
//log("dirPath: " + dirPath);
//log("wildcard: " + wildcard);

std::vector<std::string> fileList;

Expand All @@ -246,7 +246,7 @@ std::vector<std::string> getFilesListByWildcard(const std::string& pathPattern)
wildcard.resize(wildcard.size() - 1); // Remove the trailing slash
}

//logMessage("isFolderWildcard: " + std::to_string(isFolderWildcard));
//log("isFolderWildcard: " + std::to_string(isFolderWildcard));

DIR* dir = opendir(dirPath.c_str());
if (dir != nullptr) {
Expand All @@ -257,10 +257,10 @@ std::vector<std::string> getFilesListByWildcard(const std::string& pathPattern)

bool isEntryDirectory = isDirectory(entryPath);

//logMessage("entryName: " + entryName);
//logMessage("entryPath: " + entryPath);
//logMessage("isFolderWildcard: " + std::to_string(isFolderWildcard));
//logMessage("isEntryDirectory: " + std::to_string(isEntryDirectory));
//log("entryName: " + entryName);
//log("entryPath: " + entryPath);
//log("isFolderWildcard: " + std::to_string(isFolderWildcard));
//log("isEntryDirectory: " + std::to_string(isEntryDirectory));

if (isFolderWildcard && isEntryDirectory && fnmatch(wildcard.c_str(), entryName.c_str(), FNM_NOESCAPE) == 0) {
if (entryName != "." && entryName != "..") {
Expand Down Expand Up @@ -288,7 +288,7 @@ std::vector<std::string> getFilesListByWildcard(const std::string& pathPattern)
//for (const std::string& filePath : fileList) {
// fileListAsString += filePath + "\n";
//}
//logMessage("File List:\n" + fileListAsString);
//log("File List:\n" + fileListAsString);

return fileList;
}
Expand Down Expand Up @@ -460,35 +460,35 @@ std::vector<std::vector<std::string>> getModifyCommands(const std::vector<std::v
// }
//
// replacement = replaceJsonSourcePlaceholder(replacement, jsonPath, true);
// //logMessage2("replacement: "+replacement);
// //logMessage2("pre-arg: "+arg);
// //log2("replacement: "+replacement);
// //log2("pre-arg: "+arg);
// arg.replace(startPos, endPos - startPos + 2, replacement);
// }
} else if (usingJsonSource && (arg.find("{json_source(") != std::string::npos)) {
std::string countStr = file;

// logMessage(std::string("count: ")+countStr);
// logMessage(std::string("pre arg: ") + arg);
// log(std::string("count: ")+countStr);
// log(std::string("pre arg: ") + arg);
arg = replacePlaceholder(arg, "*", file);
// logMessage(std::string("post arg: ") + arg);
// log(std::string("post arg: ") + arg);


size_t startPos = arg.find("{json_source(");
size_t endPos = arg.find(")}");
if (endPos != std::string::npos && endPos > startPos) {
replacement = replaceJsonSourcePlaceholder(arg.substr(startPos, endPos - startPos + 2), jsonPath, "{json_source(");
//logMessage2("replacement: "+replacement);
//logMessage2("pre-arg: "+arg);
//log2("replacement: "+replacement);
//log2("pre-arg: "+arg);
arg.replace(startPos, endPos - startPos + 2, replacement);
//logMessage2("post-arg: "+arg);
//log2("post-arg: "+arg);
}
} else if (usingJsonSource && (arg.find("{json_mark_cur_kip(") != std::string::npos)) {
std::string countStr = file;

// logMessage(std::string("count: ")+countStr);
// logMessage(std::string("pre arg: ") + arg);
// log(std::string("count: ")+countStr);
// log(std::string("pre arg: ") + arg);
arg = replacePlaceholder(arg, "*", file);
// logMessage(std::string("post arg: ") + arg);
// log(std::string("post arg: ") + arg);


size_t startPos = arg.find("{json_mark_cur_kip(");
Expand All @@ -500,20 +500,20 @@ std::vector<std::vector<std::string>> getModifyCommands(const std::vector<std::v
} else if (usingJsonSource && (arg.find("{json_mark_cur_ini(") != std::string::npos)) {
std::string countStr = file;

// logMessage(std::string("count: ")+countStr);
// logMessage(std::string("pre arg: ") + arg);
// log(std::string("count: ")+countStr);
// log(std::string("pre arg: ") + arg);
arg = replacePlaceholder(arg, "*", file);
// logMessage(std::string("post arg: ") + arg);
// log(std::string("post arg: ") + arg);


size_t startPos = arg.find("{json_mark_cur_ini(");
size_t endPos = arg.find(")}");
if (endPos != std::string::npos && endPos > startPos) {
replacement = replaceJsonSourcePlaceholder(arg.substr(startPos, endPos - startPos + 2), jsonPath, "{json_mark_cur_ini(");
//logMessage2("replacement: "+replacement);
//logMessage2("pre-arg: "+arg);
//log2("replacement: "+replacement);
//log2("pre-arg: "+arg);
arg.replace(startPos, endPos - startPos + 2, replacement);
//logMessage2("post-arg: "+arg);
//log2("post-arg: "+arg);
}
}
}
Expand Down
Loading

0 comments on commit 6da34a6

Please sign in to comment.