Skip to content

Commit 67e5390

Browse files
committed
Merge branch 'dev'
2 parents cde7f2a + e35ad89 commit 67e5390

File tree

12 files changed

+303
-93
lines changed

12 files changed

+303
-93
lines changed

Plugins/Wox.Plugin.BrowserBookmark/Commands/Bookmarks.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ internal static class Bookmarks
88
{
99
internal static bool MatchProgram(Bookmark bookmark, string queryString)
1010
{
11-
if (StringMatcher.FuzzySearch(queryString, bookmark.Name, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
12-
if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
13-
if (StringMatcher.FuzzySearch(queryString, bookmark.Url, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
11+
if (StringMatcher.FuzzySearch(queryString, bookmark.Name).IsSearchPrecisionScoreMet()) return true;
12+
if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName).IsSearchPrecisionScoreMet()) return true;
13+
if (StringMatcher.FuzzySearch(queryString, bookmark.Url).IsSearchPrecisionScoreMet()) return true;
1414

1515
return false;
1616
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using NLog;
2+
using NLog.Config;
3+
using NLog.Targets;
4+
using System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Runtime.CompilerServices;
8+
using System.Security;
9+
using Wox.Infrastructure;
10+
11+
namespace Wox.Plugin.Program.Logger
12+
{
13+
/// <summary>
14+
/// The Program plugin has seen many issues recorded in the Wox repo related to various loading of Windows programs.
15+
/// This is a dedicated logger for this Program plugin with the aim to output a more friendlier message and clearer
16+
/// log that will allow debugging to be quicker and easier.
17+
/// </summary>
18+
internal static class ProgramLogger
19+
{
20+
public const string DirectoryName = "Logs";
21+
22+
static ProgramLogger()
23+
{
24+
var path = Path.Combine(Constant.DataDirectory, DirectoryName, Constant.Version);
25+
if (!Directory.Exists(path))
26+
{
27+
Directory.CreateDirectory(path);
28+
}
29+
30+
var configuration = new LoggingConfiguration();
31+
var target = new FileTarget();
32+
configuration.AddTarget("file", target);
33+
target.FileName = path.Replace(@"\", "/") + "/${shortdate}.txt";
34+
#if DEBUG
35+
var rule = new LoggingRule("*", LogLevel.Debug, target);
36+
#else
37+
var rule = new LoggingRule("*", LogLevel.Error, target);
38+
#endif
39+
configuration.LoggingRules.Add(rule);
40+
LogManager.Configuration = configuration;
41+
}
42+
43+
/// <summary>
44+
/// Please follow exception format: |class name|calling method name|loading program path|user friendly message that explains the error
45+
/// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue
46+
/// </summary>
47+
[MethodImpl(MethodImplOptions.Synchronized)]
48+
internal static void LogException(string message, Exception e)
49+
{
50+
//Index 0 is always empty.
51+
var parts = message.Split('|');
52+
var classname = parts[1];
53+
var callingMethodName = parts[2];
54+
var loadingProgramPath = parts[3];
55+
var interpretationMessage = parts[4];
56+
57+
Debug.WriteLine($"ERROR{message}");
58+
59+
var logger = LogManager.GetLogger("");
60+
61+
var innerExceptionNumber = 1;
62+
63+
var possibleResolution = "Not yet known";
64+
var errorStatus = "UNKNOWN";
65+
66+
logger.Error("------------- BEGIN Wox.Plugin.Program exception -------------");
67+
68+
do
69+
{
70+
if (IsKnownWinProgramError(e, callingMethodName) || IsKnownUWPProgramError(e, callingMethodName))
71+
{
72+
possibleResolution = "Can be ignored and Wox should still continue, however the program may not be loaded";
73+
errorStatus = "KNOWN";
74+
}
75+
76+
var calledMethod = e.TargetSite != null ? e.TargetSite.ToString() : e.StackTrace;
77+
78+
calledMethod = string.IsNullOrEmpty(calledMethod) ? "Not available" : calledMethod;
79+
80+
logger.Error($"\nException full name: {e.GetType().FullName}"
81+
+ $"\nError status: {errorStatus}"
82+
+ $"\nClass name: {classname}"
83+
+ $"\nCalling method: {callingMethodName}"
84+
+ $"\nProgram path: {loadingProgramPath}"
85+
+ $"\nInnerException number: {innerExceptionNumber}"
86+
+ $"\nException message: {e.Message}"
87+
+ $"\nException error type: HResult {e.HResult}"
88+
+ $"\nException thrown in called method: {calledMethod}"
89+
+ $"\nPossible interpretation of the error: {interpretationMessage}"
90+
+ $"\nPossible resolution: {possibleResolution}");
91+
92+
innerExceptionNumber++;
93+
e = e.InnerException;
94+
} while (e != null);
95+
96+
logger.Error("------------- END Wox.Plugin.Program exception -------------");
97+
}
98+
99+
private static bool IsKnownWinProgramError(Exception e, string callingMethodName)
100+
{
101+
if (e.TargetSite?.Name == "GetDescription" && callingMethodName == "LnkProgram")
102+
return true;
103+
104+
if (e is SecurityException || e is UnauthorizedAccessException || e is DirectoryNotFoundException)
105+
return true;
106+
107+
return false;
108+
}
109+
110+
private static bool IsKnownUWPProgramError(Exception e, string callingMethodName)
111+
{
112+
if (((e.HResult == -2147024774 || e.HResult == -2147009769) && callingMethodName == "ResourceFromPri")
113+
|| (e.HResult == -2147024894 && callingMethodName == "LogoPathFromUri"))
114+
return true;
115+
116+
if (callingMethodName == "XmlNamespaces")
117+
return true;
118+
119+
return false;
120+
}
121+
}
122+
}

Plugins/Wox.Plugin.Program/Programs/UWP.cs

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
using AppxPackaing;
1616
using Shell;
1717
using Wox.Infrastructure;
18-
using Wox.Infrastructure.Logger;
18+
using Wox.Plugin.Program.Logger;
1919
using IStream = AppxPackaing.IStream;
2020
using Rect = System.Windows.Rect;
2121

@@ -84,7 +84,8 @@ private void InitializeAppInfo()
8484
else
8585
{
8686
var e = Marshal.GetExceptionForHR((int)hResult);
87-
Log.Exception($"|UWP.InitializeAppInfo|SHCreateStreamOnFileEx on path <{path}> failed with HResult <{hResult}> and location <{Location}>.", e);
87+
ProgramLogger.LogException($"|UWP|InitializeAppInfo|{path}" +
88+
"|Error caused while trying to get the details of the UWP program", e);
8889
}
8990
}
9091

@@ -108,7 +109,9 @@ private string[] XmlNamespaces(string path)
108109
}
109110
else
110111
{
111-
Log.Error($"|UWP.XmlNamespaces|can't find namespaces for <{path}>");
112+
ProgramLogger.LogException($"|UWP|XmlNamespaces|{path}" +
113+
$"|Error occured while trying to get the XML from {path}", new ArgumentNullException());
114+
112115
return new string[] { };
113116
}
114117
}
@@ -131,15 +134,13 @@ private void InitPackageVersion(string[] namespaces)
131134
}
132135
}
133136

