blob: 23c6adc80e2a09486025c7df8d81bcdbe96ff19b [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
Jens Geyer3d556242018-01-24 19:14:32 +010020unit ConsoleHelper;
Jens Geyerd5436f52014-10-03 19:50:38 +020021
22interface
23
Jens Geyer48d3bef2022-09-08 21:48:41 +020024uses Classes, SysUtils, SyncObjs;
Jens Geyerd5436f52014-10-03 19:50:38 +020025
26type
27 TThriftConsole = class
Jens Geyer48d3bef2022-09-08 21:48:41 +020028 strict private
29 FLock : TCriticalSection;
30 strict protected
31 procedure Lock;
32 procedure UnLock;
Jens Geyerd5436f52014-10-03 19:50:38 +020033 public
Jens Geyer48d3bef2022-09-08 21:48:41 +020034 constructor Create;
35 destructor Destroy; override;
36
Jens Geyerd5436f52014-10-03 19:50:38 +020037 procedure Write( const S: string); virtual;
38 procedure WriteLine( const S: string); virtual;
39 end;
40
Jens Geyer48d3bef2022-09-08 21:48:41 +020041
Jens Geyerd5436f52014-10-03 19:50:38 +020042 TGUIConsole = class( TThriftConsole )
Jens Geyer48d3bef2022-09-08 21:48:41 +020043 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +020044 FLineBreak : Boolean;
Jens Geyer9f7f11e2016-04-14 21:37:11 +020045 FMemo : TStrings;
Jens Geyerd5436f52014-10-03 19:50:38 +020046
47 procedure InternalWrite( const S: string; bWriteLine: Boolean);
48 public
Jens Geyer48d3bef2022-09-08 21:48:41 +020049 constructor Create( AMemo: TStrings);
50
Jens Geyerd5436f52014-10-03 19:50:38 +020051 procedure Write( const S: string); override;
52 procedure WriteLine( const S: string); override;
Jens Geyerd5436f52014-10-03 19:50:38 +020053 end;
54
Jens Geyer48d3bef2022-09-08 21:48:41 +020055
Jens Geyerd5436f52014-10-03 19:50:38 +020056function Console: TThriftConsole;
57procedure ChangeConsole( AConsole: TThriftConsole );
58procedure RestoreConsoleToDefault;
59
Jens Geyer48d3bef2022-09-08 21:48:41 +020060
Jens Geyerd5436f52014-10-03 19:50:38 +020061implementation
62
63var
64 FDefaultConsole : TThriftConsole;
65 FConsole : TThriftConsole;
66
67function Console: TThriftConsole;
68begin
69 Result := FConsole;
70end;
71
72{ TThriftConsole }
73
Jens Geyer48d3bef2022-09-08 21:48:41 +020074constructor TThriftConsole.Create;
75begin
76 inherited Create;
77 FLock := TCriticalSection.Create;
78end;
79
80destructor TThriftConsole.Destroy;
81begin
82 FreeAndNil( FLock);
83 inherited Destroy;
84end;
85
86procedure TThriftConsole.Lock;
87begin
88 FLock.Enter;
89end;
90
91procedure TThriftConsole.UnLock;
92begin
93 FLock.Leave;
94end;
95
Jens Geyerd5436f52014-10-03 19:50:38 +020096procedure TThriftConsole.Write(const S: string);
97begin
Jens Geyer48d3bef2022-09-08 21:48:41 +020098 Lock;
99 try
100 System.Write( S );
101 finally
102 Unlock;
103 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200104end;
105
106procedure TThriftConsole.WriteLine(const S: string);
107begin
Jens Geyer48d3bef2022-09-08 21:48:41 +0200108 Lock;
109 try
110 System.Writeln( S );
111 finally
112 Unlock;
113 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200114end;
115
116procedure ChangeConsole( AConsole: TThriftConsole );
117begin
118 FConsole := AConsole;
119end;
120
121procedure RestoreConsoleToDefault;
122begin
123 FConsole := FDefaultConsole;
124end;
125
126{ TGUIConsole }
127
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200128constructor TGUIConsole.Create( AMemo: TStrings);
Jens Geyerd5436f52014-10-03 19:50:38 +0200129begin
130 inherited Create;
131 FMemo := AMemo;
132 FLineBreak := True;
133end;
134
135procedure TGUIConsole.InternalWrite(const S: string; bWriteLine: Boolean);
Jens Geyer48d3bef2022-09-08 21:48:41 +0200136var idx : Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200137begin
Jens Geyer48d3bef2022-09-08 21:48:41 +0200138 Lock;
139 try
140
141 if FLineBreak then begin
Jens Geyerb636ffb2018-01-19 19:20:29 +0100142 FMemo.Add( S )
Jens Geyer48d3bef2022-09-08 21:48:41 +0200143 end
144 else begin
145 idx := FMemo.Count - 1;
146 if idx < 0
147 then FMemo.Add( S )
148 else FMemo[idx] := FMemo[idx] + S;
149 end;
150 FLineBreak := bWriteLine;
151
152 finally
153 Unlock;
Jens Geyerd5436f52014-10-03 19:50:38 +0200154 end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200155end;
156
157procedure TGUIConsole.Write(const S: string);
158begin
159 InternalWrite( S, False);
160end;
161
162procedure TGUIConsole.WriteLine(const S: string);
163begin
164 InternalWrite( S, True);
165end;
166
167initialization
Jens Geyerd5436f52014-10-03 19:50:38 +0200168 FDefaultConsole := TThriftConsole.Create;
169 FConsole := FDefaultConsole;
Jens Geyerd5436f52014-10-03 19:50:38 +0200170
171finalization
Jens Geyerd5436f52014-10-03 19:50:38 +0200172 FDefaultConsole.Free;
Jens Geyer48d3bef2022-09-08 21:48:41 +0200173 FDefaultConsole := nil;
Jens Geyerd5436f52014-10-03 19:50:38 +0200174
175end.
176
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200177