log.c 23 KB

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