using System; using System.Collections.Generic; namespace Sog.Log { public class BattleLogger { private static Dictionary m_allUser = new Dictionary(); private static Dictionary m_allBattle = new Dictionary(); public int m_logLevel; private string m_logPath; private string m_logFileName; // 是否log所有玩家,外网请关闭 private bool m_logAllUser; // 这个加了后就不会删,只是占用点内存,无所谓了 private long[] m_logSomeUser; private bool m_logByBattleId; public void Init(string path, string filename, int logLevel, bool logAllUser, long[] logSomeUser, bool logByBattleId) { m_logLevel = logLevel; m_logPath = path; m_logFileName = filename; m_logAllUser = logAllUser; m_logSomeUser = logSomeUser; m_logByBattleId = logByBattleId; m_allUser.Clear(); m_allBattle.Clear(); } public int GetLogLevel() { return m_logLevel; } public bool NeedLogUser(long uid) { if(m_logAllUser) { return true; } if(m_logSomeUser == null) { return false; } for (int i = 0; i < m_logSomeUser.Length; i++) { if (m_logSomeUser[i] == uid) { return true; } } return false; } public void LogByLevel(long uid, ulong battleId, int frame, int logLevel, string strFormat, params object[] argvList) { //先判断等级 if (logLevel < m_logLevel) { return; } //这个uid是否需要log if (! NeedLogUser(uid)) { return; } Logger logger; if (m_logByBattleId) { if (false == m_allBattle.TryGetValue(battleId, out logger)) { logger = new Logger(m_logPath, m_logFileName + "_bt_" + battleId + ".log", m_logLevel); m_allBattle.Add(battleId, logger); } } else { if (false == m_allUser.TryGetValue(uid, out logger)) { logger = new Logger(m_logPath, m_logFileName + "_uid_" + uid + ".log", m_logLevel); m_allUser.Add(uid, logger); } } if (OSUtils.IsWindows() == false) { // 在服务器上运行时,log不要影响逻辑 try { string strLog = string.Format(strFormat, argvList); logger.WriteLogNoTime(logLevel, frame, strLog); } catch (Exception ex) { logger.WriteLog(logLevel, ex.Message); logger.WriteLog(logLevel, ex.Source); logger.WriteLog(logLevel, ex.StackTrace); } } else { //windows版本,为了发现问题,log错误直接异常,不继续处理了 string strLog = string.Format(strFormat, argvList); logger.WriteLogNoTime(logLevel, frame, strLog); } } public void LogTraceDetail(long uid, ulong battleId, int frameCount, string strFormat, params object[] argvList) { LogByLevel(uid, battleId, frameCount, LogLevel.TraceDetail, strFormat, argvList); } public void LogTrace(long uid, ulong battleId, int frameCount, string strFormat, params object[] argvList) { LogByLevel(uid, battleId, frameCount, LogLevel.Trace, strFormat, argvList); } public void LogDebug(long uid, ulong battleId, int frameCount, string strFormat, params object[] argvList) { LogByLevel(uid, battleId, frameCount, LogLevel.Debug, strFormat, argvList); } public void LogError(long uid, ulong battleId, int frameCount, string strFormat, params object[] argvList) { LogByLevel(uid, battleId, frameCount, LogLevel.Error, strFormat, argvList); } public void LogFatal(long uid, ulong battleId, int frameCount, string strFormat, params object[] argvList) { LogByLevel(uid, battleId, frameCount, LogLevel.Fatal, strFormat, argvList); } } }