Discussion:
How to write a log in reverse order?
Jean-Luc Arnaud
2014-10-31 12:25:50 UTC
Permalink
Hi all,

I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I
want to write events in reverse order, that is, old events at the end,
new ones at the beginning.

Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)

TIA for any help or advice
--
Jean-Luc Arnaud


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Christian Schmitz
2014-10-31 13:37:25 UTC
Permalink
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
1. read file in memory
2. write new log message
3. write old content
4. close file.

there is no real way to append data before.
Except the file is a SQLDatabase where you may be able to add new record and count IDs down.

Sincerely
Christian
--
Read our blog about news on our plugins:

http://www.mbsplugins.de/


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jim Wagner
2014-10-31 15:25:41 UTC
Permalink
It is probably simpler to write a viewer that simply displays the lines in reverse order.

Jim Wagner
Oregon Research Electronics
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
TIA for any help or advice
--
Jean-Luc Arnaud
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jean-Luc Arnaud
2014-10-31 17:01:30 UTC
Permalink
You're right, Jim, simpler, and it's what I did.
Unfortunately, it's drastically slow!

After coding like Christian's suggestion, I got these results for a 900
KB file:

Display in reverse order: between 1.5 and 2 s
Display in natural order: 0.5 to 0.7 s, that is, from 2 to 4 time faster.


Jean-Luc Arnaud
Post by Jim Wagner
It is probably simpler to write a viewer that simply displays the lines in reverse order.
Jim Wagner
Oregon Research Electronics
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
TIA for any help or advice
--
Jean-Luc Arnaud
_______________________________________________
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.c
Tim Jones
2014-10-31 15:27:24 UTC
Permalink
I write the log as normal and then use the Unix command "tail -r" (basically, cat the file backwards) to create the last-in, first-out version.

new-mac-pro-1:~ tjones$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost


new-mac-pro-1:~ tjones$ tail -r /etc/hosts
fe80::1%lo0 localhost
::1 localhost
255.255.255.255 broadcasthost
127.0.0.1 localhost
##
# when the system is booting. Do not change this entry.
# localhost is used to configure the loopback interface
#
# Host Database
##
new-mac-pro-1:~ tjones$

HTH,
Tim
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
TIA for any help or advice
--
Jean-Luc Arnaud
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jean-Luc Arnaud
2014-10-31 17:03:12 UTC
Permalink
Didn't know this command!

Thanks, Tim.

Jean-Luc Arnaud
Post by Tim Jones
I write the log as normal and then use the Unix command "tail -r" (basically, cat the file backwards) to create the last-in, first-out version.
new-mac-pro-1:~ tjones$ cat /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
new-mac-pro-1:~ tjones$ tail -r /etc/hosts
fe80::1%lo0 localhost
::1 localhost
255.255.255.255 broadcasthost
127.0.0.1 localhost
##
# when the system is booting. Do not change this entry.
# localhost is used to configure the loopback interface
#
# Host Database
##
new-mac-pro-1:~ tjones$
HTH,
Tim
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
TIA for any help or advice
--
Jean-Luc Arnaud
_______________________________________________
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

Li
Marnaud
2014-10-31 16:27:16 UTC
Permalink
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
Hello,

This is one case where using a BinaryStream is way simpler. You set the BinaryStream's position to 0 (which moves the “insert point” in the beginning) and you write your line+EndOfLine.
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jean-Luc Arnaud
2014-10-31 17:06:27 UTC
Permalink
As usual, good advice, Arnaud. Thanks.
I thought to this way. But, unfortunately, I need to open and read this
log file in TextEdit (or other text editor).
This is not possible with BinaryStream, AFAIK.

Jean-Luc Arnaud
Post by Marnaud
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
Hello,
This is one case where using a BinaryStream is way simpler. You set the BinaryStream's position to 0 (which moves the “insert point” in the beginning) and you write your line+EndOfLine.
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com
Jon Ogden
2014-10-31 17:14:35 UTC
Permalink
Binary streams write text. It works.

