1+ # Include MyWrite.ps1
2+ # Provides Write-MyError, Write-MyWarning, Write-MyVerbose, Write-MyHost, Write-MyDebug
3+ # and Test-Verbose, Test-Debug functions for consistent logging and debugging output.
4+ # Use env variables ModuleHelper_VERBOSE and ModuleHelper_DEBUG to control verbosity and debug output.
5+ # Example: $env:ModuleHelper_DEBUG="all" or $env:ModuleHelper_DEBUG="Sync-Project"
6+
7+ $ModuleRootPath = Get-ModuleRootPath - ModuleRootPath $ModuleRootPath
8+ $MODULE_NAME = (Get-ChildItem - Path $ModuleRootPath - Filter * .psd1 | Select-Object - First 1 ).BaseName
19
210$ERROR_COLOR = " Red"
311$WARNING_COLOR = " Yellow"
12+ $VERBOSE_COLOR = " DarkYellow"
413$OUTPUT_COLOR = " DarkCyan"
514$DEBUG_COLOR = " DarkGray"
615
@@ -24,7 +33,10 @@ function Write-MyVerbose {
2433 param (
2534 [Parameter (ValueFromPipeline )][string ]$Message
2635 )
27- Write-Verbose - Message $message
36+
37+ if (Test-Verbose ) {
38+ Write-ToConsole $message - Color $VERBOSE_COLOR
39+ }
2840}
2941
3042function Write-MyHost {
@@ -37,33 +49,25 @@ function Write-MyHost {
3749 Write-ToConsole $message - Color $OUTPUT_COLOR - NoNewLine:$NoNewLine
3850}
3951
40- function Write-Debug {
52+ function Write-MyDebug {
4153 param (
4254 [Parameter (Position = 0 )][string ]$section ,
4355 [Parameter (Position = 1 , ValueFromPipeline )][string ]$Message ,
4456 [Parameter (Position = 2 )][object ]$Object
4557 )
4658
47- $flag = $ env: ProjectHelper_DEBUG
59+ process {
4860
49- # Enable debug
50- if ([string ]::IsNullOrWhiteSpace( $flag )) {
51- return
52- }
61+ if (Test-Debug - section $section ) {
5362
54- $trace = ($flag -like ' *all*' ) -or ( $section -like " *$flag *" )
55-
56- # Write-Host $message -ForegroundColor $DEBUG_COLOR
57- if ($trace ) {
58-
59- if ($Object ) {
60- $objJson = $Object | ConvertTo-Json - Depth 10 - ErrorAction SilentlyContinue
61- $message = $message + " - " + $objJson
63+ if ($Object ) {
64+ $objString = $Object | Get-ObjetString
65+ $message = $message + " - " + $objString
66+ }
67+ $timestamp = Get-Date - Format ' HH:mm:ss.fff'
68+ " [$timestamp ][D][$section ] $message " | Write-ToConsole - Color $DEBUG_COLOR
6269 }
63-
64- $message = " [DEBUG][$section ] " + $message
65- Write-ToConsole $message - Color $DEBUG_COLOR
66- }
70+ }
6771}
6872
6973function Write-ToConsole {
@@ -74,4 +78,58 @@ function Write-ToConsole {
7478
7579 )
7680 Microsoft.PowerShell.Utility\Write-Host $message - ForegroundColor $Color - NoNewLine:$NoNewLine
81+ }
82+
83+
84+ function Test-Verbose {
85+ param (
86+ [Parameter (Position = 0 )][string ]$section
87+ )
88+
89+ $moduleDebugVarName = $MODULE_NAME + " _VERBOSE"
90+ $flag = [System.Environment ]::GetEnvironmentVariable($moduleDebugVarName )
91+
92+ # Enable debug
93+ if ([string ]::IsNullOrWhiteSpace( $flag )) {
94+ return $false
95+ }
96+
97+ $trace = ($flag -like ' *all*' ) -or ( $section -like " *$flag *" )
98+ return $trace
99+ }
100+
101+ function Test-Debug {
102+ param (
103+ [Parameter (Position = 0 )][string ]$section
104+ )
105+
106+ $moduleDebugVarName = $MODULE_NAME + " _DEBUG"
107+ $flag = [System.Environment ]::GetEnvironmentVariable($moduleDebugVarName )
108+
109+ # Enable debug
110+ if ([string ]::IsNullOrWhiteSpace( $flag )) {
111+ return $false
112+ }
113+
114+ $trace = ($flag -like ' *all*' ) -or ( $section -like " *$flag *" )
115+ return $trace
116+ }
117+
118+ function Get-ObjetString {
119+ param (
120+ [Parameter (ValueFromPipeline , Position = 0 )][object ]$Object
121+ )
122+
123+ process {
124+
125+ if ($null -eq $Object ) {
126+ return " null"
127+ }
128+
129+ if ($Object -is [string ]) {
130+ return $Object
131+ }
132+
133+ return $Object | ConvertTo-Json - Depth 10 - ErrorAction SilentlyContinue
134+ }
77135}
0 commit comments