Utilities/Logging.ps1

# Defining a loggfile name based on current date and time and the script name


# Global default configuration
$global:gsDefaultLogLevel = 'verbose'
$global:gsDefaultEnableFileLogging = $true
$global:gsDefaultLogDirectory = "$env:ProgramData\GinShell\logs"
$global:gsDefaultLogFile = $DynamicLogName



# Helper: Convert log type to numeric priority
function script:Get-LogLevelValue {
    param ([string]$Level)
    switch ($Level.ToLower()) {
        'verbose' { return 0 }
        'debug' { return 1 }
        'action' { return 2 }
        'success' { return 3 }
        'info' { return 4 }
        'warning' { return 5 }
        'error' { return 6 }
        'critical' { return 7 }
        default { return 0 }  # Default to 'verbose'
    }
}

# Helper: Choose color for each log type
function script:Get-LogColor {
    param ([string]$Type)
    switch ($Type.ToLower()) {
        'verbose' { return 'DarkGray' }
        'debug' { return 'Gray' }
        'action' { return 'Cyan' }
        'success' { return 'Green' }
        'info' { return 'White' }
        'warning' { return 'Yellow' }
        'error' { return 'Red' }
        'critical' { return 'Magenta' }
        default { return 'White' }
    }
}


function Write-Log {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$Message,

        [Parameter()]
        [ValidateSet('Verbose', 'Debug', 'Action', 'Success', 'Info', 'Warning', 'Error', 'Critical')]
        [string]$LogType = 'Info',

        [Parameter()]
        [ValidateSet('Verbose', 'Debug', 'Action', 'Success', 'Info', 'Warning', 'Error', 'Critical')]
        [string]$LogLevel = $global:gsDefaultLogLevel,

        [Parameter()]
        [string]$LogFile = $null,

        [Parameter()]
        [bool]$EnableFileLogging = $global:gsDefaultEnableFileLogging
    )
    # if log file is not provided, create a dynamic name based on the current date and script name
    if (-Not $LogFile) {
        $CurrentScriptPath = $MyInvocation.PSCommandPath
        if ($CurrentScriptPath) {
            $DynamicLogName = "$(Get-Date -Format 'yyyy_MM_dd')__$([System.IO.Path]::GetFileNameWithoutExtension($CurrentScriptPath)).log"
        }
        else {
            $DynamicLogName = "$(Get-Date -Format 'yyyy_MM_dd')_interactive.log"
        }
        $LogFile = Join-Path -Path $global:gsDefaultLogDirectory -ChildPath $DynamicLogName
    }

    try {
        $threshold = Get-LogLevelValue -Level $LogLevel
        $current = Get-LogLevelValue -Level $LogType

        if ($current -lt $threshold) {
            return  # Skip message below threshold
        }

        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        $scriptName = $MyInvocation.PSCommandPath
        if (-not $scriptName) { $scriptName = "<interactive>" }  # If called from console or unknown

        $logMessage = "$timestamp [$LogType] [$scriptName] - $Message"
        $color = Get-LogColor -Type $LogType

        Write-Host $logMessage -ForegroundColor $color

        if ($EnableFileLogging) {
            $logDirectory = Split-Path -Path $LogFile -Parent
            if (-not (Test-Path -Path $logDirectory)) {
                New-Item -Path $logDirectory -ItemType Directory -Force | Out-Null
            }

            Add-Content -Path $LogFile -Value $logMessage
        }
    }
    catch {
        Write-Error "Failed to write log: $_"
    }
}



Export-ModuleMember -Function Write-Log
Export-ModuleMember -Variable '*'