Sent from my iPhone
Post by Jean-Luc Arnaud
As usual, good advice, Arnaud. Thanks.
I thought to this way. But, unfortunately, I need to open and read this log file in TextEdit (or other text editor).
This is not possible with BinaryStream, AFAIK.
Jean-Luc Arnaud
Post by Marnaud
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
Given that AppendText writes at the end of a file, how could I code this?
(MBS commands, if any, welcome)
Hello,
This is one case where using a BinaryStream is way simpler. You set the BinaryStream's position to 0 (which moves the “insert point” in the beginning) and you write your line+EndOfLine.
_______________________________________________
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List
Marnaud
2014-10-31 17:46:25 UTC
Permalink
Post by Jean-Luc Arnaud
As usual, good advice, Arnaud. Thanks.
I thought to this way. But, unfortunately, I need to open and read this log file in TextEdit (or other text editor).
This is not possible with BinaryStream, AFAIK.
Yes it is. Text (in text files) is just binary. The opposite is not always true (binary can be not readable-text). I'm certain if the documentation mentioned that BinaryStream can write text files fine, some (a lot of?) people could use it more. It's currently being depreciated because it's bad known.

To write text with a BinaryStream, just avoid every write method except BinaryStream.Write (all other put some kind of marker or unprintable character). Write an end of line using the EndOfLine class.
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jon Ogden
2014-10-31 19:05:41 UTC
Permalink
Post by Marnaud
Post by Jean-Luc Arnaud
As usual, good advice, Arnaud. Thanks.
I thought to this way. But, unfortunately, I need to open and read this log file in TextEdit (or other text editor).
This is not possible with BinaryStream, AFAIK.
Yes it is. Text (in text files) is just binary. The opposite is not always true (binary can be not readable-text). I'm certain if the documentation mentioned that BinaryStream can write text files fine, some (a lot of?) people could use it more. It's currently being depreciated because it's bad known.
What do you mean it’s being deprecated? I just looked at the Binary Stream docs on 2014r2.1 and there’s no mention of deprecation….



_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Marnaud
2014-10-31 19:17:02 UTC
Permalink
Post by Jon Ogden
What do you mean it’s being deprecated? I just looked at the Binary Stream docs on 2014r2.1 and there’s no mention of deprecation….
Not deprecated, I wrote “depreciated”.
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jon Ogden
2014-10-31 19:25:34 UTC
Permalink
Post by Marnaud
Post by Jon Ogden
What do you mean it’s being deprecated? I just looked at the Binary Stream docs on 2014r2.1 and there’s no mention of deprecation….
Not deprecated, I wrote “depreciated”.
Ah, I think you mean unappreciated. Ah, the complexities of the English language….


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Marnaud
2014-10-31 21:43:47 UTC
Permalink
Post by Jon Ogden
Ah, I think you mean unappreciated. Ah, the complexities of the English language….
Oops, certainly. For whatever reason, I thought “depreciated” was the opposite of “appreciated”; complex, indeed, but like most other languages.
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jon Ogden
2014-10-31 23:49:54 UTC
Permalink
Post by Marnaud
Post by Jon Ogden
Ah, I think you mean unappreciated. Ah, the complexities of the English language….
Oops, certainly. For whatever reason, I thought “depreciated” was the opposite of “appreciated”; complex, indeed, but like most other languages.
If English followed logical rules, then you’d be right! But it doesn’t.

