CSV chatlog

This commit is contained in:
nullifiedcat 2017-12-18 13:14:19 +03:00
parent 12066e377c
commit e7ebfe3e87
5 changed files with 70 additions and 36 deletions

View File

@ -15,4 +15,5 @@ extern int count_hits;
extern int count_hits_head; extern int count_hits_head;
void Update(); void Update();
} }

View File

@ -15,28 +15,28 @@ namespace chatlog
{ {
CatVar enabled(CV_SWITCH, "chat_log", "0", "Chat log", "Log chat to file"); CatVar enabled(CV_SWITCH, "chat_log", "0", "Chat log", "Log chat to file");
CatVar message_template(CV_STRING, "chat_log_template", "[%t] [U:1:%u] %n: %m",
"Log template",
"%u - SteamID\n%n - name\n%m - message\n%t - time");
CatVar dont_log_spam(CV_SWITCH, "chat_log_nospam", "1", "No Spam", CatVar dont_log_spam(CV_SWITCH, "chat_log_nospam", "1", "No Spam",
"Don't log your messages if spam is active"); "Don't log your messages if spam is active");
CatVar dont_log_ipc(CV_SWITCH, "chat_log_noipc", "1", "No IPC", CatVar dont_log_ipc(CV_SWITCH, "chat_log_noipc", "1", "No IPC",
"Don't log messages sent by bots"); "Don't log messages sent by bots");
class RAIILog class csv_stream
{ {
public: public:
RAIILog() struct end_t {};
static constexpr end_t end{};
public:
csv_stream()
{ {
open(); open();
} }
~RAIILog() ~csv_stream()
{ {
stream.close(); stream.close();
} }
void open() bool open()
{ {
logging::Info("Trying to open log file"); logging::Info("csvstream: Trying to open log file");
uid_t uid = geteuid(); uid_t uid = geteuid();
struct passwd *pw = getpwuid(uid); struct passwd *pw = getpwuid(uid);
std::string uname = ""; std::string uname = "";
@ -44,26 +44,57 @@ public:
{ {
uname = std::string(pw->pw_name); uname = std::string(pw->pw_name);
} }
stream.open(DATA_PATH "/chat-" + uname + ".log", stream.open(DATA_PATH "/chat-" + uname + ".csv",
std::ios::out | std::ios::app); std::ios::out | std::ios::app);
return stream.good();
} }
void log(const std::string &msg) public:
{ int columns{ 0 };
if (stream.bad() or not stream.is_open())
{
logging::Info("[ERROR] RAIILog stream is bad!");
open();
return;
}
stream << msg << "\n";
stream.flush();
}
std::ofstream stream; std::ofstream stream;
}; };
RAIILog &logger() csv_stream& operator<<(csv_stream& log, const std::string& string)
{ {
static RAIILog object{}; if (!log.stream.good())
{
logging::Info("[ERROR] csvstream is not open!");
if (!log.open())
return log;
}
if (log.columns)
log.stream << ',';
log.stream << '"';
for (const auto& i : string)
{
if (i == '"')
{
log.stream << '"';
}
log.stream << i;
}
log.stream << '"';
log.columns++;
return log;
}
csv_stream& operator<<(csv_stream& log, const csv_stream::end_t& end)
{
if (!log.stream.good())
{
logging::Info("[ERROR] csvstream is not open!");
if (!log.open())
return log;
}
log.stream << '\n';
log.stream.flush();
log.columns = 0;
return log;
}
csv_stream &logger()
{
static csv_stream object{};
return object; return object;
} }
@ -83,6 +114,7 @@ void LogMessage(int eid, std::string message)
playerlist::AccessData(info.friendsID).state == playerlist::AccessData(info.friendsID).state ==
playerlist::k_EState::IPC) playerlist::k_EState::IPC)
return; return;
std::string name(info.name); std::string name(info.name);
for (auto &x : name) for (auto &x : name)
{ {
@ -94,17 +126,12 @@ void LogMessage(int eid, std::string message)
if (x == '\n' || x == '\r') if (x == '\n' || x == '\r')
x = '*'; x = '*';
} }
time_t current_time;
struct tm *time_info; logger()
char timeString[9]; << std::to_string(time(nullptr))
time(&current_time); << std::to_string(info.friendsID)
time_info = localtime(&current_time); << name
strftime(timeString, sizeof(timeString), "%H:%M:%S", time_info); << message
std::string msg(message_template.GetString()); << csv_stream::end;
ReplaceString(msg, "%t", std::string(timeString));
ReplaceString(msg, "%u", std::to_string(info.friendsID));
ReplaceString(msg, "%n", name);
ReplaceString(msg, "%m", message);
logger().log(msg);
} }
} }

View File

@ -44,7 +44,7 @@ CatCommand debug_hitrate("debug_hitrate", "Debug hitrate", []() {
} }
if (count_hits) if (count_hits)
{ {
p1 = float(count_hits_head) / float(count_hits) * 100.0f; p2 = float(count_hits_head) / float(count_hits) * 100.0f;
} }
logging::Info("%d / %d (%d%%)", count_hits, count_shots, p1); logging::Info("%d / %d (%d%%)", count_hits, count_shots, p1);
logging::Info("%d / %d (%d%%)", count_hits_head, count_hits, p2); logging::Info("%d / %d (%d%%)", count_hits_head, count_hits, p2);

View File

@ -585,7 +585,7 @@ void FireGameEvent_hook(void *_this, IGameEvent *event)
original(_this, event); original(_this, event);
} }
static CatVar hitrate_check(CV_SWITCH, "hitrate", "0", "Monitor hitrate"); static CatVar hitrate_check(CV_SWITCH, "hitrate", "1", "Monitor hitrate");
void FrameStageNotify_hook(void *_this, int stage) void FrameStageNotify_hook(void *_this, int stage)
{ {

View File

@ -37,6 +37,10 @@ void queue_start()
re::CTFPartyClient::LoadSavedCasualCriteria(client); re::CTFPartyClient::LoadSavedCasualCriteria(client);
re::CTFPartyClient::RequestQueueForMatch(client); re::CTFPartyClient::RequestQueueForMatch(client);
} }
else
{
logging::Info("queue_start: CTFPartyClient == null!");
}
} }
void abandon() void abandon()
@ -44,5 +48,7 @@ void abandon()
re::CTFGCClientSystem *gc = re::CTFGCClientSystem::GTFGCClientSystem(); re::CTFGCClientSystem *gc = re::CTFGCClientSystem::GTFGCClientSystem();
if (gc != nullptr) if (gc != nullptr)
gc->AbandonCurrentMatch(); gc->AbandonCurrentMatch();
else
logging::Info("abandon: CTFGCClientSystem == null!");
} }
} }