log.c 23 KB

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