# Copyright 2008-2014 Cumulus Systems Incorporated.
# All Rights Reserved.

# This module provides a function to get appropriate instance of Perl logger module which caller can use to generate log messages. This function
# checks whether system has Perl modules installed for log4Perl logging module. If yes then it will return the Perl logger module which uses
# log4Perl module internally to generate log files. If log4Perl modules are not present on the system then this function returns the Perl logger
# module which uses custom logging mechanism.

# Name of the Package.
package commonLogModule;

# It is used for strict compilation.
use strict;

# It helps in debugging.
use warnings;

# It is used for getting instance of commonDefaultLogModule.
use commonDefaultLogModule;

# --------------------------------------------------------------------------------------------------------------------------------------------------
# Sub-routines
# --------------------------------------------------------------------------------------------------------------------------------------------------
# Constructor of class commonLogModule.
#
# @param :
#   $_[0] - [In] Class name.
#   $_[1] - [In] Base log folder.
#   $_[2] - [In] Probe Type like NetApp.
#   $_[3] - [In] Probe Id "192.168.20.100".
#
# @return :
#   Instance of Perl logger module which user can use for generating log files.
sub getInstance {
    # This variable stores package name.
    my $class = $_[0];

    # This variable stores base folder in which we generate logs.
    my $baseFolder = $_[1];

    # This variable stores probe type.
    my $probeType = $_[2];

    # This variable stores probe id.
    my $probeId = $_[3];

    # This variable stores log object.
    my $commLogModule = "";

    # If dependencies of commonLog4PerlModule installed then return commonLog4PerlModule instance. Otherwise return commonDefaultLogModule instance.
    eval("require Log::Log4perl; require Log::Log4perl::Layout::PatternLayout; require Log::Dispatch::FileRotate; require commonLog4PerlModule; 1");

    if (! length($@)) {
        $commLogModule = commonLog4PerlModule->getInstance($baseFolder, $probeType, $probeId);
    } else {
        $commLogModule = commonDefaultLogModule->getInstance($baseFolder, $probeType, $probeId);
    }

    return $commLogModule;
}

# The file must return true ("1") as the last statement to indicate successful execution of any initialization code. So it's customary to end such a
# file with 1 unless we are sure that we will return true otherwise. But it's better just to put the 1;, in case we add more statements.
1;