Browse Source

feat(CAPI): 🔊 add debug api logger

tags/0.1.0
DragonAura 2 years ago
parent
commit
74d8d2970f
5 changed files with 132 additions and 40 deletions
  1. +8
    -8
      CAPI/API/include/API.h
  2. +1
    -1
      CAPI/API/include/logic.h
  3. +79
    -0
      CAPI/API/src/DebugAPI.cpp
  4. +28
    -18
      CAPI/API/src/logic.cpp
  5. +16
    -13
      CAPI/API/src/main.cpp

+ 8
- 8
CAPI/API/include/API.h View File

@@ -15,6 +15,10 @@
#include <iostream>
#include <vector>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#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<spdlog::logger> 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<spdlog::logger> logger;
ILogic& logic;
};



+ 1
- 1
CAPI/API/include/logic.h View File

@@ -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

+ 79
- 0
CAPI/API/src/DebugAPI.cpp View File

@@ -1,7 +1,53 @@
#include <string>
#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<spdlog::sinks::basic_file_sink_mt>(fileName, true);
auto printLogger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::logger>("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<spdlog::sinks::basic_file_sink_mt>(fileName, true);
auto printLogger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::logger>("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<bool> 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<bool> HumanDebugAPI::MoveDown(int64_t timeInMilliseconds)
{
logger->info("MoveDown: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, 0);
}

std::future<bool> HumanDebugAPI::MoveRight(int64_t timeInMilliseconds)
{
logger->info("MoveRight: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI * 0.5);
}

std::future<bool> HumanDebugAPI::MoveUp(int64_t timeInMilliseconds)
{
logger->info("MoveUp: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI);
}

std::future<bool> HumanDebugAPI::MoveLeft(int64_t timeInMilliseconds)
{
logger->info("MoveLeft: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI * 1.5);
}

std::future<bool> 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<bool> ButcherDebugAPI::MoveDown(int64_t timeInMilliseconds)
{
logger->info("MoveDown: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, 0);
}

std::future<bool> ButcherDebugAPI::MoveRight(int64_t timeInMilliseconds)
{
logger->info("MoveRight: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI * 0.5);
}

std::future<bool> ButcherDebugAPI::MoveUp(int64_t timeInMilliseconds)
{
logger->info("MoveUp: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI);
}

std::future<bool> ButcherDebugAPI::MoveLeft(int64_t timeInMilliseconds)
{
logger->info("MoveLeft: timeInMilliseconds = {}", timeInMilliseconds);
return Move(timeInMilliseconds, PI * 1.5);
}

std::future<bool> HumanDebugAPI::PickProp(THUAI6::PropType prop)
{
logger->info("PickProp: prop = {}", THUAI6::propDict[prop]);
return std::async(std::launch::async, [&]()
{ return logic.PickProp(prop); });
}

std::future<bool> HumanDebugAPI::UseProp()
{
logger->info("UseProp");
return std::async(std::launch::async, [&]()
{ return logic.UseProp(); });
}

std::future<bool> ButcherDebugAPI::PickProp(THUAI6::PropType prop)
{
logger->info("PickProp: prop = {}", THUAI6::propDict[prop]);
return std::async(std::launch::async, [&]()
{ return logic.PickProp(prop); });
}

std::future<bool> ButcherDebugAPI::UseProp()
{
logger->info("UseProp");
return std::async(std::launch::async, [&]()
{ return logic.UseProp(); });
}

std::future<bool> HumanDebugAPI::UseSkill()
{
logger->info("UseSkill");
return std::async(std::launch::async, [&]()
{ return logic.UseSkill(); });
}

std::future<bool> ButcherDebugAPI::UseSkill()
{
logger->info("UseSkill");
return std::async(std::launch::async, [&]()
{ return logic.UseSkill(); });
}

std::future<bool> 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<bool> 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<bool> HumanDebugAPI::HaveMessage()
{
logger->info("HaveMessage");
return std::async(std::launch::async, [&]()
{ return logic.HaveMessage(); });
}

std::future<bool> ButcherDebugAPI::HaveMessage()
{
logger->info("HaveMessage");
return std::async(std::launch::async, [&]()
{ return logic.HaveMessage(); });
}

std::future<std::optional<std::pair<int64_t, std::string>>> HumanDebugAPI::GetMessage()
{
logger->info("GetMessage");
return std::async(std::launch::async, [&]()
{ return logic.GetMessage(); });
}

std::future<std::optional<std::pair<int64_t, std::string>>> ButcherDebugAPI::GetMessage()
{
logger->info("GetMessage");
return std::async(std::launch::async, [&]()
{ return logic.GetMessage(); });
}

std::future<bool> HumanDebugAPI::Wait()
{
logger->info("Wait");
if (logic.GetCounter() == -1)
return std::async(std::launch::async, [&]()
{ return false; });
@@ -168,6 +237,7 @@ std::future<bool> HumanDebugAPI::Wait()

std::future<bool> ButcherDebugAPI::Wait()
{
logger->info("Wait");
if (logic.GetCounter() == -1)
return std::async(std::launch::async, [&]()
{ return false; });
@@ -238,30 +308,35 @@ const std::vector<int64_t> ButcherDebugAPI::GetPlayerGUIDs() const

std::future<bool> HumanDebugAPI::StartFixMachine()
{
logger->info("StartFixMachine");
return std::async(std::launch::async, [&]()
{ return logic.StartFixMachine(); });
}

std::future<bool> HumanDebugAPI::EndFixMachine()
{
logger->info("EndFixMachine");
return std::async(std::launch::async, [&]()
{ return logic.EndFixMachine(); });
}

std::future<bool> HumanDebugAPI::StartSaveHuman()
{
logger->info("StartSaveHuman");
return std::async(std::launch::async, [&]()
{ return logic.StartSaveHuman(); });
}

std::future<bool> HumanDebugAPI::EndSaveHuman()
{
logger->info("EndSaveHuman");
return std::async(std::launch::async, [&]()
{ return logic.EndSaveHuman(); });
}

std::future<bool> HumanDebugAPI::Escape()
{
logger->info("Escape");
return std::async(std::launch::async, [&]()
{ return logic.Escape(); });
}
@@ -273,24 +348,28 @@ std::shared_ptr<const THUAI6::Human> HumanDebugAPI::GetSelfInfo() const

std::future<bool> ButcherDebugAPI::Attack(double angleInRadian)
{
logger->info("Attack");
return std::async(std::launch::async, [&]()
{ return logic.Attack(angleInRadian); });
}

std::future<bool> ButcherDebugAPI::CarryHuman()
{
logger->info("CarryHuman");
return std::async(std::launch::async, [&]()
{ return logic.CarryHuman(); });
}

std::future<bool> ButcherDebugAPI::ReleaseHuman()
{
logger->info("ReleaseHuman");
return std::async(std::launch::async, [&]()
{ return logic.ReleaseHuman(); });
}

std::future<bool> ButcherDebugAPI::HangHuman()
{
logger->info("HangHuman");
return std::async(std::launch::async, [&]()
{ return logic.HangHuman(); });
}


+ 28
- 18
CAPI/API/src/logic.cpp View File

@@ -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<spdlog::sinks::basic_file_sink_mt>("logs/logic-log.txt", true);
auto stdout_logger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::logger>("logicLogger", spdlog::sinks_init_list{file_logger, stdout_logger});
}
auto fileLogger = std::make_shared<spdlog::sinks::basic_file_sink_mt>("logs/logic-log.txt", true);
auto printLogger = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
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<spdlog::sinks::basic_file_sink_mt>("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<spdlog::logger>("logicLogger", spdlog::sinks_init_list{fileLogger, printLogger});

// 建立与服务器之间通信的组件
pComm = std::make_unique<Communication>(IP, port);

// 构造timer
if (playerType == THUAI6::PlayerType::HumanPlayer)
timer = std::make_unique<HumanAPI>(*this);
{
if (!file && !print)
timer = std::make_unique<HumanAPI>(*this);
else
timer = std::make_unique<HumanDebugAPI>(*this, file, print, warnOnly, playerID);
}
else if (playerType == THUAI6::PlayerType::ButcherPlayer)
timer = std::make_unique<ButcherAPI>(*this);
{
if (!file && !print)
timer = std::make_unique<ButcherAPI>(*this);
else
timer = std::make_unique<ButcherDebugAPI>(*this, file, print, warnOnly, playerID);
}

// 构造AI线程
auto AIThread = [&]()


+ 16
- 13
CAPI/API/src/main.cpp View File

@@ -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<int> 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;
}



Loading…
Cancel
Save