Files

138 lines
4.1 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace AdvertsingProfitControl
{
public sealed partial class LogConsole : Form
{
//Source code: https://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
private static readonly LogConsole gLogConsoleInstance = new LogConsole();
private static bool gIsShown = false;
public LogConsole()
{
InitializeComponent();
logListView.Scrollable = true;
logListView.View = View.Details;
//Information for drawing header columns and sub-items in ListView:
//http://stackoverflow.com/questions/561798/how-do-i-align-text-for-a-single-subitem-in-a-listview-using-c
ColumnHeader header = new ColumnHeader();
header.Text = "Advertising Profit Control " + Application.ProductVersion + " Debug Console";
header.Name = "LogConsoleHeader";
header.Width = logListView.Width;
logListView.Columns.Add(header);
}
static LogConsole()
{
GetStaticInstance.FormClosing += new FormClosingEventHandler(LogConsole_FormClosing);
//Set the maximum and minimum size of the form.
GetStaticInstance.MaximumSize = new Size(900, 900);
GetStaticInstance.MinimumSize = new Size(400, 400);
}
public new void Show()
{
if (gIsShown)
{
base.Show();
}
else
{
base.Show();
gIsShown = true;
}
}
public new void Hide()
{
if (gIsShown)
{
base.Hide();
gIsShown = false;
}
}
public enum Level : int
{
Critical = 0,
Error = 1,
Warning = 2,
Info = 3,
Verbose = 4,
Debug = 5
};
public void WriteToLog(Level level, string message)
{
Color color;
int index;
switch (level)
{
case Level.Critical:
color = Color.White;
break;
case Level.Error:
color = Color.Red;
break;
case Level.Warning:
color = Color.Goldenrod;
break;
case Level.Info:
color = Color.Green;
break;
case Level.Verbose:
color = Color.Blue;
break;
default:
color = Color.Black;
break;
}
index = logListView.Items.Count;
try
{
message = String.Format("{0}: {1}", level, message);
if (level != Level.Info)
{
GlobalClasses.WriteToLog(DateTime.Now + ": " + message + Environment.NewLine);
}
logListView.Items.Add(message);
logListView.Items[index].ForeColor = color;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
if (level == Level.Critical)
{
logListView.Items[index].BackColor = Color.Red;
}
else
{
logListView.Items[index].BackColor = Color.WhiteSmoke;
}
}
private static void LogConsole_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
GetStaticInstance.Hide();
gIsShown = false;
}
public static LogConsole GetStaticInstance
{
get { return gLogConsoleInstance; }
}
public bool IsVisable
{
get { return gIsShown; }
}
}
}