zoukankan      html  css  js  c++  java
  • 最近用到的一个Debug类

    这个是我最近写的一个记录调试信息的类,基本功能和以前写过的UdpTraceListener类似:通过UDP数据报文发送调试信息发送出去。为了方便调试,增加了颜色和一些简单的指令功能。感觉还比较方便,这里记录一下,以备后续使用。

        static class Debug
        {
            static UdpClient client;
            static Debug()
            {
                client = new UdpClient();
                client.Connect(new IPEndPoint(IPAddress.Loopback, 3001));

                Clear();
            }

            public static void Write(ConsoleColor color, object obj)
            {
                Write(color, obj.ToString());
            }

            [MethodImpl(MethodImplOptions.Synchronized)]
            public static void Write(ConsoleColor color, string message)
            {
                if (message == null)
                    return;

                var dbgMsg = message;
                if (dbgMsg.Length > 80)
                    dbgMsg = dbgMsg.Substring(0, 70) + "..." + Environment.NewLine;

                Console.ForegroundColor = color;
                Console.Write(dbgMsg);

                SendCmd(0xff, (byte)color);
                SendMsg(message);
            }

            public static void WriteLine(ConsoleColor color, object obj)
            {
                if (obj == null)
                    return;

                WriteLine(color, obj.ToString());
            }

            public static void WriteLine(ConsoleColor color, string message)
            {
                Write(color, message + Environment.NewLine);
            }

            public static void Clear()
            {
                Console.Clear();
                SendCmd(0xfe, 0);
            }

            static void SendCmd(byte cmdType, byte cmdInfo)
            {
                client.Send(new byte[] { cmdType, cmdInfo }, 2);
            }

            static void SendMsg(string msg)
            {
                var data = Encoding.Default.GetBytes(msg);
                client.Send(data, data.Length);
            }
        }

  • 相关阅读:
    FZU OJ 1056 :扫雷游戏
    HPU 1166: 阶乘问题(一)
    常用的一些模板
    PAT天梯:L1-019. 谁先倒
    HPU 1437: 王小二的求值问题
    《编程珠玑》阅读小记(7) — 代码调优与节省空间
    《编程珠玑》阅读小记(6) — 算法设计技术
    《编程珠玑》阅读小记(5) — 编程小事
    《编程珠玑》阅读小记(4) — 编写正确的程序
    《C/C++专项练习》— (1)
  • 原文地址:https://www.cnblogs.com/TianFang/p/2497278.html
Copyright © 2011-2022 走看看