Skip to content

Commit

Permalink
first part of refactoring to the BasicHome/BasicLogger/HomeDirManager…
Browse files Browse the repository at this point in the history
… classes, got a decent speed boost on the hot loop out of it too!
  • Loading branch information
coornio committed Aug 22, 2024
1 parent 4446f9c commit 73ed93e
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 140 deletions.
18 changes: 10 additions & 8 deletions src/Assistants/BasicHome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ bool BasicHome::showErrorBox(
);
}

BasicHome::BasicHome(const char* path) {
char* platformHome{ SDL_GetPrefPath(nullptr, path) };
BasicHome::BasicHome(const std::string_view homeName) {
auto* path{ SDL_GetPrefPath(nullptr, homeName.data()) };

if (!platformHome) {
if (!path) {
throw PathException("Failed to get platform home directory!", "");
}

homeDir = platformHome;
SDL_free(reinterpret_cast<void*>(platformHome));
mHomeDirectory.assign(path);
SDL_free(path);

std::filesystem::create_directories(homeDir);
if (!std::filesystem::exists(homeDir)) {
throw PathException("Cannot create home directory: ", homeDir);
namespace fs = std::filesystem;

fs::create_directories(mHomeDirectory);
if (!fs::exists(mHomeDirectory) || !fs::is_directory(mHomeDirectory)) {
throw PathException("Cannot create home directory: ", mHomeDirectory);
}
}
7 changes: 4 additions & 3 deletions src/Assistants/BasicHome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#pragma once

#include <filesystem>
#include <string_view>

class BasicHome {
std::filesystem::path homeDir{};
std::filesystem::path mHomeDirectory{};

protected:
static bool showErrorBox(std::string_view, std::string_view);
const std::filesystem::path& getHome() const noexcept { return homeDir; }
const auto& getHome() const noexcept { return mHomeDirectory; }

BasicHome(const char*);
BasicHome(const std::string_view);
};
79 changes: 27 additions & 52 deletions src/Assistants/BasicLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <iostream>
#include <sstream>
#include <fstream>

#include "BasicLogger.hpp"
Expand All @@ -18,24 +19,27 @@ BasicLogger& blogger::blog{ BasicLogger::create() };

void BasicLogger::createDirectory(
const std::string& filename,
const std::filesystem::path& directory,
std::filesystem::path& logFilePath
) const {
if (filename.empty() || directory.empty())
const std::filesystem::path& directory
) {
if (filename.empty() || directory.empty()) {
throw PathException("The log file must have a path/name!", "");
}

std::filesystem::create_directories(directory);
namespace fs = std::filesystem;

if (!std::filesystem::exists(directory))
fs::create_directories(directory);
if (!fs::exists(directory) || !fs::is_directory(directory)) {
throw PathException("Unable to create directory at: ", directory);
}

// append file.ext to directory, save to path
logFilePath = directory / filename;
mLogPath = directory / filename;

// attempt to delete file if it exists already
if (std::filesystem::exists(logFilePath)) {
if (!std::filesystem::remove(logFilePath))
throw PathException("Unable to clear old log file: ", logFilePath);
if (fs::exists(mLogPath)) {
if (!fs::remove_all(mLogPath)) {
throw PathException("Unable to clear old log file: ", mLogPath);
}
}
}

Expand All @@ -44,19 +48,7 @@ void BasicLogger::setStdLogFile(
const std::filesystem::path& path
) {
try {
createDirectory(name, path, stdLogPath);
} catch (const std::exception& e) {
std::cerr << ":: ERROR :: " << e.what() << std::endl;
throw;
}
}

void BasicLogger::setDbgLogFile(
const std::string& name,
const std::filesystem::path& path
) {
try {
createDirectory(name, path, dbgLogPath);
createDirectory(name, path);
} catch (const std::exception& e) {
std::cerr << ":: ERROR :: " << e.what() << std::endl;
throw;
Expand All @@ -65,38 +57,21 @@ void BasicLogger::setDbgLogFile(

/*==================================================================*/

void BasicLogger::writeLogFile(
const std::string& message,
const std::filesystem::path& logFilePath,
std::size_t& counter
) const {
if (std::filesystem::exists(logFilePath)) {
if (!std::filesystem::is_regular_file(logFilePath)) {
throw PathException("Log file is malformed: ", logFilePath);
}
}
void BasicLogger::newEntry(const BLOG type, const std::string& message) noexcept {
static std::size_t lineCount;

std::cout << ++counter << " :: " << message << std::endl;
std::ofstream logFile(logFilePath, std::ios::app);
if (!logFile.is_open()) {
throw PathException("Unable to access log file: ", logFilePath);
}
logFile << counter << " :: " << message << std::endl;
}
std::ostringstream output;
output << ++lineCount << " :: " << getSeverity(type) << " :: " << message << std::endl;

void BasicLogger::stdLogOut(const std::string& text) {
try {
writeLogFile(text, stdLogPath, cStd);
} catch (const std::exception& e) {
std::cerr << ":: ERROR :: " << e.what() << std::endl;
}
}
std::cout << output.str();
if (lineCount % 10 == 0) { std::cout.flush(); }

void BasicLogger::dbgLogOut(const std::string& text) {
try {
writeLogFile(text, dbgLogPath, cDbg);
} catch (const std::exception& e) {
std::cerr << ":: ERROR :: " << e.what() << std::endl;
std::ofstream logFile(mLogPath, std::ios::app);
if (!logFile) {
std::cerr << "Unable to open log file: " << mLogPath << std::endl;
return;
} else {
logFile << output.str() << std::endl;
}
}

Expand Down
40 changes: 25 additions & 15 deletions src/Assistants/BasicLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <string>
#include <filesystem>

enum class BLOG { INFO, WARN, ERROR, DEBUG };

/*==================================================================*/
#pragma region BasicLogger Singleton Class
/*==================================================================*/
Expand All @@ -18,21 +20,31 @@ class BasicLogger final {
BasicLogger(const BasicLogger&) = delete;
BasicLogger& operator=(const BasicLogger&) = delete;

std::filesystem::path stdLogPath{};
std::filesystem::path dbgLogPath{};

std::size_t cStd{}, cDbg{};
std::filesystem::path mLogPath{};

void createDirectory(
const std::string&,
const std::filesystem::path&,
std::filesystem::path&
) const;
void writeLogFile(
const std::string&,
const std::filesystem::path&,
std::size_t&
) const;
const std::filesystem::path&
);

std::string_view getSeverity(BLOG type) const noexcept {
switch (type) {
case BLOG::INFO:
return "INFO";

case BLOG::WARN:
return "WARN";

case BLOG::ERROR:
return "ERROR";

case BLOG::DEBUG:
return "DEBUG";

default:
return "OTHER";
}
}

public:
static BasicLogger& create() {
Expand All @@ -41,10 +53,8 @@ class BasicLogger final {
}

void setStdLogFile(const std::string&, const std::filesystem::path&);
void setDbgLogFile(const std::string&, const std::filesystem::path&);

void stdLogOut(const std::string&);
void dbgLogOut(const std::string&);
void newEntry(const BLOG type, const std::string&) noexcept;
};

/*ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ*/
Expand Down
1 change: 1 addition & 0 deletions src/Assistants/PathExceptionClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include <filesystem>
#include <string_view>
#include <stdexcept>

struct PathException : public std::runtime_error {
Expand Down
8 changes: 4 additions & 4 deletions src/GuestClass/EmuCores/EmuCores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void EmuCores::setInterrupt(const Interrupt type) {
}

void EmuCores::operationError(std::string_view msg) {
blog.stdLogOut(msg.data());
blog.newEntry(BLOG::INFO, msg.data());
setInterrupt(Interrupt::ERROR);
}

Expand All @@ -48,20 +48,20 @@ std::string EmuCores::formatOpcode(const u32 HI, const u32 LO) const {
}

void EmuCores::instructionError(const u32 HI, const u32 LO) {
blog.stdLogOut("Error :: Unknown instruction: " + formatOpcode(HI, LO));
blog.newEntry(BLOG::INFO, "Unknown instruction: " + formatOpcode(HI, LO));
setInterrupt(Interrupt::ERROR);
}

void EmuCores::instructionErrorML(const u32 HI, const u32 LO) {
blog.stdLogOut("Error :: ML routines unsupported: " + formatOpcode(HI, LO));
blog.newEntry(BLOG::INFO, "ML routines unsupported: " + formatOpcode(HI, LO));
setInterrupt(Interrupt::ERROR);
}

bool EmuCores::copyGameToMemory(u8* dest, const u32 offset) {
std::basic_ifstream<char> ifs(HDM.path, std::ios::binary);
ifs.read(reinterpret_cast<char*>(dest + offset), HDM.size);
if (ifs.fail()) {
blog.stdLogOut("Failed to copy rom data to memory, aborting.");
blog.newEntry(BLOG::ERROR, "Failed to copy rom data to memory, aborting.");
return false;
} else {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/GuestClass/GameFileChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool GameFileChecker::validate(
const std::uint64_t size,
const std::string_view type,
const std::string_view sha1
) {
) noexcept {
static const std::unordered_map <std::string_view, GameFileType> sExtMap{
{".c2x", GameFileType::c2x},
{".c4x", GameFileType::c4x},
Expand Down
2 changes: 1 addition & 1 deletion src/GuestClass/GameFileChecker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@ class GameFileChecker final {
std::uint64_t size,
std::string_view type,
std::string_view sha1 = ""
);
) noexcept;
};
18 changes: 9 additions & 9 deletions src/GuestClass/GuestFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,15 @@ void MEGACORE::setInterrupt(const Interrupt type) {
}

void MEGACORE::triggerError(std::string_view msg) {
blog.stdLogOut(msg.data());
blog.newEntry(BLOG::INFO, msg.data());
setInterrupt(Interrupt::ERROR);
}

void MEGACORE::triggerOpcodeError(const u32 opcode) {
if (opcode & 0xF000) {
blog.stdLogOut("Error :: Unknown instruction detected: " + hexOpcode(opcode));
blog.newEntry(BLOG::INFO, "Unknown instruction detected: " + hexOpcode(opcode));
} else {
blog.stdLogOut("Error :: ML routines are unsupported: " + hexOpcode(opcode));
blog.newEntry(BLOG::INFO, "ML routines are unsupported: " + hexOpcode(opcode));
}
setInterrupt(Interrupt::ERROR);
}
Expand Down Expand Up @@ -598,7 +598,7 @@ bool MEGACORE::readPermRegs(const usz X) {

if (std::filesystem::exists(path)) {
if (!std::filesystem::is_regular_file(path)) {
blog.stdLogOut("SHA1 file is malformed: " + path.string());
blog.newEntry(BLOG::ERROR, "SHA1 file is malformed: " + path.string());
return true;
}

Expand All @@ -615,7 +615,7 @@ bool MEGACORE::readPermRegs(const usz X) {
std::fill_n(mRegisterV + totalBytes, X - totalBytes, u8());
}
} else {
blog.stdLogOut("Could not open SHA1 file to read: " + path.string());
blog.newEntry(BLOG::ERROR, "Could not open SHA1 file to read: " + path.string());
return true;
}
} else {
Expand All @@ -629,7 +629,7 @@ bool MEGACORE::writePermRegs(const usz X) {

if (std::filesystem::exists(path)) {
if (!std::filesystem::is_regular_file(path)) {
blog.stdLogOut("SHA1 file is malformed: " + path.string());
blog.newEntry(BLOG::ERROR, "SHA1 file is malformed: " + path.string());
return true;
}

Expand All @@ -644,7 +644,7 @@ bool MEGACORE::writePermRegs(const usz X) {
in.read(tempV, std::min<std::streamsize>(totalBytes, X));
in.close();
} else {
blog.stdLogOut("Could not open SHA1 file to read: " + path.string());
blog.newEntry(BLOG::ERROR, "Could not open SHA1 file to read: " + path.string());
return true;
}

Expand All @@ -655,7 +655,7 @@ bool MEGACORE::writePermRegs(const usz X) {
out.write(tempV, 16);
out.close();
} else {
blog.stdLogOut("Could not open SHA1 file to write: " + path.string());
blog.newEntry(BLOG::ERROR, "Could not open SHA1 file to write: " + path.string());
return true;
}
} else {
Expand All @@ -668,7 +668,7 @@ bool MEGACORE::writePermRegs(const usz X) {
}
out.close();
} else {
blog.stdLogOut("Could not open SHA1 file to write: " + path.string());
blog.newEntry(BLOG::ERROR, "Could not open SHA1 file to write: " + path.string());
return true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/GuestClass/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bool MEGACORE::setupMachine() {
initPlatform();
fontCopyToMemory();

blog.stdLogOut("Successfully initialized rom/platform.");
blog.newEntry(BLOG::INFO, "Successfully initialized rom/platform.");
return true;
}

Expand Down Expand Up @@ -98,7 +98,7 @@ bool MEGACORE::romTypeCheck() {
if (!romCopyToMemory(4'096, 0x200))
return false;
if (readMemory(0x200) != 0x12 || readMemory(0x201) != 0x60) {
blog.stdLogOut("Invalid TPD rom patch, aborting.");
blog.newEntry(BLOG::WARN, "Invalid TPD rom patch, aborting.");
return false;
}
initProgramParams(0x2C0, 30);
Expand Down Expand Up @@ -161,7 +161,7 @@ bool MEGACORE::romTypeCheck() {
break;

default:
blog.stdLogOut("Unknown rom type, we shouldn't be here!");
blog.newEntry(BLOG::WARN, "Unknown rom type, we shouldn't be here!");
return false;
}
return true;
Expand All @@ -173,7 +173,7 @@ bool MEGACORE::romCopyToMemory(const usz size, const usz offset) {
std::basic_ifstream<char> ifs(HDM.path, std::ios::binary);
ifs.read(reinterpret_cast<char*>(mMemoryBank.data() + offset), HDM.size);
if (ifs.fail()) {
blog.stdLogOut("Failed to copy rom data to memory, aborting.");
blog.newEntry(BLOG::ERROR, "Failed to copy rom data to memory, aborting.");
return false;
} else {
return true;
Expand Down
Loading

0 comments on commit 73ed93e

Please sign in to comment.