2022-06-16 21:33:18 +00:00
|
|
|
#include "logger.hpp"
|
|
|
|
#include "helpers.hpp"
|
|
|
|
#include "nn/result.h"
|
|
|
|
|
2022-06-26 03:30:38 +00:00
|
|
|
// If connection fails, try X ports above the specified one
|
|
|
|
// Useful for debugging multple clients on the same machine
|
|
|
|
constexpr u32 ADDITIONAL_LOG_PORT_COUNT = 2;
|
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
Logger* Logger::sInstance = nullptr;
|
|
|
|
|
|
|
|
void Logger::createInstance() {
|
2022-06-20 06:56:37 +00:00
|
|
|
#ifdef SERVERIP
|
2022-06-16 21:33:18 +00:00
|
|
|
sInstance = new Logger(TOSTRING(SERVERIP), 3080, "MainLogger");
|
2022-06-20 06:56:37 +00:00
|
|
|
#else
|
2022-06-16 21:33:18 +00:00
|
|
|
sInstance = new Logger(0, 3080, "MainLogger");
|
2022-06-20 06:56:37 +00:00
|
|
|
#endif
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nn::Result Logger::init(const char* ip, u16 port) {
|
2022-06-19 14:10:20 +00:00
|
|
|
|
2024-05-15 20:27:53 +00:00
|
|
|
strcpy(this->sock_ip, ip);
|
2022-06-26 03:30:38 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
this->port = port;
|
2022-06-26 03:30:38 +00:00
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
in_addr hostAddress = { 0 };
|
|
|
|
sockaddr serverAddress = { 0 };
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (this->socket_log_state != SOCKET_LOG_UNINITIALIZED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
nn::nifm::Initialize();
|
|
|
|
nn::nifm::SubmitNetworkRequest();
|
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
while (nn::nifm::IsNetworkRequestOnHold()) { }
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
// emulators make this return false always, so skip it during init
|
|
|
|
#ifndef EMU
|
2022-06-16 21:33:18 +00:00
|
|
|
|
|
|
|
if (!nn::nifm::IsNetworkAvailable()) {
|
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
#endif
|
2022-06-26 03:30:38 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
if ((this->socket_log_socket = nn::socket::Socket(2, 1, 0)) < 0) {
|
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return -1;
|
|
|
|
}
|
2022-06-26 03:30:38 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
nn::socket::InetAton(this->sock_ip, &hostAddress);
|
|
|
|
|
|
|
|
serverAddress.address = hostAddress;
|
|
|
|
serverAddress.port = nn::socket::InetHtons(this->port);
|
|
|
|
serverAddress.family = 2;
|
|
|
|
|
|
|
|
nn::Result result;
|
2022-06-26 03:30:38 +00:00
|
|
|
bool connected = false;
|
|
|
|
for (u32 i = 0; i < ADDITIONAL_LOG_PORT_COUNT + 1; ++i) {
|
|
|
|
result = nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress));
|
|
|
|
if (result.isSuccess()) {
|
|
|
|
connected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this->port++;
|
|
|
|
serverAddress.port = nn::socket::InetHtons(this->port);
|
|
|
|
}
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-06-26 03:30:38 +00:00
|
|
|
if (connected) {
|
|
|
|
this->socket_log_state = SOCKET_LOG_CONNECTED;
|
|
|
|
this->isDisableName = false;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2022-06-16 21:33:18 +00:00
|
|
|
this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
void Logger::log(const char *fmt, va_list args) { // impl for replacing seads system::print
|
2022-06-16 21:33:18 +00:00
|
|
|
if (!sInstance)
|
|
|
|
return;
|
|
|
|
char buf[0x500];
|
|
|
|
if (nn::util::VSNPrintf(buf, sizeof(buf), fmt, args) > 0) {
|
|
|
|
sInstance->socket_log(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
s32 Logger::read(char *out) {
|
2022-06-16 21:33:18 +00:00
|
|
|
return this->socket_read_char(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::log(const char* fmt, ...) {
|
|
|
|
if (!sInstance)
|
|
|
|
return;
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
char buf[0x500];
|
2022-06-16 21:33:18 +00:00
|
|
|
|
2022-06-20 06:56:37 +00:00
|
|
|
if (nn::util::VSNPrintf(buf, sizeof(buf), fmt, args) > 0) {
|
|
|
|
if (!sInstance->isDisableName) {
|
|
|
|
char prefix[0x510];
|
|
|
|
nn::util::SNPrintf(prefix, sizeof(prefix), "[%s] %s", sInstance->sockName, buf);
|
|
|
|
sInstance->socket_log(prefix);
|
|
|
|
} else {
|
|
|
|
sInstance->socket_log(buf);
|
|
|
|
}
|
|
|
|
}
|
2022-06-19 14:10:20 +00:00
|
|
|
|
2022-06-16 21:33:18 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Logger::pingSocket() {
|
2022-10-30 01:21:58 +00:00
|
|
|
return socket_log("ping") > 0; // if value is greater than zero, than the socket received our message, otherwise the connection was lost.
|
2022-06-16 21:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tryInitSocket() {
|
|
|
|
__asm("STR X20, [X8,#0x18]");
|
2022-08-10 21:11:52 +00:00
|
|
|
#if DEBUGLOG
|
2022-06-16 21:33:18 +00:00
|
|
|
Logger::createInstance(); // creates a static instance for debug logger
|
2022-06-20 06:56:37 +00:00
|
|
|
#endif
|
2022-10-30 01:21:58 +00:00
|
|
|
}
|