Load log settings from the database

This commit is contained in:
Akkadius 2019-07-05 03:50:01 -05:00
parent feea52f79e
commit ffd652a643
5 changed files with 117 additions and 32 deletions

View File

@ -2065,7 +2065,8 @@ uint32 Database::GetGuildIDByCharID(uint32 character_id)
return atoi(row[0]);
}
void Database::LoadLogSettings(EQEmuLogSys::LogSettings* log_settings) {
void Database::LoadLogSettings(EQEmuLogSys::LogSettings *log_settings)
{
// log_settings previously initialized to '0' by EQEmuLogSys::LoadLogSettingsDefaults()
std::string query =

View File

@ -164,10 +164,10 @@ bool Database::GetLoginTokenDataFromToken(
return false;
}
bool found_username = false;
bool found_login_id = false;
bool found_login_server_name = false;
for (auto row = results.begin(); row != results.end(); ++row) {
bool found_username = false;
bool found_login_id = false;
bool found_login_server_name = false;
for (auto row = results.begin(); row != results.end(); ++row) {
if (strcmp(row[2], "username") == 0) {
user = row[3];
found_username = true;
@ -508,3 +508,92 @@ bool Database::CreateWorldRegistration(std::string long_name, std::string short_
return true;
}
/**
* @param log_settings
*/
void Database::LoadLogSettings(EQEmuLogSys::LogSettings *log_settings)
{
std::string query =
"SELECT "
"log_category_id, "
"log_category_description, "
"log_to_console, "
"log_to_file, "
"log_to_gmsay "
"FROM "
"logsys_categories "
"ORDER BY log_category_id";
auto results = QueryDatabase(query);
int log_category_id = 0;
int categories_in_database[1000] = {};
for (auto row = results.begin(); row != results.end(); ++row) {
log_category_id = atoi(row[0]);
if (log_category_id <= Logs::None || log_category_id >= Logs::MaxCategoryID) {
continue;
}
log_settings[log_category_id].log_to_console = static_cast<uint8>(atoi(row[2]));
log_settings[log_category_id].log_to_file = static_cast<uint8>(atoi(row[3]));
log_settings[log_category_id].log_to_gmsay = static_cast<uint8>(atoi(row[4]));
/**
* Determine if any output method is enabled for the category
* and set it to 1 so it can used to check if category is enabled
*/
const bool log_to_console = log_settings[log_category_id].log_to_console > 0;
const bool log_to_file = log_settings[log_category_id].log_to_file > 0;
const bool log_to_gmsay = log_settings[log_category_id].log_to_gmsay > 0;
const bool is_category_enabled = log_to_console || log_to_file || log_to_gmsay;
if (is_category_enabled) {
log_settings[log_category_id].is_category_enabled = 1;
}
/**
* This determines whether or not the process needs to actually file log anything.
* If we go through this whole loop and nothing is set to any debug level, there is no point to create a file or keep anything open
*/
if (log_settings[log_category_id].log_to_file > 0) {
LogSys.file_logs_enabled = true;
}
categories_in_database[log_category_id] = 1;
}
/**
* Auto inject categories that don't exist in the database...
*/
for (int log_index = Logs::AA; log_index != Logs::MaxCategoryID; log_index++) {
if (!categories_in_database[log_index]) {
Log(Logs::General,
Logs::Status,
"New Log Category '%s' doesn't exist... Automatically adding to `logsys_categories` table...",
Logs::LogCategoryName[log_index]
);
std::string inject_query = StringFormat(
"INSERT INTO logsys_categories "
"(log_category_id, "
"log_category_description, "
"log_to_console, "
"log_to_file, "
"log_to_gmsay) "
"VALUES "
"(%i, '%s', %i, %i, %i)",
log_index,
EscapeString(Logs::LogCategoryName[log_index]).c_str(),
log_settings[log_category_id].log_to_console,
log_settings[log_category_id].log_to_file,
log_settings[log_category_id].log_to_gmsay
);
QueryDatabase(inject_query);
}
}
}

View File

@ -22,6 +22,7 @@
#define EQEMU_DATABASEMYSQL_H
#include "../common/dbcore.h"
#include "../common/eqemu_logsys.h"
#include <string>
#include <sstream>
@ -126,6 +127,7 @@ public:
void UpdateLSAccountInfo(unsigned int id, std::string name, std::string password, std::string email);
void UpdateWorldRegistration(unsigned int id, std::string long_name, std::string ip_address);
bool CreateWorldRegistration(std::string long_name, std::string short_name, unsigned int &id);
void LoadLogSettings(EQEmuLogSys::LogSettings *log_settings);
protected:
std::string user, pass, host, port, name;
MYSQL *database;

View File

@ -48,17 +48,6 @@ int main()
LogSys.LoadLogSettingsDefaults();
LogSys.log_settings[Logs::Error].log_to_console = Logs::General;
LogSys.log_settings[Logs::Error].is_category_enabled = 1;
LogSys.log_settings[Logs::MySQLError].log_to_console = Logs::General;
LogSys.log_settings[Logs::MySQLError].is_category_enabled = 1;
LogSys.log_settings[Logs::MySQLQuery].log_to_console = Logs::General;
LogSys.log_settings[Logs::MySQLQuery].is_category_enabled = 1;
LogSys.log_settings[Logs::Netcode].log_to_console = Logs::General;
LogSys.log_settings[Logs::Netcode].is_category_enabled = Logs::General;
LogSys.log_settings[Logs::Login_Server].log_to_console = Logs::Detail;
LogLoginserver("Logging System Init");
server.config = EQ::JsonConfigFile::Load("login.json");
@ -141,6 +130,8 @@ int main()
server.config.GetVariableString("database", "db", "peq")
);
server.db->LoadLogSettings(LogSys.log_settings);
/**
* make sure our database got created okay, otherwise cleanup and exit
*/

View File

@ -440,7 +440,10 @@ void WorldServer::ProcessLSAccountUpdate(uint16_t opcode, const EQ::Net::Packet
auto *loginserver_update = (ServerLSAccountUpdate_Struct *) packet.Data();
if (is_server_trusted) {
Log(Logs::General, Logs::Netcode, "ServerOP_LSAccountUpdate update processed for: %s", loginserver_update->useraccount);
Log(Logs::General,
Logs::Netcode,
"ServerOP_LSAccountUpdate update processed for: %s",
loginserver_update->useraccount);
std::string name = "";
std::string password = "";
std::string email = "";
@ -698,18 +701,18 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *new_world_server_info
* this is the second of two cases where we should deny access even if unregistered is allowed
*/
if (server_account_name.size() > 0 || server_account_password.size() > 0) {
Log(Logs::General,
Logs::World_Server,
"Server %s(%s) did not attempt to log in but this server requires a password.",
long_name.c_str(),
short_name.c_str());
LogLoginserver(
"Server [{0}] [{1}] did not login but this server required a password to login",
long_name,
short_name
);
}
else {
Log(Logs::General,
Logs::World_Server,
"Server %s(%s) did not attempt to log in but unregistered servers are allowed.",
long_name.c_str(),
short_name.c_str());
LogLoginserver(
"Server [{0}] [{1}] did not login but unregistered servers are allowed",
long_name,
short_name
);
is_server_authorized = true;
SetRuntimeID(server_id);
@ -718,11 +721,10 @@ void WorldServer::Handle_NewLSInfo(ServerNewLSInfo_Struct *new_world_server_info
}
}
else {
LogF(Logs::General,
Logs::World_Server,
"Server [{0}] ({1}) is not registered but unregistered servers are allowed",
long_name,
short_name
LogLoginserver(
"Server [{0}] ({1}) is not registered but unregistered servers are allowed",
long_name,
short_name
);
if (server.db->CreateWorldRegistration(long_name, short_name, server_id)) {