Forums > MaxMSP

append text to file

May 21, 2009 | 3:15 am

I have made a standalone on a windows machine that (amongst other things) creates a log file of its own activity. The program is designed to work automatically in a remote location over a period of months or years. Basically the log file records when the program boots and quits, when a MIDI file starts and stops playing, when a user changes settings etc.

Because the computer is in a remote setting, the log file is important to verify that things are working alright. It gets rewritten as a uniquely-named file once a week.

I have made a logging subpatch using the text object, but it is not particularly robust. What it does is: write to temp file when program quits, load temp file again when program loads, append to ram when event occurs, re-write to temp file when program quits again. The temp file is renamed once a week to become ‘permanent’.

Sometimes the info gets lost in ram-land (this happens when using remote access while a midi file is playing, but that’s not what I’m writing about) and so the whole log (up to that day of the week) is lost because it has to read the whole file and write the whole thing back again to disk.

One way around this is to only open the file when a logging event occurs, append using the text object and write to file again, but there is still the danger of the whole file being corrupted while it is ram.

What i would like is an ‘append-to-file’ mode for the text object so that it appends directly to the disk file rather than having to load all the info into ram, just to append one line.

Am I missing something obvious here?

Thanks for comments or suggestions.



May 21, 2009 | 8:57 am

actually, I was curious so I tried it:

something like this in a .js:

function text(sometext)
{
    var f, date;
    date = new Date();
    f = new File("/log.txt", "readwrite", "TEXT");
    if(f.isopen)
    {
        f.position = f.eof;
        f.writeline("Log ["+date+"]: "+sometext);
    }
    f.close();
}

then in Max just send message

text "the text I want in the log file"

this would append the text to the file.


May 22, 2009 | 2:18 am

Thanks, Martin–

that’s exactly what I was looking for– obviously time for me to jump into javascript (must admit I have been avoiding it for a while now).

Thanks again.
TM


May 23, 2009 | 6:31 am

…actually the f.close() should be inside the if()..


Viewing 5 posts - 1 through 5 (of 5 total)