diff options
Diffstat (limited to 'Runtime')
-rw-r--r-- | Runtime/Debug/Log.cpp | 94 | ||||
-rw-r--r-- | Runtime/Debug/Log.h | 22 | ||||
-rw-r--r-- | Runtime/Scripting/Debug/Debug.bind.cpp | 120 |
3 files changed, 236 insertions, 0 deletions
diff --git a/Runtime/Debug/Log.cpp b/Runtime/Debug/Log.cpp new file mode 100644 index 0000000..08c7acf --- /dev/null +++ b/Runtime/Debug/Log.cpp @@ -0,0 +1,94 @@ +#include "log.h" +#include <iostream> +#include <ctime> +#include <unordered_set> + +using namespace std; + +//long g_LogTags = LogTag::All; +unordered_set<string> s_OpenTags; + +#ifdef GAMELAB_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; +} + +void log_open_tag(std::string tag) +{ + s_OpenTags.insert(tag); +} + +// https://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c + +void log_info(std::string log) +{ + cout << "\x1B[97m" << currentDateTime() << " [Info] " << log << "\033[0m" << endl; +} + +void log_warning(std::string log) +{ + cout << "\x1B[93m" << currentDateTime() << " [Wanning] " << log << "\033[0m" << endl; +} + +void log_error(std::string log) +{ + cout << "\x1B[91m" << currentDateTime() << " [Error] " << log << "\033[0m" << endl; +} + +void log_error_null_param(std::string funcName, std::string param) +{ + log_error("Null parameter in " + funcName + " called " + param); +} + +void log_info(string tag, std::string log) +{ + if (s_OpenTags.count(tag) <= 0) + return; + log_info("[" + tag + "] " + log); +} + +void log_warning(string tag, std::string log) +{ + if (s_OpenTags.count(tag) <= 0) + return; + log_warning("[" + tag + "] " + log); +} +void log_error(string tag, std::string log) +{ + if (s_OpenTags.count(tag) <= 0) + return; + log_error("[" + tag + "] " + log); +} +#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) {} + +#endif
\ No newline at end of file diff --git a/Runtime/Debug/Log.h b/Runtime/Debug/Log.h new file mode 100644 index 0000000..d66f705 --- /dev/null +++ b/Runtime/Debug/Log.h @@ -0,0 +1,22 @@ +#pragma once +#include <string> + +//enum LogTag : unsigned long +//{ +// +// All = ~0 +//}; +// +//extern long g_LogTags; + +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(std::string tag, std::string log); +void log_warning(std::string tag, std::string log); +void log_error(std::string tag, std::string log); diff --git a/Runtime/Scripting/Debug/Debug.bind.cpp b/Runtime/Scripting/Debug/Debug.bind.cpp new file mode 100644 index 0000000..1785f01 --- /dev/null +++ b/Runtime/Scripting/Debug/Debug.bind.cpp @@ -0,0 +1,120 @@ +#include "Runtime/LuaBind/LuaBind.h" +#include "Runtime/Debug/Log.h" + +int log(lua_State* L) +{ + LUA_BIND_STATE(L); + int n = state.GetTop(); + if (n == 1) + { + const char* msg = state.GetValue<const char*>(-1, ""); + log_info(msg); + } + else if (n == 2) + { + const char* tag = state.GetValue<const char*>(-2, ""); + const char* msg = state.GetValue<const char*>(-1, ""); + log_info(tag, msg); + } + else + { + log_error("Debug.Log()错误的参数个数"); + } + return 0; +} + +int logWwarning(lua_State* L) +{ + LUA_BIND_STATE(L); + int n = state.GetTop(); + if (n == 1) + { + const char* msg = state.GetValue<const char*>(-1, ""); + log_warning(msg); + } + else if (n == 2) + { + const char* tag = state.GetValue<const char*>(-2, ""); + const char* msg = state.GetValue<const char*>(-1, ""); + log_warning(tag, msg); + } + else + { + log_error("Debug.Log()错误的参数个数"); + } + return 0; +} + +int logError(lua_State* L) +{ + LUA_BIND_STATE(L); + int n = state.GetTop(); + if (n == 1) + { + const char* msg = state.GetValue<const char*>(-1, ""); + log_error(msg); + } + else if (n == 2) + { + const char* tag = state.GetValue<const char*>(-2, ""); + const char* msg = state.GetValue<const char*>(-1, ""); + log_error(tag, msg); + } + else + { + log_error("Debug.Log()错误的参数个数"); + } + return 0; +} + +int logEditor(lua_State* L) +{ +#ifdef GAMELAB_EDITOR + LUA_BIND_STATE(L); + int n = state.GetTop(); + if (n == 1) + { + const char* msg = state.GetValue<const char*>(-1, ""); + log_info(msg); + } + else if (n == 2) + { + const char* tag = state.GetValue<const char*>(-2, ""); + const char* msg = state.GetValue<const char*>(-1, ""); + log_info(tag, msg); + } + else + { + log_error("Debug.Log()错误的参数个数"); + } + return 0; +#endif +} + +int openTag(lua_State* L) +{ + LUA_BIND_STATE(L); + const char* tag = state.GetValue<const char*>(-1, ""); + log_open_tag(tag); + return 0; +} + +int luaopen_GameLab_Debug(lua_State* L) +{ + log_info("Scripting", "luaopen_GameLab_Debug()"); + + LUA_BIND_STATE(L); + + state.PushGlobalNamespace(); + state.PushNamespace("GameLab"); + state.PushNamespace("Debug"); + + state.RegisterMethod("Log", log); + state.RegisterMethod("LogWarning", logWwarning); + state.RegisterMethod("LogError", logError); + state.RegisterMethod("LogEditor", logEditor); + + state.RegisterMethod("OpenTag", openTag); + + return 1; +}
\ No newline at end of file |