Private/LoggingFunctions.ps1

function GetLogDir
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "'$_' path is not valid. Please, provide a valid value" }})]
        [string]$Directory
    )

    $logsDirectory = Join-Path -Path $Directory -ChildPath "_SitecoreDeploymentPS"

    if (Test-Path $logsDirectory) 
    {
        return $logsDirectory
    }
    else 
    {
        return $null
    }
}

function NewLogDir
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "'$_' path is not valid. Please, provide a valid value" }})]
        [string]$Directory
    )

    $logsDirectoryPath = Join-Path -Path $Directory -ChildPath "_SitecoreDeploymentPS"
    
    if (!(Test-Path $logsDirectoryPath))
    {
        $logsDirectory = New-Item -Path $Directory -Name "_SitecoreDeploymentPS" -ItemType Directory
        return $logsDirectory.FullName
    }

    return $null
}

function NewExcludedDirsFile
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "'$_' path is not valid. Please, provide a valid value" }})]
        [string]$Directory,

        [string[]]$ExcludedDirs
    )

    if($ExcludedDirs.count -gt 0)
    {
        $excludedDirsFile = New-Item -Path $Directory -Name "ExcludedDirectories.txt" -ItemType File

        if ($excludedDirsFile) 
        {
            $ExcludedDirs | Out-File -FilePath $excludedDirsFile.FullName
            return $excludedDirsFile.FullName
        }
    }

    return $null
}

function GetExcludedDirs
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "'$_' path is not valid. Please, provide a valid value" }})]
        [string]$Directory
    )

    $excludedDirs = @()

    $excludedDirsFilePath = Join-Path -Path $Directory -ChildPath "ExcludedDirectories.txt"

    if (!(Test-Path $excludedDirsFilePath))
    {
        return $excludedDirs
    } 

    $excludedDirs += Get-Content $excludedDirsFilePath

    return $excludedDirs
}

function NewSystemLog
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [Alias('LogName')]
        [string]$Name,

        [string]$Directory
    )
    
    try 
    {
        $logsDirectory = $null;

        if (![String]::IsNullOrWhiteSpace($Directory))
        {
            if(!(Test-Path $Directory))
            {
                Throw "The '$Directory' is not valid. Please, provide a valid value"
            }

            $logsDirectory = $Directory
            Write-Verbose "User defined system logs directory will be used '$logsDirectory'"
        }
        else
        {
            $module = Get-Module -ListAvailable Sitecore.Deployment -Verbose:$false
            $moduleDirectory = Split-Path -Path $module.Path -Parent
            
            $logsDirectory = GetLogDir -Directory $moduleDirectory
            
            if (!$logsDirectory) 
            {
                $logsDirectory = NewLogDir -Directory $moduleDirectory
            }
        }
        
        $date = Get-Date -Format "yyyy-MM-dd-HHmmss"
        $newLogPath = Join-Path -Path $logsDirectory -ChildPath "$Name.log.$date.txt"

        if(!(Test-Path $newLogPath))
        {
            $logFile = New-Item $newLogPath -Force -ItemType File
            Write-Verbose "VERBOSE: New system log file is created '$($logFile.FullName)'"
            return $logFile.FullName
        }
        else
        {
            Throw "System log file already exists"
        }
    }
    catch
    {
        Write-Warning "WARNING: System log file was not created" *>&1
        $exMessage = $_.Exception | Format-List -Force | Out-String
        Write-Verbose "VERBOSE: $exMessage"
    }
}

function WriteLog
{
    [CmdletBinding()]
    Param
    (
        [string]$Path,

        [Alias("logContent")]
        [string]$Message,

        [bool]$RedirectStream = $false
    )

    Process
    {
        try 
        {
            if ([String]::IsNullOrWhiteSpace($Message)) 
            {
                return
            }

            if ($Message.StartsWith("VERBOSE_ERROR:")) 
            {
                $Message = $Message.Replace("VERBOSE_", "")

                if ($RedirectStream) 
                {
                    Write-Verbose $Message *>&1
                }
                else 
                {
                    Write-Verbose $Message
                }
            }
            elseif($Message.StartsWith("OUTPUT:"))
            {
                Write-Output $Message
            }
            elseif($Message.StartsWith("ERROR:"))
            {
                if ($RedirectStream) 
                {
                    Write-Warning $Message *>&1
                }
                else 
                {
                    Write-Warning $Message
                }
            }
            elseif ($Message.StartsWith("WARNING:")) 
            {
                if ($RedirectStream) 
                {
                    Write-Warning $Message *>&1
                }
                else
                {
                    Write-Warning $Message
                }
            }
            else 
            {
                if (!$Message.StartsWith("VERBOSE:")) 
                {
                    $Message = "VERBOSE: $Message"
                }

                if ($RedirectStream) 
                {
                    Write-Verbose $Message *>&1
                }
                else
                {
                    Write-Verbose $Message
                }
            }

            if ($Path) 
            {
                $formattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
                "$formattedDate $Message" | Out-File -FilePath $Path -Append
            }
        }
        catch 
        {
            Write-Warning "WARNING: System logging error occurred" *>&1
            $exMessage = $_.Exception | Format-List -Force | Out-String
            Write-Verbose "VERBOSE: $exMessage"
        }
    }
}