summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/console.cpp93
-rw-r--r--common/console.h42
2 files changed, 135 insertions, 0 deletions
diff --git a/common/console.cpp b/common/console.cpp
new file mode 100644
index 0000000..3429bbc
--- /dev/null
+++ b/common/console.cpp
@@ -0,0 +1,93 @@
+//
+// Debug Console
+//
+
+#include <stdarg.h>
+#include <stdio.h>
+#include "console.h"
+
+Console console;
+
+// ============================================================================
+
+Console::Console ()
+{
+ m_pWindowFunc = NULL;
+}
+
+Console::~Console ()
+{
+}
+
+// ============================================================================
+
+void Console::Print (LC_CONSOLE_LEVEL level, const char* format, ...)
+{
+ char text[512];
+ va_list args;
+
+ va_start (args, format);
+ vsprintf (text, format, args);
+ va_end (args);
+
+ InternalPrint (level, text);
+}
+
+void Console::PrintMisc (const char* format, ...)
+{
+ char text[512];
+ va_list args;
+
+ va_start (args, format);
+ vsprintf (text, format, args);
+ va_end (args);
+
+ InternalPrint (LC_CONSOLE_MISC, text);
+}
+
+void Console::PrintDebug (const char* format, ...)
+{
+ char text[512];
+ va_list args;
+
+ va_start (args, format);
+ vsprintf (text, format, args);
+ va_end (args);
+
+ InternalPrint (LC_CONSOLE_DEBUG, text);
+}
+
+void Console::PrintWarning (const char* format, ...)
+{
+ char text[512];
+ va_list args;
+
+ va_start (args, format);
+ vsprintf (text, format, args);
+ va_end (args);
+
+ InternalPrint (LC_CONSOLE_WARNING, text);
+}
+
+void Console::PrintError (const char* format, ...)
+{
+ char text[512];
+ va_list args;
+
+ va_start (args, format);
+ vsprintf (text, format, args);
+ va_end (args);
+
+ InternalPrint (LC_CONSOLE_ERROR, text);
+}
+
+void Console::InternalPrint (LC_CONSOLE_LEVEL level, const char* text)
+{
+#ifdef LC_DEBUG
+ if (level == LC_CONSOLE_DEBUG)
+ return;
+#endif
+
+ if (m_pWindowFunc)
+ (*m_pWindowFunc) (level, text, m_pWindowFuncData);
+}
diff --git a/common/console.h b/common/console.h
new file mode 100644
index 0000000..d3aac8a
--- /dev/null
+++ b/common/console.h
@@ -0,0 +1,42 @@
+#ifndef _CONSOLE_H_
+#define _CONSOLE_H_
+
+typedef enum
+{
+ LC_CONSOLE_ERROR,
+ LC_CONSOLE_WARNING,
+ LC_CONSOLE_DEBUG,
+ LC_CONSOLE_MISC
+} LC_CONSOLE_LEVEL;
+
+typedef void (*CONSOLECALLBACK) (LC_CONSOLE_LEVEL level, const char* text, void* user_data);
+
+class Console
+{
+public:
+ Console ();
+ virtual ~Console ();
+
+ void Print (LC_CONSOLE_LEVEL level, const char* format, ...);
+ void PrintMisc (const char* format, ...);
+ void PrintDebug (const char* format, ...);
+ void PrintWarning (const char* format, ...);
+ void PrintError (const char* format, ...);
+
+ void SetWindowCallback (CONSOLECALLBACK func, void* data)
+ { m_pWindowFunc = func; m_pWindowFuncData = data; };
+
+protected:
+ void InternalPrint (LC_CONSOLE_LEVEL level, const char* text);
+
+ CONSOLECALLBACK m_pWindowFunc;
+ void* m_pWindowFuncData;
+
+ // variables
+ bool use_tty;
+ bool use_file;
+};
+
+extern Console console;
+
+#endif // _CONSOLE_H_