Page 1 of 1
Newline character for logging
Posted: Mon Oct 29, 2012 2:28 pm
by nextyoyoma
I'm using a livecode app to create a log on a computer, but the "return" constant uses a carriage return as the line delimiter. Since this is not the standard for UNIX files, it causes a problem with things like tail (can't use it at all) and awk (have to specify record separator). Is it possible to configure LiveCode to use the newline (\n) character instead of a carriage return (/r) for the "return" constant?
EDIT: The LiveCode dictionary says that LiveCode will translate the return character to whatever is appropriate for your platform when dealing with external files, but in case I can confirm that LF, CR, return, numtochar(10) and numtochar(13) all put a carriage return when writing to a text file. For my test workflow, I am typing this into the message box:
Code: Select all
put "hello" & return & "world" into url "file:outputfile.txt"
I have tried that same command with the aforementioned return character references, and every time I run this through the OS X terminal:
this always yields this output:
0000000 h e l l o \r h e l l o
0000013
Also, running:
tail -n 2
yields "world" when it should yield "hello" and "world" on two different lines.
Am I missing something here?
Re: Newline character for logging
Posted: Mon Oct 29, 2012 3:13 pm
by snm
Use numToChar() function to make any custom constant.
Marek
Re: Newline character for logging
Posted: Mon Oct 29, 2012 3:17 pm
by FourthWorld
The LiveCode engine uses ASCII 10 as the value for the constant "CR" internally, in fields, variables, etc. This is the same line-break character used on Unix, where the engine was born in '92, and also uses this on Linux, the first platform the engine was ported to and the one which uses the same convention. Mac uses ASCII 13, and and Windows uses a two-character combination (ASCII 13 & ASCII 10).
With file I/O, the engine offers two different methods for read and write operations: "open file <filename> for binary write" writes the data specified without modification, while "open file <filename> for text write" translates line endings to whatever form is being used on the host OS it's running under at that time. The "text" mode is the default, so if you don't specify which form to use the line-ending translation will occur.
Without seeing your code it's not possible to guess why your line endings aren't in the form you're looking for, esp. given that the platform-specific form is the default.
If you specify "binary" you'll get ASCII 10 used as line endings, consistent with what Unix and Linux expect. But if your app is running on either Unix or Linux at the time then that's what you should be getting anyway, so if you're not it may be helpful to see the relevant portion of your code.
Re: Newline character for logging
Posted: Mon Oct 29, 2012 3:31 pm
by nextyoyoma
Sorry, when I started editing my post there were no replies. Just for completeness:
Code: Select all
command logEntry tMessage
put the short date && the long time && "-" && tMessage & return after URL "file:window.log"
end logEntry
Is what I am using. Should I not use URL for writing to text files?
Re: Newline character for logging
Posted: Mon Oct 29, 2012 5:09 pm
by FourthWorld
Similar to how you can specify "text" or "binary" when using the "open file" command, you can also specify binary mode with using the "put...url" syntax as well by just replacing "file:" with "binfile:".
Re: Newline character for logging
Posted: Mon Oct 29, 2012 5:13 pm
by FourthWorld
It may be worth noting that for most logging the data can get quite large, so rather than writing log entries by putting the entire container in the file as a whole thing, you may want to consider incrementally appending the log as most logging tools do.
In LiveCode you can do this by opening the file once using the "append" option, writing to it as needed, and then closing it when your app quits:
open file "MyLog.txt" for binary append
When a file is opened using "append", all writes go to the end of the file and alter no previous data, making it the fastest and most robust method for logging.
Re: Newline character for logging
Posted: Mon Oct 29, 2012 6:16 pm
by nextyoyoma
Thanks, Richard, that's very helpful. I still don't understand why, if I'm on OS X, LiveCode thinks that a CR is the correct end-of-line marker, but at least I know how to deal with it now. Thanks again.
Re: Newline character for logging
Posted: Mon Oct 29, 2012 7:21 pm
by FourthWorld
nextyoyoma wrote:I still don't understand why, if I'm on OS X, LiveCode thinks that a CR is the correct end-of-line marker
The reason is largely historical, but at this point protects legacy code written expecting it.
Before OS X the Mac line ending was always ASCII 13, and since OS X is a Unix much of the OS now uses ASCII 10. But not all of it. IIRC AppleScript still converts line endings to ASCII 13, and there are likely others as well. LiveCode is just one more.
