Subversion Repositories DevTools

Rev

Rev 7319 | Rev 7322 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
########################################################################
7300 dpurdie 2
# COPYRIGHT - VIX IP PTY LTD ("VIX"). ALL RIGHTS RESERVED.
227 dpurdie 3
#
279 dpurdie 4
# Module name   : JatsError
227 dpurdie 5
# Module type   : Perl Package
279 dpurdie 6
# Compiler(s)   : Perl
227 dpurdie 7
# Environment(s): jats
8
#
9
# Description   : A Perl Package to perform error handling within JATS
10
#
11
#                 Uses global variables
12
#                       $::ScmVerbose;
13
#                       $::ScmQuiet;
14
#                       $::ScmDebug;
15
#                 For use with existing scripts
16
#
17
#......................................................................#
18
 
283 dpurdie 19
package JatsError;
20
use base qw(Exporter);
21
 
255 dpurdie 22
require 5.006_001;
227 dpurdie 23
use strict;
24
use warnings;
25
use Data::Dumper;
26
use IO::Handle;
27
 
7307 dpurdie 28
# exported package globals go here
29
#our $ScmVerbose;
30
#our $ScmDebug;
31
#our $ScmQuiet;
32
our $ScmOnExit;
33
our $ScmDelayExit;
34
our $ScmErrorCount;
35
our $ScmExitCode;
36
 
37
my $EPrefix = '';
38
my $EName = '';
39
my $EFn = '';
40
my $ElPrefix = '';
7308 dpurdie 41
my $EIndent = '';
42
my $EOffset = '';
7307 dpurdie 43
 
227 dpurdie 44
#-------------------------------------------------------------------------------
45
# Function        : BEGIN
46
#
47
# Description     : Standard Package Interface
48
#
49
# Inputs          :
50
#
51
# Returns         :
52
#
53
 
