Advertisement
maujogador

Form - output_log parser

Jan 31st, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.22 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text.RegularExpressions;
  7. using System.Windows.Forms;
  8. using output_parser.Properties;
  9.  
  10. namespace output_parser
  11. {
  12.     public partial class Form : System.Windows.Forms.Form
  13.     {
  14.         private static string[] toAppend;
  15.         private static Font defaultFont;
  16.  
  17.         public Form()
  18.         {
  19.             InitializeComponent();
  20.             defaultFont = text_box.Font;
  21.             Load += load;
  22.             FormClosing += close;
  23.         }
  24.  
  25.         private void btn_browse_Click(object sender, EventArgs e)
  26.         {
  27.             var result = fileDialog.ShowDialog();
  28.             if (result == DialogResult.OK)
  29.                 path.Text = fileDialog.FileName;
  30.             else
  31.                 return;
  32.             btn_open_Click(sender, e);
  33.         }
  34.  
  35.         private void btn_open_Click(object sender, EventArgs e)
  36.         {
  37.             text_box.Clear();
  38.             if (!string.IsNullOrEmpty(path.Text))
  39.                 toAppend = File.ReadAllLines(path.Text);
  40.             remove_stuff();
  41.             parse_text();
  42.             parse_stack_trace();
  43.         }
  44.  
  45.         private static void remove_stuff()
  46.         {
  47.             var str = toAppend.Aggregate("", (current, s) => current + (s + "\n"));
  48.             //str = Regex.Replace(str, @"\(Filename.+?:\s([\-\d]+)\)", "--------------------$1--------------------");
  49.             str = Regex.Replace(str, @"\(Filename: .*?\)\n\n", "");  //(Filename:  Line: 1638)
  50.             str = Regex.Replace(str, @"Loading.+\n", "");  //Loading C:\...\*.dll into Unity Child Domain
  51.             str = Regex.Replace(str, @"The.+Beh.+sing!\s+", "");  //The referenced script on this Behaviour is missing!
  52.             str = Regex.Replace(str, @"\n(?=\s+\n)", "");  //space & new line
  53.             str = str.Replace("(this message is harmless)", "");
  54.             str = Regex.Replace(str, @"(\..+)\s(\(.*?\))", "$1$2");
  55.             toAppend = str.Split(new []{ "\n" }, StringSplitOptions.None);
  56.         }
  57.  
  58.         private void parse_text()
  59.         {
  60.             for (var i = 0; i < toAppend.Length - 1; i++)
  61.             {
  62.                 var str = toAppend[i];
  63.                 var color = Color.White;
  64.                 var nFont = defaultFont;
  65.                 if (Regex.Match(str, @"^(?:\s+)?[\w\d]+?:").Success && !str.Contains("Exception")) //todo put me in a method
  66.                     color = Color.FromArgb(0, 255, 81);
  67.                 if (Regex.Match(str, @"^(?:.+)?assembly:").Success)
  68.                     color = Color.FromArgb(255, 200, 0);
  69.                 if (str.Contains("nused"))
  70.                     color = Color.FromArgb(117, 209, 191);
  71.                 if (Regex.Match(str, @"^On[\w\d]+").Success)
  72.                     color = Color.FromArgb(0, 182, 255);
  73.                 if (str.Contains("/") && str.Contains("\""))
  74.                    color = Color.FromArgb(255, 70, 0);
  75.                 if (str.Contains(">>>>"))
  76.                     color = Color.FromArgb(232, 0, 255);
  77.                 if (str.Contains("Exception"))
  78.                     color = Color.FromArgb(255, 130, 177);
  79.                 if (str.StartsWith("  at"))
  80.                     color = Color.FromArgb(187, 62, 109);
  81.                 if (str.StartsWith("="))
  82.                 {
  83.                     color = Color.FromArgb(255, 50, 50);
  84.                     nFont = new Font(defaultFont.FontFamily, 17, FontStyle.Bold);
  85.                 }
  86.  
  87.                 text_box.SelectionColor = color;
  88.                 text_box.SelectionFont = nFont;
  89.                 text_box.AppendText(str);
  90.                 if (!toAppend[i + 1].StartsWith("  at") || str.Contains("Exception"))
  91.                     text_box.AppendText("\n");
  92.                 /*if (!toAppend[i + 1].StartsWith("  at") || !str.Contains("Exception"))
  93.                     text_box.AppendText("\n");*/
  94.             }
  95.         }
  96.  
  97.         private void parse_stack_trace()
  98.         {
  99.             var matches = Regex.Matches(text_box.Text, @"t\s([A-Za-z0-9_]+)\.(.+){1}(\(.*?\))");
  100.             for (var i = 0; i < matches.Count; i++)
  101.             {
  102.                 var idx = matches[i].Index;
  103.                 var aa = text_box.Text.IndexOf(matches[i].Groups[1].Value, idx, StringComparison.Ordinal);
  104.                 var bb = text_box.Text.IndexOf(matches[i].Groups[2].Value, idx, StringComparison.Ordinal);
  105.                 var cc = text_box.Text.IndexOf(matches[i].Groups[3].Value, idx, StringComparison.Ordinal);
  106.  
  107.                 //Namespace | Library
  108.                 text_box.SelectionStart = aa;  //plus 1 for the \s in the regex
  109.                 text_box.SelectionLength = matches[i].Groups[1].Value.Length;
  110.                 text_box.SelectionColor = Color.FromArgb(255, 220, 0);
  111.  
  112.                 //Class | Method
  113.                 text_box.SelectionStart = bb;
  114.                 text_box.SelectionLength = matches[i].Groups[2].Value.Length;
  115.                 text_box.SelectionColor = Color.FromArgb(255, 128, 0);
  116.  
  117.                 //Signature
  118.                 text_box.SelectionStart = cc;
  119.                 text_box.SelectionLength = matches[i].Groups[3].Value.Length;
  120.                 text_box.SelectionColor = Color.FromArgb(255, 54, 0);
  121.             }
  122.         }
  123.  
  124.         private void load(object sender, EventArgs e)
  125.         {
  126.             path.Text = Settings.Default.Path;
  127.             if (Settings.Default.Maximised)
  128.             {
  129.                 WindowState = FormWindowState.Maximized;
  130.                 Location = Settings.Default.Location;
  131.                 Size = Settings.Default.Size;
  132.             }
  133.             else if (Settings.Default.Minimised)
  134.             {
  135.                 WindowState = FormWindowState.Minimized;
  136.                 Location = Settings.Default.Location;
  137.                 Size = Settings.Default.Size;
  138.             }
  139.             else
  140.             {
  141.                 Location = Settings.Default.Location;
  142.                 Size = Settings.Default.Size;
  143.             }
  144.         }
  145.  
  146.         private void close(object sender, EventArgs e)
  147.         {
  148.             Settings.Default.Path = path.Text;
  149.             if (WindowState == FormWindowState.Maximized)
  150.             {
  151.                 Settings.Default.Location = RestoreBounds.Location;
  152.                 Settings.Default.Size = RestoreBounds.Size;
  153.                 Settings.Default.Maximised = true;
  154.                 Settings.Default.Minimised = false;
  155.             }
  156.             else if (WindowState == FormWindowState.Normal)
  157.             {
  158.                 Settings.Default.Location = Location;
  159.                 Settings.Default.Size = Size;
  160.                 Settings.Default.Maximised = false;
  161.                 Settings.Default.Minimised = false;
  162.             }
  163.             else
  164.             {
  165.                 Settings.Default.Location = RestoreBounds.Location;
  166.                 Settings.Default.Size = RestoreBounds.Size;
  167.                 Settings.Default.Maximised = false;
  168.                 Settings.Default.Minimised = true;
  169.             }
  170.             Settings.Default.Save();
  171.         }
  172.  
  173.         private void btn_settings_Click(object sender, EventArgs e)
  174.         {
  175.             var frm = new System.Windows.Forms.Form()
  176.             {
  177.                
  178.             };
  179.             frm.Show();
  180.         }
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement