Skip to content

Commit bcbd9f0

Browse files
author
RandomEngy
committed
Added color coding to log window. Errors are highlighted in red, VidCoder messages in dark blue and HandBrake core messages in black.
1 parent 125790c commit bcbd9f0

11 files changed

Lines changed: 169 additions & 50 deletions

File tree

HandBrakeInterop/HandBrakeInterop/HandBrakeUtils.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public static void ErrorHandler(string message)
7676
{
7777
if (!string.IsNullOrEmpty(message))
7878
{
79+
// This error happens in normal operations. Log it as a message.
80+
if (message == "dvd: ifoOpen failed")
81+
{
82+
if (MessageLogged != null)
83+
{
84+
MessageLogged(null, new MessageLoggedEventArgs { Message = message });
85+
}
86+
87+
return;
88+
}
89+
7990
if (ErrorLogged != null)
8091
{
8192
ErrorLogged(null, new MessageLoggedEventArgs { Message = message });

VidCoder/Model/LogEntry.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace VidCoder.Model
2+
{
3+
public class LogEntry
4+
{
5+
public LogType LogType { get; set; }
6+
public LogSource Source { get; set; }
7+
public string Text { get; set; }
8+
}
9+
}

VidCoder/Model/LogSource.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace VidCoder.Model
2+
{
3+
public enum LogSource
4+
{
5+
VidCoder,
6+
HandBrake
7+
}
8+
}

VidCoder/Model/LogType.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace VidCoder.Model
2+
{
3+
public enum LogType
4+
{
5+
Message,
6+
Error
7+
}
8+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
using System;
2+
using System.Collections.Generic;
3+
using VidCoder.Model;
4+
25
namespace VidCoder.Services
36
{
47
public interface ILogger : IDisposable
58
{
69
void ClearLog();
710
void Log(string message);
8-
string LogText { get; }
11+
List<LogEntry> LogEntries { get; }
12+
13+
event EventHandler<EventArgs<LogEntry>> EntryLogged;
14+
event EventHandler Cleared;
915
}
1016
}

VidCoder/Services/Logger.cs

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
using System.Text;
55
using HandBrake.Interop;
66
using System.IO;
7+
using VidCoder.Model;
78
using VidCoder.ViewModel;
89

910
namespace VidCoder.Services
1011
{
1112
public class Logger : IDisposable, ILogger
1213
{
14+
private List<LogEntry> logEntries = new List<LogEntry>();
1315
private StreamWriter logFile;
1416
private bool disposed;
15-
private StringBuilder logBuilder;
1617
private object logLock = new object();
1718
private object disposeLock = new object();
1819

20+
public event EventHandler<EventArgs<LogEntry>> EntryLogged;
21+
public event EventHandler Cleared;
22+
1923
public Logger()
2024
{
2125
HandBrakeUtils.MessageLogged += this.OnMessageLogged;
@@ -30,31 +34,46 @@ public Logger()
3034
string logFilePath = Path.Combine(logFolder, DateTime.UtcNow.ToString("yyyy-MM-dd HH-mm-ss") + ".txt");
3135
this.logFile = new StreamWriter(logFilePath);
3236

33-
this.logBuilder = new StringBuilder();
34-
this.RecordMessage("## VidCoder " + Utilities.CurrentVersion + " (" + Utilities.Architecture + ")");
37+
var initialEntry = new LogEntry
38+
{
39+
LogType = LogType.Message,
40+
Source = LogSource.VidCoder,
41+
Text = "## VidCoder " + Utilities.CurrentVersion + " (" + Utilities.Architecture + ")"
42+
};
43+
44+
this.AddEntry(initialEntry);
3545
}
3646

37-
public string LogText
47+
public List<LogEntry> LogEntries
3848
{
3949
get
4050
{
41-
lock (this.logLock)
42-
{
43-
return logBuilder.ToString();
44-
}
51+
return this.logEntries;
4552
}
4653
}
4754

4855
public void Log(string message)
4956
{
5057
string prefix = string.IsNullOrEmpty(message) ? string.Empty : "# ";
5158

52-
this.RecordMessage(prefix + message);
59+
var entry = new LogEntry
60+
{
61+
LogType = LogType.Message,
62+
Source = LogSource.VidCoder,
63+
Text = prefix + message
64+
};
65+
66+
this.AddEntry(entry);
5367
}
5468

5569
public void ClearLog()
5670
{
57-
this.logBuilder.Clear();
71+
this.LogEntries.Clear();
72+
73+
if (this.Cleared != null)
74+
{
75+
this.Cleared(this, new EventArgs());
76+
}
5877
}
5978

6079
/// <summary>
@@ -72,17 +91,7 @@ public void Dispose()
7291
}
7392
}
7493

75-
private void OnMessageLogged(object sender, MessageLoggedEventArgs e)
76-
{
77-
this.RecordMessage(e.Message);
78-
}
79-
80-
private void OnErrorLogged(object sender, MessageLoggedEventArgs e)
81-
{
82-
this.RecordMessage("ERROR: " + e.Message);
83-
}
84-
85-
private void RecordMessage(string message)
94+
private void AddEntry(LogEntry entry)
8695
{
8796
lock (this.disposeLock)
8897
{
@@ -94,18 +103,40 @@ private void RecordMessage(string message)
94103

95104
lock (this.logLock)
96105
{
97-
this.logBuilder.Append(message);
98-
this.logBuilder.Append(Environment.NewLine);
106+
this.LogEntries.Add(entry);
99107
}
100108

101-
var logWindow = WindowManager.FindWindow(typeof(LogViewModel)) as LogViewModel;
102-
if (logWindow != null)
109+
if (this.EntryLogged != null)
103110
{
104-
logWindow.RefreshLogs();
111+
this.EntryLogged(this, new EventArgs<LogEntry>(entry));
105112
}
106113

107-
this.logFile.WriteLine(message);
114+
this.logFile.WriteLine(entry.Text);
108115
this.logFile.Flush();
109116
}
117+
118+
private void OnMessageLogged(object sender, MessageLoggedEventArgs e)
119+
{
120+
var entry = new LogEntry
121+
{
122+
LogType = LogType.Message,
123+
Source = LogSource.HandBrake,
124+
Text = e.Message
125+
};
126+
127+
this.AddEntry(entry);
128+
}
129+
130+
private void OnErrorLogged(object sender, MessageLoggedEventArgs e)
131+
{
132+
var entry = new LogEntry
133+
{
134+
LogType = LogType.Error,
135+
Source = LogSource.HandBrake,
136+
Text = "ERROR: " + e.Message
137+
};
138+
139+
this.AddEntry(entry);
140+
}
110141
}
111142
}

VidCoder/VidCoder.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@
289289
<Compile Include="Model\Database.cs" />
290290
<Compile Include="Model\DatabaseConfig.cs" />
291291
<Compile Include="Model\EncodeResult.cs" />
292+
<Compile Include="Model\LogEntry.cs" />
293+
<Compile Include="Model\LogSource.cs" />
294+
<Compile Include="Model\LogType.cs" />
292295
<Compile Include="Model\PreviewImageJob.cs" />
293296
<Compile Include="Model\SaveImageJob.cs" />
294297
<Compile Include="Services\Interfaces\ILogger.cs" />

VidCoder/View/LogWindow.xaml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99
<KeyBinding Key="P" Modifiers="Control" Command="{Binding MainViewModel.OpenPreviewWindowCommand}" />
1010
</Window.InputBindings>
1111
<Grid>
12-
<TextBox
12+
<RichTextBox
1313
Name="logTextBox"
1414
Margin="0,0,0,36"
1515
IsReadOnly="True"
16-
Text="{Binding LogText, Mode=OneWay}"
17-
TextWrapping="WrapWithOverflow" TextChanged="logTextBox_TextChanged"
18-
VerticalScrollBarVisibility="Auto"/>
16+
VerticalScrollBarVisibility="Auto">
17+
<FlowDocument Name="logDocument">
18+
<FlowDocument.Resources>
19+
<!-- Eliminate blank line between paragraphs -->
20+
<Style TargetType="{x:Type Paragraph}">
21+
<Setter Property="Margin" Value="0"/>
22+
</Style>
23+
</FlowDocument.Resources>
24+
</FlowDocument>
25+
</RichTextBox>
1926
<Button
2027
Content="Clear"
2128
Command="{Binding ClearLogCommand}"

VidCoder/View/LogWindow.xaml.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
using System.Windows.Media;
1111
using System.Windows.Media.Imaging;
1212
using System.Windows.Shapes;
13+
using Microsoft.Practices.Unity;
14+
using VidCoder.Model;
1315
using VidCoder.Properties;
16+
using VidCoder.Services;
1417

1518
namespace VidCoder.View
1619
{
@@ -19,15 +22,32 @@ namespace VidCoder.View
1922
/// </summary>
2023
public partial class LogWindow : Window
2124
{
25+
private ILogger logger = Unity.Container.Resolve<ILogger>();
26+
2227
public LogWindow()
2328
{
2429
InitializeComponent();
30+
31+
// Add all existing log entries
32+
foreach (LogEntry entry in this.logger.LogEntries)
33+
{
34+
this.AddEntry(entry);
35+
}
36+
37+
this.Loaded += (sender, e) =>
38+
{
39+
this.logTextBox.ScrollToEnd();
40+
};
41+
42+
// Subscribe to events
43+
this.logger.EntryLogged += OnEntryLogged;
44+
this.logger.Cleared += OnCleared;
2545
}
2646

2747
protected override void OnSourceInitialized(EventArgs e)
2848
{
2949
base.OnSourceInitialized(e);
30-
this.SetPlacement(Properties.Settings.Default.LogWindowPlacement);
50+
this.SetPlacement(Settings.Default.LogWindowPlacement);
3151
}
3252

3353
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
@@ -36,9 +56,37 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
3656
Settings.Default.Save();
3757
}
3858

39-
private void logTextBox_TextChanged(object sender, TextChangedEventArgs e)
59+
private void OnEntryLogged(object sender, EventArgs<LogEntry> e)
60+
{
61+
this.Dispatcher.BeginInvoke(new Action(() =>
62+
{
63+
this.AddEntry(e.Value);
64+
this.logTextBox.ScrollToEnd();
65+
}));
66+
}
67+
68+
private void OnCleared(object sender, EventArgs e)
69+
{
70+
this.Dispatcher.BeginInvoke(new Action(() =>
71+
{
72+
this.logDocument.Blocks.Clear();
73+
}));
74+
}
75+
76+
private void AddEntry(LogEntry entry)
4077
{
41-
this.logTextBox.ScrollToEnd();
78+
var run = new Run(entry.Text);
79+
80+
if (entry.LogType == LogType.Error)
81+
{
82+
run.Foreground = new SolidColorBrush(Colors.Red);
83+
}
84+
else if (entry.Source == LogSource.VidCoder)
85+
{
86+
run.Foreground = new SolidColorBrush(Colors.DarkBlue);
87+
}
88+
89+
this.logDocument.Blocks.Add(new Paragraph(run));
4290
}
4391
}
4492
}

VidCoder/View/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390
Orientation="Horizontal"
391391
Visibility="{Binding SourcePicked, Converter={StaticResource VisibilityConverter}}">
392392
<TextBlock Text="Source:" Style="{StaticResource SectionHeader}" />
393-
<Image Source="{Binding SourceIcon}" Width="16" Height="16" Margin="4,2,0,2" />
393+
<Image Source="{Binding SourceIcon}" Width="16" Height="16" Margin="4,2,0,2" VerticalAlignment="Top" HorizontalAlignment="Left" />
394394
<TextBlock Text="{Binding SourceText}" Style="{StaticResource SourceDescription}" Margin="4,4,4,3" />
395395
<Polygon
396396
VerticalAlignment="Center"
@@ -563,7 +563,7 @@
563563
Grid.Row="1" Grid.Column="0"
564564
HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,9,10,0" />
565565
<StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal">
566-
<ItemsControl Grid.Row="1" Grid.Column="1" ItemsSource="{Binding AudioChoices}" MinHeight="31" MaxHeight="58" HorizontalAlignment="Left" Style="{StaticResource RightGridItem}">
566+
<ItemsControl Grid.Row="1" Grid.Column="1" ItemsSource="{Binding AudioChoices}" MinWidth="210" MinHeight="31" MaxHeight="58" HorizontalAlignment="Left" Style="{StaticResource RightGridItem}">
567567
<ItemsControl.ItemTemplate>
568568
<DataTemplate>
569569
<StackPanel Orientation="Horizontal">

0 commit comments

Comments
 (0)