log.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "log.hpp"
  4. #include <stdlib.h>
  5. #include "../common/cbasetypes.hpp"
  6. #include "../common/nullpo.hpp"
  7. #include "../common/showmsg.hpp"
  8. #include "../common/sql.hpp" // SQL_INNODB
  9. #include "../common/strlib.hpp"
  10. #include "battle.hpp"
  11. #include "homunculus.hpp"
  12. #include "itemdb.hpp"
  13. #include "map.hpp"
  14. #include "mob.hpp"
  15. #include "npc.hpp"
  16. #include "pc.hpp"
  17. #include "pet.hpp"
  18. static char log_timestamp_format[20];
  19. /// filters for item logging
  20. typedef enum e_log_filter
  21. {
  22. LOG_FILTER_NONE = 0x000,
  23. LOG_FILTER_ALL = 0x001,
  24. // bits
  25. LOG_FILTER_HEALING = 0x002, // Healing items (0)
  26. LOG_FILTER_ETC_AMMO = 0x004, // Etc Items(3) + Arrows (10)
  27. LOG_FILTER_USABLE = 0x008, // Usable Items(2) + Scrolls, Lures(11) + Usable Cash Items(18)
  28. LOG_FILTER_WEAPON = 0x010, // Weapons(4)
  29. LOG_FILTER_ARMOR = 0x020, // Shields, Armors, Headgears, Accessories, Garments and Shoes(5)
  30. LOG_FILTER_CARD = 0x040, // Cards(6)
  31. LOG_FILTER_PETITEM = 0x080, // Pet Accessories(8) + Eggs(7) (well, monsters don't drop 'em but we'll use the same system for ALL logs)
  32. LOG_FILTER_PRICE = 0x100, // Log expensive items ( >= price_log )
  33. LOG_FILTER_AMOUNT = 0x200, // Log large amount of items ( >= amount_log )
  34. LOG_FILTER_REFINE = 0x400, // Log refined items ( refine >= refine_log ) [not implemented]
  35. LOG_FILTER_CHANCE = 0x800, // Log rare items and Emperium ( drop chance <= rare_log )
  36. }
  37. e_log_filter;
  38. struct Log_Config log_config;
  39. #ifdef SQL_INNODB
  40. // database is using an InnoDB engine so do not use DELAYED
  41. #define LOG_QUERY "INSERT"
  42. #else
  43. // database is using a MyISAM engine so use DELAYED
  44. #define LOG_QUERY "INSERT DELAYED"
  45. #endif
  46. /// obtain log type character for item/zeny logs
  47. static char log_picktype2char(e_log_pick_type type)
  48. {
  49. switch( type )
  50. {
  51. case LOG_TYPE_TRADE: return 'T'; // (T)rade
  52. case LOG_TYPE_VENDING: return 'V'; // (V)ending
  53. case LOG_TYPE_PICKDROP_PLAYER: return 'P'; // (P)layer
  54. case LOG_TYPE_PICKDROP_MONSTER: return 'M'; // (M)onster
  55. case LOG_TYPE_NPC: return 'S'; // NPC (S)hop
  56. case LOG_TYPE_SCRIPT: return 'N'; // (N)PC Script
  57. case LOG_TYPE_STEAL: return 'D'; // Steal/Snatcher
  58. case LOG_TYPE_CONSUME: return 'C'; // (C)onsumed
  59. case LOG_TYPE_PRODUCE: return 'O'; // Pr(O)duced/Ingredients
  60. case LOG_TYPE_MVP: return 'U'; // MVP Rewards
  61. case LOG_TYPE_COMMAND: return 'A'; // (A)dmin command
  62. case LOG_TYPE_STORAGE: return 'R'; // Sto(R)age
  63. case LOG_TYPE_GSTORAGE: return 'G'; // (G)uild storage
  64. case LOG_TYPE_MAIL: return 'E'; // (E)mail attachment
  65. case LOG_TYPE_AUCTION: return 'I'; // Auct(I)on
  66. case LOG_TYPE_BUYING_STORE: return 'B'; // (B)uying Store
  67. case LOG_TYPE_LOOT: return 'L'; // (L)oot (consumed monster pick/drop)
  68. case LOG_TYPE_BANK: return 'K'; // Ban(K) Transactions
  69. case LOG_TYPE_OTHER: return 'X'; // Other
  70. case LOG_TYPE_CASH: return '$'; // Cash
  71. case LOG_TYPE_BOUND_REMOVAL: return 'F'; // Removed bound items when guild/party is broken
  72. case LOG_TYPE_ROULETTE: return 'Y'; // Roulette Lotter(Y)
  73. case LOG_TYPE_MERGE_ITEM: return 'Z'; // Merged Item
  74. case LOG_TYPE_QUEST: return 'Q'; // (Q)uest Item
  75. case LOG_TYPE_PRIVATE_AIRSHIP: return 'H'; // Private Airs(H)ip
  76. case LOG_TYPE_BARTER: return 'J'; // Barter Shop
  77. case LOG_TYPE_LAPHINE: return 'W'; // Laphine UI
  78. }
  79. // should not get here, fallback
  80. ShowDebug("log_picktype2char: Unknown pick type %d.\n", type);
  81. return 'X';
  82. }
  83. /// obtain log type character for chat logs
  84. static char log_chattype2char(e_log_chat_type type)
  85. {
  86. switch( type )
  87. {
  88. case LOG_CHAT_GLOBAL: return 'O'; // Gl(O)bal
  89. case LOG_CHAT_WHISPER: return 'W'; // (W)hisper
  90. case LOG_CHAT_PARTY: return 'P'; // (P)arty
  91. case LOG_CHAT_GUILD: return 'G'; // (G)uild
  92. case LOG_CHAT_MAINCHAT: return 'M'; // (M)ain chat
  93. case LOG_CHAT_CLAN: return 'C'; // (C)lan
  94. }
  95. // should not get here, fallback
  96. ShowDebug("log_chattype2char: Unknown chat type %d.\n", type);
  97. return 'O';
  98. }
  99. static char log_cashtype2char( e_log_cash_type type ){
  100. switch( type ){
  101. case LOG_CASH_TYPE_CASH:
  102. return 'C';
  103. case LOG_CASH_TYPE_KAFRA:
  104. return 'K';
  105. }
  106. ShowDebug("log_chattype2char: Unknown chat type %d.\n", type);
  107. return 'O';
  108. }
  109. static char log_feedingtype2char(e_log_feeding_type type) {
  110. switch(type) {
  111. case LOG_FEED_HOMUNCULUS:
  112. return 'H';
  113. case LOG_FEED_PET:
  114. return 'P';
  115. }
  116. ShowDebug("log_feedingtype2char: Unknown feeding type %d.\n", type);
  117. return 'O';
  118. }
  119. /// check if this item should be logged according the settings
  120. static bool should_log_item(t_itemid nameid, int amount, int refine)
  121. {
  122. int filter = log_config.filter;
  123. struct item_data* id;
  124. if( ( id = itemdb_exists(nameid) ) == NULL )
  125. return false;
  126. if( ( filter&LOG_FILTER_ALL ) ||
  127. ( filter&LOG_FILTER_HEALING && id->type == IT_HEALING ) ||
  128. ( filter&LOG_FILTER_ETC_AMMO && ( id->type == IT_ETC || id->type == IT_AMMO ) ) ||
  129. ( filter&LOG_FILTER_USABLE && ( id->type == IT_USABLE || id->type == IT_CASH ) ) ||
  130. ( filter&LOG_FILTER_WEAPON && id->type == IT_WEAPON ) ||
  131. ( filter&LOG_FILTER_ARMOR && id->type == IT_ARMOR ) ||
  132. ( filter&LOG_FILTER_CARD && id->type == IT_CARD ) ||
  133. ( filter&LOG_FILTER_PETITEM && ( id->type == IT_PETEGG || id->type == IT_PETARMOR ) ) ||
  134. ( filter&LOG_FILTER_PRICE && id->value_buy >= log_config.price_items_log ) ||
  135. ( filter&LOG_FILTER_AMOUNT && abs(amount) >= log_config.amount_items_log ) ||
  136. ( filter&LOG_FILTER_REFINE && refine >= log_config.refine_items_log ) ||
  137. ( filter&LOG_FILTER_CHANCE && ( ( id->maxchance != -1 && id->maxchance <= log_config.rare_items_log ) || id->nameid == ITEMID_EMPERIUM ) )
  138. )
  139. return true;
  140. return false;
  141. }
  142. /// logs items, that summon monsters
  143. void log_branch(struct map_session_data* sd)
  144. {
  145. nullpo_retv(sd);
  146. if( !log_config.branch )
  147. return;
  148. if( log_config.sql_logs ) {
  149. SqlStmt* stmt;
  150. stmt = SqlStmt_Malloc(logmysql_handle);
  151. if( SQL_SUCCESS != SqlStmt_Prepare(stmt, LOG_QUERY " INTO `%s` (`branch_date`, `account_id`, `char_id`, `char_name`, `map`) VALUES (NOW(), '%d', '%d', ?, '%s')", log_config.log_branch, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex) )
  152. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  153. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  154. {
  155. SqlStmt_ShowDebug(stmt);
  156. SqlStmt_Free(stmt);
  157. return;
  158. }
  159. SqlStmt_Free(stmt);
  160. }
  161. else
  162. {
  163. char timestring[255];
  164. time_t curtime;
  165. FILE* logfp;
  166. if( ( logfp = fopen(log_config.log_branch, "a") ) == NULL )
  167. return;
  168. time(&curtime);
  169. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  170. fprintf(logfp,"%s - %s[%d:%d]\t%s\n", timestring, sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex));
  171. fclose(logfp);
  172. }
  173. }
  174. /// logs item transactions (generic)
  175. void log_pick(int id, int16 m, e_log_pick_type type, int amount, struct item* itm)
  176. {
  177. nullpo_retv(itm);
  178. if( ( log_config.enable_logs&type ) == 0 )
  179. {// disabled
  180. return;
  181. }
  182. if( !should_log_item(itm->nameid, amount, itm->refine) )
  183. return; //we skip logging this item set - it doesn't meet our logging conditions [Lupus]
  184. if( log_config.sql_logs )
  185. {
  186. int i;
  187. SqlStmt* stmt = SqlStmt_Malloc(logmysql_handle);
  188. StringBuf buf;
  189. StringBuf_Init(&buf);
  190. StringBuf_Printf(&buf, "%s INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `map`, `unique_id`, `bound`, `enchantgrade`", LOG_QUERY, log_config.log_pick);
  191. for (i = 0; i < MAX_SLOTS; ++i)
  192. StringBuf_Printf(&buf, ", `card%d`", i);
  193. for (i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  194. StringBuf_Printf(&buf, ", `option_id%d`", i);
  195. StringBuf_Printf(&buf, ", `option_val%d`", i);
  196. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  197. }
  198. StringBuf_Printf(&buf, ") VALUES(NOW(),'%u','%c','%u','%d','%d','%s','%" PRIu64 "','%d','%d'",
  199. id, log_picktype2char(type), itm->nameid, amount, itm->refine, map_getmapdata(m)->name[0] ? map_getmapdata(m)->name : "", itm->unique_id, itm->bound, itm->enchantgrade);
  200. for (i = 0; i < MAX_SLOTS; i++)
  201. StringBuf_Printf(&buf, ",'%u'", itm->card[i]);
  202. for (i = 0; i < MAX_ITEM_RDM_OPT; i++)
  203. StringBuf_Printf(&buf, ",'%d','%d','%d'", itm->option[i].id, itm->option[i].value, itm->option[i].param);
  204. StringBuf_Printf(&buf, ")");
  205. if (SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_SUCCESS != SqlStmt_Execute(stmt))
  206. SqlStmt_ShowDebug(stmt);
  207. SqlStmt_Free(stmt);
  208. StringBuf_Destroy(&buf);
  209. }
  210. else
  211. {
  212. char timestring[255];
  213. time_t curtime;
  214. FILE* logfp;
  215. if( ( logfp = fopen(log_config.log_pick, "a") ) == NULL )
  216. return;
  217. time(&curtime);
  218. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  219. fprintf(logfp,"%s - %d\t%c\t%u,%d,%d,%u,%u,%u,%u,%s,'%" PRIu64 "',%d,%d\n", timestring, id, log_picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], map_getmapdata(m)->name[0]?map_getmapdata(m)->name:"", itm->unique_id, itm->bound, itm->enchantgrade);
  220. fclose(logfp);
  221. }
  222. }
  223. /// logs item transactions (players)
  224. void log_pick_pc(struct map_session_data* sd, e_log_pick_type type, int amount, struct item* itm)
  225. {
  226. nullpo_retv(sd);
  227. log_pick(sd->status.char_id, sd->bl.m, type, amount, itm);
  228. }
  229. /// logs item transactions (monsters)
  230. void log_pick_mob(struct mob_data* md, e_log_pick_type type, int amount, struct item* itm)
  231. {
  232. nullpo_retv(md);
  233. log_pick(md->mob_id, md->bl.m, type, amount, itm);
  234. }
  235. /// logs zeny transactions
  236. void log_zeny(struct map_session_data* sd, e_log_pick_type type, struct map_session_data* src_sd, int amount)
  237. {
  238. nullpo_retv(sd);
  239. if( !log_config.zeny || ( log_config.zeny != 1 && abs(amount) < log_config.zeny ) )
  240. return;
  241. if( log_config.sql_logs )
  242. {
  243. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `src_id`, `type`, `amount`, `map`) VALUES (NOW(), '%d', '%d', '%c', '%d', '%s')",
  244. log_config.log_zeny, sd->status.char_id, src_sd->status.char_id, log_picktype2char(type), amount, mapindex_id2name(sd->mapindex)) )
  245. {
  246. Sql_ShowDebug(logmysql_handle);
  247. return;
  248. }
  249. }
  250. else
  251. {
  252. char timestring[255];
  253. time_t curtime;
  254. FILE* logfp;
  255. if( ( logfp = fopen(log_config.log_zeny, "a") ) == NULL )
  256. return;
  257. time(&curtime);
  258. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  259. fprintf(logfp, "%s - %s[%d]\t%s[%d]\t%d\t\n", timestring, src_sd->status.name, src_sd->status.account_id, sd->status.name, sd->status.account_id, amount);
  260. fclose(logfp);
  261. }
  262. }
  263. /// logs MVP monster rewards
  264. void log_mvpdrop(struct map_session_data* sd, int monster_id, t_itemid nameid, t_exp exp )
  265. {
  266. nullpo_retv(sd);
  267. if( !log_config.mvpdrop )
  268. return;
  269. if( log_config.sql_logs )
  270. {
  271. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`mvp_date`, `kill_char_id`, `monster_id`, `prize`, `mvpexp`, `map`) VALUES (NOW(), '%d', '%d', '%u', '%" PRIu64 "', '%s') ",
  272. log_config.log_mvpdrop, sd->status.char_id, monster_id, nameid, exp, mapindex_id2name(sd->mapindex)) )
  273. {
  274. Sql_ShowDebug(logmysql_handle);
  275. return;
  276. }
  277. }
  278. else
  279. {
  280. char timestring[255];
  281. time_t curtime;
  282. FILE* logfp;
  283. if( ( logfp = fopen(log_config.log_mvpdrop,"a") ) == NULL )
  284. return;
  285. time(&curtime);
  286. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  287. fprintf(logfp,"%s - %s[%d:%d]\t%d\t%u,%" PRIu64 "\n", timestring, sd->status.name, sd->status.account_id, sd->status.char_id, monster_id, nameid, exp);
  288. fclose(logfp);
  289. }
  290. }
  291. /// logs used atcommands
  292. void log_atcommand(struct map_session_data* sd, const char* message)
  293. {
  294. nullpo_retv(sd);
  295. if( !log_config.commands ||
  296. !pc_should_log_commands(sd) )
  297. return;
  298. if( log_config.sql_logs )
  299. {
  300. SqlStmt* stmt;
  301. stmt = SqlStmt_Malloc(logmysql_handle);
  302. if( SQL_SUCCESS != SqlStmt_Prepare(stmt, LOG_QUERY " INTO `%s` (`atcommand_date`, `account_id`, `char_id`, `char_name`, `map`, `command`) VALUES (NOW(), '%d', '%d', ?, '%s', ?)", log_config.log_gm, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex) )
  303. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  304. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  305. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  306. {
  307. SqlStmt_ShowDebug(stmt);
  308. SqlStmt_Free(stmt);
  309. return;
  310. }
  311. SqlStmt_Free(stmt);
  312. }
  313. else
  314. {
  315. char timestring[255];
  316. time_t curtime;
  317. FILE* logfp;
  318. if( ( logfp = fopen(log_config.log_gm, "a") ) == NULL )
  319. return;
  320. time(&curtime);
  321. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  322. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  323. fclose(logfp);
  324. }
  325. }
  326. /// logs messages passed to script command 'logmes'
  327. void log_npc( struct npc_data* nd, const char* message ){
  328. nullpo_retv(nd);
  329. if( !log_config.npc )
  330. return;
  331. if( log_config.sql_logs )
  332. {
  333. SqlStmt* stmt;
  334. stmt = SqlStmt_Malloc(logmysql_handle);
  335. if( SQL_SUCCESS != SqlStmt_Prepare(stmt, LOG_QUERY " INTO `%s` (`npc_date`, `char_name`, `map`, `mes`) VALUES (NOW(), ?, '%s', ?)", log_config.log_npc, map_mapid2mapname(nd->bl.m) )
  336. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, nd->name, strnlen(nd->name, NAME_LENGTH))
  337. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  338. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  339. {
  340. SqlStmt_ShowDebug(stmt);
  341. SqlStmt_Free(stmt);
  342. return;
  343. }
  344. SqlStmt_Free(stmt);
  345. }
  346. else
  347. {
  348. char timestring[255];
  349. time_t curtime;
  350. FILE* logfp;
  351. if( ( logfp = fopen(log_config.log_npc, "a") ) == NULL )
  352. return;
  353. time(&curtime);
  354. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  355. fprintf(logfp, "%s - %s: %s\n", timestring, nd->name, message);
  356. fclose(logfp);
  357. }
  358. }
  359. /// logs messages passed to script command 'logmes'
  360. void log_npc(struct map_session_data* sd, const char* message)
  361. {
  362. nullpo_retv(sd);
  363. if( !log_config.npc )
  364. return;
  365. if( log_config.sql_logs )
  366. {
  367. SqlStmt* stmt;
  368. stmt = SqlStmt_Malloc(logmysql_handle);
  369. if( SQL_SUCCESS != SqlStmt_Prepare(stmt, LOG_QUERY " INTO `%s` (`npc_date`, `account_id`, `char_id`, `char_name`, `map`, `mes`) VALUES (NOW(), '%d', '%d', ?, '%s', ?)", log_config.log_npc, sd->status.account_id, sd->status.char_id, mapindex_id2name(sd->mapindex) )
  370. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  371. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  372. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  373. {
  374. SqlStmt_ShowDebug(stmt);
  375. SqlStmt_Free(stmt);
  376. return;
  377. }
  378. SqlStmt_Free(stmt);
  379. }
  380. else
  381. {
  382. char timestring[255];
  383. time_t curtime;
  384. FILE* logfp;
  385. if( ( logfp = fopen(log_config.log_npc, "a") ) == NULL )
  386. return;
  387. time(&curtime);
  388. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  389. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  390. fclose(logfp);
  391. }
  392. }
  393. /// logs chat
  394. void log_chat(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)
  395. {
  396. if( ( log_config.chat&type ) == 0 )
  397. {// disabled
  398. return;
  399. }
  400. if( log_config.log_chat_woe_disable && is_agit_start() )
  401. {// no chat logging during woe
  402. return;
  403. }
  404. if( dst_charname == nullptr ){
  405. dst_charname = "";
  406. }
  407. if( log_config.sql_logs ) {
  408. SqlStmt* stmt;
  409. stmt = SqlStmt_Malloc(logmysql_handle);
  410. if( SQL_SUCCESS != SqlStmt_Prepare(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', ?, ?)", log_config.log_chat, log_chattype2char(type), type_id, src_charid, src_accid, mapname, x, y)
  411. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (char*)dst_charname, safestrnlen(dst_charname, NAME_LENGTH))
  412. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, CHAT_SIZE_MAX))
  413. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  414. {
  415. SqlStmt_ShowDebug(stmt);
  416. SqlStmt_Free(stmt);
  417. return;
  418. }
  419. SqlStmt_Free(stmt);
  420. }
  421. else
  422. {
  423. char timestring[255];
  424. time_t curtime;
  425. FILE* logfp;
  426. if( ( logfp = fopen(log_config.log_chat, "a") ) == NULL )
  427. return;
  428. time(&curtime);
  429. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  430. fprintf(logfp, "%s - %c,%d,%d,%d,%s,%d,%d,%s,%s\n", timestring, log_chattype2char(type), type_id, src_charid, src_accid, mapname, x, y, dst_charname, message);
  431. fclose(logfp);
  432. }
  433. }
  434. /// logs cash transactions
  435. void log_cash( struct map_session_data* sd, e_log_pick_type type, e_log_cash_type cash_type, int amount ){
  436. nullpo_retv( sd );
  437. if( !log_config.cash )
  438. return;
  439. if( log_config.sql_logs ){
  440. if( SQL_ERROR == Sql_Query( logmysql_handle, LOG_QUERY " INTO `%s` ( `time`, `char_id`, `type`, `cash_type`, `amount`, `map` ) VALUES ( NOW(), '%d', '%c', '%c', '%d', '%s' )",
  441. log_config.log_cash, sd->status.char_id, log_picktype2char( type ), log_cashtype2char( cash_type ), amount, mapindex_id2name( sd->mapindex ) ) )
  442. {
  443. Sql_ShowDebug( logmysql_handle );
  444. return;
  445. }
  446. }else{
  447. char timestring[255];
  448. time_t curtime;
  449. FILE* logfp;
  450. if( ( logfp = fopen( log_config.log_cash, "a" ) ) == NULL )
  451. return;
  452. time( &curtime );
  453. strftime( timestring, sizeof( timestring ), log_timestamp_format, localtime( &curtime ) );
  454. fprintf( logfp, "%s - %s[%d]\t%d(%c)\t\n", timestring, sd->status.name, sd->status.account_id, amount, log_cashtype2char( cash_type ) );
  455. fclose( logfp );
  456. }
  457. }
  458. /**
  459. * Log feeding activity
  460. * @param sd Player, feeder
  461. * @param type Log type, @see e_log_feeding_type
  462. * @param nameid Item used as food
  463. **/
  464. void log_feeding(struct map_session_data *sd, e_log_feeding_type type, t_itemid nameid) {
  465. unsigned int target_id = 0, intimacy = 0;
  466. unsigned short target_class = 0;
  467. nullpo_retv( sd );
  468. if (!(log_config.feeding&type))
  469. return;
  470. switch (type) {
  471. case LOG_FEED_HOMUNCULUS:
  472. if (sd->hd) {
  473. target_id = sd->hd->homunculus.hom_id;
  474. target_class = sd->hd->homunculus.class_;
  475. intimacy = sd->hd->homunculus.intimacy;
  476. }
  477. break;
  478. case LOG_FEED_PET:
  479. if (sd->pd) {
  480. target_id = sd->pd->pet.pet_id;
  481. target_class = sd->pd->pet.class_;
  482. intimacy = sd->pd->pet.intimate;
  483. }
  484. break;
  485. }
  486. if (log_config.sql_logs) {
  487. if (SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `target_id`, `target_class`, `type`, `intimacy`, `item_id`, `map`, `x`, `y`) VALUES ( NOW(), '%" PRIu32 "', '%" PRIu32 "', '%hu', '%c', '%" PRIu32 "', '%u', '%s', '%hu', '%hu' )",
  488. log_config.log_feeding, sd->status.char_id, target_id, target_class, log_feedingtype2char(type), intimacy, nameid, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y))
  489. {
  490. Sql_ShowDebug(logmysql_handle);
  491. return;
  492. }
  493. } else {
  494. char timestring[255];
  495. time_t curtime;
  496. FILE* logfp;
  497. if ((logfp = fopen(log_config.log_feeding, "a")) == NULL)
  498. return;
  499. time(&curtime);
  500. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  501. fprintf(logfp, "%s - %s[%d]\t%d\t%d(%c)\t%d\t%u\t%s\t%hu,%hu\n", timestring, sd->status.name, sd->status.char_id, target_id, target_class, log_feedingtype2char(type), intimacy, nameid, mapindex_id2name(sd->mapindex), sd->bl.x, sd->bl.y);
  502. fclose(logfp);
  503. }
  504. }
  505. void log_set_defaults(void)
  506. {
  507. memset(&log_config, 0, sizeof(log_config));
  508. //LOG FILTER Default values
  509. log_config.refine_items_log = 5; // log refined items, with refine >= +5
  510. log_config.rare_items_log = 100; // log rare items. drop chance <= 1%
  511. log_config.price_items_log = 1000; // 1000z
  512. log_config.amount_items_log = 100;
  513. safestrncpy(log_timestamp_format, "%m/%d/%Y %H:%M:%S", sizeof(log_timestamp_format));
  514. }
  515. int log_config_read(const char* cfgName)
  516. {
  517. static int count = 0;
  518. char line[1024], w1[1024], w2[1024];
  519. FILE *fp;
  520. if( count++ == 0 )
  521. log_set_defaults();
  522. if( ( fp = fopen(cfgName, "r") ) == NULL )
  523. {
  524. ShowError("Log configuration file not found at: %s\n", cfgName);
  525. return 1;
  526. }
  527. while( fgets(line, sizeof(line), fp) )
  528. {
  529. if( line[0] == '/' && line[1] == '/' )
  530. continue;
  531. if( sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) == 2 )
  532. {
  533. if( strcmpi(w1, "enable_logs") == 0 )
  534. log_config.enable_logs = (e_log_pick_type)config_switch(w2);
  535. else if( strcmpi(w1, "sql_logs") == 0 )
  536. log_config.sql_logs = config_switch(w2) > 0;
  537. //start of common filter settings
  538. else if( strcmpi(w1, "rare_items_log") == 0 )
  539. log_config.rare_items_log = atoi(w2);
  540. else if( strcmpi(w1, "refine_items_log") == 0 )
  541. log_config.refine_items_log = atoi(w2);
  542. else if( strcmpi(w1, "price_items_log") == 0 )
  543. log_config.price_items_log = atoi(w2);
  544. else if( strcmpi(w1, "amount_items_log") == 0 )
  545. log_config.amount_items_log = atoi(w2);
  546. //end of common filter settings
  547. else if( strcmpi(w1, "log_branch") == 0 )
  548. log_config.branch = config_switch(w2);
  549. else if( strcmpi(w1, "log_filter") == 0 )
  550. log_config.filter = config_switch(w2);
  551. else if( strcmpi(w1, "log_zeny") == 0 )
  552. log_config.zeny = config_switch(w2);
  553. else if( strcmpi( w1, "log_cash" ) == 0 )
  554. log_config.cash = config_switch( w2 ) > 0;
  555. else if( strcmpi(w1, "log_commands") == 0 )
  556. log_config.commands = config_switch(w2);
  557. else if( strcmpi(w1, "log_npc") == 0 )
  558. log_config.npc = config_switch(w2);
  559. else if( strcmpi(w1, "log_chat") == 0 )
  560. log_config.chat = config_switch(w2);
  561. else if( strcmpi(w1, "log_mvpdrop") == 0 )
  562. log_config.mvpdrop = config_switch(w2);
  563. else if( strcmpi(w1, "log_feeding") == 0 )
  564. log_config.feeding = config_switch(w2);
  565. else if( strcmpi(w1, "log_chat_woe_disable") == 0 )
  566. log_config.log_chat_woe_disable = config_switch(w2) > 0;
  567. else if( strcmpi(w1, "log_branch_db") == 0 )
  568. safestrncpy(log_config.log_branch, w2, sizeof(log_config.log_branch));
  569. else if( strcmpi(w1, "log_pick_db") == 0 )
  570. safestrncpy(log_config.log_pick, w2, sizeof(log_config.log_pick));
  571. else if( strcmpi(w1, "log_zeny_db") == 0 )
  572. safestrncpy(log_config.log_zeny, w2, sizeof(log_config.log_zeny));
  573. else if( strcmpi(w1, "log_mvpdrop_db") == 0 )
  574. safestrncpy(log_config.log_mvpdrop, w2, sizeof(log_config.log_mvpdrop));
  575. else if( strcmpi(w1, "log_gm_db") == 0 )
  576. safestrncpy(log_config.log_gm, w2, sizeof(log_config.log_gm));
  577. else if( strcmpi(w1, "log_npc_db") == 0 )
  578. safestrncpy(log_config.log_npc, w2, sizeof(log_config.log_npc));
  579. else if( strcmpi(w1, "log_chat_db") == 0 )
  580. safestrncpy(log_config.log_chat, w2, sizeof(log_config.log_chat));
  581. else if( strcmpi( w1, "log_cash_db" ) == 0 )
  582. safestrncpy( log_config.log_cash, w2, sizeof( log_config.log_cash ) );
  583. else if( strcmpi( w1, "log_feeding_db" ) == 0 )
  584. safestrncpy( log_config.log_feeding, w2, sizeof( log_config.log_feeding ) );
  585. // log file timestamp format
  586. else if( strcmpi( w1, "log_timestamp_format" ) == 0 )
  587. safestrncpy(log_timestamp_format, w2, sizeof(log_timestamp_format));
  588. //support the import command, just like any other config
  589. else if( strcmpi(w1,"import") == 0 )
  590. log_config_read(w2);
  591. else
  592. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  593. }
  594. }
  595. fclose(fp);
  596. if( --count == 0 )
  597. {// report final logging state
  598. const char* target = log_config.sql_logs ? "table" : "file";
  599. if( log_config.enable_logs && log_config.filter )
  600. {
  601. ShowInfo("Logging item transactions to %s '%s'.\n", target, log_config.log_pick);
  602. }
  603. if( log_config.branch )
  604. {
  605. ShowInfo("Logging monster summon item usage to %s '%s'.\n", target, log_config.log_pick);
  606. }
  607. if( log_config.chat )
  608. {
  609. ShowInfo("Logging chat to %s '%s'.\n", target, log_config.log_chat);
  610. }
  611. if( log_config.commands )
  612. {
  613. ShowInfo("Logging commands to %s '%s'.\n", target, log_config.log_gm);
  614. }
  615. if( log_config.mvpdrop )
  616. {
  617. ShowInfo("Logging MVP monster rewards to %s '%s'.\n", target, log_config.log_mvpdrop);
  618. }
  619. if( log_config.npc )
  620. {
  621. ShowInfo("Logging 'logmes' messages to %s '%s'.\n", target, log_config.log_npc);
  622. }
  623. if( log_config.zeny )
  624. {
  625. ShowInfo("Logging Zeny transactions to %s '%s'.\n", target, log_config.log_zeny);
  626. }
  627. if( log_config.cash ){
  628. ShowInfo( "Logging Cash transactions to %s '%s'.\n", target, log_config.log_cash );
  629. }
  630. if( log_config.feeding ){
  631. ShowInfo( "Logging Feeding items to %s '%s'.\n", target, log_config.log_feeding );
  632. }
  633. }
  634. return 0;
  635. }