This file is indexed.

/usr/share/perl5/ZoneMinder/Logger.pm is in zoneminder 1.25.0-4.

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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# ==========================================================================
#
# ZoneMinder Logger Module, $Date: 2011-06-28 12:06:02 +0100 (Tue, 28 Jun 2011) $, $Revision: 3473 $
# Copyright (C) 2001-2008  Philip Coombes
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# ==========================================================================
#
# This module contains the debug definitions and functions used by the rest 
# of the ZoneMinder scripts
#
package ZoneMinder::Logger;

use 5.006;
use strict;
use warnings;

require Exporter;
require ZoneMinder::Base;

our @ISA = qw(Exporter ZoneMinder::Base);

# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.

# This allows declaration   use ZoneMinder ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = (
    'constants' => [ qw(
        DEBUG
        INFO
        WARNING
        ERROR
        FATAL
        PANIC
        NOLOG
    ) ],
    'functions' => [ qw(
        logInit
        logReinit
        logTerm
        logSetSignal
        logClearSignal
        logDebugging
        logLevel
        logTermLevel
        logDatabaseLevel
        logFileLevel
        logSyslogLevel
        Mark
        Dump
        Debug
        Info
        Warning
        Error
        Fatal
        Panic
    ) ]
);
push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS;

our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );

our @EXPORT = qw();

our $VERSION = $ZoneMinder::Base::VERSION;

# ==========================================================================
#
# Logger Facilities
#
# ==========================================================================

use ZoneMinder::Config qw(:all);

use DBI;
use Carp;
use POSIX;
use IO::Handle;
use Data::Dumper;
use Time::HiRes qw/gettimeofday/;
use Sys::Syslog;

use constant {
    DEBUG => 1,
    INFO => 0,
    WARNING => -1,
    ERROR => -2,
    FATAL => -3,
    PANIC => -4,
    NOLOG => -5
};

our %codes = (
    &DEBUG => "DBG",
    &INFO => "INF",
    &WARNING => "WAR",
    &ERROR => "ERR",
    &FATAL => "FAT",
    &PANIC => "PNC",
    &NOLOG => "OFF"
);

our %priorities = (
    &DEBUG => "debug",
    &INFO => "info",
    &WARNING => "warning",
    &ERROR => "err",
    &FATAL => "err",
    &PANIC => "err"
);

our $logger;

sub new
{
    my $class = shift;
    my $this = {};

    $this->{initialised} = undef;

    #$this->{id} = "zmundef";
    ( $this->{id} ) = $0 =~ m|^(?:.*/)?([^/]+?)(?:\.[^/.]+)?$|;
    $this->{idRoot} = $this->{id};
    $this->{idArgs} = "";

    $this->{level} = INFO;
    $this->{termLevel} = NOLOG;
    $this->{databaseLevel} = NOLOG;
    $this->{fileLevel} = NOLOG;
    $this->{syslogLevel} = NOLOG;
    $this->{effectiveLevel} = INFO;

    $this->{autoFlush} = 1;
    $this->{hasTerm} = -t STDERR;

    ( $this->{fileName} = $0 ) =~ s|^.*/||;
    $this->{logPath} = ZM_PATH_LOGS;
    $this->{logFile} = $this->{logPath}."/".$this->{id}.".log";

    $this->{trace} = 0;

    bless( $this, $class );
    return $this;
}

sub BEGIN
{
    # Fake the config variables that are used in case they are not defined yet
    # Only really necessary to support upgrade from previous version
    if ( !eval('defined(ZM_LOG_DEBUG)') )
    {
        no strict 'subs';
        no strict 'refs';
        my %dbgConfig = (
            ZM_LOG_LEVEL_DATABASE => 0,
            ZM_LOG_LEVEL_FILE => 0,
            ZM_LOG_LEVEL_SYSLOG => 0,
            ZM_LOG_DEBUG => 0,
            ZM_LOG_DEBUG_TARGET => "",
            ZM_LOG_DEBUG_LEVEL => 1,
            ZM_LOG_DEBUG_FILE => "" 
        );
        while ( my ( $name, $value ) = each( %dbgConfig ) )
        {
            *{$name} = sub { $value };
        }
        use strict 'subs';
        use strict 'refs';
    }
}

