Merge pull request #1 from fruityloops1/dev

remove fixed buffer size from logger (+ run clang-format)
This commit is contained in:
CraftyBoss 2022-06-19 19:56:19 -07:00 committed by GitHub
commit 52c3345633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 31 deletions

View File

@ -1,25 +1,26 @@
#include "logger.hpp" #include "logger.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "nn/result.h" #include "nn/result.h"
#include "nn/util.h"
#include "nx/svc.h"
Logger* Logger::sInstance = nullptr; Logger* Logger::sInstance = nullptr;
void Logger::createInstance() { void Logger::createInstance() {
#ifdef SERVERIP #ifdef SERVERIP
sInstance = new Logger(TOSTRING(SERVERIP), 3080, "MainLogger"); sInstance = new Logger(TOSTRING(SERVERIP), 3080, "MainLogger");
#else #else
sInstance = new Logger(0, 3080, "MainLogger"); sInstance = new Logger(0, 3080, "MainLogger");
#endif #endif
} }
nn::Result Logger::init(const char* ip, u16 port) { nn::Result Logger::init(const char* ip, u16 port) {
sock_ip = ip; sock_ip = ip;
this->port = port; this->port = port;
in_addr hostAddress = { 0 }; in_addr hostAddress = {0};
sockaddr serverAddress = { 0 }; sockaddr serverAddress = {0};
if (this->socket_log_state != SOCKET_LOG_UNINITIALIZED) if (this->socket_log_state != SOCKET_LOG_UNINITIALIZED)
return -1; return -1;
@ -27,23 +28,24 @@ nn::Result Logger::init(const char* ip, u16 port) {
nn::nifm::Initialize(); nn::nifm::Initialize();
nn::nifm::SubmitNetworkRequest(); nn::nifm::SubmitNetworkRequest();
while (nn::nifm::IsNetworkRequestOnHold()) { } while (nn::nifm::IsNetworkRequestOnHold()) {
}
// emulators make this return false always, so skip it during init // emulators make this return false always, so skip it during init
#ifndef EMU #ifndef EMU
if (!nn::nifm::IsNetworkAvailable()) { if (!nn::nifm::IsNetworkAvailable()) {
this->socket_log_state = SOCKET_LOG_UNAVAILABLE; this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
return -1; return -1;
} }
#endif #endif
if ((this->socket_log_socket = nn::socket::Socket(2, 1, 0)) < 0) { if ((this->socket_log_socket = nn::socket::Socket(2, 1, 0)) < 0) {
this->socket_log_state = SOCKET_LOG_UNAVAILABLE; this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
return -1; return -1;
} }
nn::socket::InetAton(this->sock_ip, &hostAddress); nn::socket::InetAton(this->sock_ip, &hostAddress);
serverAddress.address = hostAddress; serverAddress.address = hostAddress;
@ -52,11 +54,13 @@ nn::Result Logger::init(const char* ip, u16 port) {
nn::Result result; nn::Result result;
if ((result = nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress))).isFailure()) { if ((result =
nn::socket::Connect(this->socket_log_socket, &serverAddress, sizeof(serverAddress)))
.isFailure()) {
this->socket_log_state = SOCKET_LOG_UNAVAILABLE; this->socket_log_state = SOCKET_LOG_UNAVAILABLE;
return result; return result;
} }
this->socket_log_state = SOCKET_LOG_CONNECTED; this->socket_log_state = SOCKET_LOG_CONNECTED;
this->isDisableName = false; this->isDisableName = false;
@ -64,7 +68,7 @@ nn::Result Logger::init(const char* ip, u16 port) {
return 0; return 0;
} }
void Logger::log(const char *fmt, va_list args) { // impl for replacing seads system::print void Logger::log(const char* fmt, va_list args) { // impl for replacing seads system::print
if (!sInstance) if (!sInstance)
return; return;
char buf[0x500]; char buf[0x500];
@ -73,7 +77,7 @@ void Logger::log(const char *fmt, va_list args) { // impl for replacing seads sy
} }
} }
s32 Logger::read(char *out) { s32 Logger::read(char* out) {
return this->socket_read_char(out); return this->socket_read_char(out);
} }
@ -83,29 +87,35 @@ void Logger::log(const char* fmt, ...) {
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
char buf[0x500]; size_t bufSize = nn::util::VSNPrintf(nullptr, 0, fmt, args);
size_t prefixSize = bufSize += 3 /* "[] " */ + strlen(sInstance->sockName);
if (!sInstance->isDisableName)
bufSize += prefixSize;
char buf[bufSize];
if (nn::util::VSNPrintf(buf, sizeof(buf), fmt, args) > 0) { if (sInstance->isDisableName)
if (!sInstance->isDisableName) { nn::util::VSNPrintf(buf, bufSize, fmt, args);
char prefix[0x510]; else {
nn::util::SNPrintf(prefix, sizeof(prefix), "[%s] %s", sInstance->sockName, buf); nn::util::VSNPrintf(buf + prefixSize, bufSize - prefixSize, fmt, args);
sInstance->socket_log(prefix); nn::util::SNPrintf(buf, prefixSize, "[%s] ", sInstance->sockName);
} else {
sInstance->socket_log(buf);
}
} }
sInstance->socket_log(buf);
#ifdef EMU
svcOutputDebugString(buf, bufSize);
#endif
va_end(args); va_end(args);
} }
bool Logger::pingSocket() { bool Logger::pingSocket() {
return socket_log("ping") > 0; // if value is greater than zero, than the socket recieved our message, otherwise the connection was lost. return socket_log("ping") > 0; // if value is greater than zero, than the socket recieved our
// message, otherwise the connection was lost.
} }
void tryInitSocket() { void tryInitSocket() {
__asm("STR X20, [X8,#0x18]"); __asm("STR X20, [X8,#0x18]");
#ifdef DEBUGLOG #ifdef DEBUGLOG
Logger::createInstance(); // creates a static instance for debug logger Logger::createInstance(); // creates a static instance for debug logger
#endif #endif
} }