0

How to make a text file and append new lines to it? The text will contain history date and time of button pressed.

The output logs i wish would have the content like this:

1/5/2014 9:33:44 AM Print Button Pressed.
1/5/2014 9:35:44 AM Clear (popup menu).

and so on..

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Bianca
  • 973
  • 2
  • 14
  • 33
  • 1
    You write the code. There is a recent question about how to write to a text file [here](http://stackoverflow.com/questions/20873754/best-way-for-read-and-write-a-text-file). `FormatDateTime` will give you the date and time format. You'll need to write a method that logs information when something happens. – Ken White Jan 05 '14 at 03:04
  • What have you ***tried***, and what are you ***struggling with***? At the moment your question is too open-ended. So the best answer might be: "Read _Mastering Delphi_ by Marco Cantu." All aspects of your question are covered in that book. – Disillusioned Jan 05 '14 at 09:30
  • "You write the code" is a pretty bland advice. So is "read a delphi book" because they won't really tell you that logs need to be gathered into a buffer from multiple threads and dumped to the file/network using a thread specifically used for this purpose. Like the main thread, maybe in the main event loop. They also won't tell you that it might be a good idea to subclass buttons, grids, etc. to install logging functions to appropriate places. For gathering telemetry from multiple sources, you may need a network client/service which supports async operations. – nurettin Oct 24 '17 at 07:53

3 Answers3

3

Something like this works:

procedure Log(const s:string);
const FileName='log.txt';
begin
    TFile.AppendAllText(FileName, DateTimeToStr(Now()) + ' '+ s + sLineBreak);
end;

Log('Print Button Pressed.');
Log('Clear (popup menu).');

You'll have to add IOUtils to the uses clause at the top.

If you need something fancier, take a look at Log4Delphi for example.

Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
1

This is easily done with traditional Pascal I/O functionality

Procedure Log (const s: string);
var
 f: textfile;

begin
 assignfile (f, logfile);
 {$I-}
 append (f);
 if ioresult <> 0 then rewrite (f);
 {$I+}
 writeln (f, datetimetostr (now), ' ', s);
 flush (f);
 closefile (f);
end;

or alternatively

Procedure Log (const s: string);
var
 f: textfile;

begin
 assignfile (f, logfile);
 if fileexists (logfile)
  then append (f)
  else rewrite (f);
 writeln (f, datetimetostr (now), ' ', s);
 flush (f);
 closefile (f);
end;
No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • 1
    -1, simply for proposing 1980 style file I/O. There are many more modern alternatives available; proposing this as any sort of solution is ludicrous. This is the equivalent of answering a question about "What version of Windows should I install?" with "Windows 3.0". – Ken White Jan 05 '14 at 05:58
  • 2
    @KenWhite: the question did not cite which version of Delphi is in use. At the moment, there is only one other answer which requires the use of unit IOUtils, which may not be available in the version which the OP has. Thus I think that my answer is fair and I request that you remove the downvote. – No'am Newman Jan 05 '14 at 08:12
  • 1
    Unless the poster adds the Delphi 1 tag, and specifically indicates that they're asking about the old 16-bit version of Delphi, your answer is the absolute worst possible choice. You could have chosen at least 10 better ways than this, and your excuse is simply wrong. Delphi 1 even had better ways to do it than your solution. – Ken White Jan 05 '14 at 15:44
  • 1
    @KenWhite I have to admit that sometimes the simplicity of `WriteLn` has an appeal to me though. More than C-ish `sprintf` clone. – Arioch 'The Jan 05 '14 at 20:33
  • @Arioch'The: Sure. I use it all the time in console apps to dump results to the screen. It's so far outdated for file I/O now though that there's no reason to recommend it as the first alternative, especially to someone who is clearly a new programmer. – Ken White Jan 05 '14 at 20:45
  • @KenWhite I feel like a broken record on this, but it's hard to complain if TextFile has never been deprecated nor any of the other methods moved into the System unit so they're present by default. Having over a dozen different ways to read/write a file in the first place is part of the problem. Java has the same problem; their instructions provide a diagram to help the user choose the right/best method of opening a file under particular conditions! Of course another issue is that VCL never gained logging functions. – alcalde Jan 05 '14 at 21:21
0

Maybe, it will be better to use something like fastreport (built-in Delphi).

huxahetu
  • 193
  • 5