1. #include "stdafx.h"
  2. #include "constants.h"
  3. #include "config.h"
  4. #include "log.h"
  5. #include "char.h"
  6. #include "desc.h"
  7. #include "item.h"
  8. #include "locale_service.h"
  9. static char __escape_hint[1024];
  10. LogManager::LogManager() : m_bIsConnect(false)
  11. {
  12. }
  13. LogManager::~LogManager()
  14. {
  15. }
  16. bool LogManager::Connect(const char * host, const int port, const char * user, const char * pwd, const char * db)
  17. {
  18. if (m_sql.Setup(host, user, pwd, db, g_stLocale.c_str(), false, port))
  19. m_bIsConnect = true;
  20. return m_bIsConnect;
  21. }
  22. void LogManager::Query(const char* c_pszFormat, ...)
  23. {
  24. char szQuery[4096];
  25. va_list args;
  26. va_start(args, c_pszFormat);
  27. vsnprintf(szQuery, sizeof(szQuery), c_pszFormat, args);
  28. va_end(args);
  29. if (test_server)
  30. sys_log(0, "LOG: %s", szQuery);
  31. std::string sQuery(szQuery);
  32. m_sql.AsyncQuery(sQuery.substr(0, sQuery.find_first_of(";") == -1 ? sQuery.length() : sQuery.find_first_of(";")).c_str());
  33. }
  34. void LogManager::ItemLog(DWORD dwPID, DWORD x, DWORD y, DWORD dwItemID, const char * c_pszText, const char * c_pszHint, const char * c_pszIP, DWORD dwVnum)
  35. {
  36. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), c_pszHint, strlen(c_pszHint));
  37. Query("INSERT DELAYED INTO log%s (type, time, who, x, y, what, how, hint, ip, vnum) VALUES('ITEM', NOW(), %u, %u, %u, %u, '%s', '%s', '%s', %u)",
  38. get_table_postfix(), dwPID, x, y, dwItemID, c_pszText, __escape_hint, c_pszIP, dwVnum);
  39. }
  40. void LogManager::ItemLog(LPCHARACTER ch, LPITEM item, const char * c_pszText, const char * c_pszHint)
  41. {
  42. if (NULL == ch || NULL == item)
  43. {
  44. sys_err("character or item nil (ch %p item %p text %s)", get_pointer(ch), get_pointer(item), c_pszText);
  45. return;
  46. }
  47. ItemLog(ch->GetPlayerID(), ch->GetX(), ch->GetY(), item->GetID(),
  48. NULL == c_pszText ? "" : c_pszText,
  49. c_pszHint, ch->GetDesc() ? ch->GetDesc()->GetHostName() : "",
  50. item->GetOriginalVnum());
  51. }
  52. void LogManager::ItemLog(LPCHARACTER ch, int itemID, int itemVnum, const char * c_pszText, const char * c_pszHint)
  53. {
  54. ItemLog(ch->GetPlayerID(), ch->GetX(), ch->GetY(), itemID, c_pszText, c_pszHint, ch->GetDesc() ? ch->GetDesc()->GetHostName() : "", itemVnum);
  55. }
  56. void LogManager::CharLog(DWORD dwPID, DWORD x, DWORD y, DWORD dwValue, const char * c_pszText, const char * c_pszHint, const char * c_pszIP)
  57. {
  58. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), c_pszHint, strlen(c_pszHint));
  59. Query("INSERT DELAYED INTO log%s (type, time, who, x, y, what, how, hint, ip) VALUES('CHARACTER', NOW(), %u, %u, %u, %u, '%s', '%s', '%s')",
  60. get_table_postfix(), dwPID, x, y, dwValue, c_pszText, __escape_hint, c_pszIP);
  61. }
  62. void LogManager::CharLog(LPCHARACTER ch, DWORD dw, const char * c_pszText, const char * c_pszHint)
  63. {
  64. if (ch)
  65. CharLog(ch->GetPlayerID(), ch->GetX(), ch->GetY(), dw, c_pszText, c_pszHint, ch->GetDesc() ? ch->GetDesc()->GetHostName() : "");
  66. else
  67. CharLog(0, 0, 0, dw, c_pszText, c_pszHint, "");
  68. }
  69. void LogManager::LoginLog(bool isLogin, DWORD dwAccountID, DWORD dwPID, BYTE bLevel, BYTE bJob, DWORD dwPlayTime)
  70. {
  71. Query("INSERT DELAYED INTO loginlog%s (type, time, channel, account_id, pid, level, job, playtime) VALUES (%s, NOW(), %d, %u, %u, %d, %d, %u)",
  72. get_table_postfix(), isLogin ? "'LOGIN'" : "'LOGOUT'", g_bChannel, dwAccountID, dwPID, bLevel, bJob, dwPlayTime);
  73. }
  74. void LogManager::MoneyLog(BYTE type, DWORD vnum, int gold)
  75. {
  76. if (type == MONEY_LOG_RESERVED || type >= MONEY_LOG_TYPE_MAX_NUM)
  77. {
  78. sys_err("TYPE ERROR: type %d vnum %u gold %d", type, vnum, gold);
  79. return;
  80. }
  81. Query("INSERT DELAYED INTO money_log%s VALUES (NOW(), %d, %d, %d)", get_table_postfix(), type, vnum, gold);
  82. }
  83. void LogManager::HackLog(const char * c_pszHackName, const char * c_pszLogin, const char * c_pszName, const char * c_pszIP)
  84. {
  85. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), c_pszHackName, strlen(c_pszHackName));
  86. Query("INSERT INTO hack_log (time, login, name, ip, server, why) VALUES(NOW(), '%s', '%s', '%s', '%s', '%s')", c_pszLogin, c_pszName, c_pszIP, g_stHostname.c_str(), __escape_hint);
  87. }
  88. void LogManager::HackLog(const char * c_pszHackName, LPCHARACTER ch)
  89. {
  90. if (ch->GetDesc())
  91. {
  92. HackLog(c_pszHackName,
  93. ch->GetDesc()->GetAccountTable().login,
  94. ch->GetName(),
  95. ch->GetDesc()->GetHostName());
  96. }
  97. }
  98. void LogManager::HackCRCLog(const char * c_pszHackName, const char * c_pszLogin, const char * c_pszName, const char * c_pszIP, DWORD dwCRC)
  99. {
  100. Query("INSERT INTO hack_crc_log (time, login, name, ip, server, why, crc) VALUES(NOW(), '%s', '%s', '%s', '%s', '%s', %u)", c_pszLogin, c_pszName, c_pszIP, g_stHostname.c_str(), c_pszHackName, dwCRC);
  101. }
  102. void LogManager::PCBangLoginLog(DWORD dwPCBangID, const char* c_szPCBangIP, DWORD dwPlayerID, DWORD dwPlayTime)
  103. {
  104. Query("INSERT INTO pcbang_loginlog (time, pcbang_id, ip, pid, play_time) VALUES (NOW(), %u, '%s', %u, %u)",
  105. dwPCBangID, c_szPCBangIP, dwPlayerID, dwPlayTime);
  106. }
  107. void LogManager::GoldBarLog(DWORD dwPID, DWORD dwItemID, GOLDBAR_HOW eHow, const char* c_pszHint)
  108. {
  109. char szHow[32+1];
  110. switch (eHow)
  111. {
  112. case PERSONAL_SHOP_BUY:
  113. snprintf(szHow, sizeof(szHow), "'BUY'");
  114. break;
  115. case PERSONAL_SHOP_SELL:
  116. snprintf(szHow, sizeof(szHow), "'SELL'");
  117. break;
  118. case SHOP_BUY:
  119. snprintf(szHow, sizeof(szHow), "'SHOP_BUY'");
  120. break;
  121. case SHOP_SELL:
  122. snprintf(szHow, sizeof(szHow), "'SHOP_SELL'");
  123. break;
  124. case EXCHANGE_TAKE:
  125. snprintf(szHow, sizeof(szHow), "'EXCHANGE_TAKE'");
  126. break;
  127. case EXCHANGE_GIVE:
  128. snprintf(szHow, sizeof(szHow), "'EXCHANGE_GIVE'");
  129. break;
  130. case QUEST:
  131. snprintf(szHow, sizeof(szHow), "'QUEST'");
  132. break;
  133. default:
  134. snprintf(szHow, sizeof(szHow), "''");
  135. break;
  136. }
  137. Query("INSERT DELAYED INTO goldlog%s (date, time, pid, what, how, hint) VALUES(CURDATE(), CURTIME(), %u, %u, %s, '%s')",
  138. get_table_postfix(), dwPID, dwItemID, szHow, c_pszHint);
  139. }
  140. void LogManager::CubeLog(DWORD dwPID, DWORD x, DWORD y, DWORD item_vnum, DWORD item_uid, int item_count, bool success)
  141. {
  142. Query("INSERT DELAYED INTO cube%s (pid, time, x, y, item_vnum, item_uid, item_count, success) "
  143. "VALUES(%u, NOW(), %u, %u, %u, %u, %d, %d)",
  144. get_table_postfix(), dwPID, x, y, item_vnum, item_uid, item_count, success?1:0);
  145. }
  146. void LogManager::SpeedHackLog(DWORD pid, DWORD x, DWORD y, int hack_count)
  147. {
  148. Query("INSERT INTO speed_hack%s (pid, time, x, y, hack_count) "
  149. "VALUES(%u, NOW(), %u, %u, %d)",
  150. get_table_postfix(), pid, x, y, hack_count);
  151. }
  152. void LogManager::ChangeNameLog(DWORD pid, const char *old_name, const char *new_name, const char *ip)
  153. {
  154. Query("INSERT DELAYED INTO change_name%s (pid, old_name, new_name, time, ip) "
  155. "VALUES(%u, '%s', '%s', NOW(), '%s') ",
  156. get_table_postfix(), pid, old_name, new_name, ip);
  157. }
  158. void LogManager::GMCommandLog(DWORD dwPID, const char* szName, const char* szIP, BYTE byChannel, const char* szCommand)
  159. {
  160. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), szCommand, strlen(szCommand));
  161. Query("INSERT DELAYED INTO command_log%s (userid, server, ip, port, username, command, date ) "
  162. "VALUES(%u, 999, '%s', %u, '%s', '%s', NOW()) ",
  163. get_table_postfix(), dwPID, szIP, byChannel, szName, __escape_hint);
  164. }
  165. void LogManager::RefineLog(DWORD pid, const char* item_name, DWORD item_id, int item_refine_level, int is_success, const char* how)
  166. {
  167. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), item_name, strlen(item_name));
  168. Query("INSERT INTO refinelog%s (pid, item_name, item_id, step, time, is_success, setType) VALUES(%u, '%s', %u, %d, NOW(), %d, '%s')",
  169. get_table_postfix(), pid, __escape_hint, item_id, item_refine_level, is_success, how);
  170. }
  171. void LogManager::ShoutLog(BYTE bChannel, BYTE bEmpire, const char * pszText)
  172. {
  173. m_sql.EscapeString(__escape_hint, sizeof(__escape_hint), pszText, strlen(pszText));
  174. Query("INSERT INTO shout_log%s VALUES(NOW(), %d, %d,'%s')", get_table_postfix(), bChannel, bEmpire, __escape_hint);
  175. }
  176. void LogManager::LevelLog(LPCHARACTER pChar, unsigned int level, unsigned int playhour)
  177. {
  178. if (true == LC_IsEurope())
  179. {
  180. DWORD aid = 0;
  181. if (NULL != pChar->GetDesc())
  182. {
  183. aid = pChar->GetDesc()->GetAccountTable().id;
  184. }
  185. Query("REPLACE INTO levellog%s (name, level, time, account_id, pid, playtime) VALUES('%s', %u, NOW(), %u, %u, %d)",
  186. get_table_postfix(), pChar->GetName(), level, aid, pChar->GetPlayerID(), playhour);
  187. }
  188. else
  189. {
  190. Query("REPLACE INTO levellog%s (name, level, time, playtime) VALUES('%s', %u, NOW(), %d)",
  191. get_table_postfix(), pChar->GetName(), level, playhour);
  192. }
  193. }
  194. void LogManager::BootLog(const char * c_pszHostName, BYTE bChannel)
  195. {
  196. Query("INSERT INTO bootlog (time, hostname, channel) VALUES(NOW(), '%s', %d)",
  197. c_pszHostName, bChannel);
  198. }
  199. void LogManager::VCardLog(DWORD vcard_id, DWORD x, DWORD y, const char * hostname, const char * giver_name, const char * giver_ip, const char * taker_name, const char * taker_ip)
  200. {
  201. Query("INSERT DELAYED INTO vcard_log (vcard_id, x, y, hostname, giver_name, giver_ip, taker_name, taker_ip) VALUES(%u, %u, %u, '%s', '%s', '%s', '%s', '%s')",
  202. vcard_id, x, y, hostname, giver_name, giver_ip, taker_name, taker_ip);
  203. }
  204. void LogManager::FishLog(DWORD dwPID, int prob_idx, int fish_id, int fish_level, DWORD dwMiliseconds, DWORD dwVnum, DWORD dwValue)
  205. {
  206. Query("INSERT INTO fish_log%s VALUES(NOW(), %u, %d, %u, %d, %u, %u, %u)",
  207. get_table_postfix(),
  208. dwPID,
  209. prob_idx,
  210. fish_id,
  211. fish_level,
  212. dwMiliseconds,
  213. dwVnum,
  214. dwValue);
  215. }
  216. void LogManager::QuestRewardLog(const char * c_pszQuestName, DWORD dwPID, DWORD dwLevel, int iValue1, int iValue2)
  217. {
  218. Query("INSERT INTO quest_reward_log%s VALUES('%s',%u,%u,2,%u,%u,NOW())",
  219. get_table_postfix(),
  220. c_pszQuestName,
  221. dwPID,
  222. dwLevel,
  223. iValue1,
  224. iValue2);
  225. }
  226. void LogManager::DetailLoginLog(bool isLogin, LPCHARACTER ch)
  227. {
  228. if (NULL == ch->GetDesc())
  229. return;
  230. if (true == isLogin)
  231. {
  232. Query("INSERT INTO loginlog2(type, is_gm, login_time, channel, account_id, pid, ip, client_version) "
  233. "VALUES('INVALID', %s, NOW(), %d, %u, %u, inet_aton('%s'), '%s')",
  234. ch->IsGM() == true ? "'Y'" : "'N'",
  235. g_bChannel,
  236. ch->GetDesc()->GetAccountTable().id,
  237. ch->GetPlayerID(),
  238. ch->GetDesc()->GetHostName(),
  239. ch->GetDesc()->GetClientVersion());
  240. }
  241. else
  242. {
  243. Query("SET @i = (SELECT MAX(id) FROM loginlog2 WHERE account_id=%u AND pid=%u)",
  244. ch->GetDesc()->GetAccountTable().id,
  245. ch->GetPlayerID());
  246. Query("UPDATE loginlog2 SET type='VALID', logout_time=NOW(), playtime=TIMEDIFF(logout_time,login_time) WHERE id=@i");
  247. }
  248. }
  249. void LogManager::DragonSlayLog(DWORD dwGuildID, DWORD dwDragonVnum, DWORD dwStartTime, DWORD dwEndTime)
  250. {
  251. Query( "INSERT INTO dragon_slay_log%s VALUES( %d, %d, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d) )",
  252. get_table_postfix(),
  253. dwGuildID, dwDragonVnum, dwStartTime, dwEndTime);
  254. }
  255. void LogManager::HackShieldLog(unsigned long ErrorCode, LPCHARACTER ch)
  256. {
  257. struct in_addr st_addr;
  258. #ifndef __WIN32__
  259. if (0 == inet_aton(ch->GetDesc()->GetHostName(), &st_addr))
  260. #else
  261. unsigned long in_address;
  262. in_address = inet_addr(ch->GetDesc()->GetHostName());
  263. st_addr.s_addr = in_address;
  264. if (INADDR_NONE == in_address)
  265. #endif
  266. {
  267. Query( "INSERT INTO hackshield_log(time, account_id, login, pid, name, reason, ip) "
  268. "VALUES(NOW(), %u, '%s', %u, '%s', %u, 0)",
  269. ch->GetDesc()->GetAccountTable().id, ch->GetDesc()->GetAccountTable().login,
  270. ch->GetPlayerID(), ch->GetName(),
  271. ErrorCode);
  272. }
  273. else
  274. {
  275. Query( "INSERT INTO hackshield_log(time, account_id, login, pid, name, reason, ip) "
  276. "VALUES(NOW(), %u, '%s', %u, '%s', %u, inet_aton('%s'))",
  277. ch->GetDesc()->GetAccountTable().id, ch->GetDesc()->GetAccountTable().login,
  278. ch->GetPlayerID(), ch->GetName(),
  279. ErrorCode,
  280. ch->GetDesc()->GetHostName());
  281. }
  282. }

asdad