Skip to content

Commit 73705be

Browse files
authored
Merge pull request #144 from jjw24/pluginInitFail
Plugininitfail
2 parents 56c2964 + cb9e045 commit 73705be

File tree

5 files changed

+78
-15
lines changed

5 files changed

+78
-15
lines changed

Wox.Core/Plugin/PluginManager.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
34
using System.IO;
45
using System.Linq;
@@ -92,21 +93,36 @@ public static void LoadPlugins(PluginsSettings settings)
9293
Settings.UpdatePluginSettings(_metadatas);
9394
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
9495
}
96+
97+
/// <summary>
98+
/// Call initialize for all plugins
99+
/// </summary>
100+
/// <returns>return the list of failed to init plugins or null for none</returns>
95101
public static void InitializePlugins(IPublicAPI api)
96102
{
97103
API = api;
104+
var failedPlugins = new ConcurrentQueue<PluginPair>();
98105
Parallel.ForEach(AllPlugins, pair =>
99106
{
100-
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
107+
try
101108
{
102-
pair.Plugin.Init(new PluginInitContext
109+
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
103110
{
104-
CurrentPluginMetadata = pair.Metadata,
105-
API = API
111+
pair.Plugin.Init(new PluginInitContext
112+
{
113+
CurrentPluginMetadata = pair.Metadata,
114+
API = API
115+
});
106116
});
107-
});
108-
pair.Metadata.InitTime += milliseconds;
109-
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
117+
pair.Metadata.InitTime += milliseconds;
118+
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
119+
}
120+
catch (Exception e)
121+
{
122+
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
123+
pair.Metadata.Disabled = true; // TODO: not sure this really disable it later on
124+
failedPlugins.Enqueue(pair);
125+
}
110126
});
111127

112128
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
@@ -121,6 +137,11 @@ public static void InitializePlugins(IPublicAPI api)
121137
.ForEach(x => NonGlobalPlugins[x] = plugin);
122138
}
123139

140+
if (failedPlugins.Any())
141+
{
142+
var failed = string.Join(",", failedPlugins.Select(x => x.Metadata.Name));
143+
API.ShowMsg($"Fail to Init Plugins", $"Plugins: {failed} - fail to load and would be disabled, please contact plugin creator for help", "", false);
144+
}
124145
}
125146

126147
public static void InstallPlugin(string path)

Wox.Infrastructure/Logger/Log.cs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class Log
1111
{
1212
public const string DirectoryName = "Logs";
1313

14-
public static string CurrentLogDirectory { get; private set; }
14+
public static string CurrentLogDirectory { get; }
1515

1616
static Log()
1717
{
@@ -53,23 +53,32 @@ private static bool FormatValid(string message)
5353

5454
[MethodImpl(MethodImplOptions.Synchronized)]
5555
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
56+
{
57+
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
58+
59+
ExceptionInternal(classNameWithMethod, message, exception);
60+
}
61+
62+
private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,
63+
string methodName)
5664
{
5765
if (string.IsNullOrWhiteSpace(className))
5866
{
5967
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
6068
}
6169

6270
if (string.IsNullOrWhiteSpace(message))
63-
{ // todo: not sure we really need that
71+
{
72+
// todo: not sure we really need that
6473
LogFaultyFormat($"Fail to specify a message during logging");
6574
}
6675

6776
if (!string.IsNullOrWhiteSpace(methodName))
6877
{
69-
className += "." + methodName;
78+
return className + "." + methodName;
7079
}
7180

72-
ExceptionInternal(className, message, exception);
81+
return className;
7382
}
7483

7584
private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
@@ -140,18 +149,48 @@ public static void Error(string message)
140149
LogInternal(message, LogLevel.Error);
141150
}
142151

152+
public static void Error(string className, string message, [CallerMemberName] string methodName = "")
153+
{
154+
LogInternal(LogLevel.Error, className, message, methodName);
155+
}
156+
157+
private static void LogInternal(LogLevel level, string className, string message, [CallerMemberName] string methodName = "")
158+
{
159+
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);
160+
161+
var logger = LogManager.GetLogger(classNameWithMethod);
162+
163+
System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
164+
logger.Log(level, message);
165+
}
166+
167+
public static void Debug(string className, string message, [CallerMemberName] string methodName = "")
168+
{
169+
LogInternal(LogLevel.Debug, className, message, methodName);
170+
}
171+
143172
/// <param name="message">example: "|prefix|unprefixed" </param>
144173
public static void Debug(string message)
145174
{
146175
LogInternal(message, LogLevel.Debug);
147176
}
148177

178+
public static void Info(string className, string message, [CallerMemberName] string methodName = "")
179+
{
180+
LogInternal(LogLevel.Info, className, message, methodName);
181+
}
182+
149183
/// <param name="message">example: "|prefix|unprefixed" </param>
150184
public static void Info(string message)
151185
{
152186
LogInternal(message, LogLevel.Info);
153187
}
154188

189+
public static void Warn(string className, string message, [CallerMemberName] string methodName = "")
190+
{
191+
LogInternal(LogLevel.Warn, className, message, methodName);
192+
}
193+
155194
/// <param name="message">example: "|prefix|unprefixed" </param>
156195
public static void Warn(string message)
157196
{

Wox.Plugin/IPublicAPI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface IPublicAPI
7676
/// <param name="title">Message title</param>
7777
/// <param name="subTitle">Message subtitle</param>
7878
/// <param name="iconPath">Message icon path (relative path to your plugin folder)</param>
79-
void ShowMsg(string title, string subTitle = "", string iconPath = "");
79+
void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true);
8080

8181
/// <summary>
8282
/// Open setting dialog

Wox.Plugin/PluginMetadata.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public override string ToString()
4646
[Obsolete("Use IcoPath")]
4747
public string FullIcoPath => IcoPath;
4848

49+
/// <summary>
50+
/// Init time include both plugin load time and init time
51+
/// </summary>
4952
[JsonIgnore]
5053
public long InitTime { get; set; }
5154
[JsonIgnore]

Wox/PublicAPIInstance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public void ShowApp()
9191
_mainVM.MainWindowVisibility = Visibility.Visible;
9292
}
9393

94-
public void ShowMsg(string title, string subTitle = "", string iconPath = "")
94+
public void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true)
9595
{
9696
Application.Current.Dispatcher.Invoke(() =>
9797
{
98-
var m = new Msg { Owner = Application.Current.MainWindow };
99-
m.Show(title, subTitle, iconPath);
98+
var msg = useMainWindowAsOwner ? new Msg {Owner = Application.Current.MainWindow} : new Msg();
99+
msg.Show(title, subTitle, iconPath);
100100
});
101101
}
102102

0 commit comments

Comments
 (0)