- Timestamp:
- Jul 23, 2015, 1:42:15 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/linetime.lpr
r2 r3 24 24 ShowTime: Boolean; 25 25 LastLineTime: TDateTime; 26 LogFile: TFileStream; 27 LogEnabled: Boolean; 28 procedure WriteRaw(Text: string); 29 procedure WriteLog(Text: string); 26 30 procedure PrintOutput(Output: string); 27 31 procedure ProcessBuffer(Buffer: string); … … 41 45 InputStream: TIOStream; 42 46 Count: Integer; 47 LogDir: string; 48 FileName: string; 43 49 begin 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'); 48 52 if ErrorMsg <> '' then begin 49 53 ShowException(Exception.Create(ErrorMsg)); … … 52 56 end; 53 57 54 // parse parameters58 // Parse parameters 55 59 if HasOption('h', 'help') then begin 56 60 WriteHelp; … … 59 63 end; 60 64 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); 63 72 end; 64 73 … … 77 86 until Count = 0; 78 87 79 if FormatCSV then begin 80 if not FirstLine then WriteLn('"'); 81 end; 88 PrintOutput(''); 82 89 finally 83 90 InputStream.Free; 84 91 end; 85 WriteLn; 92 WriteRaw(LineEnding); 93 WriteLog(LineEnding); 86 94 87 95 // stop program loop … … 89 97 end; 90 98 99 procedure TMyApplication.WriteRaw(Text: string); 100 begin 101 Write(Text); 102 end; 103 104 procedure TMyApplication.WriteLog(Text: string); 105 begin 106 if LogEnabled then begin 107 if Length(Text) > 0 then 108 LogFile.Write(Text[1], Length(Text)); 109 end; 110 end; 111 91 112 procedure TMyApplication.PrintOutput(Output: string); 92 113 var 93 114 LineTime: TDateTime; 115 TimeStr: string; 94 116 begin 95 117 if NewLine then begin 96 118 if FormatCSV then begin 97 if not FirstLine then Write Ln('"');119 if not FirstLine then WriteRaw('"' + LineEnding); 98 120 end else begin 99 if not FirstLine then Write Ln('');121 if not FirstLine then WriteRaw(LineEnding); 100 122 end; 123 WriteLog(LineEnding); 101 124 NewLine := False; 102 125 FirstLine := False; … … 104 127 if (Length(Output) > 0) and ShowTime then begin 105 128 LineTime := Now - StartTime; 129 TimeStr := FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2); 106 130 if FormatCSV then begin 107 Write (FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2)+ ',' +131 WriteRaw(TimeStr + ',' + 108 132 FloatToStrF((LineTime - LastLineTime) / OneSecond, ffFixed, 10, 2) + ',"'); 109 133 end else begin 110 Write (#$1b'[0;32m' + FloatToStrF(LineTime / OneSecond, ffFixed, 10, 2)+ #$1b'[0m ');134 WriteRaw(#$1b'[0;32m' + TimeStr + #$1b'[0m '); 111 135 end; 136 WriteLog(TimeStr + ' '); 112 137 ShowTime := False; 113 138 LastLineTime := LineTime; 114 139 end; 115 Write(Output); 140 WriteRaw(Output); 141 WriteLog(Output); 116 142 end; 117 143 … … 144 170 destructor TMyApplication.Destroy; 145 171 begin 172 FreeAndNil(LogFile); 146 173 inherited Destroy; 147 174 end; … … 152 179 WriteLn('Usage: ', ExeName, ' -h'); 153 180 WriteLn(' -c --csv Print lines in CSV format'); 181 WriteLn(' -l --log Log output to log files in ~/.linetime/logs'); 154 182 end; 155 183
Note:
See TracChangeset
for help on using the changeset viewer.