unit DEvLog;

interface

uses
  dparser,
  dstrnum,
  dtypes;

type
TEvLogPair = record
  Key, Value: TString;
end;

PEvLogRec = ^TEvLogRec;
TEvLogRec = object
  LogLine: TString;
  Time: LongInt;
  Name: TString;
  Params: array of TEvLogPair;
  function Parse(const LogLine: TString): Boolean;
  function Get(const Name: TString; const Def: TString = ''): TString;
end;

implementation

function TEvLogRec.Parse(const LogLine: TString): Boolean;
var
  TimeStr, Rest: TString;
  KV, K, V: TString;
begin
  Self.LogLine := LogLine;
  if not ParseBinary(LogLine, ' ', TimeStr, Rest) then
    Exit(False);
  if (Rest[1] <> 'E') or (Rest[2] <> 'V') or (Rest[3] <> ' ') then
    Exit(False);
  Rest := StringDeletePre(Rest, 3);
  Time := StrToInt(TimeStr);
  while Rest <> '' do begin
    if not ParseBinary(Rest, ' ', KV, Rest) then
      break;
    if KV = '' then
      continue;
    ParseBinary(KV, '=', K, V);
    SetLength(Params, Length(Params) + 1);
    Params[High(Params)].Key := K;
    Params[High(Params)].Value := Value;
  end;
end;

function TEvLogRec.Get(const Name: TString; const Def: TString = ''): TString;
var
  I: LongInt;
begin
  for I := 0 to High(Params) do
    if Params[I].Key = Name then
      Exit(Params[I].Value);
  Exit(Def);
end;

end.
