Files
LM-Middleware/HexcalMC/Base/DebugDfn.cs
T

183 lines
5.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HexcalMC.Base
{
public class DebugDfn
{
public static string StrDebugFile = Application.StartupPath + "\\File\\Debug.txt";
public static string StrDebugSavePath = Application.StartupPath + "\\File\\DebugFiles";
public static string StrDebugFileTemp = Application.StartupPath + "\\File\\DebugTemp.txt"; //临时存储,用于菜单查看
public static string _strStartTime = ""; //程序启动时间
public static string _strEndTime = ""; //程序关闭时间
public static RichTextBox textBox_Msg;
//=================================================================
public static void StartDebugObj()
{
System.IO.TextWriter log = new System.IO.StreamWriter(DebugDfn.StrDebugFile);
TextWriterTraceListener logger = new TextWriterTraceListener(log);
Trace.Listeners.Add(logger);
_strStartTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
}
public static void SaveDebugFile()
{
AddLog("软件关闭!");
_strEndTime = DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss");
string copyFileName = StrDebugSavePath + "\\DebugFiles\\" + "Debug(" + _strStartTime + " To " +
_strEndTime + ")" + ".txt";
if (File.Exists(StrDebugFile))
File.Copy(StrDebugFile, copyFileName, true);
}
//=================================================================
#region
public static void AddLog(string str)
{
MyBase.TraceWriteLine(str);
}
public static void AddLogText(string str, Color m_Color = new Color())
{
MyBase.TraceWriteLine(str);
try
{
textBox_Msg.BeginInvoke((EventHandler)delegate
{
Color SetColor = Color.Black;
if (m_Color == new Color())
{
if (str.ToUpper().Contains("ERROR") || str.ToUpper().Contains("错误") || str.ToUpper().Contains("出错") || str.ToUpper().Contains("EXCEPTION") || str.ToUpper().Contains("异常") || str.ToUpper().Contains("失败"))
{
SetColor = Color.Red;
}
else if (str.ToUpper().Contains("WARNING") || str.ToUpper().Contains("警告"))
{
SetColor = Color.DarkOrange;
}
}
else
{
SetColor = m_Color;
}
string strText = DateTime.Now.ToString("HH:mm:ss.fff") + "--" + str + Environment.NewLine;
textBox_Msg.SelectionStart = textBox_Msg.TextLength;
if (string.IsNullOrEmpty(str))
SetText(textBox_Msg, str, SetColor, false, 16);
else
SetText(textBox_Msg, strText, SetColor, false, 16);
if (textBox_Msg.Lines.Length > 800)
{
textBox_Msg.Select(0, textBox_Msg.TextLength / 2);
textBox_Msg.Cut();
}
textBox_Msg.ScrollToCaret();
});
}
catch { }
}
public static void SetText(RichTextBox mRichTextBox, string strText, Color mColor, bool bBold = false,
float size = 16)
{
mRichTextBox.Invoke(((EventHandler)delegate
{
SetFont(mRichTextBox, mColor, bBold, size);
mRichTextBox.SelectedText = strText;
}));
}
public static void SetFont(RichTextBox mRichTextBox, Color mColor, bool bBold = false, float size = 16)
{
mRichTextBox.SelectionColor = mColor;
if (bBold)
mRichTextBox.SelectionFont = new System.Drawing.Font("Segoe UI", size, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
else
mRichTextBox.SelectionFont = new System.Drawing.Font("Segoe UI", size,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));
}
public static void SetErrorColor(Color InColor)
{
if (textBox_Msg != null)
{
textBox_Msg.BeginInvoke((EventHandler)delegate
{
textBox_Msg.BackColor = InColor;
});
}
}
#endregion
}
public class Errors
{
/// <summary>
/// 0:无故障 PLC按位求 1对应101
/// 1:通讯故障
/// 2:扫描枪读取故障
/// 3: 控制柜急停被按下
/// 4:外部急停被按下
/// 5:PCL急停,辊道在CMM测量时上料
/// </summary>
public static int iErrors = 0;
/// 101:通讯故障
/// 102:扫描枪读取故障
/// 103: 控制柜急停被按下
/// 104:外部急停被按下
/// 105:PCL急停,辊道在CMM测量时上料
public static bool bCommError = false;
public static bool bReaderError = false;
public static bool bEStop_Controller = false;
public static bool bEStop_Out = false;
public static bool bEStop_Plc = false;
public static bool bTipSwitch = true;
public static StreamWriter ErrorWrite;
public static StreamWriter OtherWrite;
public static StreamWriter StatusWrite;
public static void WriteErrorDebug(string strError)
{
try
{
if (Errors.ErrorWrite == null)
Errors.ErrorWrite = new StreamWriter(DebugDfn.StrDebugSavePath + "Error_(" + DateTime.Now.ToString("yyyy-MM-dd") + ").txt", true);
Errors.ErrorWrite.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strError);
Errors.ErrorWrite.Flush();
}
finally
{ }
}
public static void WriteOtherDebug(string strMsg)
{
try
{
if (Errors.OtherWrite == null)
Errors.OtherWrite = new StreamWriter(DebugDfn.StrDebugSavePath + "Other_(" + DateTime.Now.ToString("yyyy-MM-dd") + ").txt", true);
Errors.OtherWrite.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strMsg);
Errors.OtherWrite.Flush();
}
finally
{ }
}
}
}