using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
namespace DrumKit
{
public sealed partial class LogControl : UserControl
{
#region Constructor
///
/// Creates a new instance of log page
///
public LogControl()
{
this.InitializeComponent();
this.Loaded += LogControl_Loaded;
}
#endregion
#region Initialization
///
/// Initialization performed when the page is loaded.
///
private async void LogControl_Loaded(object sender, RoutedEventArgs e)
{
// Reload entries
await ReloadEntries();
}
private async Task ReloadEntries()
{
// Get list of log files
await Repository.LogRepository.ReadLogFiles();
// Create list
this.logEntriesList.Items.Clear();
foreach (DateTime i in Repository.LogRepository.Dates)
this.logEntriesList.Items.Add(i);
// Set selected item
int index = Repository.LogRepository.Dates.IndexOf(Repository.LogRepository.CurrentLogDate);
this.logEntriesList.SelectedIndex = index;
}
#endregion
#region UI Events
///
/// Handles selection changed action.
///
private void LogEntriesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Load selected log file
if (logEntriesList.SelectedItem is DateTime)
LoadLogFile((DateTime)logEntriesList.SelectedItem);
}
///
/// Handles clear button
///
private async void ButtonClear_Click(object sender, RoutedEventArgs e)
{
await Repository.LogRepository.Clear();
await this.ReloadEntries();
}
///
/// Handles saving currently selected log file.
///
///
///
private async void ButtonSave_Click(object sender, RoutedEventArgs e)
{
// Sanity check
if (!(logEntriesList.SelectedItem is DateTime)) return;
// Pick a destination folder
var picker = new Windows.Storage.Pickers.FolderPicker();
picker.FileTypeFilter.Add("*");
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
var destination = await picker.PickSingleFolderAsync();
// Save
if (destination != null)
await Repository.LogRepository.SaveAs((DateTime)logEntriesList.SelectedItem, destination);
}
#endregion
#region Misc
///
/// Loads a log file, and converts it to html for display.
///
private async void LoadLogFile(DateTime dt)
{
// Get file contents
var lines = await Repository.LogRepository.ReadLog(dt);
// Generate HTML
System.Text.StringBuilder html = new System.Text.StringBuilder();
html.Append("
");
foreach (var i in lines)
{
if (i.Contains("ERROR"))
{
html.Append("");
html.Append(i);
html.Append("
");
}
else if (i.Contains("EXCEPTION"))
{
html.Append("");
html.Append(i);
html.Append("
");
}
else if (i.TrimStart(' ').StartsWith("at"))
{
html.Insert(html.Length - 4, "
" + i);
}
else
{
html.Append("");
html.Append(i);
html.Append("
");
}
}
html.Append("");
// Set text
this.logText.NavigateToString(html.ToString());
}
#endregion
}
}