log.cpp 24 KB

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