sub DESTROY
{
    my $this = shift;
    $this->terminate();
}

sub initialise( @ )
{
    my $this = shift;
    my %options = @_;

    $this->{id} = $options{id} if ( defined($options{id}) );

    $this->{logPath} = $options{logPath} if ( defined($options{logPath}) );

    my $tempLogFile;
    $tempLogFile = $this->{logPath}."/".$this->{id}.".log";
    $tempLogFile = $options{logFile} if ( defined($options{logFile}) );
    if ( my $logFile = $this->getTargettedEnv('LOG_FILE') )
    {
        $tempLogFile = $logFile;
    }

    my $tempLevel = INFO;
    my $tempTermLevel = $this->{termLevel};
    my $tempDatabaseLevel = $this->{databaseLevel};
    my $tempFileLevel = $this->{fileLevel};
    my $tempSyslogLevel = $this->{syslogLevel};

    $tempTermLevel = $options{termLevel} if ( defined($options{termLevel}) );
    if ( defined($options{databaseLevel}) )
    {
        $tempDatabaseLevel = $options{databaseLevel};
    }
    else
    {
        $tempDatabaseLevel = ZM_LOG_LEVEL_DATABASE;
    }
    if ( defined($options{fileLevel}) )
    {
        $tempFileLevel = $options{fileLevel};
    }
    else
    {
        $tempFileLevel = ZM_LOG_LEVEL_FILE;
    }
    if ( defined($options{syslogLevel}) )
    {
        $tempSyslogLevel = $options{syslogLevel};
    }
    else
    {
        $tempSyslogLevel = ZM_LOG_LEVEL_SYSLOG;
    }

    if ( defined($ENV{'LOG_PRINT'}) )
    {
        $tempTermLevel = $ENV{'LOG_PRINT'}? DEBUG : NOLOG;
    }

    my $level;
    $tempLevel = $level if ( defined($level = $this->getTargettedEnv('LOG_LEVEL')) );

    $tempTermLevel = $level if ( defined($level = $this->getTargettedEnv('LOG_LEVEL_TERM')) );
    $tempDatabaseLevel = $level if ( defined($level = $this->getTargettedEnv('LOG_LEVEL_DATABASE')) );
    $tempFileLevel = $level if ( defined($level = $this->getTargettedEnv('LOG_LEVEL_FILE')) );
    $tempSyslogLevel = $level if ( defined($level = $this->getTargettedEnv('LOG_LEVEL_SYSLOG')) );

    if ( ZM_LOG_DEBUG )
    {
        foreach my $target ( split( /\|/, ZM_LOG_DEBUG_TARGET ) )
        {
            if ( $target eq $this->{id} || $target eq "_".$this->{id} || $target eq $this->{idRoot} || $target eq "_".$this->{idRoot} || $target eq "" )
            {
                if ( ZM_LOG_DEBUG_LEVEL > NOLOG )
                {
                    $tempLevel = $this->limit( ZM_LOG_DEBUG_LEVEL );
                    if ( ZM_LOG_DEBUG_FILE ne "" )
                    {
                        $tempLogFile = ZM_LOG_DEBUG_FILE;
                        $tempFileLevel = $tempLevel;
                    }
                }
            }
        }
    }

    $this->logFile( $tempLogFile );

    $this->termLevel( $tempTermLevel );
    $this->databaseLevel( $tempDatabaseLevel );
    $this->fileLevel( $tempFileLevel );
    $this->syslogLevel( $tempSyslogLevel );

    $this->level( $tempLevel );

    $this->{trace} = $options{trace} if ( defined($options{trace}) );

    $this->{autoFlush} = $ENV{'LOG_FLUSH'}?1:0 if ( defined($ENV{'LOG_FLUSH'}) );

    $this->{initialised} = !undef;
    
    Debug( "LogOpts: level=".$codes{$this->{level}}."/".$codes{$this->{effectiveLevel}}.", screen=".$codes{$this->{termLevel}}.", database=".$codes{$this->{databaseLevel}}.", logfile=".$codes{$this->{fileLevel}}."->".$this->{logFile}.", syslog=".$codes{$this->{syslogLevel}} );
}

sub terminate()
{
    my $this = shift;
    return unless ( $this->{initialised} );
    $this->syslogLevel( NOLOG );
    $this->fileLevel( NOLOG );
    $this->databaseLevel( NOLOG );
    $this->termLevel( NOLOG );
}