134-
Log.Error($"|UWP.InitPackageVersion| Unknown Appmanifest version UWP <{FullName}> with location <{Location}>.");
137+
ProgramLogger.LogException($"|UWP|XmlNamespaces|{Location}" +
138+
"|Trying to get the package version of the UWP program, but a unknown UWP appmanifest version "
139+
+ $"{FullName} from location {Location} is returned.", new FormatException());
140+
135141
Version = PackageVersion.Unknown;
136142
}
137143

138-
139-
140-
141-
142-
143144
public static Application[] All()
144145
{
145146
var windows10 = new Version(10, 0);
@@ -153,11 +154,20 @@ public static Application[] All()
153154
{
154155
u = new UWP(p);
155156
}
157+
#if !DEBUG
156158
catch (Exception e)
157159
{
158-
Log.Exception($"|UWP.All|Can't convert Package to UWP for <{p.Id.FullName}>:", e);
160+
ProgramLogger.LogException("|UWP|All|An unexpected error occured and "
161+
+ $"unable to convert Package to UWP for {p.Id.FullName}", e);
159162
return new Application[] { };
160163
}
164+
#endif
165+
#if DEBUG //make developer aware and implement handling
166+
catch(Exception)
167+
{
168+
throw;
169+
}
170+
#endif
161171
return u.Apps;
162172
}).ToArray();
163173