54
BEGIN {
283 dpurdie 55
    our ($VERSION, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
227 dpurdie 56
 
57
    # set the version for version checking
58
    $VERSION     = 1.00;
59
 
279 dpurdie 60
    @EXPORT      = qw(ErrorConfig ErrorReConfig ErrorDoExit
261 dpurdie 61
                      ReportError Fatal Error Warning
7320 dpurdie 62
                      WarnError
227 dpurdie 63
                      Message Message1
64
                      Information Information1
65
                      Question
263 dpurdie 66
                      Verbose0 Verbose Verbose2 Verbose3
227 dpurdie 67
                      Debug0 Debug Debug2 Debug3
68
                      IsVerbose IsDebug IsQuiet
231 dpurdie 69
                      DebugDumpData DebugDumpPackage DebugTraceBack
227 dpurdie 70
                      DebugPush DebugPop
5109 dpurdie 71
                      StartCapture
72
                      DumpCapture
227 dpurdie 73
                      );
74
 
75
    %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
76
 
77
    # your exported package globals go here,
78
    # as well as any optionally exported functions
79
    @EXPORT_OK   = qw();
80
 
81
    #
82
    #   Ensure globals have a defined value
83
    #
263 dpurdie 84
    $::ScmVerbose = $ENV{GBE_VERBOSE} || 0  unless defined( $::ScmVerbose );
85
    $::ScmDebug = $ENV{GBE_DEBUG} || 0      unless defined( $::ScmDebug );
86
    $::ScmQuiet = 0                         unless defined( $::ScmQuiet );
227 dpurdie 87
 
7307 dpurdie 88
    $ScmErrorCount = 0;
89
    $ScmExitCode = 1;
90
 
227 dpurdie 91
    #
92
    #   Force autoflush in an attempt to limit the intermingling of
93
    #   Error and non-error output.
94
    #
95
    STDOUT->autoflush(1);
96
    STDERR->autoflush(1);
97
}
98
 
99
# non-exported package globals go here
5109 dpurdie 100
my @captured;
101
my $capturing;
279 dpurdie 102
 
227 dpurdie 103
#-------------------------------------------------------------------------------
104
# Function        : import
105
#
106
# Description     : Package import function
279 dpurdie 107
#                   This function will examine arguments provided in the
227 dpurdie 108
#                   invoking 'uses' list and will configure the package
109
#                   accordingly.
110
#
111
# Inputs          : $pack           - Name of this package
112
#                   @vars           - User Config Options
113
#                   Config Options:
114
#                       :name=xxxx
279 dpurdie 115
#                       :function=xxxx
7307 dpurdie 116
#                       :prefix=xxxx
7308 dpurdie 117
#                       :indent=nn/xxx
118
#                       :offset=nn/xxx
227 dpurdie 119
#                       :quiet=xxx
120
#                       :debug=xxx
121
#                       :verbose=xxx
122
#                       :delay_exit=xxx
123
#
124
# Returns         : 
125
#
126
sub import {
127
    my $pack = shift;
128
    my @vars;
129
    my @config;
130
 
131
    #
132
    #   Extract options of the form: :name=value and pass them to the
133
    #   ErrorConfig function. All other arguments will be passed to the
134
    #
135
    foreach ( @_ )
136
    {
137
        if ( m/^:(.+)=(.+)/ ) {
138
            push @config, $1, $2;
139
        } else {
140
            push @vars, $_;
141
        }
142
    }
143
 
144
    ErrorConfig( @config )
145
        if ( @config );
146
 
147
    #
148
    #   Invoke Exporter function to handle the arguments that I don't understand
149
    #
150
    $pack->export_to_level(1, $pack , @vars);
151
}
152
 
153
#-------------------------------------------------------------------------------
154
# Function        : ErrorConfig
155
#
156
# Description     : Configure aspects of the JATS error handle
279 dpurdie 157
#                   See ErrorReConfig
227 dpurdie 158
#
159
# Inputs          : A hash of option,value pairs
160
#                   Valid options
161
#                       name        - Name to report in error
279 dpurdie 162
#                       function    - Name of enclosing function
7308 dpurdie 163
#                       prefix      - 1st line prefix
227 dpurdie 164
#                       verbose     - vebosity level
165
#                       debug       - debug level
166
#                       on_exit     - Register on-exit function
167
#                       delay_exit  - Delay exit on error
7307 dpurdie 168
#                       prefix      - Optional prefix. First line only
7308 dpurdie 169
#                       indent      - Optional. All lines (number or text)
170
#                       offset      - Optional. Extra lines (number or text)
227 dpurdie 171
#
172
# Returns         :
173
#
174
sub ErrorConfig
175
{
176
    my %args = @_;
177
 
178
    while (my($key, $value) = each %args)
179
    {
7307 dpurdie 180
        if ( $key =~ /^name/ ) {
279 dpurdie 181
            $EName = $value;
227 dpurdie 182
 
279 dpurdie 183
        } elsif ( $key =~ /^function/ ) {
184
            $EFn = ':' . $value;
185
 
7307 dpurdie 186
        } elsif ( $key =~ /^prefix/ ) {
187
            $ElPrefix = $value;
188
 
7308 dpurdie 189
        } elsif ( $key =~ /^indent/ ) {
190
            my $pad = $value;
191
            if ($pad =~ m~^\d+$~) {
192
                $pad = ' ' x $value;
193
            }
194
            $EIndent = $EIndent . $pad;
195
 
196
        } elsif ( $key =~ /^offset/ ) {
197
            $EOffset = $value;
198
            if ($EOffset =~ m~^\d+$~) {
199
                $EOffset = ' ' x $value;
200
            }
201
 
227 dpurdie 202
        } elsif ( $key =~ /^debug/ ) {
279 dpurdie 203
            $::ScmDebug = $value
204
                if ( defined $value && $value > $::ScmDebug  );
227 dpurdie 205
 
206
        } elsif ( $key =~ /^verbose/ ) {
279 dpurdie 207
            $::ScmVerbose = $value
208
                if ( defined $value && $value > $::ScmVerbose  );
227 dpurdie 209
 
210
        } elsif ( $key =~ /^quiet/ ) {
211
            $::ScmQuiet = $value || 0;
212
 
213
        } elsif ( $key =~ /^on_exit/ ) {
214
            $ScmOnExit = $value;
215
 
216
        } elsif ( $key =~ /^delay_exit/ ) {
217
            $ScmDelayExit = $value;
218
 
2429 dpurdie 219
        } elsif ( $key =~ /^exitCode/i ) {
220
            $ScmExitCode = $value || 1;
221
 
227 dpurdie 222
        } else {
223
            Error("ErrorConfig, Unknown option: $key");
224
        }
225
    }
226
 
227
    #
279 dpurdie 228
    #   Calculate the prefix to all messages
229
    #   Based on Name and Function( if provided
230
    #
7308 dpurdie 231
    $EPrefix = "[$EName$EFn] " if $EName;
279 dpurdie 232
 
233
    #
227 dpurdie 234
    #   Extract program specfic debug flags from the environment
235
    #   These will be based on the reporting 'name'
236
    #       GBE_name_DEBUG
237
    #       GBE_name_VERBOSE
238
    #
279 dpurdie 239
    if ( $EName )
227 dpurdie 240
    {
241
        my ($value, $tag);
242
 
279 dpurdie 243
        $tag = "GBE_${EName}_DEBUG" ;
261 dpurdie 244
        $tag =~ s~\s+~~g;
227 dpurdie 245
        $value = $ENV{ $tag };
246
        if (defined $value)
247
        {
248
            $::ScmDebug = $value;
249
            Warning("Envar: $tag setting debug: $value");
250
        }
251
 
279 dpurdie 252
        $tag = "GBE_${EName}_VERBOSE" ;
261 dpurdie 253
        $tag =~ s~\s+~~g;
227 dpurdie 254
        $value = $ENV{ $tag };
255
        if (defined $value)
256
        {
257
            $::ScmVerbose = $value;
258
            Warning("Envar: $tag setting verbose: $value");
259
        }
260
    }
261
 
262
    #
263
    #   Sanitise quiet and verbose
264
    #   Any verboseness disables quiet
265
    #
266
    $::ScmQuiet = 0 if ( $::ScmVerbose );
267
    $::ScmQuiet = 0 if ( $::ScmDebug );
268
}
269
 
279 dpurdie 270
#-------------------------------------------------------------------------------
271
# Function        : ErrorReConfig
272
#
273
# Description     : Similar to ErrorConfig , except it is used to push and
274
#                   automatically pop the current state
275
#
276
#                   Intended to be used to control error reporting
277
#                   within a function. Let the class go out of scope
278
#                   at the end of the function.
279
#
280
#                   Not intended that the user hold and pass around the
281
#                   class ref as this may confuse all.
282
#
283
# Inputs          : As for ErrorConfig
284
#
285
# Returns         : Ref to a class
286
#                   When this goes out of scope the Error State will be
287
#                   restored.
288
#
289
sub ErrorReConfig
290
{
291
    #
292
    #   Create a small class to hold existing Error Information
293
    #   The error information will be restored when the handle returned to
294
    #   the user goes out of scope.
295
    #
296
    my $self;
227 dpurdie 297
 
7307 dpurdie 298
    $self->{EPrefix}        =  $EPrefix;
299
    $self->{ElPrefix}       =  $ElPrefix;
7308 dpurdie 300
    $self->{EIndent}        =  $EIndent;
301
    $self->{EOffset}        =  $EOffset;
279 dpurdie 302
    $self->{ScmVerbose}     =  $::ScmVerbose;
303
    $self->{ScmDebug}       =  $::ScmDebug;
304
    $self->{ScmQuiet}       =  $::ScmQuiet;
305
    $self->{ScmOnExit}      =  $ScmOnExit;
306
    $self->{ScmDelayExit}   =  $ScmDelayExit;
307
    $self->{ScmErrorCount}  =  $ScmErrorCount;
308
    $self->{ScmExitCode}    =  $ScmExitCode;
309
    $self->{EName}          =  $EName;
310
    $self->{EFn}            =  $EFn;
311
 
312
    bless ($self, __PACKAGE__);
313
 
314
    #
315
    #   Invoke ErrorConfig to do the hard work
316
    #
317
    ErrorConfig (@_);
318
 
319
    #
320
    #   Return ref to stored data
321
    #
322
    return $self;
323
 
324
}
325
 
227 dpurdie 326
#-------------------------------------------------------------------------------
279 dpurdie 327
# Function        : DESTROY
328
#
7307 dpurdie 329
# Description     : Called when the handle returned by ErrorConfig goes out of
279 dpurdie 330
#                   scope.
331
#
332
#                   Restores the state of the Error Reporting information
333
#
334
# Inputs          : $self               - Created by ErrorReConfig
335
#
336
# Returns         : Nothing
337
#
338
 
339
sub DESTROY
340
{
341
    my ($self) = @_;
7307 dpurdie 342
    $EPrefix          = $self->{EPrefix};
343
    $ElPrefix         = $self->{ElPrefix};
7308 dpurdie 344
    $EIndent          = $self->{EIndent};
345
    $EOffset          = $self->{EOffset};
279 dpurdie 346
    $::ScmVerbose     = $self->{ScmVerbose};
347
    $::ScmDebug       = $self->{ScmDebug};
348
    $::ScmQuiet       = $self->{ScmQuiet};
349
    $ScmOnExit        = $self->{ScmOnExit};
350
    $ScmDelayExit     = $self->{ScmDelayExit};
351
    $ScmErrorCount    = $self->{ScmErrorCount};
352
    $ScmExitCode      = $self->{ScmExitCode};
353
    $EFn              = $self->{EFn};
354
    $EName            = $self->{EName};
355
}
356
 
357
 
358
#-------------------------------------------------------------------------------
227 dpurdie 359
# Function        : Information
360
#                   Message
361
#                   Question
362
#                   Warning
7320 dpurdie 363
#                   WarnError
227 dpurdie 364
#                   Error
365
#                   Verbose
366
#                   Debug
367
#                   _Message ( Internal use only )
368
#
369
# Description     : Error, Warning and Message routines
370
#                   These routines will display a message to the user
371
#                   with the module name.
372
#
373
#                   Multiple arguments are displayed on their own line
374
#                   with suitable spacing.
375
#
376
# Inputs          : Lines of text to display
377
#
378
# Returns         : Nothing
379
#
380
sub _Message
381
{
382
    my $tag = shift;                # First argument is a tag
383
    my $count = 0;
384
 
385
    #
386
    #   Generate the message prefix
387
    #   This will only be used on the first line
388
    #   All other lines will have a space filled prefix
389
    #
7308 dpurdie 390
    my $prefix = $EPrefix . $tag. $EIndent;
227 dpurdie 391
    #
392
    #   Kill the eol if the Question is being asked
393
    #
394
    my $eol = ( $tag =~ m/Q/ ) ? "" : "\n";
7307 dpurdie 395
    foreach my $nextline ( @_ ) {
231 dpurdie 396
        next unless ( defined $nextline );              # Ignore undefined arguments
227 dpurdie 397
        chomp( my $line = $nextline );
7307 dpurdie 398
        if ( $count == 0 ) {
399
            $line = ($ElPrefix || '') . $line;
400
 
401
        } elsif ( $count == 1 ) {
227 dpurdie 402
            my $bol = $eol ? "" : "\n";
7308 dpurdie 403
            $prefix = $bol . ' ' x length($prefix) . $EOffset;
227 dpurdie 404
        }
5109 dpurdie 405
        $count++;
227 dpurdie 406
 
7307 dpurdie 407
        if ($capturing && $tag =~ m/[MWEF]/) {
5109 dpurdie 408
            push @captured, "$prefix $line$eol" 
7307 dpurdie 409
 
410
        } else {
5109 dpurdie 411
            print "$prefix $line$eol";
412
        }
227 dpurdie 413
    }
414
}
415
 
416
#-------------------------------------------------------------------------------
417
# Function        : Information
418
#                   Information1
419
#
420
# Description     : Will display informational messages
421
#                   These are normal operational messages. These may be
422
#                   supressed through the use of QUIET options
423
#
424
# Inputs          : An array of strings to display
425
#
426
sub Information
427
{
428
    _Message '(I)', @_ unless ( $::ScmQuiet);
429
}
430
 
431
sub Information1
432
{
433
    _Message '(I)', @_ unless ( $::ScmQuiet > 1);
434
}
435
 
436
 
437
#-------------------------------------------------------------------------------
438
# Function        : Message
439
#                   Message1
440
#
441
# Description     : Same as Information, except a different prefix
442
#
443
# Inputs          : An array of strings to display
444
#
445
sub Message
446
{
447
    _Message '(M)', @_ unless ( $::ScmQuiet > 1);
448
}
449
 
450
sub Message1
451
{
452
    _Message '(M)', @_ unless ( $::ScmQuiet);
453
}
454
 
455
#-------------------------------------------------------------------------------
456
# Function        : IsQuiet
457
#
458
# Description     : Determine if an Infrmation or Message will be displayed
459
#                   May be used to reduce excessive processing that may be
460
#                   discarded
461
#
462
# Inputs          : $level      - Level to test
463
#
464
# Returns         : TRUE:       A Message at $level would be displayed
465
#
466
sub IsQuiet
467
{
468
    my( $level) = @_;
469
    return $::ScmQuiet >= $level;
470
}
471
 
472
#-------------------------------------------------------------------------------
473
# Function        : Warning
474
#
475
# Description     : Display a warning message
476
#                   These may be disabled with a high quiet level
477
#
478
# Inputs          : An array of strings to display
479
#
480
sub Warning
481
{
482
    _Message '(W)', @_ unless ( $::ScmQuiet > 2);
483
}
484
 
485
#-------------------------------------------------------------------------------
7320 dpurdie 486
# Function        : WarnError 
487
#
488
# Description     : Display a warnign or an error based onthe first argument
489
#
490
# Inputs          : Mode    - True. Error, False warn
491
#                   ...     - Fed to warn or error
492
#
493
# Returns         : May not return 
494
#
495
sub WarnError
496
{
497
    my $mode = shift;
498
    if ($mode) {
499
        Error (@_);
500
    }
501
    Warning(@_);
502
}
503
 
504
 
505
#-------------------------------------------------------------------------------
227 dpurdie 506
# Function        : Question
507
#
508
# Description     : Display a Question message
509
#                   These cannot be disabled
510
#
511
# Inputs          : An array of strings to display
512
#
513
sub Question
514
{
515
    _Message '(Q)', @_;
516
    STDERR->flush;              # Force output to be displayed
517
    STDOUT->flush;              # Force output to be displayed
518
}
519
 
520
#-------------------------------------------------------------------------------
261 dpurdie 521
# Function        : Fatal
522
#
523
# Description     : Display a multi line fatal message
524
#                   This will cause the program to exit.
525
#
526
#                   Similar to Error(), except
527
#                       Display a (F) prefix
528
#                       Alters the exit code to "2"
529
#                       Will terminate program execution.
530
#                       Will not honor delayed exit configuration.
531
#
532
#                   Fatal is to be used to indicate to consumer processes that
533
#                   the error is a function of the infrastructure and cannot be
534
#                   corrected by a user. ie:
535
#                       clearcase is not available
536
#                           Not just a bad user parameter
537
#                       dpkg_archive is not available
538
#                       release manager database is not available
539
#
540
#                   Intended to be used by build deamons to determine if building
541
#                   should continue, or if the entire build process should be
542
#                   terminated.
543
#
544
# Inputs          : An array of strings to display
545
#
546
# Returns         : May not return
547
#
548
sub Fatal
549
{
550
    _Message '(F)', @_;
551
    $ScmErrorCount++;
552
    $ScmExitCode = 2;
553
    ErrorDoExit() unless ( $ScmDelayExit );
554
}
555
 
556
#-------------------------------------------------------------------------------
227 dpurdie 557
# Function        : Error
558
#
261 dpurdie 559
# Description     : Display a multi line error message
227 dpurdie 560
#                   This may cause the program to exit, or the user may have
561
#                   configured the package to accumulate error messages
562
#
563
#                   This could be used to generate multiple error messages
564
#                   while parsing a file, and then terminate program execution at
565
#                   the end of the phase.
566
#
567
# Inputs          : An array of strings to display
2429 dpurdie 568
#                   First entry May be an exist code of the form
569
#                       ExitCode=nnn
227 dpurdie 570
#
571
# Returns         : May not return
572
#
573
 
574
sub Error
575
{
2429 dpurdie 576
    if ( $_[0] =~ m~^ExitCode=(\d+)$~i )
577
    {
578
        $ScmExitCode = $1 || 1;
579
        shift @_;
580
    }
227 dpurdie 581
    _Message '(E)', @_;
582
    $ScmErrorCount++;
7320 dpurdie 583
    #DebugTraceBack();
227 dpurdie 584
    ErrorDoExit() unless ( $ScmDelayExit );
585
}
586
 
587
#-------------------------------------------------------------------------------
588
# Function        : ReportError
589
#
590
# Description     : Like Error, but the error exit is delayed
591
#
592
# Inputs          : An array of strings to display
593
#
594
sub ReportError
595
{
596
    _Message '(E)', @_;
597
    $ScmErrorCount++;
598
}
599
 
600
#-------------------------------------------------------------------------------
601
# Function        : ErrorDoExit
602
#
603
# Description     : Will terminate the program if delayed error messages
604
#                   have been seen.
605
#
606
# Inputs          : None
607
#
608
# Returns         : Will return if no errors have been reported
609
#
610
 
611
sub ErrorDoExit
612
{
613
    if ( $ScmErrorCount )
614
    {
5109 dpurdie 615
        # If capturing, then force the captured messages to be displayed
616
        DumpCapture();
617
 
227 dpurdie 618
        #
619
        #   Prevent recusion.
620
        #   Kill error processing while doing error exit processing
621
        #
622
        if ( my $func = $ScmOnExit )
623
        {
624
            $ScmOnExit = undef;
283 dpurdie 625
            &$func( $ScmExitCode );
227 dpurdie 626
        }
261 dpurdie 627
        exit $ScmExitCode;
227 dpurdie 628
    }
629
}
630
 
631
#-------------------------------------------------------------------------------
263 dpurdie 632
# Function        : ArgsToString
633
#
634
# Description     : Convert an array of arguments to a string
635
#                   Main purpose is to allow Debug and Verbose
636
#                   calls to pass undef values without causing warnings
637
#
638
# Inputs          : REF to a list of scalar values
639
#                   Passing a REF is faster
640
#
641
# Returns         : A string
642
#
643
sub ArgsToString
644
{
645
    my $result = '';
646
 
647
    $result .= (defined ($_) ? $_ : '\'undef\'') . ' ' foreach  ( @{$_[0]} );
648
    return $result;
649
}
650
 
651
#-------------------------------------------------------------------------------
652
# Function        : Verbose0
653
#                   Verbose
227 dpurdie 654
#                   Verbose2
655
#                   Verbose3
656
#
657
# Description     : Various levels of progress reporting
658
#                   By default none are displayed
659
#
660
# Inputs          : A single line string
661
#                   Multi-line output is not supported
263 dpurdie 662
#                   Arguments will be processed such that undef is handled well
227 dpurdie 663
#
263 dpurdie 664
sub Verbose0
665
{
666
    _Message '------', ArgsToString (\@_);
667
}
227 dpurdie 668
sub Verbose
669
{
263 dpurdie 670
    _Message '(V)', ArgsToString (\@_) if ($::ScmVerbose);
227 dpurdie 671
}
672
 
673
sub Verbose2
674
{
263 dpurdie 675
    _Message '(V)', ArgsToString (\@_) if ($::ScmVerbose >= 2);
227 dpurdie 676
}
677
 
678
sub Verbose3
679
{
263 dpurdie 680
    _Message '(V)', ArgsToString (\@_) if ($::ScmVerbose >= 3);
227 dpurdie 681
}
682
 
683
sub IsVerbose
684
{
685
    my( $level) = @_;
686
    return $::ScmVerbose >= $level;
687
}
688
 
689
#-------------------------------------------------------------------------------
690
# Function        : Debug
691
#                   Debug0
692
#                   Debug1
693
#                   Debug2
694
#                   Debug3
695
#
696
# Description     : Various levels of debug reporting
697
#                   By default none are displayed
698
#
699
# Inputs          : A single line string
700
#                   Multi-line output is not supported
263 dpurdie 701
#                   Arguments will be processed such that undef is handled well
227 dpurdie 702
#
703
sub Debug0
704
{
263 dpurdie 705
    _Message '------', ArgsToString (\@_);
227 dpurdie 706
}
707
 
708
sub Debug
709
{
263 dpurdie 710
    _Message '(D)', ArgsToString (\@_) if ($::ScmDebug >= 1 ) ;
227 dpurdie 711
}
712
 
713
 
714
sub Debug2
715
{
263 dpurdie 716
    _Message '(D)', ArgsToString (\@_) if ($::ScmDebug >= 2) ;
227 dpurdie 717
}
718
 
719
 
720
sub Debug3
721
{
263 dpurdie 722
    _Message '(D)', ArgsToString (\@_) if ($::ScmDebug >= 3) ;
227 dpurdie 723
}
724
 
725
sub IsDebug
726
{
727
    my( $level) = @_;
728
    return $::ScmDebug >= $level;
729
}
730
 
731
#-------------------------------------------------------------------------------
5109 dpurdie 732
# Function        : StartCapture 
733
#
734
# Description     : Start capturing non-debug non-verbose messages 
735
#
736
# Inputs          : mode    - True: Start 
737
#
738
# Returns         : 
739
#
740
sub StartCapture
741
{
742
    my ($mode) = @_;
743
    $capturing = $mode;
744
}
745
 
746
#-------------------------------------------------------------------------------
747
# Function        : DumpCapture 
748
#
749
# Description     : Dump the captured output
750
#
751
# Inputs          : None
752
#
753
# Returns         : Nothing
754
#
755
sub DumpCapture
756
{
757
    foreach my $line ( @captured) {
758
        print $line;
759
    }
760
    @captured = ();
761
    $capturing = 0;
762
}
763
 
764
#-------------------------------------------------------------------------------
227 dpurdie 765
# Function        : DebugDumpData
766
#
767
# Description     : Dump a data structure
768
#
769
# Inputs          : $name           - A name to give the structure
770
#                   @refp           - An array of references
771
#
772
# Returns         :
773
#
774
sub DebugDumpData
775
{
776
    my ($name, @refp) = @_;
777
 
778
    my $ii = 0;
4085 dpurdie 779
    $Data::Dumper::Sortkeys = 1;
227 dpurdie 780
    foreach  ( @refp )
781
    {
782
        print Data::Dumper->Dump ( [$_], ["*[Arg:$ii] $name" ]);
783
        $ii++
784
    }
785
}
786
 
787
#-------------------------------------------------------------------------------
231 dpurdie 788
# Function        : DebugTraceBack
789
#
790
# Description     : Display the call stack
791
#
792
# Inputs          : $tag
793
#
794
# Returns         : Nothing
795
#
796
sub DebugTraceBack
797
{
798
    my ($tag) = @_;
799
    $tag = 'TraceBack' unless ( $tag );
800
 
801
    #
802
    #   Limit the stack stace.
803
    #   It can't go on forever
804
    #
805
    foreach my $ii ( 0 .. 20 )
806
    {
807
         my ($package, $filename, $line) = caller($ii);
808
         last unless ( $package );
809
         print "$tag: $ii: $package, $filename, $line\n";
810
    }
811
}
812
 
813
#-------------------------------------------------------------------------------
227 dpurdie 814
# Function        : DebugPush
815
#
816
# Description     : Save the current debug level and then use a new name and
817
#                   debug level for future reporting
818
#
279 dpurdie 819
#                   Provided for backward compatability
820
#                   Preferred solution is ErrorReConfig
821
#
822
# Inputs          : $name       - New program name
227 dpurdie 823
#                   $level      - New program debug level
824
#
825
# Returns         : Current debug level
826
#
827
 
828
my @DebugStack = ();
829
sub DebugPush
830
{
831
    my ($name, $new_level) = @_;
279 dpurdie 832
    my %args;
227 dpurdie 833
 
279 dpurdie 834
    #
835
    #   Save current state on a stack
836
    #
837
    my $estate = ErrorReConfig ();
838
    push @DebugStack, $estate;
839
 
227 dpurdie 840
    $::ScmDebug = $new_level if ( defined $new_level && $new_level );
7307 dpurdie 841
    $EPrefix =   $name        if ( defined $name && $name );
227 dpurdie 842
 
843
    return $::ScmDebug;
844
}
845
 
846
#-------------------------------------------------------------------------------
847
# Function        : DebugPop
848
#
849
# Description     : Restores the operation of the DebugPush
850
#
851
# Inputs          : None
852
#
853
sub DebugPop
854
{
279 dpurdie 855
    pop @DebugStack;
227 dpurdie 856
}
857
 
858
#-------------------------------------------------------------------------------
859
# Function        : DebugDumpPackage
860
#
861
# Description     : Dump data within the scope of a given package
862
#
863
# Inputs          : $packageName            - To dump
864
#
865
# Returns         : 
866
#
867
 
868
sub DebugDumpPackage
869
{
870
    no strict "vars";
871
    no strict "refs";
872
    my ($packageName) = @_;
873
    print "==DebugDumpPackage: $packageName =============================\n";
874
 
875
    local $Data::Dumper::Pad = "\t ";
876
    local $Data::Dumper::Maxdepth = 2;
877
    local $Data::Dumper::Indent  = 1;
878
 
879
    # We want to get access to the stash corresponding to the package name
880
 
881
    *stash = *{"${packageName}::"};  # Now %stash is the symbol table
7300 dpurdie 882
 
227 dpurdie 883
    # Iterate through the symbol table, which contains glob values
884
    # indexed by symbol names.
7300 dpurdie 885
 
886
    foreach my $varName ( sort keys %stash)
227 dpurdie 887
    {
7300 dpurdie 888
        my $globValue = $stash{$varName};
227 dpurdie 889
        print "$varName =============================\n";
890
        next if ( $varName eq 'stash' );
283 dpurdie 891
        local *alias = $globValue;
227 dpurdie 892
        if (defined ($alias)) {
893
            print Data::Dumper->Dump ( [$alias], ["*$varName" ]);
894
#            print "\t \$$varName $alias \n";
895
        } 
369 dpurdie 896
        if (@alias) {
227 dpurdie 897
            print Data::Dumper->Dump ( [\@alias], ["*$varName" ]);
898
#            print "\t \@$varName @alias \n";
899
        } 
369 dpurdie 900
        if (%alias) {
227 dpurdie 901
            print Data::Dumper->Dump ( [\%alias], ["*$varName" ]);
902
#            print "\t \%$varName ",%alias," \n";
903
        }
904
        if (defined (&alias)) {
905
#            print Data::Dumper->Dump ( [\&alias], ["*$varName" ]);
906
            print "\t \&$varName ","Code Fragment"," \n";
907
        }
908
     }
909
}
910
 
911
#
912
#
913
1;