예제 #1
0
        static void Start(ref string[] args)
        {
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SafeNativeMethods.AllocConsole();
            }

            const string source = "..{0}..{0}..{0}Tests{0}Code{0}isolated.ahk";
            const string binary = "test.exe";

            args = string.Format(source + " --out " + binary, Path.DirectorySeparatorChar).Split(' ');
        }
예제 #2
0
        public static void Debug(string exMsg, string exTra = null)
        {
            if (!FirstCall || DebugMode < 1 || string.IsNullOrEmpty(exMsg))
            {
                return;
            }

            if (!FirstEntry)
            {
                FirstEntry = true;
                if (!File.Exists(FilePath))
                {
                    try
                    {
                        if (!Directory.Exists(FileLocation))
                        {
                            Directory.CreateDirectory(FileLocation);
                        }
                        File.Create(FilePath).Close();
                    }
                    catch (Exception ex)
                    {
                        if (DebugMode > 1)
                        {
                            DebugMode = 3;
                            Debug(ex);
                        }
                    }
                }
                Debug("***Logging has been started***", $"'{Environment.OSVersion}' - '{Application.ProductName}' - '{Application.ProductVersion}' - '{FilePath}'");
            }
            if (!File.Exists(FilePath) && DebugMode < 1)
            {
                return;
            }

            string date  = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff zzz");
            string exmsg = $"Time:  {date}\r\nMsg:   {Filter(exMsg)}\r\n";

            if (!string.IsNullOrWhiteSpace(exTra))
            {
                string extra = Filter(exTra);
                extra  = extra.Replace("\r\n", " - ");
                exmsg += $"Trace: {extra}\r\n";
            }

            if (DebugMode < 3 && File.Exists(FilePath))
            {
                try
                {
                    File.AppendAllText(FilePath, $"{exmsg}\r\n");
                }
                catch (Exception ex)
                {
                    try
                    {
                        string exFileName = $"{Application.ProductName}_{DateTime.Now.ToString("yyyy-MM-dd_fffffff")}.log";
                        string exFilePath = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), exFileName);
                        exmsg += $"Msg2:  {ex.Message}\r\n";
                        File.AppendAllText(exFilePath, exmsg);
                    }
                    catch (Exception exc)
                    {
                        if (DebugMode > 1)
                        {
                            exmsg += $"Msg3:  {exc.Message}\r\n";
                            MessageBox.Show(exmsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }

            if (DebugMode > 1)
            {
                try
                {
                    if (!IsRunning)
                    {
                        SafeNativeMethods.AllocConsole();
                        SafeNativeMethods.DeleteMenu(SafeNativeMethods.GetSystemMenu(SafeNativeMethods.GetConsoleWindow(), false), 0xF060, 0x0);
                        stdHandle = SafeNativeMethods.GetStdHandle(-11);
                        sfh       = new SafeFileHandle(stdHandle, true);
                        fs        = new FileStream(sfh, FileAccess.Write);
                        if (Console.Title != ConsoleTitle)
                        {
                            Console.Title        = ConsoleTitle;
                            Console.BufferHeight = short.MaxValue - 1;
                            Console.BufferWidth  = Console.WindowWidth;
                            Console.SetWindowSize(Math.Min(100, Console.LargestWindowWidth), Math.Min(40, Console.LargestWindowHeight));
                            Console.ForegroundColor = ConsoleColor.DarkGreen;
                            Console.WriteLine(new Crypt.Base85().DecodeString(string.Concat(new string[]
                            {
                                "<~+<V", "e6?XI", "/I?XI", "/I/mh", "s.+CA", "J_?Q`", "]_?XI", "/I?XF",
                                "ou+<V", "dL+<Y", "#u?XI", "/I?XI", ".I+<W", "<[+<Y", "#u?XI", ".nHs^",
                                "6.04,", "hE+FI", "F$?XI", "/I+<X", "o3+<Y", "#u?XI", "&F?XI", "/I?Q^",
                                "Ir$6U", "Hr?XI", "/I?Q^", "Ir+FG", ":SHm!", "eZ+<Z", "%S+C?", "O(?Q^",
                                "IR+<W", "<[+<V", "e3+<V", "d[+<V", "dL0+&", "gE0-D", "A[+<V", "dL+C'",
                                "::+FG", ";Z+<V", "eS+>4", "i[+<V", "dL+C'", "::+<Z", "%S+C$", "$B+<V",
                                "dL0+&", "gT?XI", "/I?XI", "._+>8", "+L?[N", "uD?XI", "/f04/", "'n?XI",
                                "._+>8", "+L?XJ", "1'+>5", "BT?XI", ".n$6U", "H6+<V", "dL+<X", "oB+<V",
                                "dL+<V", "dL+<V", "dL+<V", "dL+<X", "oB+<V", "dL+<X", "oB~>"
                            })));
                            Console.ForegroundColor = ConsoleColor.DarkYellow;
                            Console.WriteLine("           D E B U G    C O N S O L E");
                            Console.ResetColor();
                            Console.WriteLine();
                        }
                        IsRunning = true;
                    }
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine(new string('-', Console.BufferWidth - 1));
                    foreach (string line in exmsg.Split(new string[] { "\r\n" }, StringSplitOptions.None))
                    {
                        string[] sa = line.Split(' ');
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write(sa[0]);
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.WriteLine($" {string.Join(" ", sa.Skip(1).ToArray())}");
                    }
                    Console.ResetColor();
                    sw = new StreamWriter(fs, Encoding.ASCII)
                    {
                        AutoFlush = true
                    };
                    Console.SetOut(sw);
                }
                catch (Exception ex)
                {
                    DebugMode = 1;
                    Debug(ex);
                }
            }
        }