From 491a6298d12687bde2e336a2720f49358a853513 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 23 Aug 2012 23:16:36 +0000 Subject: [PATCH] Introduce property LogToFile in TMainForm, which is synced with the registry setting asLogToFile. Fixes issue #2936. --- source/helpers.pas | 6 +-- source/main.dfm | 1 + source/main.pas | 130 +++++++++++++++++++++------------------------ source/options.pas | 5 +- 4 files changed, 65 insertions(+), 77 deletions(-) diff --git a/source/helpers.pas b/source/helpers.pas index 857fa0a0..0871fe08 100644 --- a/source/helpers.pas +++ b/source/helpers.pas @@ -3371,8 +3371,8 @@ begin if (not SameAsDefault) and (not SameAsCurrent) then begin FRegistry.WriteInteger(ValueName, I); Inc(FWrites); - FSettings[Index].CurrentInt := I; end; + FSettings[Index].CurrentInt := I; end; adBool: begin SameAsDefault := B = FSettings[Index].DefaultBool; @@ -3380,8 +3380,8 @@ begin if (not SameAsDefault) and (not SameAsCurrent) then begin FRegistry.WriteBool(ValueName, B); Inc(FWrites); - FSettings[Index].CurrentBool := B; end; + FSettings[Index].CurrentBool := B; end; adString: begin SameAsDefault := S = FSettings[Index].DefaultString; @@ -3389,8 +3389,8 @@ begin if (not SameAsDefault) and (not SameAsCurrent) then begin FRegistry.WriteString(ValueName, S); Inc(FWrites); - FSettings[Index].CurrentString := S; end; + FSettings[Index].CurrentString := S; end; else raise Exception.CreateFmt(SUnsupportedSettingsDatatype, [FSettings[Index].Name]); diff --git a/source/main.dfm b/source/main.dfm index 9db4aef6..a74251ab 100644 --- a/source/main.dfm +++ b/source/main.dfm @@ -8844,6 +8844,7 @@ object MainForm: TMainForm end object popupSqlLog: TPopupMenu Images = ImageListMain + OnPopup = popupSqlLogPopup Left = 8 Top = 96 object Copy1: TMenuItem diff --git a/source/main.pas b/source/main.pas index 3cf42c7d..da9fb38a 100644 --- a/source/main.pas +++ b/source/main.pas @@ -880,10 +880,12 @@ type var Allowed: Boolean); procedure actGroupObjectsExecute(Sender: TObject); procedure lblExplainProcessAnalyzerClick(Sender: TObject); + procedure popupSqlLogPopup(Sender: TObject); private FLastHintMousepos: TPoint; FLastHintControlIndex: Integer; FDelimiter: String; + FLogToFile: Boolean; FFileNameSessionLog: String; FFileHandleSessionLog: Textfile; FLastMouseUpOnPageControl: Cardinal; @@ -949,6 +951,7 @@ type function TreeClickHistoryPrevious(MayBeNil: Boolean=False): PVirtualNode; procedure OperationRunning(Runs: Boolean); function RunQueryFiles(Filenames: TStrings; Encoding: TEncoding): Boolean; + procedure SetLogToFile(Value: Boolean); public QueryTabs: TObjectList; ActiveObjectEditor: TDBObjectEditor; @@ -1003,8 +1006,7 @@ type property ActiveConnection: TDBConnection read GetActiveConnection; property ActiveDatabase: String read GetActiveDatabase; property ActiveDbObj: TDBObject read FActiveDbObj write SetActiveDBObj; - procedure ActivateFileLogging; - procedure DeactivateFileLogging; + property LogToFile: Boolean read FLogToFile write SetLogToFile; procedure RefreshTree(FocusNewObject: TDBObject=nil); function GetRootNode(Tree: TBaseVirtualTree; Connection: TDBConnection): PVirtualNode; function FindDBObjectNode(Tree: TBaseVirtualTree; Obj: TDBObject): PVirtualNode; @@ -1276,9 +1278,7 @@ begin SaveListSetup(ListCommandStats); SaveListSetup(ListTables); - if AppSettings.ReadBool(asLogToFile) then - DeactivateFileLogging; - + LogToFile := False; AppSettings.Free; end; @@ -1498,8 +1498,7 @@ begin actDataShowNext.Hint := 'Show next '+FormatNumber(AppSettings.ReadInt(asDatagridRowsPerStep))+' rows ...'; actAboutBox.Caption := 'About '+APPNAME+' '+AppVersion; // Activate logging - if AppSettings.ReadBool(asLogToFile) then - ActivateFileLogging; + LogToFile := AppSettings.ReadBool(asLogToFile); if AppSettings.ReadBool(asLogHorizontalScrollbar) then actLogHorizontalScrollbar.Execute; @@ -4127,7 +4126,7 @@ begin SynMemoSQLLog.Repaint; // Log to file? - if AppSettings.ReadBool(asLogToFile) then + if FLogToFile then try Sess := ''; if Assigned(Connection) then @@ -4135,8 +4134,9 @@ begin WriteLn(FFileHandleSessionLog, Format('/* %s [%s] */ %s', [DateTimeToStr(Now), Sess, msg])); except on E:Exception do begin - DeactivateFileLogging; - ErrorDialog('Error writing to session log file.', FFileNameSessionLog+CRLF+CRLF+E.Message+CRLF+CRLF+'Logging is disabled now.'); + LogToFile := False; + AppSettings.WriteBool(asLogToFile, False); + ErrorDialog('Error writing to session log file.', E.Message+CRLF+'Filename: '+FFileNameSessionLog+CRLF+CRLF+'Logging is disabled now.'); end; end; end; @@ -5318,6 +5318,15 @@ begin ActiveQueryMemo.SetFocus; end; + +procedure TMainForm.popupSqlLogPopup(Sender: TObject); +begin + // Update popupMenu items + menuLogToFile.Checked := FLogToFile; + menuOpenLogFolder.Enabled := FLogToFile; +end; + + procedure TMainForm.AutoRefreshSetInterval(Sender: TObject); var SecondsStr: String; @@ -6399,66 +6408,54 @@ end; Start writing logfile. Called either in FormShow or after closing preferences dialog } -procedure TMainForm.ActivateFileLogging; +procedure TMainForm.SetLogToFile(Value: Boolean); var LogfilePattern, LogDir: String; i : Integer; begin - if AppSettings.ReadBool(asLogToFile) then + if Value = FLogToFile then Exit; - // Ensure directory exists - LogDir := AppSettings.ReadString(asSessionLogsDirectory); - if LogDir[Length(LogDir)] <> '\' then - LogDir := LogDir + '\'; - ForceDirectories(LogDir); + if Value then begin + // Ensure directory exists + LogDir := AppSettings.ReadString(asSessionLogsDirectory); + if LogDir[Length(LogDir)] <> '\' then + LogDir := LogDir + '\'; + ForceDirectories(LogDir); - // Determine free filename - LogfilePattern := '%.6u.log'; - i := 1; - FFileNameSessionLog := LogDir + goodfilename(Format(LogfilePattern, [i])); - while FileExists(FFileNameSessionLog) do begin - inc(i); + // Determine free filename + LogfilePattern := '%.6u.log'; + i := 1; FFileNameSessionLog := LogDir + goodfilename(Format(LogfilePattern, [i])); + while FileExists(FFileNameSessionLog) do begin + inc(i); + FFileNameSessionLog := LogDir + goodfilename(Format(LogfilePattern, [i])); + end; + + // Create file handle for writing + AssignFile( FFileHandleSessionLog, FFileNameSessionLog ); + {$I-} // Supress errors + if FileExists(FFileNameSessionLog) then + Append(FFileHandleSessionLog) + else + Rewrite(FFileHandleSessionLog); + {$I+} + if IOResult <> 0 then begin + AppSettings.WriteBool(asLogToFile, False); + ErrorDialog('Error opening session log file', FFileNameSessionLog+CRLF+CRLF+'Logging is disabled now.'); + end else begin + FLogToFile := Value; + LogSQL('Writing to session log file now: '+FFileNameSessionLog); + end; + end else begin + {$I-} // Supress errors + CloseFile(FFileHandleSessionLog); + {$I+} + // Reset IOResult so later checks in ActivateFileLogging doesn't get an old value + IOResult; + FLogToFile := Value; + LogSQL('Writing to session log file disabled now'); end; - - // Create file handle for writing - AssignFile( FFileHandleSessionLog, FFileNameSessionLog ); - {$I-} // Supress errors - if FileExists(FFileNameSessionLog) then - Append(FFileHandleSessionLog) - else - Rewrite(FFileHandleSessionLog); - {$I+} - if IOResult <> 0 then - begin - ErrorDialog('Error opening session log file', FFileNameSessionLog+CRLF+CRLF+'Logging is disabled now.'); - AppSettings.WriteBool(asLogToFile, False); - end else - AppSettings.WriteBool(asLogToFile, True); - // Update popupMenu items - menuLogToFile.Checked := AppSettings.ReadBool(asLogToFile); - menuOpenLogFolder.Enabled := AppSettings.ReadBool(asLogToFile); -end; - - -{** - Close logfile. - Called in FormClose, in ActivateFileLogging and on closing preferences dialog -} -procedure TMainForm.DeactivateFileLogging; -begin - if not AppSettings.ReadBool(asLogToFile) then - Exit; - AppSettings.WriteBool(asLogToFile, False); - {$I-} // Supress errors - CloseFile(FFileHandleSessionLog); - {$I+} - // Reset IOResult so later checks in ActivateFileLogging doesn't get an old value - IOResult; - // Update popupMenu items - menuLogToFile.Checked := AppSettings.ReadBool(asLogToFile); - menuOpenLogFolder.Enabled := AppSettings.ReadBool(asLogToFile); end; @@ -6505,18 +6502,11 @@ end; Enable/disable file logging by popupmenuclick } procedure TMainForm.menuLogToFileClick(Sender: TObject); -var - WasActivated: Boolean; begin - WasActivated := AppSettings.ReadBool(asLogToFile); - if not WasActivated then - ActivateFileLogging - else - DeactivateFileLogging; - + LogToFile := not LogToFile; // Save option AppSettings.ResetPath; - AppSettings.WriteBool(asLogToFile, not WasActivated); + AppSettings.WriteBool(asLogToFile, LogToFile); end; diff --git a/source/options.pas b/source/options.pas index 25bd4992..8936ba5c 100644 --- a/source/options.pas +++ b/source/options.pas @@ -303,10 +303,7 @@ begin end; end; - if chkLogToFile.Checked then - Mainform.ActivateFileLogging - else - Mainform.DeactivateFileLogging; + Mainform.LogToFile := chkLogToFile.Checked; DatatypeCategories[dtcInteger].Color := FGridTextColors[dtcInteger]; DatatypeCategories[dtcReal].Color := FGridTextColors[dtcReal]; DatatypeCategories[dtcText].Color := FGridTextColors[dtcText];