Skip to content

Commit

Permalink
Centralize exception logging [Zeugma440#164]
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeugma440 committed Sep 25, 2022
1 parent 11e27bc commit 63cb5ad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 38 deletions.
4 changes: 2 additions & 2 deletions ATL.test/Playlist/PlaylistIOTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void PLIO_R_Common()
catch
{
IList<LogItem> logItems = log.GetAllItems(Log.LV_ERROR);
Assert.AreEqual(1, logItems.Count);
Assert.AreEqual(2, logItems.Count); // Message and stacktrace
Assert.IsTrue(logItems[0].Message.Contains("efiufhziuefizeub.m3u")); // Can't do much more than than because the exception message is localized
}
}
Expand All @@ -75,7 +75,7 @@ public void PL_Common()
catch
{
IList<LogItem> logItems = log.GetAllItems(Log.LV_ERROR);
Assert.AreEqual(1, logItems.Count);
Assert.AreEqual(2, logItems.Count); // Message and stacktrace
Assert.IsTrue(logItems[0].Message.Contains("efiufhziuefizeub.m3u")); // Can't do much more than than because the exception message is localized
}
}
Expand Down
34 changes: 6 additions & 28 deletions ATL/AudioData/AudioDataManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ATL.AudioData.IO;
using ATL.Logging;
using Commons;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -336,16 +337,7 @@ public bool ReadFromFile(bool readEmbeddedPictures = false, bool readAllMetaFram
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception BEGIN");
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
Console.WriteLine("Inner Exception END");
}
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
Utils.TraceException(e);
result = false;
}

Expand Down Expand Up @@ -404,7 +396,7 @@ public bool UpdateTagInFile(TagData theTag, TagType tagType, ProgressManager wri
}
catch (Exception e)
{
handleWriteException(e);
Utils.TraceException(e);
result = false;
}
}
Expand Down Expand Up @@ -455,7 +447,7 @@ public async Task<bool> UpdateTagInFileAsync(TagData theTag, TagType tagType, Pr
}
catch (Exception e)
{
handleWriteException(e);
Utils.TraceException(e);
result = false;
}
}
Expand Down Expand Up @@ -508,7 +500,7 @@ public bool RemoveTagFromFile(TagType tagType, ProgressManager progressManager =
}
catch (Exception e)
{
handleWriteException(e);
Utils.TraceException(e);
result = false;
}

Expand Down Expand Up @@ -544,27 +536,13 @@ public async Task<bool> RemoveTagFromFileAsync(TagType tagType, ProgressManager
}
catch (Exception e)
{
handleWriteException(e);
Utils.TraceException(e);
result = false;
}

return result;
}

private void handleWriteException(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception BEGIN");
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
Console.WriteLine("Inner Exception END");
}
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
}

private bool read(BinaryReader source, bool readEmbeddedPictures = false, bool readAllMetaFrames = false, bool prepareForWriting = false)
{
sizeInfo.ResetData();
Expand Down
2 changes: 1 addition & 1 deletion ATL/CatalogDataReaders/BinaryLogic/Cue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void read()
using (FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.Read, 2048, FileOptions.SequentialScan))
{
// Determine encoding
Encoding encoding = Utils.guessTextEncoding(fs);
Encoding encoding = Utils.GuessTextEncoding(fs);
fs.Seek(0, SeekOrigin.Begin);

// Read contents
Expand Down
9 changes: 3 additions & 6 deletions ATL/Playlist/PlaylistIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public IList<string> getFiles()
}
catch (Exception e)
{
System.Console.WriteLine(e.StackTrace);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
Utils.TraceException(e);
}

return result;
Expand All @@ -121,8 +120,7 @@ public IList<Track> getTracks()
}
catch (Exception e)
{
System.Console.WriteLine(e.StackTrace);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
Utils.TraceException(e);
}

return result;
Expand Down Expand Up @@ -162,8 +160,7 @@ public void setTracks(IList<Track> trackList)
}
catch (Exception e)
{
System.Console.WriteLine(e.StackTrace);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
Utils.TraceException(e);
}
}

Expand Down
7 changes: 7 additions & 0 deletions ATL/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public static class Settings
/// </summary>
public static bool NullAbsentValues = false;

/// <summary>
/// Write stacktraces to stdout
/// NB : Regardless of this setting, stacktraces are aways logged to ATL's logging system
/// Default : true
/// </summary>
public static bool OutputStacktracesToConsole = true;


/*
* ========= GENERIC FUNCTIONAL SETTINGS
Expand Down
28 changes: 27 additions & 1 deletion ATL/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Collections.Generic;
using System.IO;
using ATL.Logging;

namespace Commons
{
Expand Down Expand Up @@ -507,13 +508,38 @@ private static Encoding getEncodingCached(string code)
return encodingCache[code];
}

public static Encoding guessTextEncoding(FileStream fs)
public static Encoding GuessTextEncoding(FileStream fs)
{
Ude.CharsetDetector cdet = new Ude.CharsetDetector();
cdet.Feed(fs);
cdet.DataEnd();
if (cdet.Charset != null) return getEncodingCached(cdet.Charset);
else return Settings.DefaultTextEncoding;
}

public static void TraceException(Exception e)
{
if (Settings.OutputStacktracesToConsole)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
if (e.InnerException != null)
{
Console.WriteLine("Inner Exception BEGIN");
Console.WriteLine(e.InnerException.Message);
Console.WriteLine(e.InnerException.StackTrace);
Console.WriteLine("Inner Exception END");
}
}
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.StackTrace);
if (e.InnerException != null)
{
LogDelegator.GetLogDelegate()(Log.LV_ERROR, "Inner Exception BEGIN");
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.InnerException.Message);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.InnerException.StackTrace);
LogDelegator.GetLogDelegate()(Log.LV_ERROR, "Inner Exception END");
}
}
}
}

0 comments on commit 63cb5ad

Please sign in to comment.