96 lines
2.7 KiB
PowerShell
96 lines
2.7 KiB
PowerShell
# Module Logger - Gestion des logs console + fichier
|
|
|
|
$script:LogFile = $null
|
|
$script:Stopwatch = $null
|
|
$script:Stats = @{
|
|
AutomatesTotal = 0
|
|
AutomatesError = 0
|
|
DevicesProcessed = 0
|
|
}
|
|
|
|
function Initialize-Logger {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd_HH\hmm"
|
|
$script:LogFile = Join-Path (Get-Location) "enocean_$timestamp.log"
|
|
New-Item -ItemType File -Path $script:LogFile -Force | Out-Null
|
|
|
|
$script:Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
|
|
|
$script:Stats = @{
|
|
AutomatesTotal = 0
|
|
AutomatesError = 0
|
|
DevicesProcessed = 0
|
|
}
|
|
|
|
Write-Log -Message "Logger initialise - fichier: $($script:LogFile)" -Level INFO
|
|
}
|
|
|
|
function Write-Log {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)]
|
|
[string]$Message,
|
|
|
|
[ValidateSet("INFO", "WARN", "ERROR", "SUCCESS")]
|
|
[string]$Level = "INFO"
|
|
)
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$line = "[$timestamp] [$Level] $Message"
|
|
|
|
# Couleur console selon le niveau
|
|
switch ($Level) {
|
|
"INFO" { Write-Host $line -ForegroundColor Cyan }
|
|
"WARN" { Write-Host $line -ForegroundColor Yellow }
|
|
"ERROR" { Write-Host $line -ForegroundColor Red }
|
|
"SUCCESS" { Write-Host $line -ForegroundColor Green }
|
|
}
|
|
|
|
# Ecriture fichier
|
|
if ($script:LogFile) {
|
|
Add-Content -Path $script:LogFile -Value $line -Encoding UTF8
|
|
}
|
|
}
|
|
|
|
function Update-Stats {
|
|
[CmdletBinding()]
|
|
param(
|
|
[Parameter(Mandatory)]
|
|
[ValidateSet("AutomatesTotal", "AutomatesError", "DevicesProcessed")]
|
|
[string]$Counter,
|
|
|
|
[int]$Increment = 1
|
|
)
|
|
|
|
$script:Stats[$Counter] += $Increment
|
|
}
|
|
|
|
function Write-Summary {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
$script:Stopwatch.Stop()
|
|
$duration = $script:Stopwatch.Elapsed
|
|
|
|
Write-Log -Message "========== RESUME ==========" -Level INFO
|
|
Write-Log -Message "Automates traites : $($script:Stats.AutomatesTotal)" -Level INFO
|
|
Write-Log -Message "Automates en erreur : $($script:Stats.AutomatesError)" -Level $(if ($script:Stats.AutomatesError -gt 0) { "WARN" } else { "INFO" })
|
|
Write-Log -Message "Devices traites : $($script:Stats.DevicesProcessed)" -Level INFO
|
|
Write-Log -Message "Duree totale : $($duration.ToString('hh\:mm\:ss\.ff'))" -Level INFO
|
|
Write-Log -Message "============================" -Level INFO
|
|
}
|
|
|
|
function Get-LogDirectory {
|
|
[CmdletBinding()]
|
|
param()
|
|
|
|
if ($script:LogFile) {
|
|
return Split-Path $script:LogFile -Parent
|
|
}
|
|
return $null
|
|
}
|
|
|
|
Export-ModuleMember -Function Initialize-Logger, Write-Log, Update-Stats, Write-Summary, Get-LogDirectory
|