--- ibx/branches/udr/udr/source/FBUDRController.pas 2022/01/10 10:13:17 380 +++ ibx/branches/udr/udr/source/FBUDRController.pas 2022/01/15 00:06:22 381 @@ -73,12 +73,12 @@ type AllowConfigFileOverrides: boolean; LogFileNameTemplate: AnsiString; ConfigFileNameTemplate: AnsiString; - ForceWriteJournalEntries: boolean; + ForceWriteLogEntries: boolean; LogOptions: TFBUDRControllerLogOptions; ThreadSafeLogging: boolean; end; - {LogFileNameTemplate, ConfigFileName and DebugLogTemplate macros: + {LogFileNameTemplate and ConfigFileName macros: $LOGDIR = Firebird log directory $UDRDIR = Firebird UDR directory $TEMP = System temp directory @@ -91,7 +91,7 @@ const FBUDRControllerOptions: TFBUDRCont AllowConfigFileOverrides: false; LogFileNameTemplate:'$LOGDIR$TIMESTAMP$MODULE.log'; ConfigFileNameTemplate: '$UDRDIR$MODULE.conf'; - ForceWriteJournalEntries: false; + ForceWriteLogEntries: false; LogOptions: []; ThreadSafeLogging: false); @@ -130,12 +130,12 @@ type aName: AnsiString; factory: TObject); procedure FreeFactoryList; procedure LoadConfig; + function NeedLogStream: boolean; public constructor Create(status: Firebird.IStatus; udrPlugin: Firebird.IUdrPlugin; aTheirUnloadFlag: booleanPtr; var aMyUnloadFlag: booleanPtr); destructor Destroy; override; procedure FBSetStatusFromException(E: Exception; aStatus: Firebird.IStatus); - function GetLogStream: TStream; function ProcessTemplateMacros(aTemplate: AnsiString): AnsiString; procedure WriteToLog(Msg: AnsiString); overload; procedure WriteToLog(aTitle: AnsiString; Params: IFBUDRInputParams); overload; @@ -2098,7 +2098,7 @@ var aLogOptions: TFBUDRControllerLogOpti aConfigFileName: Ansistring; begin aConfigFileName := ProcessTemplateMacros(FBUDRControllerOptions.ConfigFileNameTemplate); - if (FConfigFile = nil) and FileExists(aConfigFileName) then + if (FConfigFile = nil) and (aConfigFileName <> '') and FileExists(aConfigFileName) then begin FConfigFile := TIniFile.Create(aConfigFileName); {$if declared(TStringArray)} @@ -2111,8 +2111,8 @@ begin begin LogFileNameTemplate := FConfigFile.ReadString('Controller','LogFileNameTemplate',LogFileNameTemplate); WriteToLog('LogFileNameTemplate = ' + LogFileNameTemplate); - ForceWriteJournalEntries := FConfigFile.ReadBool('Controller','ForceWriteJournalEntries',ForceWriteJournalEntries); - WriteToLog('ForceWriteJournalEntries = ' + BooleanToStr(ForceWriteJournalEntries ,'true','false')); + ForceWriteLogEntries := FConfigFile.ReadBool('Controller','ForceWriteLogEntries',ForceWriteLogEntries); + WriteToLog('ForceWriteLogEntries = ' + BooleanToStr(ForceWriteLogEntries ,'true','false')); ThreadSafeLogging := FConfigFile.ReadBool('Controller','ThreadSafeLogging',ThreadSafeLogging); WriteToLog('ThreadSafeLogging = ' + BooleanToStr(ThreadSafeLogging,'true','false')); if GetLogOptions( FConfigFile.ReadString('Controller','LogOptions',''),aLogOptions) then @@ -2122,6 +2122,29 @@ begin end; end; +function TFBUDRController.NeedLogStream: boolean; +var FilePathName: AnsiString; +begin + Result := false; + if FLogStream = nil then + begin + FilePathName := ProcessTemplateMacros(FBUDRControllerOptions.LogFileNameTemplate); + if FilePathName = '' then + Exit; + if FJnlOpenAppend then + begin + FLogStream := TFileStream.Create(FilePathName,fmOpenWrite or fmShareDenyNone); + FLogStream.Seek(0, soFromEnd); + end + else + begin + FLogStream := TFileStream.Create(FilePathName,fmCreate or fmShareDenyNone); + FJnlOpenAppend := true; + end; + end; + Result := true; +end; + function TFBUDRController.LogOptionsToStr(aLogOptions: TFBUDRControllerLogOptions): AnsiString; var i: TFBUDRControllerLogOption; separator: AnsiString; @@ -2191,41 +2214,25 @@ begin WriteToLog(SExceptionRaised + LineEnding + E.Message); end; -function TFBUDRController.GetLogStream: TStream; -var FilePathName: AnsiString; -begin - if FLogStream = nil then - begin - FilePathName := ProcessTemplateMacros(FBUDRControllerOptions.LogFileNameTemplate); - if FJnlOpenAppend then - begin - FLogStream := TFileStream.Create(FilePathName,fmOpenWrite or fmShareDenyNone); - FLogStream.Seek(0, soFromEnd); - end - else - FLogStream := TFileStream.Create(FilePathName,fmCreate or fmShareDenyNone); - end; - Result := FLogStream; - FJnlOpenAppend := true; -end; - procedure TFBUDRController.WriteToLog(Msg: AnsiString); var LogEntry: AnsiString; begin + if not NeedLogStream then + Exit; {no log file available} LogEntry := Format(sLogFormat,[FBFormatDateTime(GetDateTimeFmt,Now),Msg]) + LineEnding; if FBUDRControllerOptions.ThreadSafeLogging then begin FCriticalSection.Enter; try - GetLogStream.Write(LogEntry[1],Length(LogEntry)); - if FBUDRControllerOptions.ForceWriteJournalEntries then + FLogStream.Write(LogEntry[1],Length(LogEntry)); + if FBUDRControllerOptions.ForceWriteLogEntries then FreeAndNil(FLogStream); finally FCriticalSection.Leave; end; end else - GetLogStream.Write(LogEntry[1],Length(LogEntry)); + FLogStream.Write(LogEntry[1],Length(LogEntry)); end; function TFBUDRController.CharSetIDToText(att: IAttachment; id: integer): AnsiString; @@ -2252,7 +2259,7 @@ begin 'SQLType = ' + GetSQLTypeName + NewLineTAB + 'sub type = ' + IntToStr(getSubType) + NewLineTAB + 'Scale = ' + IntToStr(getScale) + NewLineTAB + - 'Charset = ' + CharSetIDToText(Params.GetAttachment,getCharSetID) + NewLineTAB + + 'Charset = ' + CharSetIDToText((Params as TFBUDRInputParams).GetAttachment,getCharSetID) + NewLineTAB + BooleanToStr(getIsNullable,'Nullable','Not Nullable') + NewLineTAB + 'Size = ' + IntToStr(GetSize) + NewLineTAB + 'Value = ' + BooleanToStr(IsNull,'NULL',GetStrValue(Params[i] as TColumnMetaData)) + LineEnding; @@ -2308,7 +2315,7 @@ begin 'SQLType = ' + GetSQLTypeName + NewLineTAB + 'sub type = ' + IntToStr(getSubType) + NewLineTAB + 'Scale = ' + IntToStr(getScale) + NewLineTAB + - 'Charset = ' + CharSetIDToText(OutputData.GetAttachment,getCharSetID) + NewLineTAB + + 'Charset = ' + CharSetIDToText((OutputData as TFBUDROutputParams).GetAttachment,getCharSetID) + NewLineTAB + BooleanToStr(getIsNullable,'Nullable','Not Nullable') + NewLineTAB + 'Size = ' + IntToStr(GetSize) + NewLineTAB + 'Value = ' + BooleanToStr(IsNull,'NULL', GetStrValue(OutputData[i] as TColumnMetaData)) + LineEnding; @@ -2325,7 +2332,10 @@ begin if loReadOnlyQueries in FBUDRControllerOptions.LogOptions then JnlOptions := JnlOptions + [joReadOnlyQueries]; if JnlOptions <> [] then - context.GetAttachment.StartJournaling(GetLogStream,JnlOptions); + begin + if NeedLogStream then + context.GetAttachment.StartJournaling(FLogStream,JnlOptions); + end; end; function TFBUDRController.HasConfigFile: boolean;