forked from grijjy/GrijjyFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grijjy.CloudLogging.Protocol.pas
351 lines (299 loc) · 9.17 KB
/
Grijjy.CloudLogging.Protocol.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
unit Grijjy.CloudLogging.Protocol;
{$INCLUDE 'Grijjy.inc'}
interface
uses
System.SysUtils,
System.Messaging,
Grijjy.ProtocolBuffers,
PascalZMQ,
ZMQ.ClientProtocol,
ZMQ.Shared;
type
{ Protocol buffer that defines the message and metadata for a log message }
TgoLogMessageProtocol = record
public
{ The log message }
[Serialize(1)] MessageText: String;
{ The log level (Info, Warning or Error) }
[Serialize(2)] Level: Integer;
{ The ID of the process that send the message }
[Serialize(3)] ProcessId: FixedUInt32;
{ The ID of the thread that send the message }
[Serialize(4)] ThreadId: FixedUInt32;
{ The name of the application that send the message }
[Serialize(5)] AppName: String;
{ The date and time at which the message was sent }
[Serialize(6)] TimeStamp: TDateTime;
{ Describes the format of any optional data }
[Serialize(7)] DataFormat: Integer;
{ Any optional data to send along with the messsage }
[Serialize(8)] Data: TBytes;
end;
type
{ Protocol buffer that defines a command from the log viewer }
TgoLogCommandProtocol = record
public
{ The command }
[Serialize(1)] Command: Integer;
{ Serialized arguments }
[Serialize(2)] Args: TBytes;
end;
type
TgoHandleArray = record
{ Array of handles }
[Serialize(1)] Handles: TArray<THandle>;
end;
type
{ Text alignment options for watch values }
TgoWatchAlign = (Left, Center, Right);
type
{ A live watch as used by TgoLogLiveWatchesProtocol }
TgoLiveWatch = record
public
{ Name of the live watch }
[Serialize(1)] Name: String;
{ Value of the live watch }
[Serialize(2)] Value: String;
{ Display alignment of the value. }
[Serialize(3)] ValueAlign: TgoWatchAlign;
end;
type
{ Protocol buffer that defines a list of live watches }
TgoLogLiveWatchesProtocol = record
public
{ Array of live watches }
[Serialize(1)] Watches: TArray<TgoLiveWatch>;
end;
type
{ Protocol buffer that defines a memory usage report }
TgoLogMemoryUsageProtocol = record
public type
{ Represents a single instance }
TInstance = record
{ Caption to use to display details about the instance in the log viewer.
For classes derived from TComponent, this will be the name of the owner
and name of the instance if available. Otherwise, it will be its
ToString value. }
[Serialize(1)] Caption: String;
end;
public type
{ Single entry for a class }
TEntry = record
{ Name of this class. }
[Serialize(1)] ClassName: String;
{ Handle of this class. This is a TClass. But since TClass cannot be
used across process boundaries, it is typecast to a THandle. It is
only used for identification purposes. }
[Serialize(2)] ClassHandle: THandle;
{ Number of live instances for this class. }
[Serialize(3)] InstanceCount: Integer;
{ Live instances for this class (if requested). }
[Serialize(4)] Instances: TArray<TInstance>;
end;
PEntry = ^TEntry;
public
{ Array of class names and its number of instances }
[Serialize(1)] Entries: TArray<TEntry>;
{ Approximate number of bytes allocated by the current process. }
[Serialize(2)] AllocatedBytes: Int64;
end;
type
TgoCloudLogger = class(TZMQClientProtocol)
private
{ Internal }
FBroker: String;
FAppName: String;
FProcessId: Cardinal;
procedure SetBroker(const Value: String);
private
class function GetMemoryUsage(const AArgs: TBytes): TBytes; static;
class function GetLiveWatches: TBytes; static;
protected
{ Implements the DoRecv from the client protocol class }
procedure DoRecv(const ACommand: TZMQCommand;
var AMsg: PZMessage; var ASentFrom: PZFrame); override;
public
constructor Create;
destructor Destroy; override;
{ Sends a message to the specified service, with optional data }
procedure Send(const AService: String; const AMsg: String;
const ALevel, ADataFormat: Integer; const AData: TBytes); reintroduce;
property Broker: String read FBroker write SetBroker;
end;
type
{ This message is broadcast to receive fill a TgoLogMemoryUsageProtocol
record with information about live instances.
The Grijjy.CloudLogging.InstanceTracker unit listens for this message. }
TgoGetInstancesMessage = class(TMessage)
private
FClasses: TArray<TClass>;
public
constructor Create(const AClasses: TArray<TClass>);
{ Is set to an array of classes for which to receive details (instances).
This are the classes that are expanded in the corresponding tree view in
the log viewer. If nil, only class summaries are returned. }
property Classes: TArray<TClass> read FClasses;
public
{ The protocol to be filled in by the message listener. }
Protocol: TgoLogMemoryUsageProtocol;
end;
const
{ These constants are used internally and are shared with the Log Viewer.
You should not use these yourself. }
LOG_FORMAT_NONE = 0;
LOG_FORMAT_TSTRINGS = 1;
LOG_FORMAT_MEMORY = 2;
LOG_FORMAT_OBJECT = 3;
LOG_FORMAT_CONNECTED = -1;
LOG_FORMAT_MEMORY_USAGE = -2;
LOG_FORMAT_LIVE_WATCHES = -3;
implementation
uses
System.Classes,
{$IF Defined(MSWINDOWS)}
Winapi.Windows,
{$ELSEIF Defined(ANDROID)}
Androidapi.Helpers,
{$ELSEIF Defined(IOS)}
iOSapi.Foundation,
Macapi.Helpers,
Macapi.ObjectiveC,
{$ENDIF}
{$IF Defined(POSIX)}
Posix.Unistd,
{$ENDIF}
Grijjy.SysUtils,
Grijjy.CloudLogging;
{ TgoCloudLogger }
constructor TgoCloudLogger.Create;
{$IF Defined(IOS)}
var
AppNameKey: Pointer;
AppBundle: NSBundle;
NSAppName: NSString;
{$ENDIF}
begin
inherited Create;
{$IF Defined(IOS)}
AppNameKey := (StrToNSStr('CFBundleName') as ILocalObject).GetObjectID;
AppBundle := TNSBundle.Wrap(TNSBundle.OCClass.mainBundle);
NSAppName := TNSString.Wrap(AppBundle.infoDictionary.objectForKey(AppNameKey));
FAppName := UTF8ToString(NSAppName.UTF8String);
{$ELSEIF Defined(Android)}
FAppName := TAndroidHelper.ApplicationTitle;
{$ELSE}
FAppName := ChangeFileExt(ExtractFileName(GetModuleName(0)), '');
{$ENDIF}
{$IF Defined(MSWINDOWS)}
FProcessId := GetCurrentProcessId;
{$ELSEIF Defined(POSIX)}
FProcessId := getpid;
{$ENDIF}
end;
destructor TgoCloudLogger.Destroy;
begin
inherited;
end;
{ Sends a message to the specified service, with optional userdefined and data }
procedure TgoCloudLogger.Send(const AService: String; const AMsg: String;
const ALevel, ADataFormat: Integer; const AData: TBytes);
var
Msg: PZMessage;
Protocol: TgoLogMessageProtocol;
begin
Msg := TZMessage.Create;
try
Protocol.MessageText := AMsg;
Protocol.Level := ALevel;
Protocol.ProcessId := FProcessId;
Protocol.ThreadId := TThread.CurrentThread.ThreadID;
Protocol.AppName := FAppName;
Protocol.TimeStamp := Now;
Protocol.DataFormat := ADataFormat;
Protocol.Data := AData;
Msg.PushProtocolBuffer<TgoLogMessageProtocol>(Protocol);
if (AService = '') then
inherited Send(GrijjyLog.Service, Msg)
else
inherited Send(AService, Msg);
finally
Msg.Free;
end;
end;
procedure TgoCloudLogger.SetBroker(const Value: String);
begin
if (Value <> FBroker) then
begin
FBroker := Value;
Connect(FBroker);
end;
end;
{ Implements the DoRecv from the client protocol class }
procedure TgoCloudLogger.DoRecv(const ACommand: TZMQCommand;
var AMsg: PZMessage; var ASentFrom: PZFrame);
var
Service: String;
Protocol: TgoLogCommandProtocol;
ReturnData: TBytes;
begin
Service := AMsg.PopString;
AMsg.PopProtocolBuffer(Protocol);
ReturnData := nil;
case Protocol.Command of
LOG_FORMAT_MEMORY_USAGE:
ReturnData := GetMemoryUsage(Protocol.Args);
LOG_FORMAT_LIVE_WATCHES:
ReturnData := GetLiveWatches;
else
Exit;
end;
Send(Service, '', Ord(TgoLogLevel.Error), Protocol.Command, ReturnData);
end;
class function TgoCloudLogger.GetLiveWatches: TBytes;
var
Msg: TgoLiveWatchesMessage;
Protocol: TgoLogLiveWatchesProtocol;
begin
Msg := TgoLiveWatchesMessage.Create;
try
{$IFDEF CONSOLE}
TMessageManager.DefaultManager.SendMessage(nil, Msg, False);
{$ELSE}
{ Listeners for this message may need to access the GUI, so always send
this message in the UI thread. }
TThread.Synchronize(nil,
procedure
begin
TMessageManager.DefaultManager.SendMessage(nil, Msg, False);
end);
{$ENDIF}
Protocol.Watches := Msg.GetWatches;
Result := TgoProtocolBuffer.Serialize(Protocol);
finally
Msg.Free;
end;
end;
class function TgoCloudLogger.GetMemoryUsage(const AArgs: TBytes): TBytes;
var
Msg: TgoGetInstancesMessage;
Handles: TgoHandleArray;
begin
Handles.Handles := nil;
if Assigned(AArgs) then
TgoProtocolBuffer.Deserialize(Handles, AArgs);
Msg := TgoGetInstancesMessage.Create(TArray<TClass>(Handles.Handles));
try
TMessageManager.DefaultManager.SendMessage(nil, Msg, False);
Msg.Protocol.AllocatedBytes := goGetAllocatedMemory;
Result := TgoProtocolBuffer.Serialize(Msg.Protocol);
finally
Msg.Free;
end;
end;
{ TgoGetInstancesMessage }
constructor TgoGetInstancesMessage.Create(const AClasses: TArray<TClass>);
begin
inherited Create;
FClasses := AClasses;
end;
end.