/// <summary> /// 日志元素出队列 /// </summary> /// <returns>出队列是否成功</returns> public LogElement DeQ() { lock (lockQ) { LogElement retElem = null; if (errLogQ.Count > 0) { retElem = errLogQ.Dequeue(); } else if (infLogQ.Count > 0) { retElem = infLogQ.Dequeue(); } else if (CLILogQ.Count > 0) { retElem = CLILogQ.Dequeue(); } else { retElem = null; } return(retElem); } }
/// <summary> /// 记录信息日志 /// </summary> /// <param name="layer">协议层</param> /// <param name="inf">数据</param> /// <param name="len">数据量</param> public static void RecordInf(enLogLayer layer, byte[] inf, int len = 0) { lock (lockLog) { if (layerLogSwitch[(int)layer]) { LogElement aLogElement = null; if (inf != null && inf.Length != 0) { if (len > inf.Length) { aLogElement = new LogElement(layer, enLogType.eInfo, ToHexString(inf)); } else { byte[] arrValid = new byte[len]; Array.Copy(inf, 0, arrValid, 0, len); aLogElement = new LogElement(layer, enLogType.eInfo, ToHexString(arrValid)); } } logQ.EnQ(aLogElement); } } }
/// <summary> /// 记录错误日志 /// </summary> /// <param name="layer">协议层</param> /// <param name="err">错误信息</param> public static void RecordErr(enLogLayer layer, string err) { lock (lockLog) { LogElement aLogElement = new LogElement(layer, enLogType.eErr, err); logQ.EnQ(aLogElement); } }
/// <summary> /// 记录信息日志 /// </summary> /// <param name="layer">协议层</param> /// <param name="inf">信息</param> public static void RecordInf(enLogLayer layer, string inf) { lock (lockLog) { if (layerLogSwitch[(int)layer]) { LogElement aLogElement = new LogElement(layer, enLogType.eInfo, inf); logQ.EnQ(aLogElement); } } }
/// <summary> /// 日志处理主线程函数 /// </summary> private static void CommStackLogger() { CommStackLog.RecordInf(enLogLayer.eAdapter, "CommStackLogger running"); while (!bExit && logQ.HaveLogElement) { LogElement logElem = logQ.DeQ(); if (logElem != null) { WriteLog(logElem.LogType, logElem.ToString(), logElem.NewLine); } } asigThreadExit.Set(); }
/// <summary> /// 日志元素入队列 /// </summary> /// <param name="element">日志元素</param> /// <returns>入队列是否成功</returns> public bool EnQ(LogElement element) { lock (lockQ) { if (element.LogType == enLogType.eErr) { if (errLogQ.Count >= capacity) { return(false); } else { errLogQ.Enqueue(element); sigNewLogElement.Set(); return(true); } } else if (element.LogType == enLogType.eInfo) { if (infLogQ.Count >= capacity) { return(false); } else { infLogQ.Enqueue(element); sigNewLogElement.Set(); return(true); } } else if (element.LogType == enLogType.eCli) { if (CLILogQ.Count >= capacity) { return(false); } else { CLILogQ.Enqueue(element); sigNewLogElement.Set(); return(true); } } else { return(false); } } }
/// <summary> /// 记录错误日志 /// </summary> /// <param name="layer">协议层</param> /// <param name="err">错误数据</param> /// <param name="len">错误数据量</param> public static void RecordErr(enLogLayer layer, byte[] err, int len = 0) { lock (lockLog) { LogElement aLogElement = null; if (err != null && err.Length != 0) { if (len > err.Length) { aLogElement = new LogElement(layer, enLogType.eErr, ToHexString(err)); } else { byte[] arrValid = new byte[len]; Array.Copy(err, 0, arrValid, 0, len); aLogElement = new LogElement(layer, enLogType.eErr, ToHexString(arrValid)); } } logQ.EnQ(aLogElement); } }