From 74d8d2970fe91e2cc21a7cfe6d98d31b2293c588 Mon Sep 17 00:00:00 2001 From: DragonAura Date: Mon, 16 Jan 2023 15:05:58 +0800 Subject: [PATCH 1/2] feat(CAPI): :loud_sound: add debug api logger --- CAPI/API/include/API.h | 16 ++++---- CAPI/API/include/logic.h | 2 +- CAPI/API/src/DebugAPI.cpp | 79 +++++++++++++++++++++++++++++++++++++++ CAPI/API/src/logic.cpp | 46 ++++++++++++++--------- CAPI/API/src/main.cpp | 29 +++++++------- 5 files changed, 132 insertions(+), 40 deletions(-) diff --git a/CAPI/API/include/API.h b/CAPI/API/include/API.h index 728491a..a123998 100644 --- a/CAPI/API/include/API.h +++ b/CAPI/API/include/API.h @@ -15,6 +15,10 @@ #include #include +#include +#include +#include + #include "structures.h" const constexpr int num_of_grid_per_cell = 1000; @@ -269,10 +273,7 @@ private: class HumanDebugAPI : public IHumanAPI, public IGameTimer { public: - HumanDebugAPI(ILogic& logic) : - logic(logic) - { - } + HumanDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID); void StartTimer() override; void EndTimer() override; void Play(IAI& ai) override; @@ -314,16 +315,14 @@ public: private: std::chrono::system_clock::time_point StartPoint; + std::shared_ptr logger; ILogic& logic; }; class ButcherDebugAPI : public IButcherAPI, public IGameTimer { public: - ButcherDebugAPI(ILogic& logic) : - logic(logic) - { - } + ButcherDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID); void StartTimer() override; void EndTimer() override; void Play(IAI& ai) override; @@ -364,6 +363,7 @@ public: private: std::chrono::system_clock::time_point StartPoint; + std::shared_ptr logger; ILogic& logic; }; diff --git a/CAPI/API/include/logic.h b/CAPI/API/include/logic.h index ce6910c..a095a5c 100644 --- a/CAPI/API/include/logic.h +++ b/CAPI/API/include/logic.h @@ -156,7 +156,7 @@ public: } // Main函数同上 - void Main(CreateAIFunc createAI, std::string IP, std::string port, bool debug, bool level); + void Main(CreateAIFunc createAI, std::string IP, std::string port, bool file, bool print, bool warnOnly); }; #endif diff --git a/CAPI/API/src/DebugAPI.cpp b/CAPI/API/src/DebugAPI.cpp index c18dc0a..26894ae 100644 --- a/CAPI/API/src/DebugAPI.cpp +++ b/CAPI/API/src/DebugAPI.cpp @@ -1,7 +1,53 @@ +#include #include "AI.h" #include "API.h" +#include "structures.h" #define PI 3.14159265358979323846 +HumanDebugAPI::HumanDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID) : + logic(logic) +{ + std::string fileName = "logs/api-" + std::to_string(playerID) + "-log.txt"; + auto fileLogger = std::make_shared(fileName, true); + auto printLogger = std::make_shared(); + std::string pattern = "[api " + std::to_string(playerID) + "] [%H:%M:%S.%e] [%l] %v"; + fileLogger->set_pattern(pattern); + printLogger->set_pattern(pattern); + if (file) + fileLogger->set_level(spdlog::level::trace); + else + fileLogger->set_level(spdlog::level::off); + if (print) + printLogger->set_level(spdlog::level::info); + else + printLogger->set_level(spdlog::level::off); + if (warnOnly) + printLogger->set_level(spdlog::level::warn); + logger = std::make_shared("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); +} + +ButcherDebugAPI::ButcherDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID) : + logic(logic) +{ + std::string fileName = "logs/api-" + std::to_string(playerID) + "-log.txt"; + auto fileLogger = std::make_shared(fileName, true); + auto printLogger = std::make_shared(); + std::string pattern = "[api" + std::to_string(playerID) + "] [%H:%M:%S.%e] [%l] %v"; + fileLogger->set_pattern(pattern); + printLogger->set_pattern(pattern); + if (file) + fileLogger->set_level(spdlog::level::trace); + else + fileLogger->set_level(spdlog::level::off); + if (print) + printLogger->set_level(spdlog::level::info); + else + printLogger->set_level(spdlog::level::off); + if (warnOnly) + printLogger->set_level(spdlog::level::warn); + logger = std::make_shared("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); +} + void HumanDebugAPI::StartTimer() { StartPoint = std::chrono::system_clock::now(); @@ -34,130 +80,153 @@ int ButcherDebugAPI::GetFrameCount() const std::future HumanDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian) { + logger->info("Move: timeInMilliseconds = {}, angleInRadian = {}", timeInMilliseconds, angleInRadian); return std::async(std::launch::async, [&]() { return logic.Move(timeInMilliseconds, angleInRadian); }); } std::future HumanDebugAPI::MoveDown(int64_t timeInMilliseconds) { + logger->info("MoveDown: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, 0); } std::future HumanDebugAPI::MoveRight(int64_t timeInMilliseconds) { + logger->info("MoveRight: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI * 0.5); } std::future HumanDebugAPI::MoveUp(int64_t timeInMilliseconds) { + logger->info("MoveUp: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI); } std::future HumanDebugAPI::MoveLeft(int64_t timeInMilliseconds) { + logger->info("MoveLeft: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI * 1.5); } std::future ButcherDebugAPI::Move(int64_t timeInMilliseconds, double angleInRadian) { + logger->info("Move: timeInMilliseconds = {}, angleInRadian = {}", timeInMilliseconds, angleInRadian); return std::async(std::launch::async, [&]() { return logic.Move(timeInMilliseconds, angleInRadian); }); } std::future ButcherDebugAPI::MoveDown(int64_t timeInMilliseconds) { + logger->info("MoveDown: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, 0); } std::future ButcherDebugAPI::MoveRight(int64_t timeInMilliseconds) { + logger->info("MoveRight: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI * 0.5); } std::future ButcherDebugAPI::MoveUp(int64_t timeInMilliseconds) { + logger->info("MoveUp: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI); } std::future ButcherDebugAPI::MoveLeft(int64_t timeInMilliseconds) { + logger->info("MoveLeft: timeInMilliseconds = {}", timeInMilliseconds); return Move(timeInMilliseconds, PI * 1.5); } std::future HumanDebugAPI::PickProp(THUAI6::PropType prop) { + logger->info("PickProp: prop = {}", THUAI6::propDict[prop]); return std::async(std::launch::async, [&]() { return logic.PickProp(prop); }); } std::future HumanDebugAPI::UseProp() { + logger->info("UseProp"); return std::async(std::launch::async, [&]() { return logic.UseProp(); }); } std::future ButcherDebugAPI::PickProp(THUAI6::PropType prop) { + logger->info("PickProp: prop = {}", THUAI6::propDict[prop]); return std::async(std::launch::async, [&]() { return logic.PickProp(prop); }); } std::future ButcherDebugAPI::UseProp() { + logger->info("UseProp"); return std::async(std::launch::async, [&]() { return logic.UseProp(); }); } std::future HumanDebugAPI::UseSkill() { + logger->info("UseSkill"); return std::async(std::launch::async, [&]() { return logic.UseSkill(); }); } std::future ButcherDebugAPI::UseSkill() { + logger->info("UseSkill"); return std::async(std::launch::async, [&]() { return logic.UseSkill(); }); } std::future HumanDebugAPI::SendMessage(int64_t toID, std::string message) { + logger->info("SendMessage: toID = {}, message = {}", toID, message); return std::async(std::launch::async, [&]() { return logic.SendMessage(toID, message); }); } std::future ButcherDebugAPI::SendMessage(int64_t toID, std::string message) { + logger->info("SendMessage: toID = {}, message = {}", toID, message); return std::async(std::launch::async, [&]() { return logic.SendMessage(toID, message); }); } std::future HumanDebugAPI::HaveMessage() { + logger->info("HaveMessage"); return std::async(std::launch::async, [&]() { return logic.HaveMessage(); }); } std::future ButcherDebugAPI::HaveMessage() { + logger->info("HaveMessage"); return std::async(std::launch::async, [&]() { return logic.HaveMessage(); }); } std::future>> HumanDebugAPI::GetMessage() { + logger->info("GetMessage"); return std::async(std::launch::async, [&]() { return logic.GetMessage(); }); } std::future>> ButcherDebugAPI::GetMessage() { + logger->info("GetMessage"); return std::async(std::launch::async, [&]() { return logic.GetMessage(); }); } std::future HumanDebugAPI::Wait() { + logger->info("Wait"); if (logic.GetCounter() == -1) return std::async(std::launch::async, [&]() { return false; }); @@ -168,6 +237,7 @@ std::future HumanDebugAPI::Wait() std::future ButcherDebugAPI::Wait() { + logger->info("Wait"); if (logic.GetCounter() == -1) return std::async(std::launch::async, [&]() { return false; }); @@ -238,30 +308,35 @@ const std::vector ButcherDebugAPI::GetPlayerGUIDs() const std::future HumanDebugAPI::StartFixMachine() { + logger->info("StartFixMachine"); return std::async(std::launch::async, [&]() { return logic.StartFixMachine(); }); } std::future HumanDebugAPI::EndFixMachine() { + logger->info("EndFixMachine"); return std::async(std::launch::async, [&]() { return logic.EndFixMachine(); }); } std::future HumanDebugAPI::StartSaveHuman() { + logger->info("StartSaveHuman"); return std::async(std::launch::async, [&]() { return logic.StartSaveHuman(); }); } std::future HumanDebugAPI::EndSaveHuman() { + logger->info("EndSaveHuman"); return std::async(std::launch::async, [&]() { return logic.EndSaveHuman(); }); } std::future HumanDebugAPI::Escape() { + logger->info("Escape"); return std::async(std::launch::async, [&]() { return logic.Escape(); }); } @@ -273,24 +348,28 @@ std::shared_ptr HumanDebugAPI::GetSelfInfo() const std::future ButcherDebugAPI::Attack(double angleInRadian) { + logger->info("Attack"); return std::async(std::launch::async, [&]() { return logic.Attack(angleInRadian); }); } std::future ButcherDebugAPI::CarryHuman() { + logger->info("CarryHuman"); return std::async(std::launch::async, [&]() { return logic.CarryHuman(); }); } std::future ButcherDebugAPI::ReleaseHuman() { + logger->info("ReleaseHuman"); return std::async(std::launch::async, [&]() { return logic.ReleaseHuman(); }); } std::future ButcherDebugAPI::HangHuman() { + logger->info("HangHuman"); return std::async(std::launch::async, [&]() { return logic.HangHuman(); }); } diff --git a/CAPI/API/src/logic.cpp b/CAPI/API/src/logic.cpp index b12be1b..efcf5f6 100644 --- a/CAPI/API/src/logic.cpp +++ b/CAPI/API/src/logic.cpp @@ -431,34 +431,44 @@ bool Logic::TryConnection() return result; } -void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port, bool debug, bool level) +void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port, bool file, bool print, bool warnOnly) { // 建立日志组件 - if (debug) - { - auto file_logger = std::make_shared("logs/logic-log.txt", true); - auto stdout_logger = std::make_shared(); - if (level) - stdout_logger->set_level(spdlog::level::warn); - else - stdout_logger->set_level(spdlog::level::info); - file_logger->set_level(spdlog::level::trace); - logger = std::make_shared("logicLogger", spdlog::sinks_init_list{file_logger, stdout_logger}); - } + auto fileLogger = std::make_shared("logs/logic-log.txt", true); + auto printLogger = std::make_shared(); + std::string pattern = "[logic] [%H:%M:%S.%e] [%l] %v"; + fileLogger->set_pattern(pattern); + printLogger->set_pattern(pattern); + if (file) + fileLogger->set_level(spdlog::level::trace); else - { - auto logger = std::make_shared("logs/logic-log.txt", true); - logger->set_level(spdlog::level::trace); - } + fileLogger->set_level(spdlog::level::off); + if (print) + printLogger->set_level(spdlog::level::info); + else + printLogger->set_level(spdlog::level::off); + if (warnOnly) + printLogger->set_level(spdlog::level::warn); + logger = std::make_shared("logicLogger", spdlog::sinks_init_list{fileLogger, printLogger}); // 建立与服务器之间通信的组件 pComm = std::make_unique(IP, port); // 构造timer if (playerType == THUAI6::PlayerType::HumanPlayer) - timer = std::make_unique(*this); + { + if (!file && !print) + timer = std::make_unique(*this); + else + timer = std::make_unique(*this, file, print, warnOnly, playerID); + } else if (playerType == THUAI6::PlayerType::ButcherPlayer) - timer = std::make_unique(*this); + { + if (!file && !print) + timer = std::make_unique(*this); + else + timer = std::make_unique(*this, file, print, warnOnly, playerID); + } // 构造AI线程 auto AIThread = [&]() diff --git a/CAPI/API/src/main.cpp b/CAPI/API/src/main.cpp index d995537..41bb9ae 100644 --- a/CAPI/API/src/main.cpp +++ b/CAPI/API/src/main.cpp @@ -12,15 +12,18 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder) int pID = 114514; std::string sIP = "114.51.41.91"; std::string sPort = "9810"; + bool file = false; bool print = false; - bool level = false; + bool warnOnly = false; extern const THUAI6::PlayerType playerType; extern const THUAI6::ButcherType butcherType; extern const THUAI6::HumanType humanType; // 仅供早期调试使用 { + file = true; + print = true; Logic logic(playerType, pID, butcherType, humanType); - logic.Main(AIBuilder, sIP, sPort, print, level); + logic.Main(AIBuilder, sIP, sPort, file, print, warnOnly); return 0; } @@ -40,13 +43,16 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder) TCLAP::ValueArg playerID("p", "playerID", "Player ID 0,1,2,3 valid only", true, -1, &playerIdConstraint); cmd.add(playerID); - std::string DebugDesc = "Set this flag to print the debug log on the screen.\n" - "The log will always be saved to ./logs folder.\n"; + std::string DebugDesc = "Set this flag to save the debug log to ./logs folder.\n"; TCLAP::SwitchArg debug("d", "debug", DebugDesc); cmd.add(debug); + std::string OutputDesc = "Set this flag to print the debug log to the screen.\n"; + TCLAP::SwitchArg output("o", "output", OutputDesc); + cmd.add(output); + TCLAP::SwitchArg warning("w", "warning", "Set this flag to only print warning on the screen.\n" - "This flag will be ignored if the debug flag is not set\n"); + "This flag will be ignored if the output flag is not set\n"); cmd.add(warning); cmd.parse(argc, argv); @@ -54,13 +60,10 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder) sIP = serverIP.getValue(); sPort = serverPort.getValue(); - bool d = debug.getValue(); - bool w = warning.getValue(); - if (d) - { - print = true; - level = w; - } + file = debug.getValue(); + print = output.getValue(); + if (print) + warnOnly = warning.getValue(); } catch (TCLAP::ArgException& e) { @@ -68,7 +71,7 @@ int THUAI6Main(int argc, char** argv, CreateAIFunc AIBuilder) return 1; } Logic logic(playerType, pID, butcherType, humanType); - logic.Main(AIBuilder, sIP, sPort, print, level); + logic.Main(AIBuilder, sIP, sPort, file, print, warnOnly); return 0; } From 50afe72cdeec772ccee67fefbb60d76c3fb69cf5 Mon Sep 17 00:00:00 2001 From: DragonAura Date: Mon, 16 Jan 2023 16:41:59 +0800 Subject: [PATCH 2/2] refactor(CAPI): :zap: change logger to unique ptr --- CAPI/API/include/API.h | 4 ++-- CAPI/API/include/logic.h | 2 +- CAPI/API/src/DebugAPI.cpp | 4 ++-- CAPI/API/src/logic.cpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CAPI/API/include/API.h b/CAPI/API/include/API.h index a123998..ecb8430 100644 --- a/CAPI/API/include/API.h +++ b/CAPI/API/include/API.h @@ -315,7 +315,7 @@ public: private: std::chrono::system_clock::time_point StartPoint; - std::shared_ptr logger; + std::unique_ptr logger; ILogic& logic; }; @@ -363,7 +363,7 @@ public: private: std::chrono::system_clock::time_point StartPoint; - std::shared_ptr logger; + std::unique_ptr logger; ILogic& logic; }; diff --git a/CAPI/API/include/logic.h b/CAPI/API/include/logic.h index a095a5c..dfc80dc 100644 --- a/CAPI/API/include/logic.h +++ b/CAPI/API/include/logic.h @@ -35,7 +35,7 @@ class Logic : public ILogic { private: // 日志组件 - std::shared_ptr logger; + std::unique_ptr logger; // 通信组件 std::unique_ptr pComm; diff --git a/CAPI/API/src/DebugAPI.cpp b/CAPI/API/src/DebugAPI.cpp index 26894ae..3bd066c 100644 --- a/CAPI/API/src/DebugAPI.cpp +++ b/CAPI/API/src/DebugAPI.cpp @@ -23,7 +23,7 @@ HumanDebugAPI::HumanDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly printLogger->set_level(spdlog::level::off); if (warnOnly) printLogger->set_level(spdlog::level::warn); - logger = std::make_shared("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); + logger = std::make_unique("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); } ButcherDebugAPI::ButcherDebugAPI(ILogic& logic, bool file, bool print, bool warnOnly, int64_t playerID) : @@ -45,7 +45,7 @@ ButcherDebugAPI::ButcherDebugAPI(ILogic& logic, bool file, bool print, bool warn printLogger->set_level(spdlog::level::off); if (warnOnly) printLogger->set_level(spdlog::level::warn); - logger = std::make_shared("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); + logger = std::make_unique("apiLogger", spdlog::sinks_init_list{fileLogger, printLogger}); } void HumanDebugAPI::StartTimer() diff --git a/CAPI/API/src/logic.cpp b/CAPI/API/src/logic.cpp index efcf5f6..073638f 100644 --- a/CAPI/API/src/logic.cpp +++ b/CAPI/API/src/logic.cpp @@ -449,7 +449,7 @@ void Logic::Main(CreateAIFunc createAI, std::string IP, std::string port, bool f printLogger->set_level(spdlog::level::off); if (warnOnly) printLogger->set_level(spdlog::level::warn); - logger = std::make_shared("logicLogger", spdlog::sinks_init_list{fileLogger, printLogger}); + logger = std::make_unique("logicLogger", spdlog::sinks_init_list{fileLogger, printLogger}); // 建立与服务器之间通信的组件 pComm = std::make_unique(IP, port);