foxygit / fake-08-xbox360 Log in
commit 88944bab6750f225cef5172be43f2562316bee45
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 18 08:16:48 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 18 08:16:48 2020 -0600

    Add logger
---
 Makefile          |  2 +-
 source/logger.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 source/logger.h   | 25 +++++++++++++++++++++++++
 source/main.cpp   |  6 ++++++
 4 files changed, 82 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 87beee4..b2d1a47 100644
--- a/Makefile
+++ b/Makefile
@@ -52,7 +52,7 @@ CFLAGS	:=	-g -Wall -O2 -mword-relocations \

 CFLAGS	+=	$(INCLUDE) -DARM11 -D_3DS

-CXXFLAGS	:= $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
+CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17

 ASFLAGS	:=	-g $(ARCH)
 LDFLAGS	=	-specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
diff --git a/source/logger.cpp b/source/logger.cpp
new file mode 100644
index 0000000..51946c7
--- /dev/null
+++ b/source/logger.cpp
@@ -0,0 +1,50 @@
+#include <strings.h>
+#include <stdarg.h>
+
+#include "logger.h"
+
+
+void Logger::Initialize()
+{
+    Logger::m_file = freopen("pico.log", "w", stderr);
+    Logger::m_enabled = true;
+}
+
+/*
+** {Function Name}:{Line}
+** {Resolved printf stuff}
+** {Newline}
+** {Start of next Output}
+*/
+void Logger::LogOutput(const char * func, size_t line, const char * format, ...)
+{
+    if (!m_enabled || !m_file)
+        return;
+
+    va_list args;
+    va_start(args, format);
+
+    fprintf(m_file, "%s:%zu:\n", func, line);
+    vfprintf(m_file, format, args);
+    fprintf(m_file, "\n\n");
+
+    fflush(m_file);
+}
+
+void Logger::Write(const char * msg)
+{
+    if (!m_enabled || !m_file)
+        return;
+
+    fprintf(m_file, msg);
+
+    fflush(m_file);
+}
+
+void Logger::Exit()
+{
+    if (!m_enabled || !m_file)
+        return;
+
+    fclose(m_file);
+}
diff --git a/source/logger.h b/source/logger.h
new file mode 100644
index 0000000..77ba716
--- /dev/null
+++ b/source/logger.h
@@ -0,0 +1,25 @@
+/*
+LovePotion logger
+** logger.h
+** @brief   : Logs shit when enabled
+*/
+
+#pragma once
+
+#include <stdio.h>
+
+namespace Logger
+{
+    void Initialize();
+
+    void Exit();
+
+    void LogOutput(const char * func, size_t line, const char * format, ...);
+
+    void Write(const char * msg);
+
+    inline FILE * m_file = nullptr;
+    inline bool m_enabled = false;
+}
+
+#define LOG(format, ...) Logger::LogOutput(__PRETTY_FUNCTION__, __LINE__, format, ## __VA_ARGS__)
\ No newline at end of file
diff --git a/source/main.cpp b/source/main.cpp
index c82c062..b58be59 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -10,6 +10,7 @@
 #include "graphics.h"
 #include "fakecart.h"
 #include "console.h"
+#include "logger.h"

 //this has the macro _TEST defined in it
 #include "tests/test_base.h"
@@ -17,6 +18,8 @@
 int main(int argc, char* argv[])
 {
 	int frames = 0;
+	Logger::Initialize();
+	Logger::Write("created Logger\n");
 	gfxInitDefault();

 	Console console;
@@ -69,6 +72,9 @@ int main(int argc, char* argv[])
     	frames++;
 	}

+	Logger::Write("Exiting\n");
+
+	Logger::Exit();
 	gfxExit();
 	return 0;
 }