-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzmqlogger.pas
1145 lines (971 loc) · 42.8 KB
/
zmqlogger.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit ZMQLogger;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
laz2_DOM, laz2_XMLRead,
LCLIntf,
{$ifndef cpuarm}
sqlite3conn, SQLite3db,
{$endif}
IBConnection, mssqlconn,
odbcconn, pqconnection, oracleconnection
{$ifndef cpuarm}
,mysql50conn, mysql51conn, mysql55conn, mysql55, mysql51, mysql50
,mysql40conn, mysql41conn, mysql41, mysql40,
{$endif cpuarm}
FavSystem, FavUtils, FavProtoLib, FavDBConnection, FavDBUtils, FavSemaphore,
zmq, ZMQClass
;
type
T0MQLogRecord = record
debug_level: string; // DEBUG, INFO, NOTE, ERROR, ALERT
severity: int64; // 0..9
message_type: string; // user message type - duration, login,...
code: int64; // integer;
msg_in: string; // input into function -- formal message
msg_out: string; // output from function -- formal description
debug_info: string; // extended debug info data -- formal data
duration: int64; // message duration - cumulative (immediatelly before existing function - copy to http response stream)
duration_prepare: int64; // message preparation duration (http prepare) -- new
msg_size: int64; // message size -- new
client_timestamp: TDateTime; // client timestamp
server_timestamp: TDateTime; // server timestamp
address: string; // ip address of machine or machine name
application: string; // application name
application_path: string; // full application path
application_version: string; // application version
os: string; // operating system
os_version: string; // operating system version
thread: integer; // thread process id
main_thread: boolean; // is it main thread of applicetion
msg_id_client: int64; // client message id
end;
TProcCustomSaveLog = procedure (var InMsg: T0MQLogRecord) of object;
{ TLogThreadSaver }
// This class is used to save log message to database
TLogThreadSaver = class(TThread)
private
{ Private declarations }
protected
{ Protected declarations }
procedure Execute; override;
public
_BFinished: Boolean;
_PQueue: TFavQueue;
_ProtoLog, _ProtoErr: TFavProto;
_ThreadNo: Integer;
_Connection: TFavConnection;
_ConnectionParams: TFavRecConnection;
_Query: TFavQuery;
_ConnectionLogName: TFavString;
_CustomSaveLog: TProcCustomSaveLog;
constructor Create(InThreadNo: Integer; InConnectionParams: TFavRecConnection; InQueue: TFavQueue);
destructor Destroy; override;
procedure SaveLog(InString: String);
procedure SendShutDownMessage;
procedure SetCustomeSaveLog(InCustomSaveLog: TProcCustomSaveLog);
end;
{ TLogThreadForwarder }
// This class is used to send log message to log server via 0MQ
// Client thread put log message to queue and forwarder thread send it to
// log server
TLogThreadForwarder = class(TThread)
private
{ Private declarations }
protected
{ Protected declarations }
procedure Execute; override;
public
_BFinished: Boolean; // finished boolean variable - if true thread is free to destroy
_PLogQueue: TFavQueue; // this queue holds messages which have to be sent to log server
_LogDirectory: TFavString; // proto files location
_ProtoLog, _ProtoErr: TFavProto; // proto files
_LogNamePrefix: TFavString;
_ThreadNo: Integer; // thread number - optional - usually there is only one log forwarder per application
_PContext: pointer; // pointer to 0mq context - passed in constructor - onyl one 0mq context per application
_LogServer: T0MQSocket; // log server socket
_Settings: T0MQSetup; // settings for log server queue
_SendError: TFavInteger; // send error counter
_SendError0mq: TFavInteger; // send error counter for 0mq errors
_SendCount: TFavInteger; // send count
_LastErrorMessage: TFavString; // last error message
_LastErrorNo: TFavInteger; // last error number
_LastErrorTimestamp: TFavDateTime; // last error timestamp
constructor Create(InLogNamePrefix: TFavString; // this is prefix of log file
InContext: pointer; // context is created when application starts
InThreadNo: Integer; // optional thread number
InLogDirectory: TFavString; // proto files log directory
InQueue: TFavQueue; // queue
InSettings: T0MQSetup // log server settings
);
destructor Destroy; override;
procedure ConnectToLogServer; // connecto to log server
procedure SendLogMessage(InString: String); // send one log message string
procedure SendShutDownMessage; // send shutdown message for queue
end;
const LOG_LEVEL_DEBUG = 'DEBUG';
const LOG_LEVEL_INFO = 'INFO';
const LOG_LEVEL_ALERT = 'ALERT';
const LOG_LEVEL_EMERG = 'EMERG';
const LOG_LEVEL_NONE = 'NONE';
var InitT0MQLogRecord: T0MQLogRecord = ({%H-});
//var FAV_SYS_SEND_TO_LOG: Boolean = True;
var FAV_SYS_LOG_LEVEL: String = LOG_LEVEL_EMERG;
function LogEncode(var InLog: T0MQLogRecord): String;
procedure LogDecode(InString: String; var OutLog: T0MQLogRecord);
procedure LogToFile(InFileName: TFavString; var InLog: T0MQLogRecord);
procedure LogToProto(InProto: TFavProto; var InLog: T0MQLogRecord);
procedure LogToDB(InConnection: TFavConnection; InQuery: TFavQuery; var InLog: T0MQLogRecord);
procedure LogPrepareQuery(InConnection: TFavConnection; OutQuery: TFavQuery);
procedure LogCreateTable(InConnection: TFavConnection);
function LogCreateDBConnection(InConnectionParams: TFavRecConnection): TFavConnection;
procedure LogCreateMessage(var OutLog: T0MQLogRecord;
InDebugLevel: TFavString;
InSeverity: TFavInteger;
InMessageType: TFavString;
InAddress: TFavString;
InApplicationVersion: TFavString;
InErrorCode: TFavInteger;
InMsgIn: TFavString;
InMsgOut: TFavString;
InDebugInfo: TFavString;
InDuration: TFavInteger;
InDurationPrepare: TFavInteger;
InMsgSize: TFavInteger
);
function LogCreateMessageString(var OutLog: T0MQLogRecord;
InDebugLevel: TFavString;
InSeverity: TFavInteger;
InMessageType: TFavString;
InAddress: TFavString;
InApplicationVersion: TFavString;
InErrorCode: TFavInteger;
InMsgIn: TFavString;
InMsgOut: TFavString;
InDebugInfo: TFavString;
InDuration: TFavInteger;
InDurationPrepare: TFavInteger;
InMsgSize: TFavInteger
): TFavString;
procedure LogToQueue(InProto: TFavProto; InQueue: TFavQueue; InLogMessage: TFavString);
implementation
{ TLogThreadForwarder }
procedure TLogThreadForwarder.Execute;
var MyString: String;
begin
FavLProtoLn(_ProtoLog, dlEmerg, 'Starting thread');
_BFinished := False;
while true do begin
MyString := _PLogQueue.GetData;
if MyString = 'BSHUTDOWN=TRUE' then begin
FavLProtoLn(_ProtoLog, dlEmerg, 'Received shutdown message');
Break;
end
else begin
// Insert data into database
SendLogMessage(MyString);
end;
end;
FavLProtoLn(_ProtoLog, dlEmerg, 'Destroying thread');
_BFinished := True;
end;
constructor TLogThreadForwarder.Create(InLogNamePrefix: TFavString;
InContext: pointer; InThreadNo: Integer; InLogDirectory: TFavString;
InQueue: TFavQueue; InSettings: T0MQSetup);
begin
_BFinished := False;
_PLogQueue := InQueue;
_ThreadNo := InThreadNo;
_Settings := InSettings;
_LogNamePrefix := InLogNamePrefix;
_LogDirectory := InLogDirectory;
// ensure that last character is path separator
_LogDirectory := FavCorrectFileName(_LogDirectory);
if Length(_LogDirectory) > 0 then begin
if _LogDirectory[Length(_LogDirectory)] <> FAV_SYS_PATH_DELIM then begin
_LogDirectory := _LogDirectory + FAV_SYS_PATH_DELIM;
end;
end;
_PContext := InContext;
// create log files
_ProtoLog := TFavProto.Create(_LogDirectory + _LogNamePrefix + '.log', 1024 * 1024 * 10);
_ProtoErr := TFavProto.Create(_LogDirectory + _LogNamePrefix + '.err', 1024 * 1024 * 10);
ConnectToLogServer;
inherited Create(True);
end;
destructor TLogThreadForwarder.Destroy;
begin
// _PLogQueue.CleanQueue;
// FavFreeAndNil(_PLogQueue);
if _LogServer <> nil then begin
FavLProtoLn(_ProtoLog, dlEmerg, 'Closing socket');
_LogServer.CloseSocket;
end;
FavLProtoLn(_ProtoLog, dlEmerg, 'Free LogServer socket');
FreeAndNil(_LogServer);
FavLProtoLn(_ProtoLog, dlEmerg, 'Destroying proto objects');
if _ProtoLog <> nil then FavFreeAndNil(_ProtoLog);
if _ProtoErr <> nil then FavFreeAndNil(_ProtoErr);
inherited Destroy;
end;
procedure TLogThreadForwarder.ConnectToLogServer;
begin
try
FavLProtoLn(_ProtoLog, dlEmerg, 'Creating 0mq Socket: ' + _Settings.address);
_LogServer := T0MQSocket.Create(_PContext);
FavLProtoLn(_ProtoLog, dlEmerg, 'Get Socket: ' + _Settings.address);
if _Settings.socket_type = 'PUB' then begin
_LogServer.GetSocket(ZMQ_PUB);
end
else begin
_LogServer.GetSocket(ZMQ_PUSH);
end;
FavLProtoLn(_ProtoLog, dlEmerg, 'SetHWM: ' + FavToString(_Settings.hwm));
_LogServer.SetSockOptInteger(ZMQ_SNDHWM, _Settings.hwm);
FavLProtoLn(_ProtoLog, dlEmerg, 'SetTimeout: ' + FavToString(_Settings.send_timeout));
_LogServer.SetSockOptInteger(ZMQ_SNDTIMEO, _Settings.send_timeout);
FavLProtoLn(_ProtoLog, dlEmerg, 'Connect socket: ' + _Settings.address);
_LogServer.ConnectSocket(_Settings.address);
except
on E: Exception do begin
FavLProtoLn(_ProtoLog, dlEmerg, 'ConnectToLogServer Error: ' + E.Message);
end;
end;
end;
procedure TLogThreadForwarder.SendLogMessage(InString: String);
//var MySendRV: Integer;
begin
try
if _Settings.send_timeout <= 0 then begin
//MySendRV :=
_LogServer.Send(PChar(InString), Length(InString), 0);
end
else begin
{$WARNINGS OFF}
// MySendRV :=
_LogServer.Send(PChar(InString), Length(InString), ZMQ_NOBLOCK);
{$WARNINGS ON}
end;
_SendCount := _SendCount + 1;
except
on E: E0MQError do begin
_SendError0mq := _SendError0mq + 1;
_LastErrorMessage := E._ErrMsg;
_LastErrorNo := E._ErrNo;
_LastErrorTimestamp := FavUTCNow;
FavLProtoLn(_ProtoLog, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoLog, dlEmerg, 'SendLogMessage0mqError: ' + _LastErrorMessage);
FavLProtoLn(_ProtoLog, dlEmerg, 'SendLogMessage0mqErrorCode: ' + FavToString(_LastErrorNo));
FavLProtoLn(_ProtoLog, dlEmerg, 'String: ' + InString);
FavLProtoLn(_ProtoLog, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoErr, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoErr, dlEmerg, 'SendLogMessage0mqError: ' + _LastErrorMessage);
FavLProtoLn(_ProtoErr, dlEmerg, 'SendLogMessage0mqErrorCode: ' + FavToString(_LastErrorNo));
FavLProtoLn(_ProtoErr, dlEmerg, 'String: ' + InString);
FavLProtoLn(_ProtoErr, dlEmerg, FAV_SYS_ASTERIX_LINE);
end;
on E: Exception do begin
_SendError := _SendError + 1;
_LastErrorMessage := E.Message;
_LastErrorNo := -1;
_LastErrorTimestamp := FavUTCNow;
FavLProtoLn(_ProtoLog, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoLog, dlEmerg, 'SendLogMessageError: ' + _LastErrorMessage);
FavLProtoLn(_ProtoLog, dlEmerg, 'String: ' + InString);
FavLProtoLn(_ProtoLog, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoErr, dlEmerg, FAV_SYS_ASTERIX_LINE);
FavLProtoLn(_ProtoErr, dlEmerg, 'SendLogMessageError: ' + _LastErrorMessage);
FavLProtoLn(_ProtoErr, dlEmerg, 'String: ' + InString);
FavLProtoLn(_ProtoErr, dlEmerg, FAV_SYS_ASTERIX_LINE);
end;
end;
end;
procedure TLogThreadForwarder.SendShutDownMessage;
var MyString: String;
begin
MyString := 'BSHUTDOWN=TRUE';
_PLogQueue.PutData(MyString);
// wait for thread to finish
while _BFinished = False do begin
Sleep(1)
end;
end;
{ TLogThreadSaver }
procedure TLogThreadSaver.Execute;
var MyString: String;
begin
while true do begin
MyString := _PQueue.GetData;
if MyString = 'BSHUTDOWN=TRUE' then begin
Break;
end
else begin
// Insert data into database
SaveLog(MyString);
end;
end;
_BFinished := True;
end;
constructor TLogThreadSaver.Create(InThreadNo: Integer;
InConnectionParams: TFavRecConnection; InQueue: TFavQueue);
begin
_CustomSaveLog := nil;
_BFinished := False;
_PQueue := InQueue;
_ThreadNo := InThreadNo;
_ConnectionParams := InConnectionParams;
_Connection := LogCreateDBConnection(_ConnectionParams);
_ConnectionLogName := 'Connection_' + FavToString(_ThreadNo) + '.log';
_Query := TFavQuery.Create(nil);
LogPrepareQuery(_Connection, _Query);
inherited Create(True);
end;
destructor TLogThreadSaver.Destroy;
begin
// _PQueue.CleanQueue;
// FavFreeAndNil(_PQueue);
if _Query <> nil then FavFreeAndNil(_Query);
if _Connection <> nil then begin
FavDBDestroyConnection(_Connection);
end;
inherited Destroy;
end;
procedure TLogThreadSaver.SaveLog(InString: String);
var MyFunctionName: TFavString;
MyStartTime, MyFinishTime: TFavDateTime;
MyQuery: TFavQuery;
MyError: TFavString;
MyIStartedTransaction: TFavBoolean;
MyLog: T0MQLogRecord;
begin
MyQuery := nil;
MyFunctionName := 'LogToDB';
FavProtoStart(_ProtoLog, _ProtoErr, '', MyFunctionName);
MyStartTime := FavUTCNow;
MyIStartedTransaction := False;
try
MyQuery := _Query;
MyLog := InitT0MQLogRecord;
LogDecode(InString, MyLog);
// if FavDBCheckConnection(_Connection, _ConnectionLogName) then begin
if FavDBCheckConnection(_Connection, '') then begin
MyIStartedTransaction := FavDbUtils.FavDBStartTransaction(_ProtoLog, _ProtoErr, _Connection);
if _CustomSaveLog <> nil then begin
_CustomSaveLog(MyLog);
end;
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'msg_in', MyLog.msg_in);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'msg_out', MyLog.msg_out);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'debug_info', MyLog.debug_info);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'debug_level', MyLog.debug_level, 16);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'severity', MyLog.severity);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'message_type', MyLog.message_type, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'code', MyLog.code);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'duration', MyLog.duration);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'duration_prepare', MyLog.duration_prepare);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'msg_size', MyLog.msg_size);
FavDBSetParamDateTime(_ProtoLog, _ProtoErr, MyQuery, 'client_timestamp', MyLog.client_timestamp);
FavDBSetParamDateTime(_ProtoLog, _ProtoErr, MyQuery, 'server_timestamp', FavUTCNow);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'address', MyLog.address, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'application', MyLog.application, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'application_path', MyLog.application_path, 128);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'application_version', MyLog.application_version, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'os', MyLog.os, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'os_version', MyLog.os_version, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'thread', MyLog.thread);
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'main_thread', FavToString(MyLog.main_thread));
FavDBSetParam(_ProtoLog, _ProtoErr, MyQuery, 'msg_id_client', FavToString(MyLog.msg_id_client));
FavDBExecQuery(_ProtoLog, _ProtoErr, MyQuery);
FavDBCloseQuery(_ProtoLog, _ProtoErr, MyQuery);
if MyIStartedTransaction then begin
FavDbUtils.FavDBCommitTransaction(_ProtoLog, _ProtoErr, _Connection);
MyIStartedTransaction := False;
end;
end;
except
on E: Exception do begin
MyFinishTime := FavUTCNow;
MyError := E.Message;
FavDBCloseQuery(_ProtoLog, _ProtoErr, MyQuery);
FavProtoDuration(_ProtoLog, MyFunctionName, MyStartTime, MyFinishTime);
FavProtoError(_ProtoLog , _ProtoErr, '', MyFunctionName, E, MyError);
if MyIStartedTransaction then begin
FavDbUtils.FavDBRollbackTransaction(_ProtoLog, _ProtoErr, _Connection);
MyIStartedTransaction := False;
end;
raise;
end;
end;
MyFinishTime := FavUTCNow;
FavProtoDuration(_ProtoLog, MyFunctionName, MyStartTime, MyFinishTime);
FavProtoEnd(_ProtoLog, _ProtoErr, '', MyFunctionName);
end;
procedure TLogThreadSaver.SendShutDownMessage;
var MyString: String;
begin
MyString := 'BSHUTDOWN=TRUE';
_PQueue.PutData(MyString);
// wait for thread to finish
while _BFinished = False do begin
Sleep(1)
end;
end;
procedure TLogThreadSaver.SetCustomeSaveLog(InCustomSaveLog: TProcCustomSaveLog
);
begin
_CustomSaveLog := InCustomSaveLog;
end;
function LogEncode(var InLog: T0MQLogRecord): String;
var MyResult: String;
begin
MyResult := Format('<%s ' +
'debug_level="%s" ' +
'severity="%s" ' +
'message_type="%s" ' +
'code="%s" ' +
'msg_in="%s" ' +
'msg_out="%s" ' +
'debug_info="%s" ' +
'duration="%s" ' +
'duration_prepare="%s" ' +
'msg_size="%s" ' +
'client_timestamp="%s" ' +
'server_timestamp="%s" ' +
'address="%s" ' +
'application="%s" ' +
'application_path="%s" ' +
'application_version="%s" ' +
'os="%s" ' +
'os_version="%s" ' +
'thread="%s" ' +
'main_thread="%s" ' +
'msg_id_client="%s" ' +
'/>',
[
'item',
FavForXML(InLog.debug_level),
FavForXML(InLog.severity),
FavForXML(InLog.message_type),
FavForXML(InLog.code),
FavForXML(InLog.msg_in),
FavForXML(InLog.msg_out),
FavForXML(InLog.debug_info),
FavForXML(InLog.duration),
FavForXML(InLog.duration_prepare),
FavForXML(InLog.msg_size),
FavForXMLDateTime(InLog.client_timestamp),
FavForXMLDateTime(InLog.server_timestamp),
FavForXML(InLog.address),
FavForXML(InLog.application),
FavForXML(InLog.application_path),
FavForXML(InLog.application_version),
FavForXML(InLog.os),
FavForXML(InLog.os_version),
FavForXML(InLog.thread),
FavForXML(InLog.main_thread),
FavForXML(InLog.msg_id_client)
]);
Result := FAV_SYS_XML_HEADER + FAV_SYS_NEWLINE +
FAV_SYS_XML_DATA_OPEN + FAV_SYS_NEWLINE +
MyResult + FAV_SYS_NEWLINE +
FAV_SYS_XML_DATA_CLOSE;
end;
procedure LogDecode(InString: String; var OutLog: T0MQLogRecord);
var MyDoc: TXMLDocument;
MySrc: TXMLInputSource;
MyParser: TDOMParser;
MyChildData: TDOMNode;
MyChildItem: TDOMNode;
MyNodeName: TFavString;
MyNodeValue: TFavString;
F, G, H: Integer;
begin
// I receive xml in format
(*
<xml>
<data>
<item address="192.168.0.1" ......./>
</data>
</xml>
*)
OutLog := InitT0MQLogRecord;
MyDoc := nil;
MySrc := nil;
MyParser := nil;
try
if InString <> '' then begin
try
MyParser := TDOMParser.Create;
MyParser.Options.Validate := True;
MySrc := TXMLInputSource.Create(InString);
// MyParser.Options.PreserveWhitespace := True;
try
MyParser.Parse(MySrc, MyDoc);
except
on E: Exception do begin
if (InString <> '') and (InString[1] <> '<') then begin
raise Exception.Create(Copy(InString, 1, 255));
end;
end;
end;
if (MyDoc.DocumentElement.NodeType = ELEMENT_NODE) and (MyDoc.DocumentElement.NodeName = 'xml') then begin
for F := 0 to MyDoc.DocumentElement.ChildNodes.Count-1 do begin
MyChildData := MyDoc.DocumentElement.ChildNodes[F];
if (MyChildData.NodeType = ELEMENT_NODE) and (MyChildData.NodeName = 'data') then begin
for G := 0 to MyChildData.ChildNodes.Count-1 do begin
MyChildItem := MyChildData.ChildNodes[G];
if (MyChildItem.NodeType = ELEMENT_NODE) and (MyChildItem.NodeName = 'item') then begin
for H := 0 to MyChildItem.Attributes.Length-1 do begin
MyNodeName := FavCorrectXMLValue(MyChildItem.Attributes[H].NodeName);
MyNodeValue := FavCorrectXMLValue(MyChildItem.Attributes[H].NodeValue);
if MyNodeName = 'address' then begin
OutLog.address := MyNodeValue;
end
else if MyNodeName = 'debug_level' then OutLog.debug_level := MyNodeValue
else if MyNodeName = 'severity' then OutLog.severity := FavToInteger(MyNodeValue)
else if MyNodeName = 'message_type' then OutLog.message_type := (MyNodeValue)
else if MyNodeName = 'code' then OutLog.code := FavToInteger(MyNodeValue)
else if MyNodeName = 'msg_in' then OutLog.msg_in := MyNodeValue
else if MyNodeName = 'msg_out' then OutLog.msg_out := MyNodeValue
else if MyNodeName = 'debug_info' then OutLog.debug_info := MyNodeValue
else if MyNodeName = 'client_timestamp' then OutLog.client_timestamp := FavToDateTime(MyNodeValue)
else if MyNodeName = 'server_timestamp' then OutLog.server_timestamp := FavToDateTime(MyNodeValue)
else if MyNodeName = 'duration' then OutLog.duration := FavToInteger(MyNodeValue)
else if MyNodeName = 'duration_prepare' then OutLog.duration_prepare := FavToInteger(MyNodeValue)
else if MyNodeName = 'msg_size' then OutLog.msg_size := FavToInteger(MyNodeValue)
else if MyNodeName = 'application' then OutLog.application := MyNodeValue
else if MyNodeName = 'application_path' then OutLog.application_path := MyNodeValue
else if MyNodeName = 'application_version' then OutLog.application_version := MyNodeValue
else if MyNodeName = 'os' then OutLog.os := MyNodeValue
else if MyNodeName = 'os_version' then OutLog.os_version := MyNodeValue
else if MyNodeName = 'thread' then OutLog.thread := FavToInteger(MyNodeValue)
else if MyNodeName = 'main_thread' then OutLog.main_thread := FavToBoolean(MyNodeValue)
else if MyNodeName = 'msg_id_client' then OutLog.msg_id_client := FavToInteger(MyNodeValue)
end;
end;
end;
end;
end;
end;
if MySrc <> nil then FreeAndNil(MySrc);
if MyParser <> nil then FreeAndNil(MyParser);
if MyDoc <> nil then MyDoc.Free;
except
on E: Exception do begin
if MySrc <> nil then FavFreeAndNil(MySrc);
if MyParser <> nil then FavFreeAndNil(MyParser);
if MyDoc <> nil then FavFreeAndNil(MyDoc);
end;
end;
end;
except
end;
end;
procedure LogToFile(InFileName: TFavString; var InLog: T0MQLogRecord);
begin
FavAppendToFile(InFileName, FAV_SYS_ASTERIX_LINE + FAV_SYS_NEWLINE + LogEncode(InLog) + FAV_SYS_NEWLINE);
end;
procedure LogToProto(InProto: TFavProto; var InLog: T0MQLogRecord);
begin
FavFullProtoLn(InProto, dlDebug, FAV_SYS_NEWLINE + LogEncode(InLog));
end;
procedure LogToDB(InConnection: TFavConnection; InQuery: TFavQuery;
var InLog: T0MQLogRecord);
var MyFunctionName: TFavString;
MyStartTime, MyFinishTime: TFavDateTime;
MyError: TFavString;
MyIStartedTransaction: TFavBoolean;
_ProtoLog, _ProtoErr: TFavProto;
begin
_ProtoLog := nil;
_ProtoErr := nil;
MyFunctionName := 'LogToDB';
FavProtoStart(_ProtoLog, _ProtoErr, '', MyFunctionName);
MyStartTime := FavUTCNow;
MyIStartedTransaction := False;
try
MyIStartedTransaction := FavDbUtils.FavDBStartTransaction(_ProtoLog, _ProtoErr, InConnection);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'debug_level', InLog.debug_level, 16);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'severity', InLog.severity);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'message_type', InLog.message_type, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'code', InLog.code);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'msg_in', InLog.msg_in);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'msg_out', InLog.msg_out);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'debug_info', InLog.debug_info);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'duration', InLog.duration);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'duration_prepare', InLog.duration_prepare);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'msg_size', InLog.msg_size);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'address', InLog.address, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'application', InLog.application, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'application_path', InLog.application_path, 128);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'application_version', InLog.application_version, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'os', InLog.os, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'os_version', InLog.os_version, 32);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'thread', InLog.thread);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'main_thread', FavToString(InLog.main_thread), 1);
FavDBSetParam(_ProtoLog, _ProtoErr, InQuery, 'msg_id_client', InLog.msg_id_client);
FavDBSetParamDateTime(_ProtoLog, _ProtoErr, InQuery, 'client_timestamp', InLog.client_timestamp);
FavDBSetParamDateTime(_ProtoLog, _ProtoErr, InQuery, 'server_timestamp', FavUTCNow);
FavDBExecQuery(_ProtoLog, _ProtoErr, InQuery);
FavDBCloseQuery(_ProtoLog, _ProtoErr, InQuery);
if MyIStartedTransaction then begin
FavDbUtils.FavDBCommitTransaction(_ProtoLog, _ProtoErr, InConnection);
MyIStartedTransaction := False;
end;
except
on E: Exception do begin
MyFinishTime := FavUTCNow;
MyError := E.Message;
FavDBCloseQuery(_ProtoLog, _ProtoErr, InQuery);
FavProtoDuration(_ProtoLog, MyFunctionName, MyStartTime, MyFinishTime);
FavProtoError(_ProtoLog , _ProtoErr, '', MyFunctionName, E, MyError);
if MyIStartedTransaction then begin
FavDbUtils.FavDBRollbackTransaction(_ProtoLog, _ProtoErr, InConnection);
MyIStartedTransaction := False;
end;
raise;
end;
end;
MyFinishTime := FavUTCNow;
FavProtoDuration(_ProtoLog, MyFunctionName, MyStartTime, MyFinishTime);
FavProtoEnd(_ProtoLog, _ProtoErr, '', MyFunctionName);
end;
procedure LogPrepareQuery(InConnection: TFavConnection; OutQuery: TFavQuery);
begin
OutQuery.SQLConnection := InConnection;
OutQuery.Name := 'LogDB';
if InConnection is TPQConnection then begin
OutQuery.SQL.Text :=
'insert into log_log (' + FAV_SYS_NEWLINE +
' id, ' + FAV_SYS_NEWLINE +
' debug_level, ' + FAV_SYS_NEWLINE +
' severity, ' + FAV_SYS_NEWLINE +
' message_type, ' + FAV_SYS_NEWLINE +
' code, ' + FAV_SYS_NEWLINE +
' msg_in, ' + FAV_SYS_NEWLINE +
' msg_out, ' + FAV_SYS_NEWLINE +
' debug_info, ' + FAV_SYS_NEWLINE +
' client_timestamp, ' + FAV_SYS_NEWLINE +
' server_timestamp, ' + FAV_SYS_NEWLINE +
' duration, ' + FAV_SYS_NEWLINE +
' duration_prepare, ' + FAV_SYS_NEWLINE +
' msg_size, ' + FAV_SYS_NEWLINE +
' address, ' + FAV_SYS_NEWLINE +
' application, ' + FAV_SYS_NEWLINE +
' application_path, ' + FAV_SYS_NEWLINE +
' application_version, ' + FAV_SYS_NEWLINE +
' os, ' + FAV_SYS_NEWLINE +
' os_version, ' + FAV_SYS_NEWLINE +
' thread, ' + FAV_SYS_NEWLINE +
' main_thread, ' + FAV_SYS_NEWLINE +
' msg_id_client) ' + FAV_SYS_NEWLINE +
'values (' + FAV_SYS_NEWLINE +
' (select nextval(''"SEQ_LOG_LOG"'')), ' + FAV_SYS_NEWLINE +
' :debug_level, ' + FAV_SYS_NEWLINE +
' :severity, ' + FAV_SYS_NEWLINE +
' :message_type, ' + FAV_SYS_NEWLINE +
' :code, ' + FAV_SYS_NEWLINE +
' :msg_in, ' + FAV_SYS_NEWLINE +
' :msg_out, ' + FAV_SYS_NEWLINE +
' :debug_info, ' + FAV_SYS_NEWLINE +
' :client_timestamp, ' + FAV_SYS_NEWLINE +
' :server_timestamp, ' + FAV_SYS_NEWLINE +
' :duration, ' + FAV_SYS_NEWLINE +
' :duration_prepare, ' + FAV_SYS_NEWLINE +
' :msg_size, ' + FAV_SYS_NEWLINE +
' :address, ' + FAV_SYS_NEWLINE +
' :application, ' + FAV_SYS_NEWLINE +
' :application_path, ' + FAV_SYS_NEWLINE +
' :application_version, ' + FAV_SYS_NEWLINE +
' :os, ' + FAV_SYS_NEWLINE +
' :os_version, ' + FAV_SYS_NEWLINE +
' :thread, ' + FAV_SYS_NEWLINE +
' :main_thread, ' + FAV_SYS_NEWLINE +
' :msg_id_client) ';
end
else if (InConnection is TMySQL40Connection) or
(InConnection is TMySQL41Connection) or
(InConnection is TMySQL50Connection) or
(InConnection is TMySQL50Connection) or
(InConnection is TMySQL55Connection)
then begin
OutQuery.SQL.Text :=
'insert into log_log (' + FAV_SYS_NEWLINE +
' debug_level, ' + FAV_SYS_NEWLINE +
' severity, ' + FAV_SYS_NEWLINE +
' message_type, ' + FAV_SYS_NEWLINE +
' code, ' + FAV_SYS_NEWLINE +
' msg_in, ' + FAV_SYS_NEWLINE +
' msg_out, ' + FAV_SYS_NEWLINE +
' debug_info, ' + FAV_SYS_NEWLINE +
' client_timestamp, ' + FAV_SYS_NEWLINE +
' server_timestamp, ' + FAV_SYS_NEWLINE +
' duration, ' + FAV_SYS_NEWLINE +
' duration_prepare, ' + FAV_SYS_NEWLINE +
' msg_size, ' + FAV_SYS_NEWLINE +
' address, ' + FAV_SYS_NEWLINE +
' application, ' + FAV_SYS_NEWLINE +
' application_path, ' + FAV_SYS_NEWLINE +
' application_version, ' + FAV_SYS_NEWLINE +
' os, ' + FAV_SYS_NEWLINE +
' os_version, ' + FAV_SYS_NEWLINE +
' thread, ' + FAV_SYS_NEWLINE +
' main_thread, ' + FAV_SYS_NEWLINE +
' msg_id_client) ' + FAV_SYS_NEWLINE +
'values (' + FAV_SYS_NEWLINE +
' :debug_level, ' + FAV_SYS_NEWLINE +
' :severity, ' + FAV_SYS_NEWLINE +
' :message_type, ' + FAV_SYS_NEWLINE +
' :code, ' + FAV_SYS_NEWLINE +
' :msg_in, ' + FAV_SYS_NEWLINE +
' :msg_out, ' + FAV_SYS_NEWLINE +
' :debug_info, ' + FAV_SYS_NEWLINE +
' :client_timestamp, ' + FAV_SYS_NEWLINE +
' :server_timestamp, ' + FAV_SYS_NEWLINE +
' :duration, ' + FAV_SYS_NEWLINE +
' :duration_prepare, ' + FAV_SYS_NEWLINE +
' :msg_size, ' + FAV_SYS_NEWLINE +
' :address, ' + FAV_SYS_NEWLINE +
' :application, ' + FAV_SYS_NEWLINE +
' :application_path, ' + FAV_SYS_NEWLINE +
' :application_version, ' + FAV_SYS_NEWLINE +
' :os, ' + FAV_SYS_NEWLINE +
' :os_version, ' + FAV_SYS_NEWLINE +
' :thread, ' + FAV_SYS_NEWLINE +
' :main_thread, ' + FAV_SYS_NEWLINE +
' :msg_id_client) ';
end
else if (InConnection is TSQLite3Connection) then begin
OutQuery.SQL.Text :=
'insert into log_log (' + FAV_SYS_NEWLINE +
' debug_level, ' + FAV_SYS_NEWLINE +
' severity, ' + FAV_SYS_NEWLINE +
' message_type, ' + FAV_SYS_NEWLINE +
' code, ' + FAV_SYS_NEWLINE +
' msg_in, ' + FAV_SYS_NEWLINE +
' msg_out, ' + FAV_SYS_NEWLINE +
' debug_info, ' + FAV_SYS_NEWLINE +
' client_timestamp, ' + FAV_SYS_NEWLINE +
' server_timestamp, ' + FAV_SYS_NEWLINE +
' duration, ' + FAV_SYS_NEWLINE +
' duration_prepare, ' + FAV_SYS_NEWLINE +
' msg_size, ' + FAV_SYS_NEWLINE +
' address, ' + FAV_SYS_NEWLINE +
' application, ' + FAV_SYS_NEWLINE +
' application_path, ' + FAV_SYS_NEWLINE +
' application_version, ' + FAV_SYS_NEWLINE +
' os, ' + FAV_SYS_NEWLINE +
' os_version, ' + FAV_SYS_NEWLINE +
' thread, ' + FAV_SYS_NEWLINE +
' main_thread, ' + FAV_SYS_NEWLINE +
' msg_id_client) ' + FAV_SYS_NEWLINE +
'values (' + FAV_SYS_NEWLINE +
' :debug_level, ' + FAV_SYS_NEWLINE +
' :severity, ' + FAV_SYS_NEWLINE +
' :message_type, ' + FAV_SYS_NEWLINE +
' :code, ' + FAV_SYS_NEWLINE +
' :msg_in, ' + FAV_SYS_NEWLINE +
' :msg_out, ' + FAV_SYS_NEWLINE +
' :debug_info, ' + FAV_SYS_NEWLINE +
' :client_timestamp, ' + FAV_SYS_NEWLINE +
' :server_timestamp, ' + FAV_SYS_NEWLINE +
' :duration, ' + FAV_SYS_NEWLINE +
' :duration_prepare, ' + FAV_SYS_NEWLINE +
' :msg_size, ' + FAV_SYS_NEWLINE +
' :address, ' + FAV_SYS_NEWLINE +
' :application, ' + FAV_SYS_NEWLINE +
' :application_path, ' + FAV_SYS_NEWLINE +
' :application_version, ' + FAV_SYS_NEWLINE +
' :os, ' + FAV_SYS_NEWLINE +
' :os_version, ' + FAV_SYS_NEWLINE +
' :thread, ' + FAV_SYS_NEWLINE +
' :main_thread, ' + FAV_SYS_NEWLINE +
' :msg_id_client) ';
end
else begin
raise Exception.Create('Not supported DB type. Only Postgres, MYSQL or SQLLite is supported');
end;
end;
procedure LogCreateTable(InConnection: TFavConnection);
var MyFunctionName: TFavString;
MyStartTime, MyFinishTime: TFavDateTime;
MyQuery: TFavQuery;
MyError: TFavString;
MyIStartedTransaction: TFavBoolean;
_ProtoLog, _ProtoErr: TFavProto;
begin
_ProtoLog := nil;
_ProtoErr := nil;
MyQuery := nil;
MyFunctionName := 'LogCreateTable';
FavProtoStart(_ProtoLog, _ProtoErr, '', MyFunctionName);
MyStartTime := FavUTCNow;
MyIStartedTransaction := False;
try
MyQuery := TFavQuery.Create(nil);
MyQuery.SQLConnection := InConnection;
MyQuery.Name := 'LogDB';
if (InConnection is TSQLite3Connection) then begin
MyQuery.SQL.Text :=
'CREATE TABLE log_log (' + FAV_SYS_NEWLINE +
' id INTEGER PRIMARY KEY AUTOINCREMENT,' + FAV_SYS_NEWLINE +
' msg_in TEXT, ' + FAV_SYS_NEWLINE +
' msg_out TEXT, ' + FAV_SYS_NEWLINE +
' debug_info TEXT, ' + FAV_SYS_NEWLINE +
' debug_level VARCHAR(16), ' + FAV_SYS_NEWLINE +
' severity BIGINT, ' + FAV_SYS_NEWLINE +
' message_type VARCHAR(32), ' + FAV_SYS_NEWLINE +
' code BIGINT, ' + FAV_SYS_NEWLINE +
' duration BIGINT, ' + FAV_SYS_NEWLINE +
' duration_prepare BIGINT, ' + FAV_SYS_NEWLINE +
' msg_size BIGINT, ' + FAV_SYS_NEWLINE +
' client_timestamp TIMESTAMP, ' + FAV_SYS_NEWLINE +
' server_timestamp TIMESTAMP, ' + FAV_SYS_NEWLINE +
' address VARCHAR(32), ' + FAV_SYS_NEWLINE +
' application VARCHAR(32), ' + FAV_SYS_NEWLINE +
' application_path VARCHAR(128), ' + FAV_SYS_NEWLINE +
' application_version VARCHAR(32), ' + FAV_SYS_NEWLINE +
' os VARCHAR(32), ' + FAV_SYS_NEWLINE +
' os_version VARCHAR(32), ' + FAV_SYS_NEWLINE +
' thread BIGINT, ' + FAV_SYS_NEWLINE +
' main_thread VARCHAR(1), ' + FAV_SYS_NEWLINE +
' msg_id_client BIGINT ' + FAV_SYS_NEWLINE +
');';
end
else begin
raise Exception.Create('Not supported DB type. Only Postgres, MYSQL or SQLLite is supported');
end;
MyIStartedTransaction := FavDbUtils.FavDBStartTransaction(_ProtoLog, _ProtoErr, InConnection);
FavDBExecQuery(_ProtoLog, _ProtoErr, MyQuery);
FavDBCloseQuery(_ProtoLog, _ProtoErr, MyQuery);
if MyIStartedTransaction then begin
FavDbUtils.FavDBCommitTransaction(_ProtoLog, _ProtoErr, InConnection);
MyIStartedTransaction := False;