#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Common::Process;
use strict;

# This class serves as the base class for all 'Process' objects.
# It provides the constructor, and the _report facility.

use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Log;
use Common::Util;

use constant kDefaultReportLevel => 2;

sub new {
    my ( $class, %args ) = @_;

    my $self = bless {}, $class;
    return $self->_init(%args);
}

sub _init {
    my ( $self, %args ) = @_;

    $self->{_logLevel} = Common::Util::returnFirstDefined( $args{logLevel}, kDefaultReportLevel );

    return $self;
}

sub _report {
    my ( $self, $msg, $level ) = @_;

    if ( $level <= $self->{_logLevel} ) {

        # If the 'msg' is a reference, we'll call Dumper on it.
        # This will allow you to write code like this:
        # $self->_report(\%someMassiveDataStructure, 4);
        # ... By doing that, you won't pay the (possibly considerable) cost of rendering
        #     the data structure unless you are running at the appropriate log level.
        #
        if ( ref($msg) ) {
            Common::Log::Print( Dumper($msg) );
        } else {
            Common::Log::Print($msg);
        }
    }
}
1;
