HL Log Standard Examples

From Valve Developer Community
Jump to: navigation, search

These examples comply with Specification Rev. 1.03.

Example 1: Perl: Log parsing routines.

if ($logline =~ /^\/\//)
{
    # matches comments

    # you should skip to the next log line
}
elsif ($logline =~ /^"([^"]+)" ([^"\(]+) "([^"]+)" ([^"\(]+) "([^"]+)"(.*)$/)
{
    # matches events 057,058,059,066

    $team = "";
    $player = $1; # parse out name, uid and team later
    $event1 = $2; # event type - "killed", "attacked", etc.
    $noun1 = $3; # victim name/objective code, etc.
    $event2 = $4; # "with", etc.
    $noun2 = $5; # weapon/victim name, etc.
    $properties = $6; # parse out keys and values later
}
elsif ($logline =~ /^"([^"]+)" ([^"\(]+) "([^"]+)"(.*)$/)
{
    # matches events 050,053,054,055,056,060,063a,063b,068,069

    $team = "";
    $player = $1;
    $event1 = $2;
    $noun1 = $3; # weapon/team code/objective code, etc.
    $event2 = "";
    $noun2 = "";
    $properties = $4;
}
elsif ($logline =~ /^"([^"]+)" ([^\(]+)(.*)$/)
{
    # matches events 050b,051,052

    $team = "";
    $player = $1;
    $event1 = $2;
    $noun1 = "";
    $event2 = "";
    $noun2 = "";
    $properties = $3;
}
elsif ($logline =~ /^Team "([^"]+)" ([^"\(]+) "([^"]+)"(.*)$/)
{
    # matches events 061,064

    $team = $1; # Team code
    $player = 0;
    $event1 = $2;
    $noun1 = $3;
    $event2 = "";
    $noun2 = "";
    $properties = $4;
}
elsif ($logline =~ /^([^"\(]+) "([^"]+)"(.*)$/)
{
    # matches events 062,003a,003b,005,006

    $team = "";
    $player = 0;
    $event1 = $1;
    $noun1 = $2;
    $event2 = "";
    $noun2 = "";
    $properties = $3;
}


# array getPlayerInfo (string player)
sub getPlayerInfo
{
    my ($player) = @_;

    if ($player =~ /^(.+)<(\d+)><(\d+)><(.*)>$/)
    {
        return ($1, $2, $3, $4);
    }
    else
    {
        return ("", 0, 0, "");
    }
}

# hash getProperties (string propstring)
sub getProperties
{
    my ($propstring) = @_;
    my %properties;

    while ($propstring =~ s/^\s*\((\S+)(?: "([^"]+)")?\)//)
    {
        if ($2)
        {
            $properties{$1} = $2;
        }
        else
        {
            $properties{$1} = 1; # for boolean properties per Note (4)
        }
    }

    return %properties;
}

# To get a player's name, uid, wonid and team from the
# Name<uid><wonid><team> string:
($player_name, $player_uid, $player_wonid, $player_team) = getPlayerInfo($player);

# To parse all (key "value") properties from end of a log line into an
# associative array:
%properties = getProperties($properties);
print $properties{'weapon'};