-
Notifications
You must be signed in to change notification settings - Fork 13
/
RenX_CommandLogging.cpp
112 lines (88 loc) · 2.58 KB
/
RenX_CommandLogging.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* 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;
}