Browse Source

Generalizing Bug 1956: Some errors and information while opening a session in scripting were not logged

https://winscp.net/tracker/1956

Source commit: 9feb2821d3330a8a6246d58c50cbe0e793b070f3
Martin Prikryl 4 years ago
parent
commit
ae928efc1c
3 changed files with 48 additions and 37 deletions
  1. 44 33
      source/core/Script.cpp
  2. 3 2
      source/core/Script.h
  3. 1 2
      source/core/Terminal.cpp

+ 44 - 33
source/core/Script.cpp

@@ -304,6 +304,7 @@ __fastcall TScript::TScript(bool LimitedOutput)
 {
   FLimitedOutput = LimitedOutput;
   FTerminal = NULL;
+  FLoggingTerminal = NULL;
   FGroups = false;
   FWantsProgress = false;
   FInteractive = false;
@@ -440,12 +441,13 @@ bool __fastcall TScript::IsTerminalLogging(TTerminal * ATerminal)
 }
 //---------------------------------------------------------------------------
 const static UnicodeString ScriptLogFormat(L"Script: %s");
-void __fastcall TScript::Log(TLogLineType Type, UnicodeString Str)
+void __fastcall TScript::Log(TLogLineType Type, const UnicodeString & AStr, TTerminal * ATerminal)
 {
-  Str = FORMAT(ScriptLogFormat, (Str));
-  if (IsTerminalLogging(Terminal))
+  UnicodeString Str = FORMAT(ScriptLogFormat, (AStr));
+  TTerminal * LoggingTerminal = (ATerminal != NULL ? Terminal : (FLoggingTerminal != NULL ? FLoggingTerminal : Terminal));
+  if (IsTerminalLogging(LoggingTerminal))
   {
-    Terminal->Log->Add(Type, Str);
+    LoggingTerminal->Log->Add(Type, Str);
   }
   else if (Configuration->Logging)
   {
@@ -895,9 +897,9 @@ void __fastcall TScript::Print(const UnicodeString Str, bool Error)
   }
 }
 //---------------------------------------------------------------------------
-void __fastcall TScript::PrintLine(const UnicodeString Str, bool Error)
+void __fastcall TScript::PrintLine(const UnicodeString Str, bool Error, TTerminal * ATerminal)
 {
-  Log(llOutput, Str);
+  Log(llOutput, Str, ATerminal);
   Print(Str + L"\n", Error);
 }
 //---------------------------------------------------------------------------
@@ -2261,7 +2263,7 @@ void __fastcall TManagementScript::TerminalInformation(
   DebugAssert(ATerminal != NULL);
   if ((Phase < 0) && (ATerminal->Status == ssOpening))
   {
-    PrintLine(Str);
+    PrintLine(Str, false, ATerminal);
   }
 }
 //---------------------------------------------------------------------------
@@ -2725,40 +2727,49 @@ void __fastcall TManagementScript::Connect(const UnicodeString Session,
 
       bool WasLogActions = Configuration->LogActions;
       TTerminal * ATerminal = FTerminalList->NewTerminal(Data);
+      DebugAssert(FLoggingTerminal == NULL);
+      FLoggingTerminal = ATerminal;
       try
       {
-        ATerminal->AutoReadDirectory = false;
+        try
+        {
+          ATerminal->AutoReadDirectory = false;
 
-        ATerminal->OnInformation = TerminalInformation;
-        ATerminal->OnPromptUser = TerminalPromptUser;
-        ATerminal->OnShowExtendedException = OnShowExtendedException;
-        ATerminal->OnQueryUser = OnTerminalQueryUser;
-        ATerminal->OnProgress = TerminalOperationProgress;
-        ATerminal->OnFinished = TerminalOperationFinished;
-        ATerminal->OnInitializeLog = TerminalInitializeLog;
+          ATerminal->OnInformation = TerminalInformation;
+          ATerminal->OnPromptUser = TerminalPromptUser;
+          ATerminal->OnShowExtendedException = OnShowExtendedException;
+          ATerminal->OnQueryUser = OnTerminalQueryUser;
+          ATerminal->OnProgress = TerminalOperationProgress;
+          ATerminal->OnFinished = TerminalOperationFinished;
+          ATerminal->OnInitializeLog = TerminalInitializeLog;
 
-        ConnectTerminal(ATerminal);
-      }
-      catch(Exception & E)
-      {
-        // fatal error, most probably caused by XML logging failure (as it has been turned off),
-        // and XML log is required => abort
-        if ((dynamic_cast<EFatal *>(&E) != NULL) &&
-            WasLogActions && !Configuration->LogActions &&
-            Configuration->LogActionsRequired)
-        {
-          FContinue = false;
+          ConnectTerminal(ATerminal);
         }
-        // make sure errors (mainly fatal ones) are associated
-        // with this terminal, not the last active one
-        bool Handled = HandleExtendedException(&E, ATerminal);
-        FTerminalList->FreeTerminal(ATerminal);
-        ATerminal = NULL;
-        if (!Handled)
+        catch(Exception & E)
         {
-          throw;
+          // fatal error, most probably caused by XML logging failure (as it has been turned off),
+          // and XML log is required => abort
+          if ((dynamic_cast<EFatal *>(&E) != NULL) &&
+              WasLogActions && !Configuration->LogActions &&
+              Configuration->LogActionsRequired)
+          {
+            FContinue = false;
+          }
+          // make sure errors (mainly fatal ones) are associated
+          // with this terminal, not the last active one
+          bool Handled = HandleExtendedException(&E, ATerminal);
+          FTerminalList->FreeTerminal(ATerminal);
+          ATerminal = NULL;
+          if (!Handled)
+          {
+            throw;
+          }
         }
       }
+      __finally
+      {
+        FLoggingTerminal = NULL;
+      }
 
       if (ATerminal != NULL)
       {

+ 3 - 2
source/core/Script.h

@@ -54,8 +54,8 @@ public:
   virtual __fastcall ~TScript();
 
   void __fastcall Command(UnicodeString Cmd);
-  void __fastcall Log(TLogLineType Type, UnicodeString Str);
-  void __fastcall PrintLine(const UnicodeString Str, bool Error = false);
+  void __fastcall Log(TLogLineType Type, const UnicodeString & Str, TTerminal * ATerminal = NULL);
+  void __fastcall PrintLine(const UnicodeString Str, bool Error = false, TTerminal * ATerminal = NULL);
   void __fastcall StartInteractive();
 
   void __fastcall Synchronize(const UnicodeString LocalDirectory,
@@ -81,6 +81,7 @@ public:
 
 protected:
   TTerminal * FTerminal;
+  TTerminal * FLoggingTerminal;
   TScriptCommands * FCommands;
   TScriptPrintEvent FOnPrint;
   TExtendedExceptionEvent FOnShowExtendedException;

+ 1 - 2
source/core/Terminal.cpp

@@ -1553,10 +1553,9 @@ void __fastcall TTerminal::OpenTunnel()
 
     FTunnelThread = new TTunnelThread(FTunnel);
   }
-  catch (Exception & E)
+  catch (...)
   {
     LogEvent(L"Error opening tunnel.");
-    Log->AddException(&E);
     CloseTunnel();
     throw;
   }