0

I want to create a log file in Scheme, but every time I add a new entry, I want it to be at the beginning of the file, so when I read X number of logs from the file again, it reads the X newest entries from new to old.

Example:

22/02/14 13:50 Newest log entry

22/02/14 13:45 Older log entry

22/02/14 13:40 Oldest log entry

Does anyone know how to do this using the 'open-input-file' and 'open-output-file' procedures?

JNevens
  • 11,202
  • 9
  • 46
  • 72
  • 2
    Don't do this, it will be incredibly slow. Just append to your log file, and use tools like the Unix command `tail` to display the end of the file. – uselpa Feb 22 '14 at 13:06
  • possible duplicate of [Writing in the beginning of a text file Java](http://stackoverflow.com/questions/6127648/writing-in-the-beginning-of-a-text-file-java) – uselpa Feb 22 '14 at 13:38

1 Answers1

1

The functionality you are requesting you need to write the whole logfile every time you need to write a new entry because you will overwrite the previous first entry with the next. Usually programs don't keep the commited parts of a logfile so this introduces more memory usage and your program must know when the log is being rotated to clear the buffer.

The standard way is to append a new entry, which leaves the previous log entries where the last log write put them.

As a compromise you might look for a program that displays a log file in the reverse order and prehaps tails it like that too. It's easy to implement so I guess it would exist already. Writing such an app if it doesn't exist would be trivial.

Sylwester
  • 47,942
  • 4
  • 47
  • 79