在Azeroth Core源码的根目录中的文件夹 src\modules 中创建一个名为"chatlog"的文件夹。
在该文件夹中创建一个 chatLog.cpp 和 chatLog.h 文件。编写以下代码:
chatLog.cpp:
#include "ServerStdafx.h"
#include "chatLog.h"
#include
ChatLog::ChatLog()
{
//初始化代码
}
ChatLog::~ChatLog()
{
//清理代码
}
void ChatLog::SaveChatMessage(Player* sender, std::string const& message)
{
std::ofstream file;
file.open("chatlog.txt", std::ios_base::app);
if (!file.good())
{
return;
}
time_t now = time(nullptr);
char timeBuf[32];
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(&now));
//写入具体的聊天记录信息
file << "[" << timeBuf << "] " << sender->GetName() << ": " << message << std::endl;
file.close();
}
chatLog.h:
#pragma once
class Player;
class ChatLog
{
public:
ChatLog();
~ChatLog();
void SaveChatMessage(Player* sender, std::string const& message);
};
#include "modules/chatLog/chatLog.h"
ChatLog chatLog; chatLog.SaveChatMessage( plr , msg->content );
注意:聊天记录文件(chatlog.txt)的路径应该更改为适合您服务器的路径。