summaryrefslogtreecommitdiff
path: root/source/debug.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2023-01-09 15:11:53 +0000
committerIan C <ianc@noddybox.co.uk>2023-01-09 15:11:53 +0000
commit2df835d00591d085de589ddf4d40ea3edc8f3fac (patch)
tree01bce387296a3590a26eb0856a51f12f9f207242 /source/debug.c
parent5b9ffe70b622cee65ed5678163bee0e22fc0433d (diff)
Added support for debug logging over a socket.
Diffstat (limited to 'source/debug.c')
-rw-r--r--source/debug.c161
1 files changed, 161 insertions, 0 deletions
diff --git a/source/debug.c b/source/debug.c
new file mode 100644
index 0000000..f297238
--- /dev/null
+++ b/source/debug.c
@@ -0,0 +1,161 @@
+/*
+ 3dsspec - Nintendo 3DS Spectrum emulator
+
+ Copyright (C) 2021 Ian Cowburn <ianc@noddybox.co.uk>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>
+
+ -------------------------------------------------------------------------
+
+ Provides an interface for debug
+
+*/
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <3ds.h>
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+
+#include "debug.h"
+#include "gui.h"
+
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+
+/* ---------------------------------------- GLOBAL EXTERNALS
+*/
+int debug_enabled = FALSE;
+
+/* ---------------------------------------- PRIVATE DAYA
+*/
+static char debug_host[256];
+static char debug_port[32];
+static int sock = -1;
+
+/* ---------------------------------------- PRIVATE FUNCTIONS
+*/
+static void Error(const char *format, ...)
+{
+ static char buff[4096];
+ va_list va;
+
+ va_start(va, format);
+ vsnprintf(buff, 4096, format, va);
+ buff[4095] = 0;
+ va_end(va);
+
+ GUI_Alert(FALSE, buff);
+}
+
+static void OpenDebugConnection(void)
+{
+ if (sock == -1)
+ {
+ struct addrinfo *addr, *addr_p;
+ struct addrinfo hints = {0};
+
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+
+ if (getaddrinfo(debug_host, debug_port, &hints, &addr) == 0)
+ {
+ for(addr_p = addr; addr_p && sock == -1; addr_p = addr_p->ai_next)
+ {
+ sock = socket(addr_p->ai_family,
+ addr_p->ai_socktype,
+ addr_p->ai_protocol);
+
+ if (sock != -1)
+ {
+ if (connect(sock, addr_p->ai_addr, addr_p->ai_addrlen)
+ == -1)
+ {
+ Error("Failed to connect socket");
+ close(sock);
+ sock = -1;
+ }
+ }
+ else
+ {
+ Error("Failed to create socket(%d,%d,%d)\n%d %s",
+ addr_p->ai_family,
+ addr_p->ai_socktype,
+ addr_p->ai_protocol,
+ errno,
+ strerror(errno));
+ }
+ }
+ }
+ else
+ {
+ Error("Failed to getaddrinfo(%s,%s)", debug_host, debug_port);
+ }
+ }
+}
+
+/* ---------------------------------------- EXPORTED INTERFACES
+*/
+void DEBUG_SetAddress(const char *host, const char *port)
+{
+ strncpy(debug_host, host, (sizeof debug_host) - 1);
+ strncpy(debug_port, port, (sizeof debug_port) - 1);
+
+ debug_host[(sizeof debug_host) - 1] = 0;
+ debug_port[(sizeof debug_port) - 1] = 0;
+
+ if (sock != -1)
+ {
+ close(sock);
+ sock = -1;
+ }
+}
+
+
+void DEBUG_Output(const char *format, ...)
+{
+ static char buff[4096];
+
+ if (debug_enabled)
+ {
+ va_list va;
+
+ OpenDebugConnection();
+
+ va_start(va, format);
+ vsnprintf(buff, 4096, format, va);
+ buff[4095] = 0;
+ va_end(va);
+
+ if (sock != -1)
+ {
+ send(sock, buff, strlen(buff), 0);
+ }
+ }
+}
+
+
+/* END OF FILE */