summaryrefslogtreecommitdiff
path: root/Client/Source/Debug/Log.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Client/Source/Debug/Log.cpp')
-rw-r--r--Client/Source/Debug/Log.cpp164
1 files changed, 164 insertions, 0 deletions
diff --git a/Client/Source/Debug/Log.cpp b/Client/Source/Debug/Log.cpp
new file mode 100644
index 0000000..1b0ddf4
--- /dev/null
+++ b/Client/Source/Debug/Log.cpp
@@ -0,0 +1,164 @@
+#include "../Threading/Mutex.h"
+#include "log.h"
+#include <iostream>
+#include <ctime>
+#include <unordered_set>
+#include <stdarg.h>
+
+using namespace std;
+
+#ifdef RAGDOLL_WIN
+#include <windows.h>
+static HANDLE s_ConsoleHandle = 0;
+#endif
+
+unordered_set<string> s_OpenTags;
+
+Mutex s_Mutex;
+
+#ifdef RAGDOLL_DEBUG
+// https://stackoverflow.com/questions/997946/how-to-get-current-time-and-date-in-c
+// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
+const std::string currentDateTime() {
+ time_t now = time(0);
+ struct tm tstruct;
+ char buf[80];
+ tstruct = *localtime(&now);
+ // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
+ // for more information about date/time format
+ strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct);
+
+ return buf;
+}
+
+static void SetOutputColor(int i) {
+ if (s_ConsoleHandle == 0) {
+ s_ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
+ }
+ if (i == 0) {
+ SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
+ }
+ else if(i == 1) {
+ SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
+ }
+ else {
+ SetConsoleTextAttribute(s_ConsoleHandle, FOREGROUND_RED | FOREGROUND_INTENSITY);
+ }
+}
+
+void log_error_null_param(const char* funcName, const char* param)
+{
+ log_error("Null parameter in %s called %s", funcName, param);
+}
+
+void log_open_tag(const char* tag)
+{
+ s_OpenTags.insert(tag);
+}
+
+void log_info(const char* fmt, ...)
+{
+ Lock(s_Mutex) {
+ SetOutputColor(0);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Info] ", currentDateTime().c_str());
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+void log_warning(const char* fmt, ...)
+{
+ Lock(s_Mutex) {
+ SetOutputColor(1);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Warning] ", currentDateTime().c_str());
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+void log_error(const char* fmt, ...)
+{
+ Lock(s_Mutex) {
+ SetOutputColor(2);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Error] ", currentDateTime().c_str());
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+#define CHECK_TAG(tag)\
+if (s_OpenTags.count(tag) == 0)\
+ return;
+
+void log_info_tag(const char* tag, const char* fmt, ...)
+{
+ CHECK_TAG(tag);
+
+ Lock(s_Mutex) {
+ SetOutputColor(0);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Info] [%s] ", currentDateTime().c_str(), tag);
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+void log_warning_tag(const char* tag, const char* fmt, ...)
+{
+ CHECK_TAG(tag);
+
+ Lock(s_Mutex) {
+ SetOutputColor(1);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Warning] [%s] ", currentDateTime().c_str(), tag);
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+void log_error_tag(const char* tag, const char* fmt, ...)
+{
+ CHECK_TAG(tag);
+
+ Lock(s_Mutex) {
+ SetOutputColor(2);
+ va_list pArgs = NULL;
+ va_start(pArgs, fmt);
+ printf("%s [Error] [%s] ", currentDateTime().c_str(), tag);
+ vprintf(fmt, pArgs);
+ printf("\n");
+ va_end(pArgs);
+ }
+}
+
+#else
+void log_open_tag(std::string tag) {}
+void log_info(std::string log) {}
+void log_warning(std::string log){}
+void log_error(std::string log){}
+void log_error_null_param(std::string funcName, std::string param){}
+void log_info(string tag, std::string log) {}
+void log_warning(string tag, std::string log) {}
+void log_error(string tag, std::string log) {}
+
+void log_open_tag(const char* tag) {}
+void log_info(const char* log, ...) {}
+void log_warning(const char* log, ...) {}
+void log_error(const char* log, ...) {}
+void log_info(const char* tag, const char* log, ...) {}
+void log_warning(const char* tag, const char* log, ...) {}
+void log_error(const char* tag, const char* log, ...) {}
+#endif \ No newline at end of file