You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

343 lines
9.5 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using Sog.Log;
namespace Sog
{
/// <summary>
/// 日志操作类,对日志,错误日志,状态日志进行简单的管理,如果需要复杂逻辑,请自己实现
/// </summary>
/// <example>
/// <code>
/// public class Program
/// {
/// static void Main()
/// {
/// TraceLog.Trace("Hello {0}", "Sog")
/// TraceLog.Error("Error {0}", "error test.")
/// }
/// }
/// </code>
/// </example>
public static class TraceLog
{
/// <summary>
/// 缺省打开这个log
/// </summary>
private static Logger s_logger = new Logger("./", "trace.log", LogLevel.Trace);
private static Logger s_errorLogger;
private static Logger s_statLogger;
private static UserLogger s_userLogger;
private static BattleLogger s_battleLogger;
private static int[] s_skipLogMsgID;
public static void SetLogPathName(string strPath,string strName)
{
if (s_logger == null)
{
s_logger = new Logger(strPath, strName, LogLevel.Trace);
}
if (s_errorLogger == null)
{
s_errorLogger = new Logger(strPath, strName, LogLevel.Error);
}
if(s_statLogger == null)
{
s_statLogger = new Logger(strPath, strName, LogLevel.Debug);
}
s_logger.SetPathName(strPath, strName+".log");
s_errorLogger.SetPathName(strPath, strName + ".error");
s_statLogger.SetPathName(strPath, strName + ".stat");
//开始写线程
LogWriteThread.Instance.Start();
}
public static void InitUserLog(string path, string filename, int logLevel,
bool logAllUser, long[] logSomeUser)
{
if(s_userLogger == null)
{
s_userLogger = new UserLogger();
}
s_userLogger.Init(path, filename, logLevel, logAllUser, logSomeUser);
}
public static void InitBattleLog(string path, string filename, int logLevel,
bool logAllUser, long[] logSomeUser, bool logByBattleId)
{
if (s_battleLogger == null)
{
s_battleLogger = new BattleLogger();
}
s_battleLogger.Init(path, filename, logLevel, logAllUser, logSomeUser, logByBattleId);
}
public static int GetLogLevel()
{
return s_logger.GetLevel();
}
public static void SetLogLevel(int level)
{
s_logger.SetLevel(level);
if (level >= LogLevel.Error)
{
s_errorLogger.SetLevel(level);
}
else
{
s_errorLogger.SetLevel(LogLevel.Error);
}
}
public static void SetSkipLogMsgID(int[] msgIDs)
{
s_skipLogMsgID = msgIDs;
}
public static bool IsSkipLogMsgID(int iMsgID)
{
if(s_skipLogMsgID == null)
{
return false;
}
for(int i=0; i< s_skipLogMsgID.Length; i++)
{
if(iMsgID == s_skipLogMsgID[i])
{
return true;
}
}
return false;
}
public static void ForceCloseLogFile()
{
LogWriteThread.Instance.ForceClose();
}
public static void Error(string strFormat, params object[] argvList)
{
s_logger.LogError(strFormat, argvList);
if (s_errorLogger != null)
{
s_errorLogger.LogError(strFormat, argvList);
}
}
public static void Trace(string strFormat, params object[] argvList)
{
s_logger.LogTrace(strFormat, argvList);
}
public static void TraceDetail(string strFormat, params object[] argvList)
{
s_logger.LogTraceDetail(strFormat, argvList);
}
public static void Debug(string strFormat, params object[] argvList)
{
s_logger.LogDebug(strFormat, argvList);
}
public static void DebugNoFormat(string strMessage)
{
s_logger.LogDebugNoFormat(strMessage);
}
//写错误日志并抛出异常
public static void Assert(string strFormat, params object[] argvList)
{
string strLog = string.Format(strFormat, argvList);
Error(strLog);
throw new System.Exception(strLog);
}
/// <summary>
/// 写异常信息
/// </summary>
/// <param name="ex"></param>
/// <param name="bWriteConsole">是否打印到console窗口</param>
public static void Exception(Exception ex, bool bWriteConsole = false)
{
try
{
Error("catch one exception");
Error("{0}", ex.Message);
if (bWriteConsole)
{
Console.WriteLine(ex.Message);
}
Error("{0}", ex.Source);
if (bWriteConsole)
{
Console.WriteLine(ex.Source);
}
Error("{0}", ex.StackTrace);
if (bWriteConsole)
{
Console.WriteLine(ex.StackTrace);
}
if (ex.InnerException != null)
{
Error("{0}", ex.InnerException);
if (bWriteConsole)
{
Console.WriteLine(ex.InnerException);
}
}
}
catch(Exception innerEx)
{
//打exception又抛出异常就算了
Error("TraceLog.Exception throw a Exception {0}", innerEx.Message);
}
}
/// <summary>
/// 写统计日志,xxx.stat
/// </summary>
/// <param name="strFormat"></param>
/// <param name="argvList"></param>
public static void Stat(string strFormat, params object[] argvList)
{
if (s_statLogger != null)
{
s_statLogger.LogDebug(strFormat, argvList);
}
}
/// <summary>
/// 玩家日志
/// </summary>
/// <param name="strFormat"></param>
/// <param name="argvList"></param>
public static void UserError(long uid, string strFormat, params object[] argvList)
{
if (s_userLogger != null && uid > 0)
{
s_userLogger.LogError(uid, strFormat, argvList);
}
s_logger.LogError(strFormat, argvList);
if (s_errorLogger != null)
{
s_errorLogger.LogError(strFormat, argvList);
}
}
public static void UserTraceDetail(long uid, string strFormat, params object[] argvList)
{
if (s_userLogger != null && uid > 0)
{
s_userLogger.LogTraceDetail(uid, strFormat, argvList);
}
s_logger.LogTraceDetail(strFormat, argvList);
}
public static void UserTrace(long uid, string strFormat, params object[] argvList)
{
if (s_userLogger != null && uid > 0)
{
s_userLogger.LogTrace(uid, strFormat, argvList);
}
s_logger.LogTrace(strFormat, argvList);
}
public static void UserDebug(long uid, string strFormat, params object[] argvList)
{
if (s_userLogger != null && uid > 0)
{
s_userLogger.LogDebug(uid, strFormat, argvList);
}
s_logger.LogDebug(strFormat, argvList);
}
public static bool NeedLogUserTraceLevel(long uid)
{
if(s_userLogger.GetLogLevel() > (int)LogLevel.TraceDetail)
{
return false;
}
return s_userLogger.NeedLogUser(uid);
}
public static void BattleTraceDetail(long uid, ulong battleId, int seq, string strFormat, params object[] argvList)
{
if (s_logger != null)
{
s_logger.LogByLevel(LogLevel.TraceDetail, strFormat, argvList);
}
if (s_battleLogger != null)
{
s_battleLogger.LogTraceDetail(uid, battleId, seq, strFormat, argvList);
}
}
public static void BattleTrace(long uid, ulong battleId, int seq, string strFormat, params object[] argvList)
{
if (s_logger != null)
{
s_logger.LogTrace(strFormat, argvList);
}
if (s_battleLogger != null)
{
s_battleLogger.LogTrace(uid, battleId, seq, strFormat, argvList);
}
}
public static void BattleDebug(long uid, ulong battleId, int seq, string strFormat, params object[] argvList)
{
if (s_logger != null)
{
s_logger.LogDebug(strFormat, argvList);
}
if (s_battleLogger != null)
{
s_battleLogger.LogDebug(uid, battleId, seq, strFormat, argvList);
}
}
public static void BattleError(long uid, ulong battleId, int seq, string strFormat, params object[] argvList)
{
if (s_logger != null)
{
s_logger.LogError(strFormat, argvList);
}
if (s_battleLogger != null)
{
s_battleLogger.LogError(uid, battleId, seq, strFormat, argvList);
}
}
}
}