Skip to content

Commit 0ccb2da

Browse files
committed
Improve log
1 parent 650875f commit 0ccb2da

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public List<Result> LoadContextMenus(Result result)
6363
private List<Result> CreateResultsFromQuery(Query query)
6464
{
6565
string termToSearch = query.Search;
66-
var processlist = processHelper.GetMatchingProcesses(termToSearch);
66+
var processlist = processHelper.GetMatchingProcesses(termToSearch, _context);
6767

6868
if (!processlist.Any())
6969
{

Plugins/Flow.Launcher.Plugin.ProcessKiller/PortHelper.cs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using Flow.Launcher.Infrastructure.Logger;
2-
using System;
1+
using System;
32
using System.Diagnostics;
3+
using System.Linq;
44
using System.Management;
55

66
namespace Flow.Launcher.Plugin.ProcessKiller
@@ -17,8 +17,8 @@ public override string ToString()
1717
{
1818
return $@" Process Name: {ProcessName}, Process ID: {ProcessID}, Port: {Port}, Path : {Path}";
1919
}
20-
2120
}
21+
2222
/// <summary>
2323
/// Usage:
2424
/// int port = 8081
@@ -32,34 +32,30 @@ public override string ToString()
3232
/// {
3333
/// Console.WriteLine("Port {0} is free ",port);
3434
/// }
35-
///
3635
/// </summary>
3736
internal class PortHelper
3837
{
3938
private const short MINIMUM_TOKEN_IN_A_LINE = 5;
4039
private const string COMMAND_EXE = "cmd";
4140

42-
public PortHelper()
43-
{
41+
private static string ClassName => nameof(PortHelper);
4442

45-
}
46-
47-
public static Tuple<bool, PortDetail> GetPortDetails(int port)
43+
public static Tuple<bool, PortDetail> GetPortDetails(int port, PluginInitContext context)
4844
{
4945
PortDetail PortDetail = new PortDetail();
5046
Tuple<bool, PortDetail> result = Tuple.Create(false, PortDetail);
5147

5248
// execute netstat command for the given port
5349
string commandArgument = string.Format("/c netstat -an -o -p tcp|findstr \":{0}.*LISTENING\"", port);
5450

55-
string commandOut = ExecuteCommandAndCaptureOutput(COMMAND_EXE, commandArgument);
51+
string commandOut = ExecuteCommandAndCaptureOutput(COMMAND_EXE, commandArgument, context);
5652
if (string.IsNullOrEmpty(commandOut))
5753
{
5854
// port is not in use
5955
return result;
6056
}
6157

62-
var stringTokens = commandOut.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries);
58+
var stringTokens = commandOut.Split(default(char[]), StringSplitOptions.RemoveEmptyEntries);
6359
if (stringTokens.Length < MINIMUM_TOKEN_IN_A_LINE)
6460
{
6561
return result;
@@ -72,45 +68,45 @@ public static Tuple<bool, PortDetail> GetPortDetails(int port)
7268
return result;
7369
}
7470

75-
int portFromHostPortToken = 0;
76-
if (!int.TryParse(hostPortTokens[1], out portFromHostPortToken))
71+
if (!int.TryParse(hostPortTokens[1], out var portFromHostPortToken))
7772
{
7873
return result;
7974
}
75+
8076
if (portFromHostPortToken != port)
8177
{
8278
return result;
8379
}
8480

8581
PortDetail.Port = port;
8682
PortDetail.ProcessID = int.Parse(stringTokens[4].Trim());
87-
Tuple<string, string> processNameAndPath = null;
83+
Tuple<string, string> processNameAndPath;
8884
try
8985
{
90-
processNameAndPath = GetProcessNameAndCommandLineArgs(PortDetail.ProcessID);
86+
processNameAndPath = GetProcessNameAndCommandLineArgs(PortDetail.ProcessID, context);
9187
PortDetail.ProcessName = processNameAndPath.Item1;
9288
PortDetail.Path = processNameAndPath.Item2;
9389
PortDetail.Process = Process.GetProcessById(PortDetail.ProcessID);
9490
result = Tuple.Create(true, PortDetail);
9591
}
96-
catch (Exception exp)
92+
catch (Exception e)
9793
{
98-
Console.WriteLine(exp.ToString());
99-
94+
context.API.LogException(ClassName, "Failed to get process name and path", e);
10095
}
10196

10297
return result;
103-
10498
}
99+
105100
/// <summary>
106101
/// Using WMI API to get process name and path instead of
107102
/// Process.GetProcessById, because if calling process ins
108103
/// 32 bit and given process id is 64 bit, caller will not be able to
109104
/// get the process name
110105
/// </summary>
111106
/// <param name="processID"></param>
107+
/// <param name="context"></param>
112108
/// <returns></returns>
113-
private static Tuple<string, string> GetProcessNameAndCommandLineArgs(int processID)
109+
private static Tuple<string, string> GetProcessNameAndCommandLineArgs(int processID, PluginInitContext context)
114110
{
115111
Tuple<string, string> result = Tuple.Create(string.Empty, string.Empty);
116112
string query = string.Format("Select Name,ExecutablePath from Win32_Process WHERE ProcessId='{0}'", processID);
@@ -121,31 +117,29 @@ private static Tuple<string, string> GetProcessNameAndCommandLineArgs(int proces
121117
ManagementObjectCollection results = searcher.Get();
122118

123119
// interested in first result.
124-
foreach (ManagementObject item in results)
120+
foreach (var item in results.Cast<ManagementObject>())
125121
{
126122
result = Tuple.Create(Convert.ToString(item["Name"]),
127123
Convert.ToString(item["ExecutablePath"]));
128124
break;
129-
130125
}
131126
}
132-
catch (Exception)
127+
catch (Exception e)
133128
{
134-
135-
throw;
129+
context.API.LogException(ClassName, "Failed to get process name and path", e);
136130
}
137131

138132
return result;
139-
140133
}
141134

142135
/// <summary>
143136
/// Execute the given command and captures the output
144137
/// </summary>
145138
/// <param name="commandName"></param>
146139
/// <param name="arguments"></param>
140+
/// <param name="context"></param>
147141
/// <returns></returns>
148-
private static string ExecuteCommandAndCaptureOutput(string commandName, string arguments)
142+
private static string ExecuteCommandAndCaptureOutput(string commandName, string arguments, PluginInitContext context)
149143
{
150144
string commandOut = string.Empty;
151145
Process process = new Process();
@@ -165,9 +159,9 @@ private static string ExecuteCommandAndCaptureOutput(string commandName, string
165159
}
166160
catch (Exception exp)
167161
{
168-
Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to ExecuteCommandAndCaptureOutput {commandName + arguments}", exp);
169-
Console.WriteLine(exp.ToString());
162+
context.API.LogException(ClassName, $"Failed to ExecuteCommandAndCaptureOutput {commandName + arguments}", exp);
170163
}
164+
171165
return commandOut;
172166
}
173167
}

Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ internal class ProcessHelper
3636
/// <summary>
3737
/// Returns a ProcessResult for evey running non-system process whose name matches the given searchTerm
3838
/// </summary>
39-
public List<ProcessResult> GetMatchingProcesses(string searchTerm)
39+
public List<ProcessResult> GetMatchingProcesses(string searchTerm, PluginInitContext context)
4040
{
4141
var processlist = new List<ProcessResult>();
4242

43-
int portNum;
44-
bool canConvert = int.TryParse(searchTerm, out portNum);
43+
bool canConvert = int.TryParse(searchTerm, out var portNum);
4544
Tuple<bool, PortDetail> tcpPortListeningProcess = null;
46-
if(canConvert)
47-
tcpPortListeningProcess = PortHelper.GetPortDetails(portNum);
45+
if (canConvert)
46+
{
47+
tcpPortListeningProcess = PortHelper.GetPortDetails(portNum, context);
48+
}
4849

4950
foreach (var p in Process.GetProcesses())
5051
{

0 commit comments

Comments
 (0)