sub reinitialise()
{
    my $this = shift;

    return unless ( $this->{initialised} );

    # Bit of a nasty hack to reopen connections to log files and the DB
    my $syslogLevel = $this->syslogLevel();
    $this->syslogLevel( NOLOG );
    my $logfileLevel = $this->fileLevel();
    $this->fileLevel( NOLOG );
    my $databaseLevel = $this->databaseLevel();
    $this->databaseLevel( NOLOG );
    my $screenLevel = $this->termLevel();
    $this->termLevel( NOLOG );

    $this->syslogLevel( $syslogLevel ) if ( $syslogLevel > NOLOG );
    $this->fileLevel( $logfileLevel ) if ( $logfileLevel > NOLOG );
    $this->databaseLevel( $databaseLevel ) if ( $databaseLevel > NOLOG );
    $this->databaseLevel( $databaseLevel ) if ( $databaseLevel > NOLOG );
}

sub limit( $ )
{
    my $this = shift;
    my $level = shift;
    return( DEBUG ) if ( $level > DEBUG );
    return( NOLOG ) if ( $level < NOLOG );
    return( $level );
}

sub getTargettedEnv( $ )
{
    my $this = shift;
    my $name = shift;
    my $envName = $name."_".$this->{id};
    my $value;
    $value = $ENV{$envName} if ( defined($ENV{$envName}) );
    if ( !defined($value) && $this->{id} ne $this->{idRoot} )
    {
        $envName = $name."_".$this->{idRoot};
        $value = $ENV{$envName} if ( defined($ENV{$envName}) );
    }
    if ( !defined($value) )
    {
        $value = $ENV{$name} if ( defined($ENV{$name}) );
    }
    if ( defined($value) )
    {
        ( $value ) = $value =~ m/(.*)/;
    }
    return( $value );
}

sub fetch()
{
    if ( !$logger )
    {
        $logger = ZoneMinder::Logger->new();
        $logger->initialise( 'syslogLevel'=>INFO, 'databaseLevel'=>INFO );
    }
    return( $logger );
}

sub id( ;$ )
{
    my $this = shift;
    my $id = shift;
    if ( defined($id) && $this->{id} ne $id )
    {
        # Remove whitespace
        $id =~ s/\S//g;
        # Replace non-alphanum with underscore
        $id =~ s/[^a-zA-Z_]/_/g;

        if ( $this->{id} ne $id )
        {
            $this->{id} = $this->{idRoot} = $id;
            if ( $id =~ /^([^_]+)_(.+)$/ )
            {
                $this->{idRoot} = $1;
                $this->{idArgs} = $2;
            }
        }
    }
    return( $this->{id} );
}

sub level( ;$ )
{
    my $this = shift;
    my $level = shift;
    if ( defined($level) )
    {
        $this->{level} = $this->limit( $level );
        $this->{effectiveLevel} = NOLOG;
        $this->{effectiveLevel} = $this->{termLevel} if ( $this->{termLevel} > $this->{effectiveLevel} );
        $this->{effectiveLevel} = $this->{databaseLevel} if ( $this->{databaseLevel} > $this->{effectiveLevel} );
        $this->{effectiveLevel} = $this->{fileLevel} if ( $this->{fileLevel} > $this->{effectiveLevel} );
        $this->{effectiveLevel} = $this->{syslogLevel} if ( $this->{syslogLevel} > $this->{level} );
        $this->{effectiveLevel} = $this->{level} if ( $this->{effectiveLevel} > $this->{level} );
    }
    return( $this->{level} );
}

sub debugOn()
{
    my $this = shift;
    return( $this->{effectiveLevel} >= DEBUG );
}

sub trace( ;$ )
{
    my $this = shift;
    $this->{trace} = $_[0] if ( @_ );
    return( $this->{trace} );
}

sub termLevel( ;$ )
{
    my $this = shift;
    my $termLevel = shift;
    if ( defined($termLevel) )
    {
        $termLevel = NOLOG if ( !$this->{hasTerm} );
        $termLevel = $this->limit( $termLevel );
        if ( $this->{termLevel} != $termLevel )
        {
            $this->{termLevel} = $termLevel;
        }
    }
    return( $this->{termLevel} );
}

