Skip to content

Commit

Permalink
#5 Added basic logging functionality, still need to add more logging …
Browse files Browse the repository at this point in the history
…points.
  • Loading branch information
squid-box committed Feb 1, 2015
1 parent dcb0443 commit ee5ef93
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 3 deletions.
10 changes: 10 additions & 0 deletions ScreenCapturer/Code/LogLevel.cs
@@ -0,0 +1,10 @@
namespace ScreenCapturer
{
public enum LogLevel
{
Exception = 0,
Error = 1,
Warning = 2,
Debug = 3
}
}
102 changes: 102 additions & 0 deletions ScreenCapturer/Code/Logger.cs
@@ -0,0 +1,102 @@
namespace ScreenCapturer.Code
{
using System;
using System.IO;

/// <summary>
/// Simple logging utility
/// </summary>
public class Logger
{
/// <summary>
/// Internal instance of Logger.
/// </summary>
private static Logger _logger;

private StreamWriter _sw;

private LogLevel _level;

public static LogLevel Level
{
get
{
return (LogLevel) Properties.Settings.Default.LogLevel;
}
}

/// <summary>
/// Get singleton instance of Logger.
/// </summary>
public static Logger Instance
{
get
{
if (_logger == null)
{
_logger = new Logger();
}

return _logger;
}
}

private Logger()
{
_level = (LogLevel) Properties.Settings.Default.LogLevel;

_sw = new StreamWriter(Properties.Settings.Default.LogPath, true, System.Text.Encoding.UTF8);
_sw.AutoFlush = true;
}

private void writeLog(string prefix, string message)
{
_sw.WriteLine("[{0}] | {1} | {2}", prefix, Utility.DateToFileString(DateTime.Now), message);
_sw.Flush();
}

/// <summary>
/// Debug message.
/// </summary>
/// <param name="message">Information to be logged.</param>
public void Debug(string message)
{
if (_level >= LogLevel.Debug)
writeLog("DBG", message);
}

/// <summary>
/// Warning message.
/// </summary>
/// <param name="message">Information to be logged.</param>
public void Warning(string message)
{
if (_level >= LogLevel.Warning)
writeLog("WRN", message);
}

/// <summary>
/// Error message.
/// </summary>
/// <param name="message">Information to be logged.</param>
public void Error(string message)
{
if (_level >= LogLevel.Error)
writeLog("ERR", message);
}

/// <summary>
/// Logs a given exception.
/// </summary>
/// <param name="e">Exception to be logged.</param>
public void Exception(string message, Exception e)
{
if (_level >= LogLevel.Exception)
{
writeLog("EXC", message);
writeLog("EXC", string.Format("Caught exception {0} : {1}", e.GetType(), e.Message));
writeLog("EXC", e.StackTrace);
}
}
}
}
9 changes: 6 additions & 3 deletions ScreenCapturer/MainWindow.cs
Expand Up @@ -90,30 +90,33 @@ public MainWindow()
/// <param name="e">Event arguments.</param>
private void MainWindowFormClosing(object sender, FormClosingEventArgs e)
{
Logger.Instance.Debug("Program closing");

// Hopefully removes all hooks.
try
{
_listenerKeyboard.Dispose();
_listenerMouse.Dispose();
}
catch (Win32Exception)
catch (Win32Exception exc)
{
// Listeners have already been disposed, probably.
// See http://globalmousekeyhook.codeplex.com/workitem/929
Logger.Instance.Exception("Disposing keyhooks, exception ignored.", exc);
}

if (_capturer.Shots.Count != 0)
{
Logger.Instance.Debug("Shots left in buffer.");
// There are still screenshots in the buffer! D:
var res = MessageBox.Show(this, strings.dialog_closingProgram_UnsavedContent, strings.dialog_closingProgram_UnsavedTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

if (res == DialogResult.Yes)
{
Logger.Instance.Debug("Saving screenshots before exiting.");
SaveShots();
}
}

notificationIcon.Visible = false;
}

#region Event handling
Expand Down
24 changes: 24 additions & 0 deletions ScreenCapturer/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions ScreenCapturer/Properties/Settings.settings
Expand Up @@ -30,5 +30,11 @@
<Setting Name="ColorOther" Type="System.Drawing.Color" Scope="User">
<Value Profile="(Default)">Lime</Value>
</Setting>
<Setting Name="LogPath" Type="System.String" Scope="User">
<Value Profile="(Default)">logs\screencapturer.log</Value>
</Setting>
<Setting Name="LogLevel" Type="System.Int32" Scope="User">
<Value Profile="(Default)">3</Value>
</Setting>
</Settings>
</SettingsFile>
2 changes: 2 additions & 0 deletions ScreenCapturer/ScreenCapturer.csproj
Expand Up @@ -80,13 +80,15 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Code\Capturer.cs" />
<Compile Include="Code\Logger.cs" />
<Compile Include="Code\MousePlotter.cs" />
<Compile Include="Code\Screenshot.cs" />
<Compile Include="Localizations\strings.se.Designer.cs">
<DependentUpon>strings.se.resx</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="Code\LogLevel.cs" />
<Compile Include="MainWindow.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
6 changes: 6 additions & 0 deletions ScreenCapturer/app.config
Expand Up @@ -37,6 +37,12 @@
<setting name="ColorOther" serializeAs="String">
<value>Lime</value>
</setting>
<setting name="LogPath" serializeAs="String">
<value>logs\screencapturer.log</value>
</setting>
<setting name="LogLevel" serializeAs="String">
<value>3</value>
</setting>
</ScreenCapturer.Properties.Settings>
</userSettings>
</configuration>

0 comments on commit ee5ef93

Please sign in to comment.