From e0d1737a0bb383ce7d8d5354e057e0cd35ccadcb Mon Sep 17 00:00:00 2001 From: Jesusaves Date: Fri, 6 May 2022 22:31:29 -0300 Subject: [TMW2] If PYTHON_LOGS is defined, send some logs to a python script (logmaster.py) If the script cannot be loaded, it fallbacks to standard SQL logging. --- src/map/log.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/map/log.h | 7 +++++ 2 files changed, 95 insertions(+), 4 deletions(-) diff --git a/src/map/log.c b/src/map/log.c index 725793a31..572a27d6e 100644 --- a/src/map/log.c +++ b/src/map/log.c @@ -40,6 +40,26 @@ #include #include #include +#ifdef PYTHON_LOGS +#include + +static int pylog(char* sql_statement) { + if (sql_statement != NULL) { + if (fprintf(logs->pystream, "%s\r\n", sql_statement) < 0) { + ShowError("Pylog: Error saving statement: %s\n", sql_statement); + return SQL_ERROR; + } + if (fflush(logs->pystream) != 0) { + ShowError("Pylog: Error flushing buffer\n"); + return SQL_ERROR; + } + if (setvbuf(logs->pystream, NULL, _IONBF, 0) == 0) + return SQL_SUCCESS; + } + ShowError("Pylog: Invalid SQL Statement or setvbuf error\n"); + return SQL_ERROR; +} +#endif static struct log_interface log_s; struct log_interface *logs; @@ -147,6 +167,7 @@ static void log_branch_sub_sql(struct map_session_data *sd) return; } SQL->StmtFree(stmt); + return; } static void log_branch_sub_txt(struct map_session_data *sd) { @@ -176,6 +197,7 @@ static void log_branch(struct map_session_data *sd) static void log_pick_sub_sql(int id, int16 m, e_log_pick_type type, int amount, struct item *itm, struct item_data *data) { nullpo_retv(itm); +#ifndef PYTHON_LOGS if (SQL_ERROR == SQL->Query(logs->mysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `card0`, `card1`, `card2`, `card3`, " "`opt_idx0`, `opt_val0`, `opt_idx1`, `opt_val1`, `opt_idx2`, `opt_val2`, `opt_idx3`, `opt_val3`, `opt_idx4`, `opt_val4`, `map`, `unique_id`) " @@ -188,6 +210,20 @@ static void log_pick_sub_sql(int id, int16 m, e_log_pick_type type, int amount, Sql_ShowDebug(logs->mysql_handle); return; } +#else + char bf1[1024], bf4[16]; + sprintf(bf1, "SQL→INSERT INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `card0`, `card1`, `card2`, `card3`, " + "`opt_idx0`, `opt_val0`, `opt_idx1`, `opt_val1`, `opt_idx2`, `opt_val2`, `opt_idx3`, `opt_val3`, `opt_idx4`, `opt_val4`, `map`, `unique_id`) " + "VALUES (NOW(), '%d', '%c', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%"PRIu64"')", + logs->config.log_pick, id, logs->picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], + itm->option[0].index, itm->option[0].value, itm->option[1].index, itm->option[1].value, itm->option[2].index, itm->option[2].value, + itm->option[3].index, itm->option[3].value, itm->option[4].index, itm->option[4].value, + map->list[m].name, itm->unique_id); + pylog(bf1); + sprintf(bf4, "SQLRUN→"); + pylog(bf4); +#endif + return; } static void log_pick_sub_txt(int id, int16 m, e_log_pick_type type, int amount, struct item *itm, struct item_data *data) { @@ -238,12 +274,22 @@ static void log_zeny_sub_sql(struct map_session_data *sd, e_log_pick_type type, { nullpo_retv(sd); nullpo_retv(src_sd); +#ifndef PYTHON_LOGS if( SQL_ERROR == SQL->Query(logs->mysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `src_id`, `type`, `amount`, `map`) VALUES (NOW(), '%d', '%d', '%c', '%d', '%s')", logs->config.log_zeny, sd->status.char_id, src_sd->status.char_id, logs->picktype2char(type), amount, mapindex_id2name(sd->mapindex)) ) { Sql_ShowDebug(logs->mysql_handle); return; } +#else + char bf1[1024], bf4[16]; + sprintf(bf1, "SQL→INSERT INTO `%s` (`time`, `char_id`, `src_id`, `type`, `amount`, `map`) VALUES (NOW(), '%d', '%d', '%c', '%d', '%s')", + logs->config.log_zeny, sd->status.char_id, src_sd->status.char_id, logs->picktype2char(type), amount, mapindex_id2name(sd->mapindex)); + pylog(bf1); + sprintf(bf4, "SQLRUN→"); + pylog(bf4); +#endif + return; } static void log_zeny_sub_txt(struct map_session_data *sd, e_log_pick_type type, struct map_session_data *src_sd, int amount) { @@ -280,6 +326,7 @@ static void log_mvpdrop_sub_sql(struct map_session_data *sd, int monster_id, int Sql_ShowDebug(logs->mysql_handle); return; } + return; } static void log_mvpdrop_sub_txt(struct map_session_data *sd, int monster_id, int *log_mvp) { @@ -309,10 +356,11 @@ static void log_mvpdrop(struct map_session_data *sd, int monster_id, int *log_mv static void log_atcommand_sub_sql(struct map_session_data *sd, const char *message) { - struct SqlStmt *stmt; - nullpo_retv(sd); nullpo_retv(message); + +#ifndef PYTHON_LOGS + struct SqlStmt *stmt; stmt = SQL->StmtMalloc(logs->mysql_handle); if( SQL_SUCCESS != SQL->StmtPrepare(stmt, LOG_QUERY " INTO `%s` (`atcommand_date`, `account_id`, `char_id`, `char_name`, `map`, `command`) VALUES (NOW(), '%d', '%d', ?, '%s', ?)", logs->config.log_gm, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex) ) || SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH)) @@ -324,6 +372,18 @@ static void log_atcommand_sub_sql(struct map_session_data *sd, const char *messa return; } SQL->StmtFree(stmt); +#else + char bf1[1024], bf2[127], bf3[512], bf4[16]; + sprintf(bf1, "SQL→INSERT INTO `%s` (`atcommand_date`, `account_id`, `char_id`, `char_name`, `map`, `command`) VALUES (NOW(), '%d', '%d', '?1', '%s', '?2')", logs->config.log_gm, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex)); + pylog(bf1); + sprintf(bf2, "SAD1→%s", sd->status.name); + pylog(bf2); + sprintf(bf3, "SAD2→%s", message); + pylog(bf3); + sprintf(bf4, "SQLRUN→"); + pylog(bf4); +#endif + return; } static void log_atcommand_sub_txt(struct map_session_data *sd, const char *message) { @@ -369,6 +429,7 @@ static void log_npc_sub_sql(struct map_session_data *sd, const char *message) return; } SQL->StmtFree(stmt); + return; } static void log_npc_sub_txt(struct map_session_data *sd, const char *message) { @@ -411,10 +472,11 @@ static void log_npc(struct map_session_data *sd, const char *message) */ static void log_chat_sub_sql(e_log_chat_type type, int type_id, int src_charid, int src_accid, const char *mapname, int x, int y, const char *dst_charname, const char *message) { - struct SqlStmt* stmt; - nullpo_retv(dst_charname); nullpo_retv(message); + +#ifndef PYTHON_LOGS + struct SqlStmt* stmt; stmt = SQL->StmtMalloc(logs->mysql_handle); if( SQL_SUCCESS != SQL->StmtPrepare(stmt, LOG_QUERY " INTO `%s` (`time`, `type`, `type_id`, `src_charid`, `src_accountid`, `src_map`, `src_map_x`, `src_map_y`, `dst_charname`, `message`) VALUES (NOW(), '%c', '%d', '%d', '%d', '%s', '%d', '%d', ?, ?)", logs->config.log_chat, logs->chattype2char(type), type_id, src_charid, src_accid, mapname, x, y) || SQL_SUCCESS != SQL->StmtBindParam(stmt, 0, SQLDT_STRING, dst_charname, safestrnlen(dst_charname, NAME_LENGTH)) @@ -426,6 +488,18 @@ static void log_chat_sub_sql(e_log_chat_type type, int type_id, int src_charid, return; } SQL->StmtFree(stmt); +#else + char bf1[1024], bf2[127], bf3[512], bf4[16]; + sprintf(bf1, "SQL→INSERT INTO `%s` (`time`, `type`, `type_id`, `src_charid`, `src_accountid`, `src_map`, `src_map_x`, `src_map_y`, `dst_charname`, `message`) VALUES (NOW(), '%c', '%d', '%d', '%d', '%s', '%d', '%d', '?1', '?2')", logs->config.log_chat, logs->chattype2char(type), type_id, src_charid, src_accid, mapname, x, y); + pylog(bf1); + sprintf(bf2, "SAD1→%s", dst_charname); + pylog(bf2); + sprintf(bf3, "SAD2→%s", message); + pylog(bf3); + sprintf(bf4, "SQLRUN→"); + pylog(bf4); +#endif + return; } /** @@ -791,6 +865,16 @@ void log_defaults(void) logs->mysql_handle = NULL; /* */ +#ifdef PYTHON_LOGS + logs->pystream = popen("./logmaster.py", "we"); + if (logs->pystream == NULL) { + ShowError("pylog: popen error\n"); + #undef PYTHON_LOGS + } else { + logs->pylog = pylog; + } +#endif + logs->pick_pc = log_pick_pc; logs->pick_mob = log_pick_mob; logs->zeny = log_zeny; diff --git a/src/map/log.h b/src/map/log.h index 116d99bfd..b12afbdb5 100644 --- a/src/map/log.h +++ b/src/map/log.h @@ -31,6 +31,9 @@ struct item; struct item_data; struct map_session_data; struct mob_data; +#ifdef PYTHON_LOGS +#include +#endif /** * Defines @@ -130,6 +133,10 @@ struct log_interface { char db_pw[100]; char db_name[32]; struct Sql *mysql_handle; + #ifdef PYTHON_LOGS + FILE * pystream; + int (*pylog) (char* sql_statement); + #endif /* */ void (*pick_pc) (struct map_session_data* sd, e_log_pick_type type, int amount, struct item* itm, struct item_data *data); void (*pick_mob) (struct mob_data* md, e_log_pick_type type, int amount, struct item* itm, struct item_data *data); -- cgit v1.2.3-60-g2f50