sub databaseLevel( ;$ )
{
    my $this = shift;
    my $databaseLevel = shift;
    if ( defined($databaseLevel) )
    {
        $databaseLevel = $this->limit( $databaseLevel );
        if ( $this->{databaseLevel} != $databaseLevel )
        {
            if ( $databaseLevel > NOLOG && $this->{databaseLevel} <= NOLOG )
            {
                if ( !$this->{dbh} )
                {
                    my ( $host, $port ) = ( ZM_DB_HOST =~ /^([^:]+)(?::(.+))?$/ );

                    if ( defined($port) )
                    {
                        $this->{dbh} = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".$host.";port=".$port, ZM_DB_USER, ZM_DB_PASS );
                    }
                    else
                    {
                        $this->{dbh} = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_HOST, ZM_DB_USER, ZM_DB_PASS );
                    }
                    if ( !$this->{dbh} )
                    {
                        $databaseLevel = NOLOG;
                        Error( "Unable to write log entries to DB, can't connect to database '".ZM_DB_NAME."' on host '".ZM_DB_HOST."'" );
                    }
                    else
                    {
                        $this->{dbh}->{AutoCommit} = 1;
                        Fatal( "Can't set AutoCommit on in database connection" ) unless( $this->{dbh}->{AutoCommit} );
                        $this->{dbh}->{mysql_auto_reconnect} = 1;
                        Fatal( "Can't set mysql_auto_reconnect on in database connection" ) unless( $this->{dbh}->{mysql_auto_reconnect} );
                        $this->{dbh}->trace( 0 );
                    }
                }
            }
            elsif ( $databaseLevel <= NOLOG && $this->{databaseLevel} > NOLOG )
            {
                if ( $this->{dbh} )
                {
                    $this->{dbh}->disconnect();
                    undef($this->{dbh});
                }
            }
            $this->{databaseLevel} = $databaseLevel;
        }
    }
    return( $this->{databaseLevel} );
}

sub fileLevel( ;$ )
{
    my $this = shift;
    my $fileLevel = shift;
    if ( defined($fileLevel) )
    {
        $fileLevel = $this->limit($fileLevel);
        if ( $this->{fileLevel} != $fileLevel )
        {
            $this->closeFile() if ( $this->{fileLevel} > NOLOG );
            $this->{fileLevel} = $fileLevel;
            $this->openFile() if ( $this->{fileLevel} > NOLOG );
        }
    }
    return( $this->{fileLevel} );
}

sub syslogLevel( ;$ )
{
    my $this = shift;
    my $syslogLevel = shift;
    if ( defined($syslogLevel) )
    {
        $syslogLevel = $this->limit($syslogLevel);
        if ( $this->{syslogLevel} != $syslogLevel )
        {
            $this->closeSyslog() if ( $syslogLevel <= NOLOG && $this->{syslogLevel} > NOLOG );
            $this->openSyslog() if ( $syslogLevel > NOLOG && $this->{syslogLevel} <= NOLOG );
            $this->{syslogLevel} = $syslogLevel;
        }
    }
    return( $this->{syslogLevel} );
}

sub openSyslog()
{
    my $this = shift;
    openlog( $this->{id}, "pid", "local1" );
}

sub closeSyslog()
{
    my $this = shift;
    #closelog();
}

sub logFile( $ )
{
    my $this = shift;
    my $logFile = shift;
    if ( $logFile =~ /^(.+)\+$/ )
    {
        $this->{logFile} = $1.'.'.$$;
    }
    else
    {
        $this->{logFile} = $logFile;
    }
}

sub openFile()
{
    my $this = shift;
    if ( open( LOGFILE, ">>".$this->{logFile} ) )
    {
        LOGFILE->autoflush() if ( $this->{autoFlush} );

        my $webUid = (getpwnam( ZM_WEB_USER ))[2];
        my $webGid = (getgrnam( ZM_WEB_GROUP ))[2];
        if ( $> == 0 )
        {
            chown( $webUid, $webGid, $this->{logFile} ) or Fatal( "Can't change permissions on log file '".$this->{logFile}."': $!" )
        }
    }
    else
    {
        $this->fileLevel( NOLOG );
        Error( "Can't open log file '".$this->{logFile}."': $!" );
    }
}

