Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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:

my $o_tail=File::Tail->new( name => $scan_log_file, maxinterval => 1, adjustafter => 1, ignore_nonexistant => 1, reset_tail => -1, tail => -1, ); while( my $line = $o_tail->read() ) { if( $line =~ m/finished/ ) { # Do stuff } }

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?


In reply to Using File::Tail on a unicode file by chrestomanci

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found