Skip to content

Commit a3e40fc

Browse files
committed
Add a logging function
1 parent 69fdb7b commit a3e40fc

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/inc/Logging.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Guard: this file is intended to be dot-sourced (included) by the main script.
2+
# If executed directly, warn and exit to avoid unintended behavior.
3+
if ($MyInvocation.InvocationName -ne '.') {
4+
Write-Host "This script is a library and must be dot-sourced from the main script (e.g. `. .\Create-VSSolution.ps1`). Exiting." -ForegroundColor Yellow
5+
exit 1
6+
}
7+
8+
# Write helpers respect numeric verbosity:0 = silent,1 = errors/warnings only,2 = errors+warnings+ok+info,3+ = debug
9+
function Write-Info { if ($VerboseMode -ge 2) { Write-Host "[INFO] " -ForegroundColor Cyan -NoNewline; Write-Host " $args" } }
10+
function Write-Ok { if ($VerboseMode -ge 2) { Write-Host "[OK] " -ForegroundColor Green -NoNewline; Write-Host " $args" } }
11+
function Write-Err { if ($VerboseMode -ge 1) { Write-Host "[ERR] " -ForegroundColor Red -NoNewline; Write-Host " $args" } }
12+
function Write-Warn { if ($VerboseMode -ge 1) { Write-Host "[WARN] " -ForegroundColor Magenta -NoNewline; Write-Host " $args" } }
13+
function Write-Debug { if ($VerboseMode -ge 3) { Write-Host "[DEBUG] " -ForegroundColor Yellow -NoNewline; Write-Host " $args" } }
14+
function Write-Run { if ($VerboseMode -ge 1) { Write-Host "[RUN] " -ForegroundColor DarkGray -NoNewline; Write-Host " $args" } }
15+
function Write-RunDone {
16+
param (
17+
[bool]$Success = $true
18+
)
19+
if ($Success) {
20+
$msg = 'DONE'
21+
$msgColor = 'DarkGray'
22+
}
23+
else {
24+
$msg = 'FAIL'
25+
$msgColor = 'Red'
26+
}
27+
if ($VerboseMode -ge1) {
28+
29+
try {
30+
$raw = $Host.UI.RawUI
31+
$pos = $raw.CursorPosition
32+
if ($pos.Y -gt 0) {
33+
# Move cursor to the start of the previous line
34+
$up = New-Object System.Management.Automation.Host.Coordinates(0, ($pos.Y -1))
35+
$raw.CursorPosition = $up
36+
# Overwrite the [RUN] marker
37+
Write-Host "[$msg] " -ForegroundColor $msgColor -NoNewline
38+
# Restore cursor to the original line start so the rest of the message prints on the next line
39+
$raw.CursorPosition = New-Object System.Management.Automation.Host.Coordinates(0, $pos.Y)
40+
}
41+
else {
42+
Write-Host "[$msg] " -ForegroundColor $msgColor -NoNewline
43+
}
44+
}
45+
catch {
46+
# Host doesn't support RawUI; just print $msg marker
47+
#Write-Host "[$msg] " -ForegroundColor $msgColor -NoNewline
48+
}
49+
50+
#Write-Host " $args"
51+
}
52+
}
53+
function Write-Header {
54+
param (
55+
[string]$Message
56+
)
57+
$line = "=" * ($Message.Length + 8)
58+
Write-Host $line -ForegroundColor DarkCyan
59+
Write-Host "=== $Message ===" -ForegroundColor DarkCyan
60+
Write-Host $line -ForegroundColor DarkCyan
61+
}

0 commit comments

Comments
 (0)