That’s one thing I really enjoyed when I first started learning Japanese. It felt like a very logically ordered language and felt very much like a computer language in a lot of ways.


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Marnaud
2014-11-01 08:23:46 UTC
Permalink
Post by Jon Ogden
If English followed logical rules, then you’d be right! But it doesn’t.
That’s one thing I really enjoyed when I first started learning Japanese. It felt like a very logically ordered language and felt very much like a computer language in a lot of ways.
Ok, two things I learnt today. I don't feel I'll learn Japanese soon, though (perhaps more Java or C++ ;-) ).
Do you mean there are rare or even no exceptions in Japanese? (note that, even in computer languages, there are exceptions…)
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
John McKernon
2014-10-31 18:38:54 UTC
Permalink
Post by Marnaud
This is one case where using a BinaryStream is way simpler. You set the BinaryStream's position to 0 (which moves the “insert point” in the beginning) and you write your line+EndOfLine.
Doesn't that overwrite whatever data already exists beginning at zero in the file, not insert it before whatever’s currently there?

- John


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jay Madren
2014-10-31 19:54:42 UTC
Permalink
Post by Marnaud
Post by Marnaud
This is one case where using a BinaryStream is way simpler. You set the
BinaryStream's position to 0 (which moves the “insert point” in the
beginning) and you write your line+EndOfLine.
Doesn't that overwrite whatever data already exists beginning at zero in
the file, not insert it before whatever’s currently there?
​Yes.​
_______________________________________________
Unsubscribe by sending a message to:
<
Marnaud
2014-10-31 21:47:19 UTC
Permalink
Post by John McKernon
Doesn't that overwrite whatever data already exists beginning at zero in the file, not insert it before whatever’s currently there?
You're right. Shame on me for having told such a bad answer, sorry.
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Norman Palardy
2014-11-05 02:21:59 UTC
Permalink
Post by Marnaud
This is one case where using a BinaryStream is way simpler. You set the BinaryStream's position to 0 (which moves the “insert point” in the beginning) and you write your line+EndOfLine.
This will continually write over whatever is at the beginning of the file
It won’t insert a line

Norman Palardy




_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
emile.a.schwarz
2014-11-01 08:40:16 UTC
Permalink
Hi Jean-Luc,


write the data in a TextField, setting the insertion point to 0 for each new line, then save it to disk and clear it.

 

So you have in the TextField the data in the order you want and Apple’s TextEdit will read it the right way.

 

Emile
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.
Jean-Luc Arnaud
2014-11-03 09:02:04 UTC
Permalink
Hello Emile,

Very crafty idea, thanks!!

Jean-Luc Arnaud
Post by emile.a.schwarz
Hi Jean-Luc,
write the data in a TextField, setting the insertion point to 0 for each new line, then save it to disk and clear it.
So you have in the TextField the data in the order you want and Apple’s TextEdit will read it the right way.
Emile
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Norman Palardy
2014-11-05 02:26:55 UTC
Permalink
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
1) write it to a db then you can dump it whatever order you want

2) if it HAS to be a text file in reverse order
a) assume the current log is in file A
b) write one line to file B
c) copy file A onto the end of file B
d) move file B to be file A & start over

this has the downside of not being super robust IF a failure occurs at any step

3) do like every other log file & jus tapped to the end & use some other app to display it in whatever order you want



_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jim Wagner
2014-11-05 04:31:32 UTC
Permalink
He wants it to be read in reverse order in a generic text editor.

Jim Wagner
Oregon Research Electronics
Post by Norman Palardy
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
1) write it to a db then you can dump it whatever order you want
2) if it HAS to be a text file in reverse order
a) assume the current log is in file A
b) write one line to file B
c) copy file A onto the end of file B
d) move file B to be file A & start over
this has the downside of not being super robust IF a failure occurs at any step
3) do like every other log file & jus tapped to the end & use some other app to display it in whatever order you want
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jean-Luc Arnaud
2014-11-05 09:29:16 UTC
Permalink
Thanks to all replyers.

I got 4 solutions to write or read a log in reverse order:

- Christian's one (and Norman's one, very close): Read existing Log file
(if any), write the new entry (replacing the old file or in a new file),
then write the old entries. Doing so, reading in reverse order is 'direct',

- Christian's and Norman's suggestion to use a DB (not my favorite...),

