Skip to content

Commit

Permalink
implement logging info/errors to file
Browse files Browse the repository at this point in the history
  • Loading branch information
p2r3 committed Dec 2, 2024
1 parent a9fe1a7 commit 3a3ded9
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 66 deletions.
3 changes: 3 additions & 0 deletions globals.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <filesystem>
#include <fstream>
#include "globals.h"

// Points to the system-specific designated cache directory
Expand All @@ -18,6 +19,8 @@ const std::filesystem::path APP_DIR = std::filesystem::path(std::getenv("APPDATA

// Points to last known Portal 2 game files directory
std::filesystem::path GAME_DIR;
// Points to info/error log file (set during runtime)
std::ofstream LOGFILE;

// Points to the external repository file
const std::filesystem::path REPO_PATH = APP_DIR / "repositories.txt";
Expand Down
2 changes: 2 additions & 0 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLOBALS_H

#include <filesystem>
#include <fstream>

// Comment this line to target Linux, uncomment to target Windows
// #define TARGET_WINDOWS
Expand All @@ -10,6 +11,7 @@ extern std::filesystem::path CACHE_DIR;
extern bool CACHE_ENABLE;
extern const std::filesystem::path APP_DIR;
extern std::filesystem::path GAME_DIR;
extern std::ofstream LOGFILE;
extern const std::filesystem::path REPO_PATH;
extern int SPPLICE_INSTALL_STATE;
extern int SPPLICE_NETCON_PORT;
Expand Down
14 changes: 8 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void checkCacheOverride (const std::filesystem::path &configPath) {

std::ifstream configFile(configPath);
if (!configFile.is_open()) {
std::cerr << "Failed to open " << configPath << " for reading." << std::endl;
std::cerr << "[E] Failed to open " << configPath.c_str() << " for reading." << std::endl;
return;
}

Expand All @@ -105,7 +105,7 @@ void checkCacheOverride (const std::filesystem::path &configPath) {
configFile.close();

if (!std::filesystem::exists(customCacheDir)) {
std::cerr << "Invalid cache directory " << customCacheDir << "." << std::endl;
std::cerr << "[E] Invalid cache directory " << customCacheDir.c_str() << "." << std::endl;
return;
}

Expand All @@ -127,14 +127,16 @@ int main (int argc, char *argv[]) {
try { // Ensure CACHE_DIR exists
std::filesystem::create_directories(CACHE_DIR);
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Failed to create temporary directory " << CACHE_DIR << ": " << e.what() << std::endl;
std::cerr << "[E] Failed to create temporary directory " << CACHE_DIR.c_str() << ": " << e.what() << std::endl;
}

try { // Ensure APP_DIR exists
std::filesystem::create_directories(APP_DIR);
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Failed to create application directory " << APP_DIR << ": " << e.what() << std::endl;
std::cerr << "[E] Failed to create application directory " << APP_DIR.c_str() << ": " << e.what() << std::endl;
}
// Open the log file
LOGFILE = std::ofstream(APP_DIR / "log.txt");

// Set up high-DPI scaling
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
Expand Down Expand Up @@ -187,7 +189,7 @@ int main (int argc, char *argv[]) {
if (!CACHE_ENABLE) {
std::ofstream disableCacheFile(APP_DIR / "disable_cache");
if (!disableCacheFile.is_open()) {
std::cerr << "Failed to create disable_cache file." << std::endl;
LOGFILE << "[E] Failed to create disable_cache file." << std::endl;
}
} else {
std::filesystem::remove(APP_DIR / "disable_cache");
Expand Down Expand Up @@ -229,7 +231,7 @@ int main (int argc, char *argv[]) {
// Write the new cache directory to the config file
std::ofstream configFile(APP_DIR / "cache_dir.txt");
if (!configFile.is_open()) {
std::cerr << "Failed to open cache config file for writing." << std::endl;
LOGFILE << "[E] Failed to open cache config file for writing." << std::endl;
} else {
configFile << CACHE_DIR.string();
}
Expand Down
18 changes: 9 additions & 9 deletions tools/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ bool ToolsCURL::downloadFile (const std::string &url, const std::filesystem::pat
std::ofstream ofs(outputPath, std::ios::binary);

if (!ofs.is_open()) {
std::cerr << "Failed to open file for writing: " << outputPath << std::endl;
LOGFILE << "[E] Failed to open file for writing: " << outputPath.c_str() << std::endl;
return false;
}

// Initialize CURL
CURL *curl = curl_easy_init();

if (!curl) {
std::cerr << "Failed to initialize CURL" << std::endl;
LOGFILE << "[E] Failed to initialize CURL" << std::endl;
return false;
}

Expand All @@ -67,7 +67,7 @@ bool ToolsCURL::downloadFile (const std::string &url, const std::filesystem::pat
curl_easy_cleanup(curl);

if (response != CURLE_OK) {
std::cerr << "Failed to download file from \"" << url << "\": " << curl_easy_strerror(response) << std::endl;
LOGFILE << "[E] Failed to download file from \"" << url.c_str() << "\": " << curl_easy_strerror(response) << std::endl;
return false;
}

Expand All @@ -82,7 +82,7 @@ std::string ToolsCURL::downloadString (const std::string &url) {
CURL *curl = curl_easy_init();

if (!curl) {
std::cerr << "Failed to initialize CURL" << std::endl;
LOGFILE << "[E] Failed to initialize CURL" << std::endl;
return "";
}

Expand All @@ -99,7 +99,7 @@ std::string ToolsCURL::downloadString (const std::string &url) {

// Check for errors
if (response != CURLE_OK) {
std::cerr << "Failed to download string from \"" << url << "\": " << curl_easy_strerror(response) << std::endl;
LOGFILE << "[E] Failed to download string from \"" << url.c_str() << "\": " << curl_easy_strerror(response) << std::endl;
return "";
}

Expand All @@ -117,7 +117,7 @@ CURL* ToolsCURL::wsConnect (const std::string &url) {
CURL *curl = curl_easy_init();

if (!curl) {
std::cerr << "Failed to initialize CURL" << std::endl;
LOGFILE << "[E] Failed to initialize CURL" << std::endl;
return nullptr;
}

Expand All @@ -129,7 +129,7 @@ CURL* ToolsCURL::wsConnect (const std::string &url) {

// Check for errors
if (response != CURLE_OK) {
std::cerr << "Failed to connect to \"" << url << "\": " << curl_easy_strerror(response) << std::endl;
LOGFILE << "[E] Failed to connect to \"" << url.c_str() << "\": " << curl_easy_strerror(response) << std::endl;
return nullptr;
}

Expand Down Expand Up @@ -157,7 +157,7 @@ bool ToolsCURL::wsSend (CURL *curl, const std::string &message) {

// Log errors, return false if response not OK
if (response != CURLE_OK) {
std::cerr << "Failed to send message to WebSocket: " << curl_easy_strerror(response) << std::endl;
LOGFILE << "[E] Failed to send message to WebSocket: " << curl_easy_strerror(response) << std::endl;
return false;
}

Expand All @@ -180,7 +180,7 @@ std::string ToolsCURL::wsReceive (CURL *curl, size_t size) {

// Log errors, return empty string if response not OK
if (response != CURLE_OK) {
std::cerr << "Failed to receive message from WebSocket: " << curl_easy_strerror(response) << std::endl;
LOGFILE << "[E] Failed to receive message from WebSocket: " << curl_easy_strerror(response) << std::endl;
return "";
}

Expand Down
46 changes: 23 additions & 23 deletions tools/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool ToolsInstall::extractLocalFile (const std::filesystem::path path, const std
archive_write_disk_set_options(extracted, ARCHIVE_EXTRACT_TIME);

if ((r = archive_read_open_filename_w(archive, path.wstring().c_str(), 10240))) {
std::cerr << "Could not open file: " << archive_error_string(archive) << std::endl;
LOGFILE << "[E] Could not open file: " << archive_error_string(archive) << std::endl;
return false;
}

Expand All @@ -60,7 +60,7 @@ bool ToolsInstall::extractLocalFile (const std::filesystem::path path, const std
std::filesystem::path full_path = dest / archive_entry_pathname(entry);
archive_entry_set_pathname_utf8(entry, full_path.string().c_str());

std::cout << "Extracting: " << archive_entry_pathname(entry) << std::endl;
LOGFILE << "[I] Extracting: " << archive_entry_pathname(entry) << std::endl;
archive_write_header(extracted, entry);

const void* buff;
Expand All @@ -71,13 +71,13 @@ bool ToolsInstall::extractLocalFile (const std::filesystem::path path, const std
r = archive_read_data_block(archive, &buff, &size, &offset);
if (r == ARCHIVE_EOF) break;
if (r != ARCHIVE_OK) {
std::cerr << "Archive read error: " << archive_error_string(archive) << std::endl;
LOGFILE << "[E] Archive read error: " << archive_error_string(archive) << std::endl;
output = false;
break;
}
r = archive_write_data_block(extracted, buff, size, offset);
if (r != ARCHIVE_OK) {
std::cerr << "Archive write error: " << archive_error_string(extracted) << std::endl;
LOGFILE << "[E] Archive write error: " << archive_error_string(extracted) << std::endl;
output = false;
break;
}
Expand Down Expand Up @@ -183,13 +183,13 @@ bool startPortal2 (const std::vector<std::string> extraArgs) {

std::string steamPath = ToolsInstall::getProcessPath("steam");
if (steamPath.length() == 0) {
std::cerr << "Failed to find Steam process path. Is Steam running?" << std::endl;
LOGFILE << "[E] Failed to find Steam process path. Is Steam running?" << std::endl;
return false;
}

pid_t pid = fork();
if (pid == -1) {
std::cerr << "Failed to fork process." << std::endl;
LOGFILE << "[E] Failed to fork process." << std::endl;
return false;
}

Expand Down Expand Up @@ -218,7 +218,7 @@ bool startPortal2 (const std::vector<std::string> extraArgs) {
execv(steamPath.c_str(), const_cast<char* const*>(args.data()));

// execv only returns on error
std::cerr << "Failed to call Steam binary from fork." << std::endl;
LOGFILE << "[E] Failed to call Steam binary from fork." << std::endl;

// If the above failed, revert to using the Steam browser protocol
std::string command = "xdg-open steam://run/620//-tempcontent";
Expand All @@ -229,7 +229,7 @@ bool startPortal2 (const std::vector<std::string> extraArgs) {
}

if (system(command.c_str()) != 0) {
std::cerr << "Failed to open Steam URI." << std::endl;
LOGFILE << "[E] Failed to open Steam URI." << std::endl;
}

// Exit from the child process
Expand All @@ -245,7 +245,7 @@ bool startPortal2 (const std::vector<std::string> extraArgs) {

std::wstring steamPath = ToolsInstall::getProcessPath("steam.exe");
if (steamPath.length() == 0) {
std::cerr << "Failed to find Steam process path. Is Steam running?" << std::endl;
LOGFILE << "[E] Failed to find Steam process path. Is Steam running?" << std::endl;
return false;
}

Expand Down Expand Up @@ -301,7 +301,7 @@ bool startPortal2 (const std::vector<std::string> extraArgs) {
bool linkDirectory (const std::filesystem::path target, const std::filesystem::path linkName) {

if (symlink(target.c_str(), linkName.c_str()) != 0) {
std::cerr << "Failed to create symbolic link " << target << " -> " << linkName << std::endl;
LOGFILE << "[E] Failed to create symbolic link " << target.c_str() << " -> " << linkName.c_str() << std::endl;
return false;
}
return true;
Expand Down Expand Up @@ -334,32 +334,32 @@ bool linkDirectory (const std::filesystem::path target, const std::filesystem::p
wcscat(szTarget, L"\\");

if (!CreateDirectoryW(szJunction, NULL)) {
std::cerr << "Failed to create directory for junction " << linkName << ": " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to create directory for junction " << linkName.c_str() << ": " << GetLastError() << std::endl;
return false;
}

// Obtain SE_RESTORE_NAME privilege (required for opening a directory)
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tp;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken)) {
std::cerr << "Failed to open process token: " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to open process token: " << GetLastError() << std::endl;
return false;
}
if (!LookupPrivilegeValue(NULL, SE_RESTORE_NAME, &tp.Privileges[0].Luid)) {
std::cerr << "Failed to look up SE_RESTORE_NAME privilege: " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to look up SE_RESTORE_NAME privilege: " << GetLastError() << std::endl;
return false;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) {
std::cerr << "Failed to adjust process privileges: " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to adjust process privileges: " << GetLastError() << std::endl;
return false;
}
if (hToken) CloseHandle(hToken);

HANDLE hDir = CreateFileW(szJunction, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (hDir == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create junction file " << linkName << ": " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to create junction file " << linkName.c_str() << ": " << GetLastError() << std::endl;
return false;
}

Expand All @@ -380,7 +380,7 @@ bool linkDirectory (const std::filesystem::path target, const std::filesystem::p
CloseHandle(hDir);
RemoveDirectoryW(szJunction);

std::cerr << "Failed to create reparse point " << target << " -> " << linkName << ": " << dr << std::endl;
LOGFILE << "[E] Failed to create reparse point " << target.c_str() << " -> " << linkName.c_str() << ": " << dr << std::endl;
return false;
}

Expand All @@ -395,7 +395,7 @@ bool linkDirectory (const std::filesystem::path target, const std::filesystem::p
bool linkFile (const std::filesystem::path target, const std::filesystem::path linkName) {

if (symlink(target.c_str(), linkName.c_str()) != 0) {
std::cerr << "Failed to create symbolic link " << target << " -> " << linkName << std::endl;
LOGFILE << "[E] Failed to create symbolic link " << target.c_str() << " -> " << linkName.c_str() << std::endl;
return false;
}
return true;
Expand All @@ -412,7 +412,7 @@ bool linkFile (const std::filesystem::path target, const std::filesystem::path l
BOOL result = CreateHardLinkW(linkNameWStr.c_str(), targetWStr.c_str(), NULL);

if (result == 0) {
std::cerr << "Failed to create hard link " << target << " -> " << linkName << ": " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to create hard link " << target.c_str() << " -> " << linkName.c_str() << ": " << GetLastError() << std::endl;
return false;
}
return true;
Expand All @@ -425,7 +425,7 @@ bool linkFile (const std::filesystem::path target, const std::filesystem::path l
bool unlinkDirectory (const std::filesystem::path target) {

if (unlink(target.c_str()) != 0) {
std::cerr << "Failed to remove symbolic link " << target << std::endl;
LOGFILE << "[E] Failed to remove symbolic link " << target.c_str() << std::endl;
return false;
}
return true;
Expand All @@ -446,7 +446,7 @@ bool unlinkDirectory (const std::filesystem::path target) {
if (success) {
return true;
} else {
std::cerr << "Failed to remove junction " << target << ": " << GetLastError() << std::endl;
LOGFILE << "[E] Failed to remove junction " << target.c_str() << ": " << GetLastError() << std::endl;
return false;
}

Expand All @@ -458,7 +458,7 @@ bool isDirectoryLink (const std::filesystem::path linkName) {

struct stat path_stat;
if (lstat(linkName.c_str(), &path_stat) != 0) {
std::cerr << "Failed to stat path " << linkName << std::endl;
LOGFILE << "[E] Failed to stat path " << linkName.c_str() << std::endl;
return false;
}
return S_ISLNK(path_stat.st_mode);
Expand All @@ -470,7 +470,7 @@ bool isDirectoryLink (const std::filesystem::path linkName) {
DWORD attributes = GetFileAttributesW(linkName.wstring().c_str());

if (attributes == INVALID_FILE_ATTRIBUTES) {
std::cerr << "Failed to get file attributes for " << linkName << std::endl;
LOGFILE << "[E] Failed to get file attributes for " << linkName.c_str() << std::endl;
return false;
}

Expand Down Expand Up @@ -534,7 +534,7 @@ std::string ToolsInstall::installPackageFile (const std::filesystem::path packag
#endif

GAME_DIR = std::filesystem::path(gameProcessPath).parent_path();
std::cout << "Found Portal 2 at " << GAME_DIR << std::endl;
LOGFILE << "[I] Found Portal 2 at " << GAME_DIR.c_str() << std::endl;

std::filesystem::path tempcontentPath = GAME_DIR / "portal2_tempcontent";

Expand Down
Loading

0 comments on commit 3a3ded9

Please sign in to comment.