浏览代码

Added possibility to log without attached player (#3260)

Fixes #2268

Thanks to @Akkarinage
Lemongrass3110 6 年之前
父节点
当前提交
3faf700443
共有 3 个文件被更改,包括 52 次插入5 次删除
  1. 37 0
      src/map/log.cpp
  2. 1 0
      src/map/log.hpp
  3. 14 5
      src/map/script.cpp

+ 37 - 0
src/map/log.cpp

@@ -16,6 +16,7 @@
 #include "itemdb.hpp"
 #include "map.hpp"
 #include "mob.hpp"
+#include "npc.hpp"
 #include "pc.hpp"
 #include "pet.hpp"
 
@@ -375,6 +376,42 @@ void log_atcommand(struct map_session_data* sd, const char* message)
 	}
 }
 
+/// logs messages passed to script command 'logmes'
+void log_npc( struct npc_data* nd, const char* message ){
+	nullpo_retv(nd);
+
+	if( !log_config.npc )
+		return;
+
+	if( log_config.sql_logs )
+	{
+		SqlStmt* stmt;
+		stmt = SqlStmt_Malloc(logmysql_handle);
+		if( SQL_SUCCESS != SqlStmt_Prepare(stmt, LOG_QUERY " INTO `%s` (`npc_date`, `char_name`, `map`, `mes`) VALUES (NOW(), ?, '%s', ?)", log_config.log_npc, map_mapid2mapname(nd->bl.m) )
+		||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, nd->name, strnlen(nd->name, NAME_LENGTH))
+		||  SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, (char*)message, safestrnlen(message, 255))
+		||  SQL_SUCCESS != SqlStmt_Execute(stmt) )
+		{
+			SqlStmt_ShowDebug(stmt);
+			SqlStmt_Free(stmt);
+			return;
+		}
+		SqlStmt_Free(stmt);
+	}
+	else
+	{
+		char timestring[255];
+		time_t curtime;
+		FILE* logfp;
+
+		if( ( logfp = fopen(log_config.log_npc, "a") ) == NULL )
+			return;
+		time(&curtime);
+		strftime(timestring, sizeof(timestring), log_timestamp_format, localtime(&curtime));
+		fprintf(logfp, "%s - %s: %s\n", timestring, nd->name, message);
+		fclose(logfp);
+	}
+}
 
 /// logs messages passed to script command 'logmes'
 void log_npc(struct map_session_data* sd, const char* message)

+ 1 - 0
src/map/log.hpp

@@ -72,6 +72,7 @@ void log_pick_pc(struct map_session_data* sd, e_log_pick_type type, int amount,
 void log_pick_mob(struct mob_data* md, e_log_pick_type type, int amount, struct item* itm);
 void log_zeny(struct map_session_data* sd, e_log_pick_type type, struct map_session_data* src_sd, int amount);
 void log_cash( struct map_session_data* sd, e_log_pick_type type, e_log_cash_type cash_type, int amount );
+void log_npc( struct npc_data* nd, const char* message );
 void log_npc(struct map_session_data* sd, const char *message);
 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);
 void log_atcommand(struct map_session_data* sd, const char* message);

+ 14 - 5
src/map/script.cpp

@@ -15231,13 +15231,22 @@ BUILDIN_FUNC(mapid2name)
 BUILDIN_FUNC(logmes)
 {
 	const char *str;
-	TBL_PC* sd;
-
-	if( !script_rid2sd(sd) )
-		return SCRIPT_CMD_FAILURE;
+	struct map_session_data* sd = map_id2sd(st->rid);
 
 	str = script_getstr(st,2);
-	log_npc(sd,str);
+	if( sd ){
+		log_npc(sd,str);
+	}else{
+		struct npc_data* nd = map_id2nd(st->oid);
+
+		if( !nd ){
+			ShowError( "buildin_logmes: Invalid usage without player or npc.\n" );
+			return SCRIPT_CMD_FAILURE;
+		}
+
+		log_npc(nd,str);
+	}
+
 	return SCRIPT_CMD_SUCCESS;
 }