1# $OpenBSD: Trace.pm,v 1.4 2012/07/08 09:36:40 jasper Exp $ 2 3# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org> 4# Copyright (c) 2012 Marc Espie <espie@openbsd.org> 5# 6# Permission to use, copy, modify, and distribute this software for any 7# purpose with or without fee is hereby granted, provided that the above 8# copyright notice and this permission notice appear in all copies. 9# 10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18use strict; 19use warnings; 20use feature qw(say state); 21 22package LT::Trace; 23use Exporter 'import'; 24our @EXPORT = qw(tprint tsay); 25 26sub print(&) 27{ 28 my $val = shift; 29 if (defined $ENV{TRACE_LIBTOOL}) { 30 state $trace_file; 31 if (!defined $trace_file) { 32 open $trace_file, '>>', $ENV{TRACE_LIBTOOL}; 33 } 34 if (defined $trace_file) { 35 print $trace_file (&$val); 36 } 37 } 38} 39 40my $trace_level = 0; 41 42sub set 43{ 44 my $class = shift; 45 $trace_level = shift; 46} 47 48sub tprint(&;$) 49{ 50 my ($args, $level) = @_; 51 52 $level = 1 if !defined $level; 53 54 if ($trace_level >= $level) { 55 print (&$args); 56 } 57} 58 59sub tsay(&;$) 60{ 61 my ($args, $level) = @_; 62 63 $level = 1 if !defined $level; 64 65 if ($trace_level >= $level) { 66 say (&$args); 67 } 68} 69 701; 71