in reply to Re^3: How to open SQL 2005 errorlog?
in thread How to open SQL 2005 errorlog?

WOW, You are GREAT! utf-16 works for SQL 2005 error log! Thank you soooooooo much!!

Can you give me one more help? How should I change this script so it will be able to open error logs of both SQL 2000 and SQL 2005? utf-16 does not work with SQL 2000 log.

I am new in Perl. I really appreciate for your help!

Replies are listed 'Best First'.
Re^5: How to open SQL 2005 errorlog?
by almut (Canon) on Jun 07, 2007 at 20:53 UTC

    I would just switch the open mode, depending on whether it's a 2005 or pre-2005 logfile. In its most simple case, looking for the "2005" in the pathname

    my $openmode = $sqlErrorlog =~ /2005/ ? ">:encoding(utf-16)" : ">"; unless (open LOG, $openmode, $sqlErrorlog) { ...

    (or, as a refinement, you could also check if the year is >= 2005 — presuming they switched to unicode with SQL Server 2005, and future versions will adhere to the same naming scheme, i.e. 2007, 2009, ...   though that might be too many presumptions :)

    However, that approach is kinda cheating, and not generally recommended programming practice (reason: when you/someone happens to change the pathname some time in the future, it's very unlikely you're going to remember this curious dependency buried deep down in the code...).

    So, another way would be to test for the existence of the BOM, and if there, set UTF-16 mode. Actually - as I only recently learned myself - there's already a module for this: File::BOM, which handles the issue even more generally... (see the docs for examples).

      I did some tests. It is hard to detect whether the log is a 2005 or pre-2005 by its pathname in our case. The best way would be to get the file's BOM before scanning. Do you know how to apply the module File::BOM, so I can use it? Please advise if you do. Thanks and have a great weekend!

        Essentially, it's as easy as replacing the open(...) with open_bom(LOG, $sqlErrorlog), the only 'challenge' being that you need to modify your error handling slightly (as open_bom() will croak in case of an error). But something like this should do the trick:

        use File::BOM 'open_bom'; # ... eval { open_bom(LOG, $sqlErrorlog) }; if ($@) { $ref->{log_open_error} = "***Error: $@"; # don't know what Win32::GetLastError would do here # just try it, if you really need it... # $ref->{log_open_error} .= Win32::FormatMessage(Win32::GetLastErr +or); return $ref; }

        File::BOM depends on Readonly, so you want to install that module too (unless you already have it). As both modules are pure Perl, there shouldn't be any problems getting them installed.

        Cheers,
        Almut