Skip to content

Commit

Permalink
Merge pull request #85 from erri120/slideshow-logging
Browse files Browse the repository at this point in the history
Added logging to file for slideshow
  • Loading branch information
halgari authored Oct 13, 2019
2 parents f12aee4 + 514c6b9 commit fadb0e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
10 changes: 10 additions & 0 deletions Wabbajack.Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public static void Log(string msg)
_loggerFn?.Invoke(msg);
}

public static void LogToFile(string msg)
{
lock (_lock)
{
msg = $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}";

File.AppendAllText(LogFile, msg + "\r\n");
}
}

public static void Status(string msg, int progress = 0)
{
_statusFn?.Invoke(msg, progress);
Expand Down
52 changes: 30 additions & 22 deletions Wabbajack/UI/SlideShow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,45 @@ public void UpdateSlideShowItem()

private void CacheImage(Slide slide)
{
Utils.LogToFile($"Caching slide for {slide.ModName} at {slide.ImageURL}");
using (var ms = new MemoryStream())
{
if (UseSync)
try
{
if (UseSync)
{
_appState.dispatcher.Invoke(() =>
{
using (var stream = new HttpClient().GetStreamSync(slide.ImageURL))
stream.CopyTo(ms);
});
}
else
{
using (Task<Stream> stream = new HttpClient().GetStreamAsync(slide.ImageURL))
{
stream.Wait();
stream.Result.CopyTo(ms);
}
}

ms.Seek(0, SeekOrigin.Begin);
_appState.dispatcher.Invoke(() =>
{
using (var stream = new HttpClient().GetStreamSync(slide.ImageURL))
stream.CopyTo(ms);
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
image.Freeze();

slide.Image = image;
});
}
else
catch (Exception e)
{
using (Task<Stream> stream = new HttpClient().GetStreamAsync(slide.ImageURL))
{
stream.Wait();
stream.Result.CopyTo(ms);
}
Utils.LogToFile($"Exception while caching slide {slide.ModName} ({slide.ModID})\n{e.ExceptionToString()}");
}

ms.Seek(0, SeekOrigin.Begin);
_appState.dispatcher.Invoke(() =>
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = ms;
image.EndInit();
image.Freeze();

slide.Image = image;
});
}
}

Expand Down

0 comments on commit fadb0e6

Please sign in to comment.