# -----------------------------------------------------------------------------
#                    S C R I P T   S P E C I F I C A T I O N
#                    Copyright 2019 Motorola Solutions, Inc.
#                              All Rights Reserved.
#                   Motorola Solutions Confidential Restricted
# -----------------------------------------------------------------------------

<#
.SYNOPSIS
    Name: LoggingFunctions.ps1 functions that help to log on screen and to file
.DESCRIPTION
    Remember to declare "scriptName" before you import this util
    ScriptName variable contains information about your function name in which "Write-Loge" was executed
#>

# MODIFICATION HISTORY
# Date         Change
# -----------------------------------------------------------------------------
# 18 Dec 19    Initial creation
# -----------------------------------------------------------------------------

Set-Variable -Name "INFO_C"  -Value "Information" -Option Constant
Set-Variable -Name "ERROR_C" -Value "ERROR" -Option Constant
Set-Variable -Name "WARN_C"  -Value "Warning" -Option Constant
Function Write-ToFile ([string]$message) {
    $message += "`r`n"
    try {
        [IO.File]::AppendAllText($logFile, $message)
    } catch {
        if ($_ -match "because it is being used by another process") {
            $mypid = [System.Diagnostics.Process]::GetCurrentProcess().ID
            [IO.File]::AppendAllText("$($logFile).$($mypid)", $message)
        }
    }
}
Function Write-Log {
    param (
        [String] $message,
        [String] $type = "Information"
    )
    $myDate = Get-Date -format "ddd yyyy-MM-dd h:mm:ss tt"
    $fileMessage = $type + ":" + $myDate + ":$($scriptName):" + $message
    Write-ToFile $fileMessage
    switch ($type) {
        $INFO_C {
            Write-Output $message
            break
        }
        $WARN_C {
            Write-Warning $message
            break
        }
        $ERROR_C {
            Write-Error $message
            break
        }
        default {
            Write-Output $message
            break
        }
    }
}