sub closeFile()
{
    my $this = shift;
    close( LOGFILE ) if ( fileno(LOGFILE) );
}

sub logPrint( $;$ )
{
    my $this = shift;
    my $level = shift;
    my $string = shift;

    if ( $level <= $this->{effectiveLevel} )
    {
        $string =~ s/[\r\n]+$//g;

        my $code = $codes{$level};

        my ($seconds, $microseconds) = gettimeofday();
        my $message = sprintf( "%s.%06d %s[%d].%s [%s]", strftime( "%x %H:%M:%S", localtime( $seconds ) ), $microseconds, $this->{id}, $$, $code, $string );
        if ( $this->{trace} )
        {
            $message = Carp::shortmess( $message );
        }
        else
        {
            $message = $message."\n";
        }
        syslog( $priorities{$level}, $code." [%s]", $string ) if ( $level <= $this->{syslogLevel} );
        print( LOGFILE $message ) if ( $level <= $this->{fileLevel} );
        if ( $level <= $this->{databaseLevel} )
        {
            my $sql = "insert into Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) values ( ?, ?, ?, ?, ?, ?, ?, NULL )";
            $this->{sth} = $this->{dbh}->prepare_cached( $sql );
            if ( !$this->{sth} )
            {
                $this->{databaseLevel} = NOLOG;
                Fatal( "Can't prepare log entry '$sql': ".$this->{dbh}->errstr() );
            }
            my $res = $this->{sth}->execute( $seconds+($microseconds/1000000.0), $this->{id}, $$, $level, $code, $string, $this->{fileName} );
            if ( !$res )
            {
                $this->{databaseLevel} = NOLOG;
                Fatal( "Can't execute log entry '$sql': ".$this->{sth}->errstr() );
            }
        }
        print( STDERR $message ) if ( $level <= $this->{termLevel} );
    }
}

sub logInit( ;@ )
{
    my %options = @_ ? @_ : ();
    $logger = ZoneMinder::Logger->new() if ( !$logger );
    $logger->initialise( %options );
}

sub logReinit()
{
    fetch()->reinitialise();
}

sub logTerm
{
    return unless ( $logger );
    $logger->terminate();
    $logger = undef;
}

sub logHupHandler()
{
    my $savedErrno = $!;
    return unless( $logger );
    fetch()->reinitialise();
    logSetSignal();
    $! = $savedErrno;
}

sub logSetSignal()
{
    $SIG{HUP} = \&logHupHandler;
}

sub logClearSignal()
{
    $SIG{HUP} = 'DEFAULT';
}

sub logLevel( ;$ )
{
    return( fetch()->level( @_ ) );
}

sub logDebugging()
{
    return( fetch()->debugOn() );
}

sub logTermLevel( ;$ )
{
    return( fetch()->termLevel( @_ ) );
}

sub logDatabaseLevel( ;$ )
{
    return( fetch()->databaseLevel( @_ ) );
}

sub logFileLevel( ;$ )
{
    return( fetch()->fileLevel( @_ ) );
}

sub logSyslogLevel( ;$ )
{
    return( fetch()->syslogLevel( @_ ) );
}

sub Mark( ;$$ )
{
    my $level = shift;
    $level = DEBUG unless( defined($level) );
    my $tag = "Mark";
    fetch()->logPrint( $level, $tag );
}

sub Dump( \$;$ )
{
    my $var = shift;
    my $label = shift;
    $label = "VAR" unless( defined($label) );
    fetch()->logPrint( DEBUG, Data::Dumper->Dump( [ $var ], [ $label ] ) );
}

sub Debug( @ )
{
    fetch()->logPrint( DEBUG, @_ );
}

sub Info( @ )
{
    fetch()->logPrint( INFO, @_ );
}

sub Warning( @ )
{
    fetch()->logPrint( WARNING, @_ );
}

sub Error( @ )
{
    fetch()->logPrint( ERROR, @_ );
}

sub Fatal( @ )
{
    fetch()->logPrint( FATAL, @_ );
    exit( -1 );
}

sub Panic( @ )
{
    fetch()->logPrint( PANIC, @_ );
    confess( $_[0] );
}

1;
__END__

=head1 NAME

ZoneMinder::Logger - ZoneMinder Logger module

