Changeset 3 for trunk


Ignore:
Timestamp:
Jul 23, 2015, 1:42:15 PM (9 years ago)
Author:
chronos
Message:

Added new feature log console output to log files located in user home directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/linetime.lpr

    r2 r3  
    2424    ShowTime: Boolean;
    2525    LastLineTime: TDateTime;
     26    LogFile: TFileStream;
     27    LogEnabled: Boolean;
     28    procedure WriteRaw(Text: string);
     29    procedure WriteLog(Text: string);
    2630    procedure PrintOutput(Output: string);
    2731    procedure ProcessBuffer(Buffer: string);
     
    4145  InputStream: TIOStream;
    4246  Count: Integer;
     47  LogDir: string;
     48  FileName: string;
    4349begin
    44   FormatCSV := False;
    45 
    46   // quick check parameters
    47   ErrorMsg := CheckOptions('hc', 'help csv');
     50  // Quick check parameters
     51  ErrorMsg := CheckOptions('hcl', 'help csv log');
    4852  if ErrorMsg <> '' then begin
    4953    ShowException(Exception.Create(ErrorMsg));
     
    5256  end;
    5357
    54   // parse parameters
     58  // Parse parameters
    5559  if HasOption('h', 'help') then begin
    5660    WriteHelp;
     
    5963  end;
    6064
    61   if HasOption('c', 'csv') then begin
    62     FormatCSV := True;
     65  FormatCSV := HasOption('c', 'csv');
     66  LogEnabled := HasOption('l', 'log');
     67  if LogEnabled then begin
     68    LogDir := GetUserDir + DirectorySeparator + '.linetime' + DirectorySeparator + 'logs';
     69    ForceDirectories(LogDir);
     70    FileName := LogDir + DirectorySeparator + FormatDateTime('yyyymmddhhnnss', Now) + '.log';
     71    LogFile := TFileStream.Create(FileName, fmCreate);
    6372  end;
    6473
     
    7786    until Count = 0;
    7887
    79     if FormatCSV then begin
    80       if not FirstLine then WriteLn('"');
    81     end;
     88    PrintOutput('');
    8289  finally
    8390    InputStream.Free;
    8491  end;
    85   WriteLn;
     92  WriteRaw(LineEnding);
     93  WriteLog(LineEnding);
    8694
    8795  // stop program loop
     
    8997end;
    9098
     99procedure TMyApplication.WriteRaw(Text: string);
     100begin
     101  Write(Text);
     102end;
     103
     104procedure TMyApplication.WriteLog(Text: string);
     105begin
     106  if LogEnabled then begin
     107    if Length(Text) > 0 then
     108      LogFile.Write(Text[1], Length(Text));
     109  end;
     110end;
     111
    91112procedure TMyApplication.PrintOutput(Output: string);
    92113var
    93114  LineTime: TDateTime;
     115  TimeStr: string;
    94116begin
    95117  if NewLine then begin
    96118    if FormatCSV then begin
    97       if not FirstLine then WriteLn('"');
     119      if not FirstLine then WriteRaw('"' + LineEnding);
    98120    end else begin
    99       if not FirstLine then WriteLn('');
     121      if not FirstLine then WriteRaw(LineEnding);
    100122    end;
     123    WriteLog(LineEnding);
    101124    NewLine := False;
    102125    FirstLine := False;
     
    104127  if (Length(Output) > 0) and ShowTime then begin
    105128    LineTime := Now - StartTime;
     129    TimeStr := FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2);
    106130    if FormatCSV then begin
    107       Write(FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2) + ',' +
     131      WriteRaw(TimeStr + ',' +
    108132        FloatToStrF((LineTime - LastLineTime) / OneSecond, ffFixed, 10, 2) + ',"');
    109133    end else begin
    110       Write(#$1b'[0;32m' + FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2) + #$1b'[0m ');
     134      WriteRaw(#$1b'[0;32m' + TimeStr + #$1b'[0m ');
    111135    end;
     136    WriteLog(TimeStr + ' ');
    112137    ShowTime := False;
    113138    LastLineTime := LineTime;
    114139  end;
    115   Write(Output);
     140  WriteRaw(Output);
     141  WriteLog(Output);
    116142end;
    117143
     
    144170destructor TMyApplication.Destroy;
    145171begin
     172  FreeAndNil(LogFile);
    146173  inherited Destroy;
    147174end;
     
    152179  WriteLn('Usage: ', ExeName, ' -h');
    153180  WriteLn(' -c --csv Print lines in CSV format');
     181  WriteLn(' -l --log Log output to log files in ~/.linetime/logs');
    154182end;
    155183
Note: See TracChangeset for help on using the changeset viewer.