|We don't bite newbies here... much|
Using File::Tail on a unicode fileby chrestomanci (Priest)
|on Mar 07, 2012 at 09:52 UTC||Need Help??|
chrestomanci has asked for the
wisdom of the Perl Monks concerning the following question:
Wise brothers, I seek your wisdom.
I am looking to monitor changes to a log file that has a Unicode (utf-16) encoding. The file is a log from a windows application, where the application will from time wake up, truncate the file and start emitting log messages to the file. When it finishes a job it will add a 'job done' message to the log and then go back to sleep. I would like to know when those job done messages are written so I can trigger other events.
My initial plan was to use File::Tail to monitor the file:
The problem with this is that the file is utf-16, and the lines I get back are encoded rather than perl strings.
From the File::Tail docs, there is no way to provide an encoding as a parameter, and looking at the source it look like that module makes a lot of sysseek and sysread calls, that I guess would not work properly via a Unicode input layer. (Because the one to one relationship between characters and bytes would no longer hold true), so I think patching File::Tail to support Unicode would be a difficult undertaking.
Another approach I thought of is to accept the octet strings from File::Tail, and then pass them through $string = decode("utf-16", $octets) (From the Encode) module. A possible problem with this approach is how newlines are encoded, and how File::Tail copes with them.
A third approach would be to abandon the use of File::Tail, and instead to stat() the file at frequent intervals, and every time it changes, read the entire file using standard IO with a suitable encoding.
Do you have any thoughts or suggestions?