log.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 "battle.h"
  9. #include "itemdb.h"
  10. #include "log.h"
  11. #include "map.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_OTHER: return 'X'; // Other
  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. /// check if this item should be logged according the settings
  88. static bool should_log_item(int nameid, int amount, int refine)
  89. {
  90. int filter = log_config.filter;
  91. struct item_data* id;
  92. if( ( id = itemdb_exists(nameid) ) == NULL )
  93. return false;
  94. if( ( filter&LOG_FILTER_ALL ) ||
  95. ( filter&LOG_FILTER_HEALING && id->type == IT_HEALING ) ||
  96. ( filter&LOG_FILTER_ETC_AMMO && ( id->type == IT_ETC || id->type == IT_AMMO ) ) ||
  97. ( filter&LOG_FILTER_USABLE && ( id->type == IT_USABLE || id->type == IT_CASH ) ) ||
  98. ( filter&LOG_FILTER_WEAPON && id->type == IT_WEAPON ) ||
  99. ( filter&LOG_FILTER_ARMOR && id->type == IT_ARMOR ) ||
  100. ( filter&LOG_FILTER_CARD && id->type == IT_CARD ) ||
  101. ( filter&LOG_FILTER_PETITEM && ( id->type == IT_PETEGG || id->type == IT_PETARMOR ) ) ||
  102. ( filter&LOG_FILTER_PRICE && id->value_buy >= log_config.price_items_log ) ||
  103. ( filter&LOG_FILTER_AMOUNT && abs(amount) >= log_config.amount_items_log ) ||
  104. ( filter&LOG_FILTER_REFINE && refine >= log_config.refine_items_log ) ||
  105. ( filter&LOG_FILTER_CHANCE && ( ( id->maxchance != -1 && id->maxchance <= log_config.rare_items_log ) || id->nameid == ITEMID_EMPERIUM ) )
  106. )
  107. return true;
  108. return false;
  109. }
  110. /// logs items, that summon monsters
  111. void log_branch(struct map_session_data* sd)
  112. {
  113. nullpo_retv(sd);
  114. if( !log_config.branch )
  115. return;
  116. #ifndef TXT_ONLY
  117. if( log_config.sql_logs )
  118. {
  119. SqlStmt* stmt;
  120. stmt = SqlStmt_Malloc(logmysql_handle);
  121. 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) )
  122. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  123. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  124. {
  125. SqlStmt_ShowDebug(stmt);
  126. SqlStmt_Free(stmt);
  127. return;
  128. }
  129. SqlStmt_Free(stmt);
  130. }
  131. else
  132. #endif
  133. {
  134. char timestring[255];
  135. time_t curtime;
  136. FILE* logfp;
  137. if( ( logfp = fopen(log_config.log_branch, "a") ) == NULL )
  138. return;
  139. time(&curtime);
  140. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  141. 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));
  142. fclose(logfp);
  143. }
  144. }
  145. /// logs item transactions (players)
  146. void log_pick_pc(struct map_session_data* sd, e_log_pick_type type, int nameid, int amount, struct item* itm)
  147. {
  148. nullpo_retv(sd);
  149. if( ( log_config.enable_logs&type ) == 0 )
  150. {// disabled
  151. return;
  152. }
  153. if( !should_log_item(nameid, amount, itm ? itm->refine : 0) )
  154. return; //we skip logging this item set - it doesn't meet our logging conditions [Lupus]
  155. #ifndef TXT_ONLY
  156. if( log_config.sql_logs )
  157. {
  158. if( itm == NULL )
  159. {//We log common item
  160. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `map`) VALUES (NOW(), '%d', '%c', '%d', '%d', '%s')",
  161. log_config.log_pick, sd->status.char_id, log_picktype2char(type), nameid, amount, mapindex_id2name(sd->mapindex)) )
  162. {
  163. Sql_ShowDebug(logmysql_handle);
  164. return;
  165. }
  166. }
  167. else
  168. {//We log Extended item
  169. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `card0`, `card1`, `card2`, `card3`, `map`) VALUES (NOW(), '%d', '%c', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s')",
  170. log_config.log_pick, sd->status.char_id, log_picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], mapindex_id2name(sd->mapindex)) )
  171. {
  172. Sql_ShowDebug(logmysql_handle);
  173. return;
  174. }
  175. }
  176. }
  177. else
  178. #endif
  179. {
  180. char timestring[255];
  181. time_t curtime;
  182. FILE* logfp;
  183. if( ( logfp = fopen(log_config.log_pick, "a") ) == NULL )
  184. return;
  185. time(&curtime);
  186. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  187. if( itm == NULL )
  188. {//We log common item
  189. fprintf(logfp,"%s - %d\t%c\t%d,%d,%s\n", timestring, sd->status.char_id, log_picktype2char(type), nameid, amount, mapindex_id2name(sd->mapindex));
  190. }
  191. else
  192. {//We log Extended item
  193. fprintf(logfp,"%s - %d\t%c\t%d,%d,%d,%d,%d,%d,%d,%s\n", timestring, sd->status.char_id, log_picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], mapindex_id2name(sd->mapindex));
  194. }
  195. fclose(logfp);
  196. }
  197. }
  198. /// logs item transactions (monsters)
  199. void log_pick_mob(struct mob_data* md, e_log_pick_type type, int nameid, int amount, struct item* itm)
  200. {
  201. char* mapname;
  202. nullpo_retv(md);
  203. if( ( log_config.enable_logs&type ) == 0 )
  204. {// disabled
  205. return;
  206. }
  207. if( !should_log_item(nameid, amount, itm ? itm->refine : 0) )
  208. return; //we skip logging this item set - it doesn't meet our logging conditions [Lupus]
  209. //either PLAYER or MOB (here we get map name and objects ID)
  210. mapname = map[md->bl.m].name;
  211. if( mapname == NULL )
  212. mapname="";
  213. #ifndef TXT_ONLY
  214. if( log_config.sql_logs )
  215. {
  216. if( itm == NULL )
  217. {//We log common item
  218. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `map`) VALUES (NOW(), '%d', '%c', '%d', '%d', '%s')",
  219. log_config.log_pick, md->class_, log_picktype2char(type), nameid, amount, mapname) )
  220. {
  221. Sql_ShowDebug(logmysql_handle);
  222. return;
  223. }
  224. }
  225. else
  226. {//We log Extended item
  227. if( SQL_ERROR == Sql_Query(logmysql_handle, LOG_QUERY " INTO `%s` (`time`, `char_id`, `type`, `nameid`, `amount`, `refine`, `card0`, `card1`, `card2`, `card3`, `map`) VALUES (NOW(), '%d', '%c', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s')",
  228. log_config.log_pick, md->class_, log_picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], mapname) )
  229. {
  230. Sql_ShowDebug(logmysql_handle);
  231. return;
  232. }
  233. }
  234. }
  235. else
  236. #endif
  237. {
  238. char timestring[255];
  239. time_t curtime;
  240. FILE *logfp;
  241. if( ( logfp = fopen(log_config.log_pick, "a") ) == NULL )
  242. return;
  243. time(&curtime);
  244. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  245. if( itm == NULL )
  246. {//We log common item
  247. fprintf(logfp,"%s - %d\t%c\t%d,%d,%s\n", timestring, md->class_, log_picktype2char(type), nameid, amount, mapname);
  248. }
  249. else
  250. {//We log Extended item
  251. fprintf(logfp,"%s - %d\t%c\t%d,%d,%d,%d,%d,%d,%d,%s\n", timestring, md->class_, log_picktype2char(type), itm->nameid, amount, itm->refine, itm->card[0], itm->card[1], itm->card[2], itm->card[3], mapname);
  252. }
  253. fclose(logfp);
  254. }
  255. }
  256. /// logs item transactions
  257. void log_pick(struct block_list* bl, e_log_pick_type type, int nameid, int amount, struct item* itm)
  258. {
  259. if( bl == NULL )
  260. {
  261. ShowError("log_pick: bl == NULL\n");
  262. }
  263. else switch( bl->type )
  264. {
  265. case BL_PC: log_pick_pc((TBL_PC*)bl, type, nameid, amount, itm); break;
  266. case BL_MOB: log_pick_mob((TBL_MOB*)bl, type, nameid, amount, itm); break;
  267. default:
  268. ShowDebug("log_pick: Unhandled bl type %d.\n", bl->type);
  269. }
  270. }
  271. /// logs zeny transactions
  272. void log_zeny(struct map_session_data* sd, e_log_pick_type type, struct map_session_data* src_sd, int amount)
  273. {
  274. nullpo_retv(sd);
  275. if( !log_config.zeny || ( log_config.zeny != 1 && abs(amount) < log_config.zeny ) )
  276. return;
  277. #ifndef TXT_ONLY
  278. if( log_config.sql_logs )
  279. {
  280. 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')",
  281. log_config.log_zeny, sd->status.char_id, src_sd->status.char_id, log_picktype2char(type), amount, mapindex_id2name(sd->mapindex)) )
  282. {
  283. Sql_ShowDebug(logmysql_handle);
  284. return;
  285. }
  286. }
  287. else
  288. #endif
  289. {
  290. char timestring[255];
  291. time_t curtime;
  292. FILE* logfp;
  293. if( ( logfp = fopen(log_config.log_zeny, "a") ) == NULL )
  294. return;
  295. time(&curtime);
  296. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  297. 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);
  298. fclose(logfp);
  299. }
  300. }
  301. /// logs MVP monster rewards
  302. void log_mvpdrop(struct map_session_data* sd, int monster_id, int* log_mvp)
  303. {
  304. nullpo_retv(sd);
  305. if( !log_config.mvpdrop )
  306. return;
  307. #ifndef TXT_ONLY
  308. if( log_config.sql_logs )
  309. {
  310. 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') ",
  311. log_config.log_mvpdrop, sd->status.char_id, monster_id, log_mvp[0], log_mvp[1], mapindex_id2name(sd->mapindex)) )
  312. {
  313. Sql_ShowDebug(logmysql_handle);
  314. return;
  315. }
  316. }
  317. else
  318. #endif
  319. {
  320. char timestring[255];
  321. time_t curtime;
  322. FILE* logfp;
  323. if( ( logfp = fopen(log_config.log_mvpdrop,"a") ) == NULL )
  324. return;
  325. time(&curtime);
  326. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  327. 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]);
  328. fclose(logfp);
  329. }
  330. }
  331. /// logs used GM commands
  332. void log_atcommand(struct map_session_data* sd, int cmdlvl, const char* message)
  333. {
  334. nullpo_retv(sd);
  335. if( cmdlvl < log_config.gm )
  336. return;
  337. #ifndef TXT_ONLY
  338. if( log_config.sql_logs )
  339. {
  340. SqlStmt* stmt;
  341. stmt = SqlStmt_Malloc(logmysql_handle);
  342. 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) )
  343. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  344. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  345. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  346. {
  347. SqlStmt_ShowDebug(stmt);
  348. SqlStmt_Free(stmt);
  349. return;
  350. }
  351. SqlStmt_Free(stmt);
  352. }
  353. else
  354. #endif
  355. {
  356. char timestring[255];
  357. time_t curtime;
  358. FILE* logfp;
  359. if( ( logfp = fopen(log_config.log_gm, "a") ) == NULL )
  360. return;
  361. time(&curtime);
  362. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  363. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  364. fclose(logfp);
  365. }
  366. }
  367. /// logs messages passed to script command 'logmes'
  368. void log_npc(struct map_session_data* sd, const char* message)
  369. {
  370. nullpo_retv(sd);
  371. if( !log_config.npc )
  372. return;
  373. #ifndef TXT_ONLY
  374. if( log_config.sql_logs )
  375. {
  376. SqlStmt* stmt;
  377. stmt = SqlStmt_Malloc(logmysql_handle);
  378. 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) )
  379. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, sd->status.name, strnlen(sd->status.name, NAME_LENGTH))
  380. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
  381. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  382. {
  383. SqlStmt_ShowDebug(stmt);
  384. SqlStmt_Free(stmt);
  385. return;
  386. }
  387. SqlStmt_Free(stmt);
  388. }
  389. else
  390. #endif
  391. {
  392. char timestring[255];
  393. time_t curtime;
  394. FILE* logfp;
  395. if( ( logfp = fopen(log_config.log_npc, "a") ) == NULL )
  396. return;
  397. time(&curtime);
  398. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  399. fprintf(logfp, "%s - %s[%d]: %s\n", timestring, sd->status.name, sd->status.account_id, message);
  400. fclose(logfp);
  401. }
  402. }
  403. /// logs chat
  404. 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)
  405. {
  406. if( ( log_config.chat&type ) == 0 )
  407. {// disabled
  408. return;
  409. }
  410. if( log_config.log_chat_woe_disable && ( agit_flag || agit2_flag ) )
  411. {// no chat logging during woe
  412. return;
  413. }
  414. #ifndef TXT_ONLY
  415. if( log_config.sql_logs )
  416. {
  417. SqlStmt* stmt;
  418. stmt = SqlStmt_Malloc(logmysql_handle);
  419. 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)
  420. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, (char*)dst_charname, safestrnlen(dst_charname, NAME_LENGTH))
  421. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, CHAT_SIZE_MAX))
  422. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  423. {
  424. SqlStmt_ShowDebug(stmt);
  425. SqlStmt_Free(stmt);
  426. return;
  427. }
  428. SqlStmt_Free(stmt);
  429. }
  430. else
  431. #endif
  432. {
  433. char timestring[255];
  434. time_t curtime;
  435. FILE* logfp;
  436. if( ( logfp = fopen(log_config.log_chat, "a") ) == NULL )
  437. return;
  438. time(&curtime);
  439. strftime(timestring, sizeof(timestring), "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  440. 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);
  441. fclose(logfp);
  442. }
  443. }
  444. void log_set_defaults(void)
  445. {
  446. memset(&log_config, 0, sizeof(log_config));
  447. //LOG FILTER Default values
  448. log_config.refine_items_log = 5; // log refined items, with refine >= +5
  449. log_config.rare_items_log = 100; // log rare items. drop chance <= 1%
  450. log_config.price_items_log = 1000; // 1000z
  451. log_config.amount_items_log = 100;
  452. }
  453. int log_config_read(const char* cfgName)
  454. {
  455. static int count = 0;
  456. char line[1024], w1[1024], w2[1024];
  457. FILE *fp;
  458. if( count++ == 0 )
  459. log_set_defaults();
  460. if( ( fp = fopen(cfgName, "r") ) == NULL )
  461. {
  462. ShowError("Log configuration file not found at: %s\n", cfgName);
  463. return 1;
  464. }
  465. while( fgets(line, sizeof(line), fp) )
  466. {
  467. if( line[0] == '/' && line[1] == '/' )
  468. continue;
  469. if( sscanf(line, "%[^:]: %[^\r\n]", w1, w2) == 2 )
  470. {
  471. if( strcmpi(w1, "enable_logs") == 0 )
  472. log_config.enable_logs = (e_log_pick_type)config_switch(w2);
  473. else if( strcmpi(w1, "sql_logs") == 0 )
  474. {
  475. log_config.sql_logs = (bool)config_switch(w2);
  476. #ifdef TXT_ONLY
  477. if( log_config.sql_logs )
  478. {
  479. ShowWarning("log_config_read: SQL logging is not supported on this server.\n");
  480. log_config.sql_logs = false;
  481. }
  482. #endif
  483. }
  484. //start of common filter settings
  485. else if( strcmpi(w1, "rare_items_log") == 0 )
  486. log_config.rare_items_log = atoi(w2);
  487. else if( strcmpi(w1, "refine_items_log") == 0 )
  488. log_config.refine_items_log = atoi(w2);
  489. else if( strcmpi(w1, "price_items_log") == 0 )
  490. log_config.price_items_log = atoi(w2);
  491. else if( strcmpi(w1, "amount_items_log") == 0 )
  492. log_config.amount_items_log = atoi(w2);
  493. //end of common filter settings
  494. else if( strcmpi(w1, "log_branch") == 0 )
  495. log_config.branch = config_switch(w2);
  496. else if( strcmpi(w1, "log_filter") == 0 )
  497. log_config.filter = config_switch(w2);
  498. else if( strcmpi(w1, "log_zeny") == 0 )
  499. log_config.zeny = config_switch(w2);
  500. else if( strcmpi(w1, "log_gm") == 0 )
  501. log_config.gm = config_switch(w2);
  502. else if( strcmpi(w1, "log_npc") == 0 )
  503. log_config.npc = config_switch(w2);
  504. else if( strcmpi(w1, "log_chat") == 0 )
  505. log_config.chat = config_switch(w2);
  506. else if( strcmpi(w1, "log_mvpdrop") == 0 )
  507. log_config.mvpdrop = config_switch(w2);
  508. else if( strcmpi(w1, "log_chat_woe_disable") == 0 )
  509. log_config.log_chat_woe_disable = (bool)config_switch(w2);
  510. else if( strcmpi(w1, "log_branch_db") == 0 )
  511. safestrncpy(log_config.log_branch, w2, sizeof(log_config.log_branch));
  512. else if( strcmpi(w1, "log_pick_db") == 0 )
  513. safestrncpy(log_config.log_pick, w2, sizeof(log_config.log_pick));
  514. else if( strcmpi(w1, "log_zeny_db") == 0 )
  515. safestrncpy(log_config.log_zeny, w2, sizeof(log_config.log_zeny));
  516. else if( strcmpi(w1, "log_mvpdrop_db") == 0 )
  517. safestrncpy(log_config.log_mvpdrop, w2, sizeof(log_config.log_mvpdrop));
  518. else if( strcmpi(w1, "log_gm_db") == 0 )
  519. safestrncpy(log_config.log_gm, w2, sizeof(log_config.log_gm));
  520. else if( strcmpi(w1, "log_npc_db") == 0 )
  521. safestrncpy(log_config.log_npc, w2, sizeof(log_config.log_npc));
  522. else if( strcmpi(w1, "log_chat_db") == 0 )
  523. safestrncpy(log_config.log_chat, w2, sizeof(log_config.log_chat));
  524. //support the import command, just like any other config
  525. else if( strcmpi(w1,"import") == 0 )
  526. log_config_read(w2);
  527. }
  528. }
  529. fclose(fp);
  530. if( --count == 0 )
  531. {// report final logging state
  532. const char* target = log_config.sql_logs ? "table" : "file";
  533. if( log_config.enable_logs && log_config.filter )
  534. {
  535. ShowInfo("Logging item transactions to %s '%s'.\n", target, log_config.log_pick);
  536. }
  537. if( log_config.branch )
  538. {
  539. ShowInfo("Logging monster summon item usage to %s '%s'.\n", target, log_config.log_pick);
  540. }
  541. if( log_config.chat )
  542. {
  543. ShowInfo("Logging chat to %s '%s'.\n", target, log_config.log_chat);
  544. }
  545. if( log_config.gm )
  546. {
  547. ShowInfo("Logging gm commands to %s '%s'.\n", target, log_config.log_gm);
  548. }
  549. if( log_config.mvpdrop )
  550. {
  551. ShowInfo("Logging MVP monster rewards to %s '%s'.\n", target, log_config.log_mvpdrop);
  552. }
  553. if( log_config.npc )
  554. {
  555. ShowInfo("Logging 'logmes' messages to %s '%s'.\n", target, log_config.log_npc);
  556. }
  557. if( log_config.zeny )
  558. {
  559. ShowInfo("Logging Zeny transactions to %s '%s'.\n", target, log_config.log_zeny);
  560. }
  561. }
  562. return 0;
  563. }