Browse Source
Add Chat Logging plugin Added RenX_OnCommandTriggered plugin event Adjusted CMake files as necessary Signed-off-by: Sarah <sarahevans045@gmail.com>pull/17/head
Sarah
4 years ago
10 changed files with 302 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||
add_renx_plugin(RenX.ChatLogging |
|||
RenX_ChatLogging.cpp |
|||
RenX_ChatLogging.h) |
@ -0,0 +1,105 @@ |
|||
/**
|
|||
* This file is in the public domain, furnished "as is", without technical |
|||
* support, and with no warranty, express or implied, as to its usefulness for |
|||
* any purpose. |
|||
* |
|||
* Written by Sarah E. <sarah.evans@qq.com> |
|||
*/ |
|||
|
|||
#include <iostream> |
|||
#include <fstream> |
|||
#include <string> |
|||
#include "RenX_ChatLogging.h" |
|||
#include "RenX_Server.h" |
|||
#include "RenX_PlayerInfo.h" |
|||
|
|||
void RenX_ChatLogPlugin::PrepFile() |
|||
{ |
|||
// Check if date changed (Format: YYYY-MM-DD)
|
|||
std::string current_date = getTimeFormat("%F"); |
|||
std::string full_date = getTimeFormat("%c"); |
|||
|
|||
if (current_date == last_date) { |
|||
return; |
|||
} |
|||
|
|||
last_date = current_date; |
|||
|
|||
// Close any currently opened file
|
|||
|
|||
if (fs.is_open()) { |
|||
fs.close(); |
|||
} |
|||
|
|||
// Open new file
|
|||
|
|||
std::string file_name = "ChatLog_" + current_date + ".log"; |
|||
fs.open(file_name, std::fstream::out | std::fstream::app); |
|||
|
|||
fs << "Session Start: " |
|||
<< full_date |
|||
<< std::endl; |
|||
} |
|||
|
|||
bool RenX_ChatLogPlugin::initialize() |
|||
{ |
|||
PrepFile(); |
|||
|
|||
return fs.is_open(); |
|||
} |
|||
|
|||
RenX_ChatLogPlugin::~RenX_ChatLogPlugin() |
|||
{ |
|||
if (fs.is_open()) { |
|||
fs.close(); |
|||
} |
|||
} |
|||
|
|||
std::ostream& operator<<(std::ostream& in_stream, const Jupiter::ReadableString& in_string) { |
|||
in_stream.write(in_string.ptr(), in_string.size()); |
|||
return in_stream; |
|||
} |
|||
|
|||
void RenX_ChatLogPlugin::RenX_OnChat(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message) |
|||
{ |
|||
WriteToLog(server, player, message, "ALL"); |
|||
} |
|||
|
|||
void RenX_ChatLogPlugin::RenX_OnTeamChat(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message) |
|||
{ |
|||
WriteToLog(server, player, message, "TEAM"); |
|||
} |
|||
|
|||
void RenX_ChatLogPlugin::WriteToLog(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message, std::string in_prefix) |
|||
{ |
|||
// Check if new file needs to be opened
|
|||
PrepFile(); |
|||
|
|||
if (!fs.is_open()) { |
|||
return; |
|||
} |
|||
|
|||
const std::string& serverHostname = server.getSocketHostname(); |
|||
unsigned short serverPort = server.getSocketPort(); |
|||
|
|||
fs << getTimeFormat("%T") |
|||
<< " " |
|||
<< serverHostname |
|||
<< ":" |
|||
<< serverPort |
|||
<< " " |
|||
<< in_prefix |
|||
<< " " |
|||
<< player.name |
|||
<< ": " |
|||
<< message |
|||
<< std::endl; |
|||
} |
|||
|
|||
// Plugin instantiation and entry point.
|
|||
RenX_ChatLogPlugin pluginInstance; |
|||
|
|||
extern "C" JUPITER_EXPORT Jupiter::Plugin *getPlugin() |
|||
{ |
|||
return &pluginInstance; |
|||
} |
@ -0,0 +1,33 @@ |
|||
/**
|
|||
* This file is in the public domain, furnished "as is", without technical |
|||
* support, and with no warranty, express or implied, as to its usefulness for |
|||
* any purpose. |
|||
* |
|||
* Written by Sarah E. <sarah.evans@qq.com> |
|||
*/ |
|||
|
|||
#if !defined _RENX_CHATLOG_H_HEADER |
|||
#define _RENX_CHATLOG_H_HEADER |
|||
|
|||
#include "Jupiter/Plugin.h" |
|||
#include "Jupiter/Reference_String.h" |
|||
#include "RenX_Plugin.h" |
|||
|
|||
class RenX_ChatLogPlugin : public RenX::Plugin |
|||
{ |
|||
public: // Jupiter::Plugin
|
|||
bool initialize() override; |
|||
~RenX_ChatLogPlugin(); |
|||
|
|||
public: // RenX::Plugin
|
|||
void RenX_OnTeamChat(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message) override; |
|||
void RenX_OnChat(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message) override; |
|||
|
|||
public: |
|||
void PrepFile(); |
|||
void WriteToLog(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message, std::string in_prefix); |
|||
std::string last_date; |
|||
std::fstream fs; |
|||
}; |
|||
|
|||
#endif // _RENX_CHATLOG_H_HEADER
|
@ -0,0 +1,3 @@ |
|||
add_renx_plugin(RenX.CommandLogging |
|||
RenX_CommandLogging.cpp |
|||
RenX_CommandLogging.h) |
@ -0,0 +1,112 @@ |
|||
/**
|
|||
* This file is in the public domain, furnished "as is", without technical |
|||
* support, and with no warranty, express or implied, as to its usefulness for |
|||
* any purpose. |
|||
* |
|||
* Written by Sarah E. <sarah.evans@qq.com> |
|||
*/ |
|||
|
|||
#include <iostream> |
|||
#include <fstream> |
|||
#include <string> |
|||
#include "RenX_CommandLogging.h" |
|||
#include "RenX_GameCommand.h" |
|||
#include "RenX_PlayerInfo.h" |
|||
#include "RenX_Server.h" |
|||
#include "RenX_Functions.h" |
|||
|
|||
using namespace Jupiter::literals; |
|||
|
|||
void RenX_CommandLoggingPlugin::PrepFile() |
|||
{ |
|||
// Check if date changed (Format: YYYY-MM-DD)
|
|||
std::string current_date = getTimeFormat("%F"); |
|||
std::string full_date = getTimeFormat("%c"); |
|||
if (current_date == last_date) { |
|||
return; |
|||
} |
|||
|
|||
last_date = current_date; |
|||
|
|||
// Close any currently opened file
|
|||
|
|||
if (fs.is_open()) { |
|||
fs.close(); |
|||
} |
|||
|
|||
// Open new file
|
|||
|
|||
std::string file_name = "CommandLog_" + current_date + ".log"; |
|||
fs.open(file_name, std::fstream::out | std::fstream::app); |
|||
|
|||
fs << "Session Start: " |
|||
<< full_date |
|||
<< std::endl; |
|||
} |
|||
|
|||
bool RenX_CommandLoggingPlugin::initialize() |
|||
{ |
|||
RenX_CommandLoggingPlugin::min_access = this->config.get<int>("MinPlayerLevelToLog"_jrs, 1); |
|||
RenX_CommandLoggingPlugin::min_cmd_access = this->config.get<int>("MinCommandLevelToLog"_jrs, 1); |
|||
|
|||
PrepFile(); |
|||
|
|||
return fs.is_open(); |
|||
} |
|||
|
|||
RenX_CommandLoggingPlugin::~RenX_CommandLoggingPlugin() |
|||
{ |
|||
if (fs.is_open()) { |
|||
fs.close(); |
|||
} |
|||
} |
|||
|
|||
void RenX_CommandLoggingPlugin::RenX_OnCommandTriggered(RenX::Server& server, const Jupiter::ReadableString& trigger, RenX::PlayerInfo& player, const Jupiter::ReadableString& parameters, RenX::GameCommand& command) |
|||
{ |
|||
if (player.access < min_access || command.getAccessLevel() < min_cmd_access) { |
|||
return; |
|||
} |
|||
|
|||
WriteToLog(server, player, trigger + " " + parameters); |
|||
} |
|||
|
|||
std::ostream& operator<<(std::ostream& in_stream, const Jupiter::ReadableString& in_string) { |
|||
in_stream.write(in_string.ptr(), in_string.size()); |
|||
return in_stream; |
|||
} |
|||
|
|||
void RenX_CommandLoggingPlugin::WriteToLog(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message) |
|||
{ |
|||
// Check if new file needs to be opened
|
|||
PrepFile(); |
|||
|
|||
if (!fs.is_open()) { |
|||
return; |
|||
} |
|||
|
|||
const std::string& serverHostname = server.getSocketHostname(); |
|||
unsigned short serverPort = server.getSocketPort(); |
|||
|
|||
fs << getTimeFormat("%T") |
|||
<< " " |
|||
<< serverHostname |
|||
<< ":" |
|||
<< serverPort |
|||
<< " " |
|||
<< std::to_string(player.access) |
|||
<< " " |
|||
<< player.name |
|||
<< ": " |
|||
<< message |
|||
<< std::endl; |
|||
} |
|||
|
|||
// Plugin instantiation and entry point.
|
|||
RenX_CommandLoggingPlugin pluginInstance; |
|||
|
|||
extern "C" JUPITER_EXPORT Jupiter::Plugin * getPlugin() |
|||
{ |
|||
return &pluginInstance; |
|||
} |
|||
|
|||
|
@ -0,0 +1,34 @@ |
|||
/**
|
|||
* This file is in the public domain, furnished "as is", without technical |
|||
* support, and with no warranty, express or implied, as to its usefulness for |
|||
* any purpose. |
|||
* |
|||
* Written by Sarah E. <sarah.evans@qq.com> |
|||
*/ |
|||
|
|||
#if !defined _RENX_CMDLOGGING_H_HEADER |
|||
#define _RENX_CMDLOGGING_H_HEADER |
|||
|
|||
#include "Jupiter/Plugin.h" |
|||
#include "Jupiter/Reference_String.h" |
|||
#include "RenX_Plugin.h" |
|||
|
|||
class RenX_CommandLoggingPlugin : public RenX::Plugin |
|||
{ |
|||
public: // Jupiter::Plugin
|
|||
bool initialize() override; |
|||
~RenX_CommandLoggingPlugin(); |
|||
|
|||
public: // RenX::Plugin
|
|||
void RenX_OnCommandTriggered(RenX::Server& server, const Jupiter::ReadableString& trigger, RenX::PlayerInfo& player, const Jupiter::ReadableString& parameters, RenX::GameCommand& command) override; |
|||
|
|||
public: |
|||
void PrepFile(); |
|||
void WriteToLog(RenX::Server& server, const RenX::PlayerInfo& player, const Jupiter::ReadableString& message); |
|||
std::string last_date; |
|||
std::fstream fs; |
|||
int min_access; |
|||
int min_cmd_access; |
|||
}; |
|||
|
|||
#endif // _RENX_CMDLOGGING_H_HEADER
|
Loading…
Reference in new issue