patterncsharpMajor
Console window to debug Windows Applications
Viewed 0 times
debugapplicationswindowswindowconsole
Problem
Since I was having some difficulties to debug the next step for Scrolly - A (very) simple infinite mouse "scroll", I've decided to make a console-like class that allows to add messages to it.
This allows you to show some messages. It also allows to pass any object you want, but it will be converted to string. You can add information, error and warning messages as well, which will have a matching color for each.
```
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ConsoleWindow
{
public sealed partial class Console
{
private static volatile Console instance = new Console();
private static ConsoleForm form = new ConsoleForm();
public static Console Instance
{
get
{
return instance;
}
}
public static bool autoShow = true;
private static string newline = "\r\n";
private static string line = "================================================================================";
///
/// Forces the prompt to go to the end of the text.
///
private static void scrollToEnd()
{
form.consoleOutput.SelectionStart = form.consoleOutput.Text.Length;
form.consoleOutput.ScrollToCaret();
}
///
/// Adds a message to the console.
///
/// Message object to log.
public static bool Log(T message)
{
if (autoShow)
{
Show();
}
scrollToEnd();
form.consoleOutput.AppendText(message.ToString() + newline);
scrollToEnd();
return true;
}
///
/// Adds an error message to the console.
///
/// Message object to log.
public static bool Error(T message)
{
if (autoShow)
{
Show();
}
scrollToEnd();
fo
This allows you to show some messages. It also allows to pass any object you want, but it will be converted to string. You can add information, error and warning messages as well, which will have a matching color for each.
```
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ConsoleWindow
{
public sealed partial class Console
{
private static volatile Console instance = new Console();
private static ConsoleForm form = new ConsoleForm();
public static Console Instance
{
get
{
return instance;
}
}
public static bool autoShow = true;
private static string newline = "\r\n";
private static string line = "================================================================================";
///
/// Forces the prompt to go to the end of the text.
///
private static void scrollToEnd()
{
form.consoleOutput.SelectionStart = form.consoleOutput.Text.Length;
form.consoleOutput.ScrollToCaret();
}
///
/// Adds a message to the console.
///
/// Message object to log.
public static bool Log(T message)
{
if (autoShow)
{
Show();
}
scrollToEnd();
form.consoleOutput.AppendText(message.ToString() + newline);
scrollToEnd();
return true;
}
///
/// Adds an error message to the console.
///
/// Message object to log.
public static bool Error(T message)
{
if (autoShow)
{
Show();
}
scrollToEnd();
fo
Solution
private static string newline = "\r\n";This is very wrong. And particularly with the open sourcing of .NET leading to the likelihood of C# being uses on non-Windows operating systems increasing, we shouldn't depend on a platform-specific newline signature.
The correct approach looks like this:
private static string newline = Environment.NewLine;And at this point, we're just providing an alias of questionable utility.
Look how much duplication we have between Log, Error, Warn, and Info. This is unnecessary.
What we need is a private internal function that handles everything we're duplicating, and then the
Log, Error, Warn, and Info methods simply are publicly exposing various means of calling this method.private static bool ConsolePrint(String message, Color color)
{
if (autoShow)
{
Show();
}
scrollToEnd();
form.consoleOutput.SelectionColor = color;
form.consoleOutput.SelectionLength = 0;
form.consoleOutput.AppendText(message + Environment.NewLine);
scrollToEnd();
form.consoleOutput.SelectionColor = form.consoleOutput.ForeColor;
return true;
}Now we just wrap this for our other methods.
public static bool Log(T message)
{
return ConsolePrint(message.ToString, form.consoleOutput.ForeColor);
}
public static bool Error(T message)
{
return ConsolePrint("(!) " + message.ToString, Color.Red);
}
// etc...Code Snippets
private static string newline = "\r\n";private static string newline = Environment.NewLine;private static bool ConsolePrint(String message, Color color)
{
if (autoShow)
{
Show();
}
scrollToEnd();
form.consoleOutput.SelectionColor = color;
form.consoleOutput.SelectionLength = 0;
form.consoleOutput.AppendText(message + Environment.NewLine);
scrollToEnd();
form.consoleOutput.SelectionColor = form.consoleOutput.ForeColor;
return true;
}public static bool Log<T>(T message)
{
return ConsolePrint(message.ToString, form.consoleOutput.ForeColor);
}
public static bool Error<T>(T message)
{
return ConsolePrint("(!) " + message.ToString, Color.Red);
}
// etc...Context
StackExchange Code Review Q#101131, answer score: 21
Revisions (0)
No revisions yet.