Skip to content

Commit

Permalink
Catch exceptions while scanning files + better logging
Browse files Browse the repository at this point in the history
- Should help in case of long paths and in case of paths with folders ending in a space
  • Loading branch information
rubenwe committed May 5, 2020
1 parent 8b19191 commit 8f52bda
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
10 changes: 9 additions & 1 deletion Assets/Scripts/Services/ImportFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace StlVault.Services
[DebuggerDisplay("{" + nameof(DisplayName) + "}")]
internal sealed class ImportFolder : FileSourceBase, IImportFolder
{
private static readonly ILogger Logger = UnityLogger.Instance;

private readonly Dictionary<string, IFileInfo> _knownFiles = new Dictionary<string, IFileInfo>();

// [CanBeNull] private IFolderWatcher _watcher;
Expand Down Expand Up @@ -74,10 +76,14 @@ private async Task RescanItemsAsync()

await Task.Run(() =>
{
Logger.Debug("Rescan of Import Folder `{0}` triggered", _config.FullPath);
var matchedFiles = _fileSystem
.GetFiles(SupportedFilePattern, _config.ScanSubDirectories)
.Where(file => file.Size > 112)
.ToDictionary(item => item.Path);
Logger.Trace("Found {0} matching files in folder.", matchedFiles.Count, _config.FullPath);
foreach (var (filePath, fileInfo) in matchedFiles)
{
Expand Down Expand Up @@ -109,12 +115,14 @@ private async Task RescanItemsAsync()
var token = _tokenSource.Token;
if (token.IsCancellationRequested) return;

Logger.Debug("Found {0} new file(s) and {1} removed file(s) since last folder scan.", importFiles.Count, removeFiles.Count);

if (removeFiles.Any()) Subscriber.OnItemsRemoved(this, removeFiles);
if (importFiles.Any()) await Subscriber.OnItemsAddedAsync(this, importFiles, token);
}
catch (Exception ex)
{
UnityLogger.Instance.Error(ex.Message);
Logger.Error(ex.Message);
}

if(State == Refreshing) State.Value = Ok;
Expand Down
28 changes: 21 additions & 7 deletions Assets/Scripts/Util/FileSystem/FolderFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using StlVault.Util.Logging;

namespace StlVault.Util.FileSystem
{
internal class FolderFileSystem : IFileSystem
{
private static readonly ILogger Logger = UnityLogger.Instance;

private readonly string _rootPath;

public FolderFileSystem(string rootPath)
Expand All @@ -21,20 +24,31 @@ public IFolderWatcher CreateWatcher(string filter, bool scanSubDirectories)

public bool FileExists(string filePath) => File.Exists(Path.Combine(_rootPath, filePath));

public IEnumerable<IFileInfo> GetFiles(string pattern, bool recursive)
public IReadOnlyList<IFileInfo> GetFiles(string pattern, bool recursive)
{
var options = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var files = new List<IFileInfo>();

foreach (var file in Directory.GetFiles(_rootPath, pattern, options))
{
var info = new System.IO.FileInfo(file);
try
{
var info = new System.IO.FileInfo(file);

yield return new FileInfo
files.Add(new FileInfo
{
LastChange = Max(info.CreationTime, info.LastWriteTime),
Path = file.Substring(_rootPath.Length).Trim(Path.DirectorySeparatorChar),
Size = info.Length
});
}
catch (Exception ex)
{
LastChange = Max(info.CreationTime, info.LastWriteTime),
Path = file.Substring(_rootPath.Length).Trim(Path.DirectorySeparatorChar),
Size = info.Length
};
Logger.Warn(ex, "Error while getting FileInfos for {0}", file);
}
}

return files;
}

public Task<byte[]> ReadAllBytesAsync(string filePath)
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Util/FileSystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal interface IFileSystem
IFolderWatcher CreateWatcher(string filter, bool scanSubDirectories);

bool FileExists(string filePath);
IEnumerable<IFileInfo> GetFiles(string pattern, bool recursive);
IReadOnlyList<IFileInfo> GetFiles(string pattern, bool recursive);
Task<byte[]> ReadAllBytesAsync(string filePath);
}
}
5 changes: 4 additions & 1 deletion Assets/Scripts/Util/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public interface ILogger

[StringFormatMethod("message")]
void Warn(string message, params object[] args);


[StringFormatMethod("message")]
void Warn(Exception ex, string message, params object[] args);

[StringFormatMethod("message")]
void Error(string message, params object[] args);

Expand Down
30 changes: 24 additions & 6 deletions Assets/Scripts/Util/Logging/UnityLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace StlVault.Util.Logging
{
Expand All @@ -19,40 +20,49 @@ public void Debug(string message, params object[] args)
{
if (LogLevel >= LogLevel.Debug)
{
_unityLogger.Log(string.Format(message, args));
_unityLogger.Log(GetFormattedMessage(message, args));
}
}

public void Trace(string message, params object[] args)
{
if (LogLevel >= LogLevel.Trace)
{
_unityLogger.Log(string.Format(message, args));
_unityLogger.Log(GetFormattedMessage(message, args));
}
}

public void Info(string message, params object[] args)
{
if (LogLevel >= LogLevel.Info)
{
_unityLogger.Log(string.Format(message, args));
_unityLogger.Log(GetFormattedMessage(message, args));
}
}

public void Warn(string message, params object[] args)
{
if (LogLevel >= LogLevel.Warn)
{
var formatted = string.Format(message, args);
var formatted = GetFormattedMessage(message, args);
_unityLogger.LogWarning(formatted, formatted);
}
}

public void Warn(Exception ex, string message, params object[] args)
{
if (LogLevel >= LogLevel.Warn)
{
var formatted = GetFormattedMessage(message, args);
_unityLogger.LogError(formatted, ex.Message);
}
}

public void Error(string message, params object[] args)
{
if (LogLevel >= LogLevel.Error)
{
var formatted = string.Format(message, args);
var formatted = GetFormattedMessage(message, args);
_unityLogger.LogError(formatted, formatted);
}
}
Expand All @@ -61,9 +71,17 @@ public void Error(Exception ex, string message, params object[] args)
{
if (LogLevel >= LogLevel.Error)
{
var formatted = string.Format(message, args);
var formatted = GetFormattedMessage(message, args);
_unityLogger.LogError(formatted, ex.Message);
}
}

private static string GetFormattedMessage(string message, object[] args)
{
var formatted = string.Format(message, args);
var logString = $"Thread {Thread.CurrentThread.ManagedThreadId:0000} | {formatted}";

return logString;
}
}
}
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 0.5.1
bundleVersion: 0.5.2
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down
27 changes: 27 additions & 0 deletions log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<b>New Features</b>
* Initial version of STLVault for Mac OSX
* Limitation: "Open in Finder" is not working
* Initial Support for Collections (#40)
* Group your files however you want!
* Extended filtering (#20)
* Searching for `tag*` will match all files with tags starting with `tag`
* Searching for `-tag` will match all files that don't have the tag `tag`
* These can be combined
* Mechanism to check for updates (#29)
* Option to specify an alias for an import folder (#37)

<b>BugFixes</b>
* Scroll handles now have a minimum size
* Simpler Mouse bindings for 3D View
* Deselecting items will now set "current" item to the last selected item
* Add tags of all import folders to models (Fixes #36)
* Prevent duplicate import folders and remove folder tags on delete (Fixes #30)
* Fix typos in save dialog
* Support Whitespace in "solid name" of ASCII STLs (Fixes #45)
* Allow adding of a tag, when some models already have that tag

<b>Misc</b>
* We added some developer documentation (#27 - Thanks to @bc3!)
* Changes for cross platform compatibility
* Application files are now stored in `%USERPROFILE%\AppData\LocalLow\StlVault\StlVault`
* Your configuration files of previous versions should be moved there automatically

0 comments on commit 8f52bda

Please sign in to comment.