log.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 <cstdlib>
  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. case LOG_TYPE_ENCHANTGRADE: return '0'; // Enchantgrade UI
  79. case LOG_TYPE_REFORM: return '1'; // Reform UI
  80. case LOG_TYPE_ENCHANT: return '2'; // Echant UI
  81. case LOG_TYPE_PACKAGE: return '3'; // Item Package Selection
  82. }
  83. // should not get here, fallback
  84. ShowDebug("log_picktype2char: Unknown pick type %d.\n", type);
  85. return 'X';
  86. }
  87. /// obtain log type character for chat logs
  88. static char log_chattype2char(e_log_chat_type type)
  89. {
  90. switch( type )
  91. {
  92. case LOG_CHAT_GLOBAL: return 'O'; // Gl(O)bal
  93. case LOG_CHAT_WHISPER: return 'W'; // (W)hisper
  94. case LOG_CHAT_PARTY: return 'P'; // (P)arty
  95. case LOG_CHAT_GUILD: return 'G'; // (G)uild
  96. case LOG_CHAT_MAINCHAT: return 'M'; // (M)ain chat
  97. case LOG_CHAT_CLAN: return 'C'; // (C)lan
  98. }
  99. // should not get here, fallback
  100. ShowDebug("log_chattype2char: Unknown chat type %d.\n", type);
  101. return 'O';
  102. }
  103. static char log_cashtype2char( e_log_cash_type type ){
  104. switch( type ){
  105. case LOG_CASH_TYPE_CASH:
  106. return 'C';
  107. case LOG_CASH_TYPE_KAFRA:
  108. return 'K';
  109. }
  110. ShowDebug("log_chattype2char: Unknown chat type %d.\n", type);
  111. return 'O';
  112. }
  113. static char log_feedingtype2char(e_log_feeding_type type) {
  114. switch(type) {
  115. case LOG_FEED_HOMUNCULUS:
  116. return 'H';
  117. case LOG_FEED_PET:
  118. return 'P';
  119. }
  120. ShowDebug("log_feedingtype2char: Unknown feeding type %d.\n", type);
  121. return 'O';
  122. }
  123. /// check if this item should be logged according the settings
  124. static bool should_log_item(t_itemid nameid, int amount, int refine)
  125. {
  126. int filter = log_config.filter;
  127. std::shared_ptr<item_data> id = item_db.find(nameid);
  128. if( id == nullptr )
  129. return false;
  130. if( ( filter&LOG_FILTER_ALL ) ||
  131. ( filter&LOG_FILTER_HEALING && id->type == IT_HEALING ) ||
  132. ( filter&LOG_FILTER_ETC_AMMO && ( id->type == IT_ETC || id->type == IT_AMMO ) ) ||
  133. ( filter&LOG_FILTER_USABLE && ( id->type == IT_USABLE || id->type == IT_CASH ) ) ||
  134. ( filter&LOG_FILTER_WEAPON && id->type == IT_WEAPON ) ||
  135. ( filter&LOG_FILTER_ARMOR && id->type == IT_ARMOR ) ||
  136. ( filter&LOG_FILTER_CARD && id->type == IT_CARD ) ||
  137. ( filter&LOG_FILTER_PETITEM && ( id->type == IT_PETEGG || id->type == IT_PETARMOR ) ) ||
  138. ( filter&LOG_FILTER_PRICE && id->value_buy >= log_config.price_items_log ) ||
  139. ( filter&LOG_FILTER_AMOUNT && abs(amount) >= log_config.amount_items_log ) ||
  140. ( filter&LOG_FILTER_REFINE && refine >= log_config.refine_items_log ) ||
  141. ( filter&LOG_FILTER_CHANCE && ( ( id->maxchance != -1 && id->maxchance <= log_config.rare_items_log ) || id->nameid == ITEMID_EMPERIUM ) )
  142. )
  143. return true;
  144. return false;
  145. }
  146. /// logs items, that summon monsters
  147. void log_branch(map_session_data* sd)
  148. {
  149. nullpo_retv(sd);
  150. if( !log_config.branch )
  151. return;
  152. if( log_config.sql_logs ) {
  153. SqlStmt* stmt;
  154. stmt = SqlStmt_Malloc(logmysql_handle);
  155. 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) )
  156. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  157. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  158. {
  159. SqlStmt_ShowDebug(stmt);
  160. SqlStmt_Free(stmt);
  161. return;
  162. }
  163. SqlStmt_Free(stmt);
  164. }
  165. else
  166. {
  167. char timestring[255];
  168. time_t curtime;
  169. FILE* logfp;
  170. if( ( logfp = fopen(log_config.log_branch, "a") ) == nullptr )
  171. return;
  172. time(&curtime);
  173. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  174. 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));
  175. fclose(logfp);
  176. }
  177. }
  178. /// logs item transactions (generic)
  179. void log_pick(int id, int16 m, e_log_pick_type type, int amount, struct item* itm)
  180. {
  181. nullpo_retv(itm);
  182. if( ( log_config.enable_logs&type ) == 0 )
  183. {// disabled
  184. return;
  185. }
  186. if( !should_log_item(itm->nameid, amount, itm->refine) )
  187. return; //we skip logging this item set - it doesn't meet our logging conditions [Lupus]
  188. if( log_config.sql_logs )
  189. {
  190. int i;
  191. SqlStmt* stmt = SqlStmt_Malloc(logmysql_handle);
  192. StringBuf buf;
  193. StringBuf_Init(&buf);
  194. StringBuf_Printf(&buf, "%s INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `map`, `unique_id`, `bound`, `enchantgrade`", LOG_QUERY, log_config.log_pick);
  195. for (i = 0; i < MAX_SLOTS; ++i)
  196. StringBuf_Printf(&buf, ", `card%d`", i);
  197. for (i = 0; i < MAX_ITEM_RDM_OPT; ++i) {
  198. StringBuf_Printf(&buf, ", `option_id%d`", i);
  199. StringBuf_Printf(&buf, ", `option_val%d`", i);
  200. StringBuf_Printf(&buf, ", `option_parm%d`", i);
  201. }
  202. StringBuf_Printf(&buf, ") VALUES(NOW(),'%u','%c','%u','%d','%d','%s','%" PRIu64 "','%d','%d'",
  203. 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);
  204. for (i = 0; i < MAX_SLOTS; i++)
  205. StringBuf_Printf(&buf, ",'%u'", itm->card[i]);
  206. for (i = 0; i < MAX_ITEM_RDM_OPT; i++)
  207. StringBuf_Printf(&buf, ",'%d','%d','%d'", itm->option[i].id, itm->option[i].value, itm->option[i].param);
  208. StringBuf_Printf(&buf, ")");
  209. if (SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf)) || SQL_SUCCESS != SqlStmt_Execute(stmt))
  210. SqlStmt_ShowDebug(stmt);
  211. SqlStmt_Free(stmt);
  212. StringBuf_Destroy(&buf);
  213. }
  214. else
  215. {
  216. char timestring[255];
  217. time_t curtime;
  218. FILE* logfp;
  219. if( ( logfp = fopen(log_config.log_pick, "a") ) == nullptr )
  220. return;
  221. time(&curtime);
  222. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  223. 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);
  224. fclose(logfp);
  225. }
  226. }
  227. /// logs item transactions (players)
  228. void log_pick_pc(map_session_data* sd, e_log_pick_type type, int amount, struct item* itm)
  229. {
  230. nullpo_retv(sd);
  231. log_pick(sd->status.char_id, sd->bl.m, type, amount, itm);
  232. }
  233. /// logs item transactions (monsters)
  234. void log_pick_mob(struct mob_data* md, e_log_pick_type type, int amount, struct item* itm)
  235. {
  236. nullpo_retv(md);
  237. log_pick(md->mob_id, md->bl.m, type, amount, itm);
  238. }
  239. /// logs zeny transactions
  240. // ids are char_ids
  241. void log_zeny(const map_session_data &target_sd, e_log_pick_type type, uint32 src_id, int amount)
  242. {
  243. if( !log_config.zeny || ( log_config.zeny != 1 && abs(amount) < log_config.zeny ) )
  244. return;
  245. if( log_config.sql_logs )
  246. {
  247. 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')",
  248. log_config.log_zeny, target_sd.status.char_id, src_id, log_picktype2char(type), amount, mapindex_id2name(target_sd.mapindex)))
  249. {
  250. Sql_ShowDebug(logmysql_handle);
  251. return;
  252. }
  253. }
  254. else
  255. {
  256. char timestring[255];
  257. time_t curtime;
  258. FILE* logfp;
  259. if( ( logfp = fopen(log_config.log_zeny, "a") ) == nullptr )
  260. return;
  261. time(&curtime);
  262. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  263. fprintf(logfp, "%s - [%d] ->\t%s[%d]\t%d\t\n", timestring, src_id, target_sd.status.name, target_sd.status.char_id, amount);
  264. fclose(logfp);
  265. }
  266. }
  267. /// logs MVP monster rewards
  268. void log_mvpdrop(map_session_data* sd, int monster_id, t_itemid nameid, t_exp exp )
  269. {
  270. nullpo_retv(sd);
  271. if( !log_config.mvpdrop )
  272. return;
  273. if( log_config.sql_logs )
  274. {
  275. 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') ",
  276. log_config.log_mvpdrop, sd->status.char_id, monster_id, nameid, exp, mapindex_id2name(sd->mapindex)) )
  277. {
  278. Sql_ShowDebug(logmysql_handle);
  279. return;
  280. }
  281. }
  282. else
  283. {
  284. char timestring[255];
  285. time_t curtime;
  286. FILE* logfp;
  287. if( ( logfp = fopen(log_config.log_mvpdrop,"a") ) == nullptr )
  288. return;
  289. time(&curtime);
  290. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  291. 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);
  292. fclose(logfp);
  293. }
  294. }
  295. /// logs used atcommands
  296. void log_atcommand(map_session_data* sd, const char* message)
  297. {
  298. nullpo_retv(sd);
  299. if( !log_config.commands ||
  300. !pc_should_log_commands(sd) )
  301. return;
  302. if( log_config.sql_logs )
  303. {
  304. SqlStmt* stmt;
  305. stmt = SqlStmt_Malloc(logmysql_handle);
  306. 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) )
  307. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  308. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  309. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  310. {
  311. SqlStmt_ShowDebug(stmt);
  312. SqlStmt_Free(stmt);
  313. return;
  314. }
  315. SqlStmt_Free(stmt);
  316. }
  317. else
  318. {
  319. char timestring[255];
  320. time_t curtime;
  321. FILE* logfp;
  322. if( ( logfp = fopen(log_config.log_gm, "a") ) == nullptr )
  323. return;
  324. time(&curtime);
  325. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  326. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  327. fclose(logfp);
  328. }
  329. }
  330. /// logs messages passed to script command 'logmes'
  331. void log_npc( struct npc_data* nd, const char* message ){
  332. nullpo_retv(nd);
  333. if( !log_config.npc )
  334. return;
  335. if( log_config.sql_logs )
  336. {
  337. SqlStmt* stmt;
  338. stmt = SqlStmt_Malloc(logmysql_handle);
  339. 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) )
  340. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, nd->name, strnlen(nd->name, NAME_LENGTH))
  341. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  342. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  343. {
  344. SqlStmt_ShowDebug(stmt);
  345. SqlStmt_Free(stmt);
  346. return;
  347. }
  348. SqlStmt_Free(stmt);
  349. }
  350. else
  351. {
  352. char timestring[255];
  353. time_t curtime;
  354. FILE* logfp;
  355. if( ( logfp = fopen(log_config.log_npc, "a") ) == nullptr )
  356. return;
  357. time(&curtime);
  358. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  359. fprintf(logfp, "%s - %s: %s\n", timestring, nd->name, message);
  360. fclose(logfp);
  361. }
  362. }
  363. /// logs messages passed to script command 'logmes'
  364. void log_npc(map_session_data* sd, const char* message)
  365. {
  366. nullpo_retv(sd);
  367. if( !log_config.npc )
  368. return;
  369. if( log_config.sql_logs )
  370. {
  371. SqlStmt* stmt;
  372. stmt = SqlStmt_Malloc(logmysql_handle);
  373. 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) )
  374. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  375. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  376. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  377. {
  378. SqlStmt_ShowDebug(stmt);
  379. SqlStmt_Free(stmt);
  380. return;
  381. }
  382. SqlStmt_Free(stmt);
  383. }
  384. else
  385. {
  386. char timestring[255];
  387. time_t curtime;
  388. FILE* logfp;
  389. if( ( logfp = fopen(log_config.log_npc, "a") ) == nullptr )
  390. return;
  391. time(&curtime);
  392. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  393. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  394. fclose(logfp);
  395. }
  396. }
  397. /// logs chat
  398. 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)
  399. {
  400. if( ( log_config.chat&type ) == 0 )
  401. {// disabled
  402. return;
  403. }
  404. if( log_config.log_chat_woe_disable && is_agit_start() )
  405. {// no chat logging during woe
  406. return;
  407. }
  408. if( dst_charname == nullptr ){
  409. dst_charname = "";
  410. }
  411. if( log_config.sql_logs ) {
  412. SqlStmt* stmt;
  413. stmt = SqlStmt_Malloc(logmysql_handle);
  414. 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)
  415. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (char*)dst_charname, safestrnlen(dst_charname, NAME_LENGTH))
  416. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, CHAT_SIZE_MAX))
  417. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  418. {
  419. SqlStmt_ShowDebug(stmt);
  420. SqlStmt_Free(stmt);
  421. return;
  422. }
  423. SqlStmt_Free(stmt);
  424. }
  425. else
  426. {
  427. char timestring[255];
  428. time_t curtime;
  429. FILE* logfp;
  430. if( ( logfp = fopen(log_config.log_chat, "a") ) == nullptr )
  431. return;
  432. time(&curtime);
  433. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  434. 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);
  435. fclose(logfp);
  436. }
  437. }
  438. /// logs cash transactions
  439. void log_cash( map_session_data* sd, e_log_pick_type type, e_log_cash_type cash_type, int amount ){
  440. nullpo_retv( sd );
  441. if( !log_config.cash )
  442. return;
  443. if( log_config.sql_logs ){
  444. 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' )",
  445. log_config.log_cash, sd->status.char_id, log_picktype2char( type ), log_cashtype2char( cash_type ), amount, mapindex_id2name( sd->mapindex ) ) )
  446. {
  447. Sql_ShowDebug( logmysql_handle );
  448. return;
  449. }
  450. }else{
  451. char timestring[255];
  452. time_t curtime;
  453. FILE* logfp;
  454. if( ( logfp = fopen( log_config.log_cash, "a" ) ) == nullptr )
  455. return;
  456. time( &curtime );
  457. strftime( timestring, sizeof( timestring ), log_timestamp_format, localtime( &curtime ) );
  458. fprintf( logfp, "%s - %s[%d]\t%d(%c)\t\n", timestring, sd->status.name, sd->status.account_id, amount, log_cashtype2char( cash_type ) );
  459. fclose( logfp );
  460. }
  461. }
  462. /**
  463. * Log feeding activity
  464. * @param sd Player, feeder
  465. * @param type Log type, @see e_log_feeding_type
  466. * @param nameid Item used as food
  467. **/
  468. void log_feeding(map_session_data *sd, e_log_feeding_type type, t_itemid nameid) {
  469. unsigned int target_id = 0, intimacy = 0;
  470. unsigned short target_class = 0;
  471. nullpo_retv( sd );
  472. if (!(log_config.feeding&type))
  473. return;
  474. switch (type) {
  475. case LOG_FEED_HOMUNCULUS:
  476. if (sd->hd) {
  477. target_id = sd->hd->homunculus.hom_id;
  478. target_class = sd->hd->homunculus.class_;
  479. intimacy = sd->hd->homunculus.intimacy;
  480. }
  481. break;
  482. case LOG_FEED_PET:
  483. if (sd->pd) {
  484. target_id = sd->pd->pet.pet_id;
  485. target_class = sd->pd->pet.class_;
  486. intimacy = sd->pd->pet.intimate;
  487. }
  488. break;
  489. }
  490. if (log_config.sql_logs) {
  491. 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' )",
  492. 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))
  493. {
  494. Sql_ShowDebug(logmysql_handle);
  495. return;
  496. }
  497. } else {
  498. char timestring[255];
  499. time_t curtime;
  500. FILE* logfp;
  501. if ((logfp = fopen(log_config.log_feeding, "a")) == nullptr)
  502. return;
  503. time(&curtime);
  504. strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
  505. 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);
  506. fclose(logfp);
  507. }
  508. }
  509. void log_set_defaults(void)
  510. {
  511. memset(&log_config, 0, sizeof(log_config));
  512. //LOG FILTER Default values
  513. log_config.refine_items_log = 5; // log refined items, with refine >= +5
  514. log_config.rare_items_log = 100; // log rare items. drop chance <= 1%
  515. log_config.price_items_log = 1000; // 1000z
  516. log_config.amount_items_log = 100;
  517. safestrncpy(log_timestamp_format, "%m/%d/%Y %H:%M:%S", sizeof(log_timestamp_format));
  518. }
  519. int log_config_read(const char* cfgName)
  520. {
  521. static int count = 0;
  522. char line[1024], w1[1024], w2[1024];
  523. FILE *fp;
  524. if( count++ == 0 )
  525. log_set_defaults();
  526. if( ( fp = fopen(cfgName, "r") ) == nullptr )
  527. {
  528. ShowError("Log configuration file not found at: %s\n", cfgName);
  529. return 1;
  530. }
  531. while( fgets(line, sizeof(line), fp) )
  532. {
  533. if( line[0] == '/' && line[1] == '/' )
  534. continue;
  535. if( sscanf(line, "%1023[^:]: %1023[^\r\n]", w1, w2) == 2 )
  536. {
  537. if( strcmpi(w1, "enable_logs") == 0 )
  538. log_config.enable_logs = (e_log_pick_type)config_switch(w2);
  539. else if( strcmpi(w1, "sql_logs") == 0 )
  540. log_config.sql_logs = config_switch(w2) > 0;
  541. //start of common filter settings
  542. else if( strcmpi(w1, "rare_items_log") == 0 )
  543. log_config.rare_items_log = atoi(w2);
  544. else if( strcmpi(w1, "refine_items_log") == 0 )
  545. log_config.refine_items_log = atoi(w2);
  546. else if( strcmpi(w1, "price_items_log") == 0 )
  547. log_config.price_items_log = atoi(w2);
  548. else if( strcmpi(w1, "amount_items_log") == 0 )
  549. log_config.amount_items_log = atoi(w2);
  550. //end of common filter settings
  551. else if( strcmpi(w1, "log_branch") == 0 )
  552. log_config.branch = config_switch(w2);
  553. else if( strcmpi(w1, "log_filter") == 0 )
  554. log_config.filter = config_switch(w2);
  555. else if( strcmpi(w1, "log_zeny") == 0 )
  556. log_config.zeny = config_switch(w2);
  557. else if( strcmpi( w1, "log_cash" ) == 0 )
  558. log_config.cash = config_switch( w2 ) > 0;
  559. else if( strcmpi(w1, "log_commands") == 0 )
  560. log_config.commands = config_switch(w2);
  561. else if( strcmpi(w1, "log_npc") == 0 )
  562. log_config.npc = config_switch(w2);
  563. else if( strcmpi(w1, "log_chat") == 0 )
  564. log_config.chat = config_switch(w2);
  565. else if( strcmpi(w1, "log_mvpdrop") == 0 )
  566. log_config.mvpdrop = config_switch(w2);
  567. else if( strcmpi(w1, "log_feeding") == 0 )
  568. log_config.feeding = config_switch(w2);
  569. else if( strcmpi(w1, "log_chat_woe_disable") == 0 )
  570. log_config.log_chat_woe_disable = config_switch(w2) > 0;
  571. else if( strcmpi(w1, "log_branch_db") == 0 )
  572. safestrncpy(log_config.log_branch, w2, sizeof(log_config.log_branch));
  573. else if( strcmpi(w1, "log_pick_db") == 0 )
  574. safestrncpy(log_config.log_pick, w2, sizeof(log_config.log_pick));
  575. else if( strcmpi(w1, "log_zeny_db") == 0 )
  576. safestrncpy(log_config.log_zeny, w2, sizeof(log_config.log_zeny));
  577. else if( strcmpi(w1, "log_mvpdrop_db") == 0 )
  578. safestrncpy(log_config.log_mvpdrop, w2, sizeof(log_config.log_mvpdrop));
  579. else if( strcmpi(w1, "log_gm_db") == 0 )
  580. safestrncpy(log_config.log_gm, w2, sizeof(log_config.log_gm));
  581. else if( strcmpi(w1, "log_npc_db") == 0 )
  582. safestrncpy(log_config.log_npc, w2, sizeof(log_config.log_npc));
  583. else if( strcmpi(w1, "log_chat_db") == 0 )
  584. safestrncpy(log_config.log_chat, w2, sizeof(log_config.log_chat));
  585. else if( strcmpi( w1, "log_cash_db" ) == 0 )
  586. safestrncpy( log_config.log_cash, w2, sizeof( log_config.log_cash ) );
  587. else if( strcmpi( w1, "log_feeding_db" ) == 0 )
  588. safestrncpy( log_config.log_feeding, w2, sizeof( log_config.log_feeding ) );
  589. // log file timestamp format
  590. else if( strcmpi( w1, "log_timestamp_format" ) == 0 )
  591. safestrncpy(log_timestamp_format, w2, sizeof(log_timestamp_format));
  592. //support the import command, just like any other config
  593. else if( strcmpi(w1,"import") == 0 )
  594. log_config_read(w2);
  595. else
  596. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  597. }
  598. }
  599. fclose(fp);
  600. if( --count == 0 )
  601. {// report final logging state
  602. const char* target = log_config.sql_logs ? "table" : "file";
  603. if( log_config.enable_logs && log_config.filter )
  604. {
  605. ShowInfo("Logging item transactions to %s '%s'.\n", target, log_config.log_pick);
  606. }
  607. if( log_config.branch )
  608. {
  609. ShowInfo("Logging monster summon item usage to %s '%s'.\n", target, log_config.log_pick);
  610. }
  611. if( log_config.chat )
  612. {
  613. ShowInfo("Logging chat to %s '%s'.\n", target, log_config.log_chat);
  614. }
  615. if( log_config.commands )
  616. {
  617. ShowInfo("Logging commands to %s '%s'.\n", target, log_config.log_gm);
  618. }
  619. if( log_config.mvpdrop )
  620. {
  621. ShowInfo("Logging MVP monster rewards to %s '%s'.\n", target, log_config.log_mvpdrop);
  622. }
  623. if( log_config.npc )
  624. {
  625. ShowInfo("Logging 'logmes' messages to %s '%s'.\n", target, log_config.log_npc);
  626. }
  627. if( log_config.zeny )
  628. {
  629. ShowInfo("Logging Zeny transactions to %s '%s'.\n", target, log_config.log_zeny);
  630. }
  631. if( log_config.cash ){
  632. ShowInfo( "Logging Cash transactions to %s '%s'.\n", target, log_config.log_cash );
  633. }
  634. if( log_config.feeding ){
  635. ShowInfo( "Logging Feeding items to %s '%s'.\n", target, log_config.log_feeding );
  636. }
  637. }
  638. return 0;
  639. }