- Tim's way is to use Shell command 'tail -r',

- Emile's idea is to write in reverse order in a textarea field, at the
beginning of the text content, setting the insertion point to 0 for each
new line, then saving it to Log file (I like this one).

The last solution (writing in chronological order in the Log file, then
reading in reverse order using, for example, NthField) is the slower
one, not suitable for big text. This was my first idea, but definetely
too slow.

Arnaud's solution based on BinaryStream and setting position to 0 before
writing seemed great, but this will erase following text, not inserting
new text. Too bad!

As inserting text works in TextArea, could it be possible to make it
work in BinaryStream or MemoryBlock, too? Christian, a new function in
MBS plugins?

Once again, thanks to all of you.

Jean-Luc Arnaud
Post by Norman Palardy
Post by Jean-Luc Arnaud
Hi all,
I need to log some information in a text file.
The problem is that, for a practical purpose when reading the file, I want to write events in reverse order, that is, old events at the end, new ones at the beginning.
1) write it to a db then you can dump it whatever order you want
2) if it HAS to be a text file in reverse order
a) assume the current log is in file A
b) write one line to file B
c) copy file A onto the end of file B
d) move file B to be file A & start over
this has the downside of not being super robust IF a failure occurs at any step
3) do like every other log file & jus tapped to the end & use some other app to display it in whatever order you want
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <lis
Marnaud
2014-11-05 20:00:00 UTC
Permalink
The last solution (writing in chronological order in the Log file, then reading in reverse order using, for example, NthField) is the slower one, not suitable for big text. This was my first idea, but definetely too slow.
What if, instead of using NthField, you used an array (using the Split command) and then you loop in reverse (from UBound downto 0)? I don't pretend it would be the fastest way, but at least it would be faster than manipulating strings.
Arnaud's solution based on BinaryStream and setting position to 0 before writing seemed great, but this will erase following text, not inserting new text. Too bad!
Yes, I wish there was a parameter to control that, but I think it's a concept at the file system (how could the physical file "insert" some string without rewriting all to the disk with an offset at each write?)


_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>
Jean-Luc Arnaud
2014-11-06 08:55:20 UTC
Permalink
Thanks, Arnaud.
Post by Marnaud
What if, instead of using NthField, you used an array (using the Split command) and then you loop in reverse (from UBound downto 0)? I don't pretend it would be the fastest way, but at least it would be faster than manipulating strings.
You're right, I always forget these Array and Split commands, although I
know they are very efficient.
Post by Marnaud
Yes, I wish there was a parameter to control that, but I think it's a concept at the file system (how could the physical file "insert" some string without rewriting all to the disk with an offset at each write?)
Xojo could code, internally, exactly what Christian and Norman
suggested: Reading the entire content of the file in a temp file (more
secure) or in memory (faster), then writing the new entry, and finally
writing the temp file content (or memory content) after the new entry.
And, in the case of a temp file, closing and deleting this file.
I'm sure they use a very close algorithm, based on a memory in the case
of inserting text in a TextArea object.

Jean-Luc Arnaud
Post by Marnaud
The last solution (writing in chronological order in the Log file, then reading in reverse order using, for example, NthField) is the slower one, not suitable for big text. This was my first idea, but definetely too slow.
What if, instead of using NthField, you used an array (using the Split command) and then you loop in reverse (from UBound downto 0)? I don't pretend it would be the fastest way, but at least it would be faster than manipulating strings.
Arnaud's solution based on BinaryStream and setting position to 0 before writing seemed great, but this will erase following text, not inserting new text. Too bad!
Yes, I wish there was a parameter to control that, but I think it's a concept at the file system (how could the physical file "insert" some string without rewriting all to the disk with an offset at each write?)
_______________________________________________
_______________________________________________
Unsubscribe by sending a message to:
<nug-***@lists.xojo.com>

List Help: <***@xojo.com>

Loading...