/usr/share/perl5/Anomy/Log.pm is in sanitizer 1.76-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | #!/usr/bin/perl
#
my $revision = '$Id: Log.pm,v 1.9 2002/01/04 18:32:49 bre Exp $';
my $version = 'Anomy 0.0.0 : sanitizer.pl';
#
## Copyright (c) 2001-2002 Bjarni R. Einarsson. All rights reserved.
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
#
##############################################################################
#
# NOTE: Sanitizer development is for the most part sponsored by
# FRISK Software International, http://www.f-prot.com/. Please
# consider buying their anti-virus products to show your
# appreciation.
#
##############################################################################
#
# This a module for handling nested log files. It provides a framework
# for building very detailed logs in memory and outputting them either as
# human readable text or XML.
#
# Example:
#
# my $log1 = new Anomy::Log;
# my $log2 = new Anomy::Log;
# my $log3;
#
# # Set up local hooks for events
# $log1->add_hook("match", sub { print "Hey, something matched!\n"; } );
# $log1->add_hook("part", sub { print "Ooh, a new part!\n"; } );
#
# # Set up a global hook (inherited by sub-logs)
# $log1->add_hook("thing", sub { print "Ooh, a new thing!\n"; }, "global" );
#
# # Record an event
# $log1->entry("match", SLOG_INFO, { rule => 3 }, "Matched rule %rule%." );
#
# # Attach $log2 to an entry in $log1.
# $log->sublog("part", SLOG_TRACE, { type => "multipart/mixed" }, $log2);
#
# # Create a new log ($log3) chained to an entry in $log1.
# $log3 = $log1->sublog("part", SLOG_TRACE, { type => "multipart/mixed" });
#
# # Print everything of level SLOG_INFO as text.
# print $log1->print_as_text(SLOG_INFO);
#
# # Print everyting (any level) as XML.
# print $log1->print_as_xml(SLOG_ALL);
#
##[ Package definition ]######################################################
package Anomy::Log;
use strict;
BEGIN {
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = do { my @r = (q$Revision: 1.9 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
@ISA = qw(Exporter);
@EXPORT = qw(&SLOG_ERROR
&SLOG_WARNING
&SLOG_INFO
&SLOG_DEBUG
&SLOG_TRACE
&SLOG_SUBLOG
&SLOG_ALL);
@EXPORT_OK = qw( );
}
use vars @EXPORT_OK;
##[ Constants ]###############################################################
# Log message types
sub SLOG_ERROR { return 0x0001; }
sub SLOG_WARNING { return 0x0002; }
sub SLOG_INFO { return 0x0004; }
sub SLOG_DEBUG { return 0x0008; }
sub SLOG_SUBLOG { return 0x4000; }
sub SLOG_TRACE { return 0x8000; }
sub SLOG_ALL { return 0xFFFF; }
sub LOG_ENTRY { return 1; }
sub LOG_SUBLOG { return 2; }
##[ Package implementation ]##################################################
sub new
{
my ($proto) = @_;
my $class = ref($proto) || $proto;
my $self = {
"local_hooks" => { },
"global_hooks" => { },
"log" => [ ],
};
bless ($self, $class);
return $self;
}
# Wipe the log clean.
sub clear
{
my $self = shift;
$self->{"log"} = [ ];
}
# Register a new hook.
sub add_hook
{
my ($self, $tag, $hook, $global) = @_;
my $h = "local_hooks";
$h = "global_hooks" if ($global);
$self->{$h}->{$tag} = [ ] unless ($self->{$h}->{$tag});
push @{ $self->{$h}->{$tag} }, $hook;
}
# Check for hooks for this log entry type, execute them if present.
# Returns 1 (true) if no hooks are found, otherwise it returns the
# value returned by the hook.
sub check_hook
{
my ($self, $entry) = @_;
my $ret = 1;
foreach my $h ("local_hooks", "global_hooks")
{
if (my $hooks = $self->{$h}->{ $entry->[1] })
{
foreach my $cmd (@{ $hooks })
{
$ret = 0 unless (&$cmd($self, $entry));
}
}
}
return $ret;
}
# Usage:
#
# $log->entry("match", SLOG_INFO, { rule => 3 }, "Matched rule %rule%." );
#
sub entry
{
my ($self, $tag, $level, $attr, $data) = @_;
my $entry = [ LOG_ENTRY, $tag, $level, $attr || { }, $data ];
push @{ $self->{"log"} }, $entry if ($self->check_hook($entry));
}
# Usage:
#
# $sublog = $log->sublog("part", SLOG_TRACE, { type => "multipart/mixed" });
# $log->sublog("part", SLOG_TRACE, { type => "multipart/mixed" }, $sublog);
#
sub sublog
{
my ($self, $tag, $level, $attr, $sl) = @_;
$sl = new Anomy::Log unless ($sl);
# Inherit global hooks from parent
$sl->{"global_hooks"} = $self->{"global_hooks"};
$level = 0 unless ($level);
$level |= SLOG_SUBLOG;
my $entry = [ LOG_SUBLOG, $tag, $level, $attr || { }, $sl ];
push @{ $self->{"log"} }, $entry if ($self->check_hook($entry));
return $sl;
}
# Render a log as a bit of HTML, using the embedded descriptions.
#
# Usage:
#
# $text = $log->print_as_text(SLOG_ALL);
#
sub print_as_html
{
my ($self, $out_level, $prefix, $color) = @_;
my $ret = $self->print_as_text($out_level, $prefix);
$ret =~ s/&/&/g;
$ret =~ s/</</g;
$ret =~ s/>/>/g;
return "<pre><font color=\"$color\">\n$ret\n</font></pre>\n";
}
# Render a log as a text file, using the embedded descriptions.
#
# Usage:
#
# $text = $log->print_as_text(SLOG_ALL);
sub print_as_text
{
my ($self, $out_level, $prefix) = @_;
my $ret = "";
$out_level = SLOG_ALL unless (defined $out_level);
foreach my $entry (@{ $self->{"log"} })
{
my ($type, $tag, $level, $attr, $data) = @{ $entry };
my $attrs = join(", ", map { $_="$_=\"".$attr->{$_}."\"" }
sort(keys(%{ $attr })));
if (LOG_SUBLOG == $type)
{
my $d = $data->print_as_text($out_level, $prefix." ");
if (($d) || ($level & $out_level))
{
$ret .= $prefix.$tag." (".$attrs."):\n".$d."\n";
}
$ret =~ s/\n\n+$/\n\n/;
}
elsif ($level & $out_level)
{
my $d = $data;
foreach my $key (sort(keys(%{ $attr })))
{
$d =~ s/%$key%/$attr->{$key}/g;
}
$d =~ s/%ATTRIBUTES%/$attrs/g;
$d =~ s/\n/\n$prefix/gs;
$ret .= $prefix.$d."\n";
}
}
return $ret;
}
# Render a log as an XML-formatted string, pruning all leaves not matching
# the requested log level.
#
# Usage:
#
# $text = $log->print_as_xml(SLOG_ALL);
#
sub print_as_xml
{
my ($self, $out_level, $prefix) = @_;
my $ret = "";
$out_level = SLOG_ALL unless (defined $out_level);
foreach my $entry (@{ $self->{"log"} })
{
my ($type, $tag, $level, $attr, $data) = @{ $entry };
if (LOG_SUBLOG == $type)
{
my $d = $data->print_as_xml($out_level, $prefix." ");
if (($d) || ($level & $out_level))
{
$ret .= $prefix;
$ret .= print_tag($tag, $attr)."\n";
$ret .= $d;
$ret .= $prefix."</".$tag.">\n";
}
}
elsif ($level & $out_level)
{
$ret .= $prefix;
$ret .= print_tag($tag, $attr);
$ret .= encode_xml($data);
$ret .= "</".$tag.">\n";
}
}
return $ret;
}
## Helper for print_as_xml
sub print_tag
{
my ($tag, $attr) = @_;
my $ret = "<".$tag;
foreach my $a (sort(keys(%{ $attr })))
{
$ret .= " $a=\"". encode_xml($attr->{$a}) ."\"";
}
$ret .= ">";
return $ret;
}
## Helper for print_as_xml
sub encode_xml
{
my $text = shift;
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
$text =~ s/"/"/g; # FIXME!! Probably not valid XML!
$text =~ s/\n/&nl;/g; # FIXME!! Probably not valid XML!
return $text;
}
1;
#EOF#
|