Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 41017be

Browse files
committed
MiraiBot.EventLoop 可接受错误记录函数;
1 parent 07df1a9 commit 41017be

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

examples/RepeatMessage.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ int main()
4242
tm.Reply(tm.MessageChain);
4343
});
4444

45-
bot.EventLoop();
45+
46+
// 记录轮询事件时的错误
47+
bot.EventLoop([](const char* errMsg)
48+
{
49+
cout << "轮询事件时出错: " << errMsg << endl;
50+
});
51+
52+
// 默认参数是在 cerr 输出错误
53+
// bot.EventLoop();
4654

4755
return 0;
4856
}

include/mirai_bot.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace Cyan
112112
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
113113
}
114114

115-
void EventLoop();
115+
void EventLoop(function<void(const char*)> errLogger = nullptr);
116116

117117
private:
118118
bool SessionVerify();

src/mirai_bot.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ namespace Cyan
562562
}
563563

564564

565-
void MiraiBot::EventLoop()
565+
void MiraiBot::EventLoop(function<void(const char*)> errLogger)
566566
{
567567
const unsigned count_per_loop = 20;
568568
const unsigned time_interval = 100;
@@ -575,7 +575,14 @@ namespace Cyan
575575
}
576576
catch (const std::exception& ex)
577577
{
578-
std::cerr << ex.what() << std::endl;
578+
if (errLogger == nullptr)
579+
{
580+
std::cerr << ex.what() << std::endl;
581+
}
582+
else
583+
{
584+
errLogger(ex.what());
585+
}
579586
}
580587

581588
if (count < count_per_loop)

0 commit comments

Comments
 (0)