client-log
This commit is contained in:
parent
0a79639aa8
commit
ea0ed85bbf
103
lms_cgi.lpr
103
lms_cgi.lpr
@ -4,7 +4,7 @@ program lms_cgi;
|
||||
|
||||
uses
|
||||
Interfaces, Classes, SysUtils, inifiles, httpDefs, fpweb, custweb, custcgi,
|
||||
lnetbase, tcpClient, tcpthreadhelper, extTypes;
|
||||
lnetbase, tcpClient, tcpthreadhelper, extTypes, eventlog;
|
||||
|
||||
Type
|
||||
|
||||
@ -33,13 +33,17 @@ Type
|
||||
flogFolder: string;
|
||||
fHost: string;
|
||||
fPort: integer;
|
||||
flogger: TEventLog;
|
||||
procedure LoadConfig;
|
||||
Protected
|
||||
function InitializeWebHandler: TWebHandler; override;
|
||||
public
|
||||
constructor CreateWithLogger(AOwner: TComponent);
|
||||
destructor Destroy; override;
|
||||
property Host: string read fHost;
|
||||
property Port: integer read fPort;
|
||||
property LogFolder: string read fLogFolder;
|
||||
property Logger: TEventLog read flogger;
|
||||
end;
|
||||
|
||||
const
|
||||
@ -106,7 +110,34 @@ log(mtDebug,self,'Command '+ARequest.Command);
|
||||
LogStrings(mtInfo, @log,self,'CookieFields',Arequest.CookieFields);
|
||||
LogStrings(mtInfo, @log,self,'CustomHeaders',Arequest.CustomHeaders);
|
||||
{$ENDIF}
|
||||
if ARequest.QueryFields.Values['action']='cgi-test' then
|
||||
begin
|
||||
AResponse.ContentType := 'text/html';
|
||||
AResponse.Contents.add('<h2>QueryFields</h2>');
|
||||
AResponse.Contents.add('<dl>');
|
||||
for i := 0 to ARequest.QueryFields.Count-1 do
|
||||
begin
|
||||
k := ARequest.QueryFields.Names[i];
|
||||
v := ARequest.QueryFields.Values[k];
|
||||
AResponse.Contents.add(format('<dt>%s</dt><dd>%s</dd>',[k,v]));
|
||||
|
||||
end;
|
||||
AResponse.Contents.add('</dl>');
|
||||
AResponse.Contents.add('');
|
||||
AResponse.Contents.add('<h2>ContentFields</h2>');
|
||||
AResponse.Contents.add('<dl>');
|
||||
for i := 0 to ARequest.ContentFields.Count-1 do
|
||||
begin
|
||||
k := ARequest.ContentFields.Names[i];
|
||||
v := ARequest.ContentFields.Values[k];
|
||||
AResponse.Contents.add(format('<dt>%s</dt><dd>%s</dd>',[k,v]));
|
||||
|
||||
end;
|
||||
AResponse.Contents.add('</dl>');
|
||||
AResponse.Contents.add('');
|
||||
AResponse.SendContent;
|
||||
exit;
|
||||
end;
|
||||
allfields := TStringList.Create;
|
||||
try
|
||||
allfields.AddStrings(ARequest.QueryFields);
|
||||
@ -129,27 +160,27 @@ log(mtDebug,self,'Data READY');
|
||||
{$ENDIF}
|
||||
if not assigned(fData) then
|
||||
begin
|
||||
AResponse.ContentType := 'application/json';
|
||||
AResponse.Contents.add('{');
|
||||
AResponse.Contents.add('"type":'+aTypes[fMode]+',');
|
||||
AResponse.Contents.add('"code":'+inttostr(fCode)+',');
|
||||
AResponse.Contents.add('"value":'+inttostr(fParam)+',');
|
||||
AResponse.Contents.add('"name":"'+(fAnswer)+'",');
|
||||
if assigned(fValues) then
|
||||
begin
|
||||
AResponse.Contents.add('"values":[');
|
||||
for i := 0 to fValues.Count-1 do
|
||||
AResponse.ContentType := 'application/json';
|
||||
AResponse.Contents.add('{');
|
||||
AResponse.Contents.add('"type":'+aTypes[fMode]+',');
|
||||
AResponse.Contents.add('"code":'+inttostr(fCode)+',');
|
||||
AResponse.Contents.add('"value":'+inttostr(fParam)+',');
|
||||
AResponse.Contents.add('"name":"'+(fAnswer)+'",');
|
||||
if assigned(fValues) then
|
||||
begin
|
||||
AResponse.Contents.Add(fValues[i]+',');
|
||||
AResponse.Contents.add('"values":[');
|
||||
for i := 0 to fValues.Count-1 do
|
||||
begin
|
||||
AResponse.Contents.Add(fValues[i]+',');
|
||||
end;
|
||||
AResponse.Contents.add(']');
|
||||
fValues.Free;
|
||||
end;
|
||||
AResponse.Contents.add(']');
|
||||
fValues.Free;
|
||||
end;
|
||||
AResponse.Contents.add('}');
|
||||
end
|
||||
AResponse.Contents.add('}');
|
||||
end
|
||||
else
|
||||
begin
|
||||
AResponse.FreeContentStream := true;
|
||||
AResponse.FreeContentStream := true;
|
||||
AResponse.ContentType:='application/pdf';
|
||||
fData.Seek(0,soFromBeginning);
|
||||
AResponse.ContentStream := fData;
|
||||
@ -164,18 +195,14 @@ var
|
||||
f: TextFile;
|
||||
s: string;
|
||||
begin
|
||||
if (Owner as TMyCGIApp).LogFolder='' then exit;
|
||||
if (Owner as TMyCGIApp).Logger=nil then exit;
|
||||
case ALevel of
|
||||
mtError: s := '!!ERROR: ';
|
||||
mtWarning: s := '!WARNING: ';
|
||||
mtInfo: s := #09;
|
||||
mtDebug: s := #09#09;
|
||||
mtExtra: s := #09#09#09;
|
||||
mtError: (Owner as TMyCGIApp).Logger.Error(msg);
|
||||
mtWarning: (Owner as TMyCGIApp).Logger.Warning(msg);
|
||||
mtInfo: (Owner as TMyCGIApp).Logger.Info(msg);
|
||||
mtDebug: (Owner as TMyCGIApp).Logger.Debug(msg);
|
||||
mtExtra: (Owner as TMyCGIApp).Logger.Log(msg);
|
||||
end;
|
||||
assignfile(f, (Owner as TMyCGIApp).LogFolder);
|
||||
if fileexists((Owner as TMyCGIApp).LogFolder) then append(f) else rewrite(f);
|
||||
writeln(f,s+msg);
|
||||
closefile(f);
|
||||
end;
|
||||
|
||||
procedure TMyCGIApp.LoadConfig;
|
||||
@ -195,13 +222,31 @@ end;
|
||||
function TMyCGIApp.InitializeWebHandler: TWebHandler;
|
||||
begin
|
||||
LoadConfig;
|
||||
flogger.FileName:=LogFolder;
|
||||
flogger.Active:=true;
|
||||
flogger.Info('start');
|
||||
Result:=TMyCgiHandler.Create(self);
|
||||
end;
|
||||
|
||||
constructor TMyCGIApp.CreateWithLogger(AOwner: TComponent);
|
||||
begin
|
||||
flogger := TEventLog.Create(self);
|
||||
flogger.Identification:='lms_cgi_client';
|
||||
flogger.LogType:={$IFDEF LINUX}ltSystem{$ELSE}ltFile{$ENDIF};
|
||||
inherited Create(AOwner);
|
||||
|
||||
end;
|
||||
|
||||
destructor TMyCGIApp.Destroy;
|
||||
begin
|
||||
flogger.free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
|
||||
|
||||
begin
|
||||
with TMyCGIApp.create(nil) do
|
||||
with TMyCGIApp.CreateWithLogger(nil) do
|
||||
try
|
||||
Initialize;
|
||||
Run;
|
||||
|
BIN
lms_cgi.obj
BIN
lms_cgi.obj
Binary file not shown.
Loading…
Reference in New Issue
Block a user