Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom logging on 3DS and Switch #3184

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 64 additions & 16 deletions src/platform/3ds/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
*/

#include <3ds.h>
#include <3dslink.h>
#include <cstdio>

#include "player.h"
#include <string>
#include <sys/stat.h>
#include <unistd.h>
#include <malloc.h>
#include <string>
#include <vector>
#include "lcf/scope_guard.h"
#include "output.h"

/* 8 MB required for booting and need extra linear memory for the sound
Expand All @@ -35,7 +36,52 @@ u32 __ctru_linear_heap_size = 12*1024*1024;
namespace {
u32 old_time_limit;
int n3dslinkSocket = -1;
u32* n3dsSocBuf = nullptr;
bool is_emu = false;

constexpr int SOC_BUFFERSIZE = 0x100000;
}

static void start3DSLink() {
// only do socket stuff, if sent by 3dslink
if (!__3dslink_host.s_addr) return;

n3dsSocBuf = (u32*)memalign(0x1000, SOC_BUFFERSIZE);
if(n3dsSocBuf == nullptr) {
printf("failed to allocate SOC buffer\n");
return;
}
auto buff_sg = lcf::makeScopeGuard([&]() {
free(n3dsSocBuf);
n3dsSocBuf = nullptr;
});

int ret = socInit(n3dsSocBuf, SOC_BUFFERSIZE);
if (ret != 0) {
printf("socInit failed: 0x%08X\n", (unsigned int)ret);
return;
}
auto serv_sg = lcf::makeScopeGuard([&]() {
socExit();
});

n3dslinkSocket = link3dsStdio();
if(n3dslinkSocket < 0) {
printf("failed to link stdio\n");
return;
}

serv_sg.Dismiss();
buff_sg.Dismiss();
}

static void stop3DSLink() {
if (n3dslinkSocket < 0) return;

close(n3dslinkSocket);
n3dslinkSocket = -1;
socExit();
free(n3dsSocBuf);
}

static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserData /* userdata */) {
Expand All @@ -54,13 +100,20 @@ static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserDat
want_log = true;
#endif
if (want_log) {
printf("%s: %s\n", prefix.c_str(), message.c_str());
printf("%s: %s\n", prefix.c_str(), msg.c_str());
}
}

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

gfxInitDefault();
#ifdef _DEBUG
consoleInit(GFX_BOTTOM, nullptr);
#endif
start3DSLink();
Output::SetLogCallback(LogCallback);

APT_GetAppCpuTimeLimit(&old_time_limit);
APT_SetAppCpuTimeLimit(30);

Expand All @@ -71,13 +124,6 @@ int main(int argc, char* argv[]) {
osSetSpeedupEnable(true);
}

gfxInitDefault();
n3dslinkSocket = link3dsStdio();
#ifdef _DEBUG
consoleInit(GFX_BOTTOM, nullptr);
#endif
Output::SetCustomMsgOut(LogMessage);

// cia/citra
is_emu = !envIsHomebrew();
bool is_cia = argc == 0;
Expand Down Expand Up @@ -113,6 +159,14 @@ int main(int argc, char* argv[]) {
} else if(is_cia) {
// No RomFS -> load games from hardcoded path
ctr_dir = tmp_path;
Output::Debug("CIA: Using hard-coded path.");
} else if(args[0].rfind("3dslink:/", 0) == 0) {
// Check if a game directory was provided, otherwise use default
if (std::none_of(args.cbegin(), args.cend(),
[](const std::string& a) { return a == "--project-path"; })) {
ctr_dir = tmp_path;
Output::Debug("3dslink: Using hard-coded path.");
}
}
// otherwise uses cwd by default or 3dslink argument
if (!ctr_dir.empty()) {
Expand All @@ -125,13 +179,7 @@ int main(int argc, char* argv[]) {
Player::Run();

romfsExit();

// Close debug log
if (n3dslinkSocket >= 0) {
close(n3dslinkSocket);
n3dslinkSocket = -1;
}

stop3DSLink();
gfxExit();

if (old_time_limit != UINT32_MAX) {
Expand Down
7 changes: 4 additions & 3 deletions src/platform/switch/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ namespace {
}

static void LogCallback(LogLevel lvl, std::string const& msg, LogCallbackUserData /* userdata */) {
std::string prefix = Output::LogLevelToString(lvl);

if (!is_nro) {
std::string m = std::string("[" GAME_TITLE "] ") +
Output::LogLevelToString(lvl) + ": " + msg;
std::string m = std::string("[" GAME_TITLE "] ") + prefix + ": " + msg;

// HLE in yuzu emulator
svcOutputDebugString(m.c_str(), m.length());
}

// additional to nxlink server
if(nxlinkSocket >= 0) {
printf("%s: %s\n", prefix.c_str(), message.c_str());
printf("%s: %s\n", prefix.c_str(), msg.c_str());
}
}

Expand Down