=head1 SYNOPSIS

  use ZoneMinder::Logger;
  use ZoneMinder::Logger qw(:all);

  logInit( "myproc", DEBUG );

  Debug( "This is what is happening" );
  Info( "Something interesting is happening" );
  Warning( "Something might be going wrong." );
  Error( "Something has gone wrong!!" );
  Fatal( "Something has gone badly wrong, gotta stop!!" );
  Panic( "Something fundamental has gone wrong, die with stack trace );

=head1 DESCRIPTION

The ZoneMinder:Logger module contains the common debug and error reporting routines used by the ZoneMinder scripts.

To use debug in your scripts you need to include this module, and call logInit. Thereafter you can sprinkle Debug or Error calls etc throughout the code safe in the knowledge that they will be reported to your error log, and possibly the syslogger, in a meaningful and consistent format.

Debug is discussed in terms of levels where 1 and above (currently only 1 for scripts) is considered debug, 0 is considered as informational, -1 is a warning, -2 is an error and -3 is a fatal error or panic. Where levels are mentioned below as thresholds the value given and anything with a lower level (ie. more serious) will be included.

=head1 METHODS

=over 4

=item logInit ( $id, %options );

Initialises the debug and prepares the logging for forthcoming operations. If not called explicitly it will be called by the first debug call in your script, but with default (and probably meaningless) options. The only compulsory arguments are $id which must be a string that will identify debug coming from this script in mixed logs. Other options may be provided as below,

 Option        Default        Description
 ---------     ---------      -----------
 level         INFO       The initial debug level which defines which statements are output and which are ignored
 trace         0          Whether to use the Carp::shortmess format in debug statements to identify where the debug was emitted from
 termLevel     NOLOG      At what level debug is written to terminal standard error, 0 is no, 1 is yes, 2 is write only if terminal
 databaseLevel INFO       At what level debug is written to the Log table in the database;
 fileLevel     NOLOG      At what level debug is written to a log file of the format of <id>.log in the standard log directory.
 syslogLevel   INFO       At what level debug is written to syslog.
 
To disable any of these action entirely set to NOLOG

=item logTerm ();

Used to end the debug session and close any logs etc. Not usually necessary.

=item $id            = logId ( [$id] );

=item $level         = logLevel ( [$level] );

=item $trace         = logTrace ( [$trace] );

=item $level         = logLevel ( [$level] );

=item $termLevel     = logTermLevel ( [$termLevel] );

=item $databaseLevel = logDatabaseLevel ( [$databaseLevel] );

=item $fileLevel     = logFileLevel ( [$fileLevel] );

=item $syslogLevel   = logSyslogLevel ( [$syslogLevel] );

These methods can be used to get and set the current settings as defined in logInit.

=item Debug( $string );

This method will output a debug message if the current debug level permits it, otherwise does nothing. This message will be tagged with the DBG string in the logs.

=item Info( $string );

This method will output an informational message if the current debug level permits it, otherwise does nothing. This message will be tagged with the INF string in the logs.

=item Warning( $string );

This method will output a warning message if the current debug level permits it, otherwise does nothing. This message will be tagged with the WAR string in the logs.

=item Error( $string );

This method will output an error message if the current debug level permits it, otherwise does nothing. This message will be tagged with the ERR string in the logs.

=item Fatal( $string );

This method will output a fatal error message and then die if the current debug level permits it, otherwise does nothing. This message will be tagged with the FAT string in the logs.

=item Panic( $string );

This method will output a panic error message and then die with a stack trace if the current debug level permits it, otherwise does nothing. This message will be tagged with the PNC string in the logs.

=head2 EXPORT

None by default.
The :constants tag will export the debug constants which define the various levels of debug
The :variables tag will export variables containing the current debug id and level
The :functions tag will export the debug functions. This or :all is what you would normally use.
The :all tag will export all above symbols.


=head1 SEE ALSO

Carp
Sys::Syslog

The ZoneMinder README file Troubleshooting section for an extended discussion on the use and configuration of syslog with ZoneMinder.

http://www.zoneminder.com

=head1 AUTHOR

Philip Coombes, E<lt>philip.coombes@zoneminder.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2001-2008  Philip Coombes

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.3 or,
at your option, any later version of Perl 5 you may have available.


=cut