Skip to content

Commit

Permalink
Merge pull request microsoft#144 from jjw24/pluginInitFail
Browse files Browse the repository at this point in the history
Plugininitfail
  • Loading branch information
jjw24 authored Feb 21, 2020
2 parents 56c2964 + cb9e045 commit 73705be
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 15 deletions.
35 changes: 28 additions & 7 deletions Wox.Core/Plugin/PluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -92,21 +93,36 @@ public static void LoadPlugins(PluginsSettings settings)
Settings.UpdatePluginSettings(_metadatas);
AllPlugins = PluginsLoader.Plugins(_metadatas, Settings);
}

/// <summary>
/// Call initialize for all plugins
/// </summary>
/// <returns>return the list of failed to init plugins or null for none</returns>
public static void InitializePlugins(IPublicAPI api)
{
API = api;
var failedPlugins = new ConcurrentQueue<PluginPair>();
Parallel.ForEach(AllPlugins, pair =>
{
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
try
{
pair.Plugin.Init(new PluginInitContext
var milliseconds = Stopwatch.Debug($"|PluginManager.InitializePlugins|Init method time cost for <{pair.Metadata.Name}>", () =>
{
CurrentPluginMetadata = pair.Metadata,
API = API
pair.Plugin.Init(new PluginInitContext
{
CurrentPluginMetadata = pair.Metadata,
API = API
});
});
});
pair.Metadata.InitTime += milliseconds;
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
pair.Metadata.InitTime += milliseconds;
Log.Info($"|PluginManager.InitializePlugins|Total init cost for <{pair.Metadata.Name}> is <{pair.Metadata.InitTime}ms>");
}
catch (Exception e)
{
Log.Exception(nameof(PluginManager), $"Fail to Init plugin: {pair.Metadata.Name}", e);
pair.Metadata.Disabled = true; // TODO: not sure this really disable it later on
failedPlugins.Enqueue(pair);
}
});

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

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

public static void InstallPlugin(string path)
Expand Down
47 changes: 43 additions & 4 deletions Wox.Infrastructure/Logger/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Log
{
public const string DirectoryName = "Logs";

public static string CurrentLogDirectory { get; private set; }
public static string CurrentLogDirectory { get; }

static Log()
{
Expand Down Expand Up @@ -53,23 +53,32 @@ private static bool FormatValid(string message)

[MethodImpl(MethodImplOptions.Synchronized)]
public static void Exception(string className, string message, System.Exception exception, [CallerMemberName] string methodName = "")
{
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);

ExceptionInternal(classNameWithMethod, message, exception);
}

private static string CheckClassAndMessageAndReturnFullClassWithMethod(string className, string message,
string methodName)
{
if (string.IsNullOrWhiteSpace(className))
{
LogFaultyFormat($"Fail to specify a class name during logging of message: {message ?? "no message entered"}");
}

if (string.IsNullOrWhiteSpace(message))
{ // todo: not sure we really need that
{
// todo: not sure we really need that
LogFaultyFormat($"Fail to specify a message during logging");
}

if (!string.IsNullOrWhiteSpace(methodName))
{
className += "." + methodName;
return className + "." + methodName;
}

ExceptionInternal(className, message, exception);
return className;
}

private static void ExceptionInternal(string classAndMethod, string message, System.Exception e)
Expand Down Expand Up @@ -140,18 +149,48 @@ public static void Error(string message)
LogInternal(message, LogLevel.Error);
}

public static void Error(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Error, className, message, methodName);
}

private static void LogInternal(LogLevel level, string className, string message, [CallerMemberName] string methodName = "")
{
var classNameWithMethod = CheckClassAndMessageAndReturnFullClassWithMethod(className, message, methodName);

var logger = LogManager.GetLogger(classNameWithMethod);

System.Diagnostics.Debug.WriteLine($"{level.Name}|{message}");
logger.Log(level, message);
}

public static void Debug(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Debug, className, message, methodName);
}

/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Debug(string message)
{
LogInternal(message, LogLevel.Debug);
}

public static void Info(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Info, className, message, methodName);
}

/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Info(string message)
{
LogInternal(message, LogLevel.Info);
}

public static void Warn(string className, string message, [CallerMemberName] string methodName = "")
{
LogInternal(LogLevel.Warn, className, message, methodName);
}

/// <param name="message">example: "|prefix|unprefixed" </param>
public static void Warn(string message)
{
Expand Down
2 changes: 1 addition & 1 deletion Wox.Plugin/IPublicAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public interface IPublicAPI
/// <param name="title">Message title</param>
/// <param name="subTitle">Message subtitle</param>
/// <param name="iconPath">Message icon path (relative path to your plugin folder)</param>
void ShowMsg(string title, string subTitle = "", string iconPath = "");
void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true);

/// <summary>
/// Open setting dialog
Expand Down
3 changes: 3 additions & 0 deletions Wox.Plugin/PluginMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public override string ToString()
[Obsolete("Use IcoPath")]
public string FullIcoPath => IcoPath;

/// <summary>
/// Init time include both plugin load time and init time
/// </summary>
[JsonIgnore]
public long InitTime { get; set; }
[JsonIgnore]
Expand Down
6 changes: 3 additions & 3 deletions Wox/PublicAPIInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public void ShowApp()
_mainVM.MainWindowVisibility = Visibility.Visible;
}

public void ShowMsg(string title, string subTitle = "", string iconPath = "")
public void ShowMsg(string title, string subTitle = "", string iconPath = "", bool useMainWindowAsOwner = true)
{
Application.Current.Dispatcher.Invoke(() =>
{
var m = new Msg { Owner = Application.Current.MainWindow };
m.Show(title, subTitle, iconPath);
var msg = useMainWindowAsOwner ? new Msg {Owner = Application.Current.MainWindow} : new Msg();
msg.Show(title, subTitle, iconPath);
});
}

Expand Down

0 comments on commit 73705be

Please sign in to comment.