|
27 | 27 | using LogExpert.UI.Services.MenuToolbarService; |
28 | 28 | using LogExpert.UI.Services.SessionHandlerService; |
29 | 29 | using LogExpert.UI.Services.TabControllerService; |
| 30 | +using LogExpert.UI.Services.ToolLaunchService; |
30 | 31 | using LogExpert.UI.Services.ToolWindowCoordinatorService; |
31 | 32 |
|
32 | 33 | using NLog; |
@@ -54,6 +55,7 @@ internal partial class LogTabWindow : Form, ILogTabWindow |
54 | 55 | private readonly ToolWindowCoordinator _toolWindowCoordinator; |
55 | 56 | private readonly FileOperationService _fileOperationService; |
56 | 57 | private readonly SessionHandler _sessionHandler; |
| 58 | + private readonly ToolLaunchService _toolLaunchService; |
57 | 59 |
|
58 | 60 | private bool _disposed; |
59 | 61 |
|
@@ -114,6 +116,7 @@ public LogTabWindow (string[] fileNames, int instanceNumber, bool showInstanceNu |
114 | 116 | _fileOperationService.FileOpened += OnFileOperationServiceFileOpened; |
115 | 117 |
|
116 | 118 | _sessionHandler = new SessionHandler(PluginRegistry.PluginRegistry.Instance, request => _fileOperationService.AddFileTab(request)); |
| 119 | + _toolLaunchService = new ToolLaunchService(PluginRegistry.PluginRegistry.Instance); |
117 | 120 |
|
118 | 121 | _logWindowCoordinator = new LogWindowCoordinator(configManager, PluginRegistry.PluginRegistry.Instance, this, _tabController, _ledService, _fileOperationService); |
119 | 122 |
|
@@ -1328,102 +1331,69 @@ private void ToolButtonClick (ToolEntry toolEntry) |
1328 | 1331 | return; |
1329 | 1332 | } |
1330 | 1333 |
|
| 1334 | + ToolLaunchRequest request; |
| 1335 | + |
1331 | 1336 | if (CurrentLogWindow != null) |
1332 | 1337 | { |
1333 | 1338 | var line = CurrentLogWindow.GetCurrentLine(); |
1334 | 1339 | var info = CurrentLogWindow.GetCurrentFileInfo(); |
1335 | | - if (line != null && info != null) |
| 1340 | + if (line == null || info == null) |
1336 | 1341 | { |
1337 | | - ArgParser parser = new(toolEntry.Args); |
1338 | | - var argLine = parser.BuildArgs(line, CurrentLogWindow.GetRealLineNum() + 1, info, this); |
1339 | | - if (argLine != null) |
1340 | | - { |
1341 | | - StartTool(toolEntry.Cmd, argLine, toolEntry.Sysout, toolEntry.ColumnizerName, toolEntry.WorkingDir, true); |
1342 | | - } |
| 1342 | + return; |
1343 | 1343 | } |
| 1344 | + |
| 1345 | + ArgParser parser = new(toolEntry.Args); |
| 1346 | + var argLine = parser.BuildArgs(line, CurrentLogWindow.GetRealLineNum() + 1, info, this); |
| 1347 | + if (argLine == null) |
| 1348 | + { |
| 1349 | + return; |
| 1350 | + } |
| 1351 | + |
| 1352 | + request = new ToolLaunchRequest |
| 1353 | + { |
| 1354 | + Cmd = toolEntry.Cmd, |
| 1355 | + Args = argLine, |
| 1356 | + SysoutPipe = toolEntry.Sysout, |
| 1357 | + ColumnizerName = toolEntry.ColumnizerName, |
| 1358 | + WorkingDir = toolEntry.WorkingDir |
| 1359 | + }; |
1344 | 1360 | } |
1345 | 1361 | else |
1346 | 1362 | { |
1347 | | - StartTool(toolEntry.Cmd, string.Empty, toolEntry.Sysout, toolEntry.ColumnizerName, toolEntry.WorkingDir); |
1348 | | - } |
1349 | | - } |
1350 | | - |
1351 | | - [SupportedOSPlatform("windows")] |
1352 | | - private void StartTool (string cmd, string args, bool sysoutPipe, string columnizerName, string workingDir, bool startWithOpenLog = false) |
1353 | | - { |
1354 | | - if (string.IsNullOrEmpty(cmd)) |
1355 | | - { |
1356 | | - return; |
1357 | | - } |
| 1363 | + if (toolEntry.Sysout) |
| 1364 | + { |
| 1365 | + _ = MessageBox.Show(Resources.LogTabWindow_UI_Message_NoLogfileWithSysOutPipeToolConfigured, Resources.LogExpert_Common_UI_Title_LogExpert); |
| 1366 | + } |
1358 | 1367 |
|
1359 | | - Process process = new(); |
1360 | | - ProcessStartInfo startInfo = new(cmd, args); |
1361 | | - if (!string.IsNullOrEmpty(workingDir)) |
1362 | | - { |
1363 | | - startInfo.WorkingDirectory = workingDir; |
| 1368 | + request = new ToolLaunchRequest |
| 1369 | + { |
| 1370 | + Cmd = toolEntry.Cmd, |
| 1371 | + Args = string.Empty, |
| 1372 | + SysoutPipe = false, |
| 1373 | + ColumnizerName = toolEntry.ColumnizerName, |
| 1374 | + WorkingDir = toolEntry.WorkingDir |
| 1375 | + }; |
1364 | 1376 | } |
1365 | 1377 |
|
1366 | | - process.StartInfo = startInfo; |
1367 | | - process.EnableRaisingEvents = true; |
| 1378 | + var result = _toolLaunchService.Launch(request); |
1368 | 1379 |
|
1369 | | - if (sysoutPipe && !startWithOpenLog) |
| 1380 | + if (result.HasError) |
1370 | 1381 | { |
1371 | | - _ = MessageBox.Show(Resources.LogTabWindow_UI_Message_NoLogfileWithSysOutPipeToolConfigured, Resources.LogExpert_Common_UI_Title_LogExpert); |
| 1382 | + _ = MessageBox.Show(result.ErrorMessage, Resources.LogExpert_Common_UI_Title_LogExpert); |
| 1383 | + return; |
1372 | 1384 | } |
1373 | 1385 |
|
1374 | | - if (sysoutPipe && startWithOpenLog) |
| 1386 | + if (result.PipeFileName != null) |
1375 | 1387 | { |
1376 | | - var columnizer = ColumnizerPicker.DecideMemoryColumnizerByName(columnizerName, PluginRegistry.PluginRegistry.Instance.RegisteredColumnizers); |
| 1388 | + var title = CurrentLogWindow!.IsTempFile |
| 1389 | + ? CurrentLogWindow.TempTitleName |
| 1390 | + : $"{Util.GetNameFromPath(CurrentLogWindow.FileName)}{Resources.LogTabWindow_UI_LogWindow_Title_ExternalStartTool_Suffix}"; |
1377 | 1391 |
|
1378 | | - //_logger.Info($"Starting external tool with sysout redirection: {cmd} {args}")); |
1379 | | - startInfo.UseShellExecute = false; |
1380 | | - startInfo.RedirectStandardOutput = true; |
1381 | | - //process.OutputDataReceived += pipe.DataReceivedEventHandler; |
1382 | | - try |
| 1392 | + var logWin = AddTempFileTab(result.PipeFileName, title); |
| 1393 | + if (result.Columnizer != null) |
1383 | 1394 | { |
1384 | | - _ = process.Start(); |
| 1395 | + logWin.ForceColumnizer(result.Columnizer); |
1385 | 1396 | } |
1386 | | - catch (Exception e) when (e is Win32Exception or |
1387 | | - InvalidOperationException or |
1388 | | - ObjectDisposedException or |
1389 | | - PlatformNotSupportedException) |
1390 | | - { |
1391 | | - _logger.Error(e); |
1392 | | - _ = MessageBox.Show(e.Message, Resources.LogExpert_Common_UI_Title_LogExpert); |
1393 | | - return; |
1394 | | - } |
1395 | | - |
1396 | | - SysoutPipe pipe = new(process.StandardOutput); |
1397 | | - |
1398 | | - var logWin = AddTempFileTab(pipe.FileName, |
1399 | | - CurrentLogWindow.IsTempFile |
1400 | | - ? CurrentLogWindow.TempTitleName |
1401 | | - : $"{Util.GetNameFromPath(CurrentLogWindow.FileName)}{Resources.LogTabWindow_UI_LogWindow_Title_ExternalStartTool_Suffix}"); |
1402 | | - logWin.ForceColumnizer(columnizer); |
1403 | | - |
1404 | | - process.Exited += pipe.ProcessExitedEventHandler; |
1405 | | - //process.BeginOutputReadLine(); |
1406 | | - } |
1407 | | - else |
1408 | | - { |
1409 | | - StartExternalTool(process, startInfo); |
1410 | | - } |
1411 | | - } |
1412 | | - |
1413 | | - private static void StartExternalTool (Process process, ProcessStartInfo startInfo) |
1414 | | - { |
1415 | | - try |
1416 | | - { |
1417 | | - startInfo.UseShellExecute = false; |
1418 | | - _ = process.Start(); |
1419 | | - } |
1420 | | - catch (Exception e) when (e is Win32Exception or |
1421 | | - InvalidOperationException or |
1422 | | - ObjectDisposedException or |
1423 | | - PlatformNotSupportedException) |
1424 | | - { |
1425 | | - _logger.Error(e); |
1426 | | - _ = MessageBox.Show(e.Message, Resources.LogExpert_Common_UI_Title_LogExpert); |
1427 | 1397 | } |
1428 | 1398 | } |
1429 | 1399 |
|
|
0 commit comments