@@ -186,18 +196,12 @@ private static IEnumerable<Package> CurrentUserPackages()
186196
ps = ps.Where(p =>
187197
{
188198
bool valid;
189-
try
190-
{
191-
var f = p.IsFramework;
192-
var d = p.IsDevelopmentMode;
193-
var path = p.InstalledLocation.Path;
194-
valid = !f && !d && !string.IsNullOrEmpty(path);
195-
}
196-
catch (Exception e)
197-
{
198-
Log.Exception($"|UWP.CurrentUserPackages|Can't get package info for <{p.Id.FullName}>", e);
199-
valid = false;
200-
}
199+
200+
var f = p.IsFramework;
201+
var d = p.IsDevelopmentMode;
202+
var path = p.InstalledLocation.Path;
203+
valid = !f && !d && !string.IsNullOrEmpty(path);
204+
201205
return valid;
202206
});
203207
return ps;
@@ -382,7 +386,8 @@ internal string ResourceFromPri(string packageFullName, string resourceReference
382386
}
383387
else
384388
{
385-
Log.Error($"|UWP.ResourceFromPri|Can't load null or empty result pri <{source}> with uwp location <{Package.Location}>.");
389+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Can't load null or empty result "
390+
+ $"pri {source} in uwp location {Package.Location}", new NullReferenceException());
386391
return string.Empty;
387392
}
388393
}
@@ -395,7 +400,7 @@ internal string ResourceFromPri(string packageFullName, string resourceReference
395400
// Microsoft.MicrosoftOfficeHub_17.7608.23501.0_x64__8wekyb3d8bbwe: ms-resource://Microsoft.MicrosoftOfficeHub/officehubintl/AppManifest_GetOffice_Description
396401
// Microsoft.BingFoodAndDrink_3.0.4.336_x64__8wekyb3d8bbwe: ms-resource:AppDescription
397402
var e = Marshal.GetExceptionForHR((int)hResult);
398-
Log.Exception($"|UWP.ResourceFromPri|Load pri failed <{source}> with HResult <{hResult}> and location <{Package.Location}>.", e);
403+
ProgramLogger.LogException($"|UWP|ResourceFromPri|{Package.Location}|Load pri failed {source} with HResult {hResult} and location {Package.Location}", e);
399404
return string.Empty;
400405
}
401406
}
@@ -474,13 +479,16 @@ internal string LogoPathFromUri(string uri)
474479
}
475480
else
476481
{
477-
Log.Error($"|UWP.LogoPathFromUri| <{UserModelId}> can't find logo uri for <{uri}>, Package location <{Package.Location}>.");
482+
ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
483+
$"|{UserModelId} can't find logo uri for {uri} in package location: {Package.Location}", new FileNotFoundException());
478484
return string.Empty;
479485
}
480486
}
481487
else
482488
{
483-
Log.Error($"|UWP.LogoPathFromUri| <{UserModelId}> cantains can't find extension for <{uri}> Package location <{Package.Location}>.");
489+
ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
490+
$"|Unable to find extension from {uri} for {UserModelId} " +
491+
$"in package location {Package.Location}", new FileNotFoundException());
484492
return string.Empty;
485493
}
486494
}
@@ -506,7 +514,9 @@ private BitmapImage ImageFromPath(string path)
506514
}
507515
else
508516
{
509-
Log.Error($"|UWP.ImageFromPath|Can't get logo for <{UserModelId}> with path <{path}> and location <{Package.Location}>");
517+
ProgramLogger.LogException($"|UWP|ImageFromPath|{path}" +
518+
$"|Unable to get logo for {UserModelId} from {path} and" +
519+
$" located in {Package.Location}", new FileNotFoundException());
510520
return new BitmapImage(new Uri(Constant.ErrorIcon));
511521
}
512522
}
@@ -553,7 +563,10 @@ private ImageSource PlatedImage(BitmapImage image)
553563
}
554564
else
555565
{
556-
Log.Error($"|UWP.PlatedImage| Can't convert background string <{BackgroundColor}> to color for <{Package.Location}>.");
566+
ProgramLogger.LogException($"|UWP|PlatedImage|{Package.Location}" +
567+
$"|Unable to convert background string {BackgroundColor} " +
568+
$"to color for {Package.Location}", new InvalidOperationException());
569+
557570
return new BitmapImage(new Uri(Constant.ErrorIcon));
558571
}
559572
}

0 commit comments

Comments
 (0)