log.ps1 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Logger variables helpers. These functions need the following variables:
  2. # LOG_LEVEL - The level of logger, default is "debug".
  3. $log_level = $env:LOG_LEVEL -or "debug"
  4. $log_colorful = $env:LOG_COLORFUL -or $true
  5. function GPUStack.Log.Errexit {
  6. trap {
  7. $ErrorMessage = $_.Exception.Message
  8. $ErrorCode = $_.Exception.HResult
  9. GPUStack.Log.Panic -message $ErrorMessage -code $ErrorCode
  10. continue
  11. }
  12. }
  13. # Debug level logging.
  14. function GPUStack.Log.Debug {
  15. param (
  16. [Parameter(Position = 0)]
  17. [string]$message
  18. )
  19. if ($log_level -ne "debug") {
  20. return
  21. }
  22. $timestamp = Get-Timestamp
  23. Write-Output "[$timestamp] [DEBG] $message"
  24. }
  25. # Info level logging.
  26. function GPUStack.Log.Info {
  27. param (
  28. [Parameter(Position = 0)]
  29. [string]$message
  30. )
  31. if ($log_level -ne "debug" -and $log_level -ne "info") {
  32. return
  33. }
  34. $timestamp = Get-Timestamp
  35. if ($log_colorful -eq $true) {
  36. Write-Host "[$timestamp] " -NoNewline
  37. Write-Host "[INFO] " -NoNewline -ForegroundColor Blue
  38. Write-Host $message
  39. }
  40. else {
  41. Write-Output "[$timestamp] [INFO] $message"
  42. }
  43. }
  44. # Warn level logging.
  45. function GPUStack.Log.Warn {
  46. param (
  47. [Parameter(Position = 0)]
  48. [string]$message
  49. )
  50. $timestamp = Get-Timestamp
  51. if ($log_colorful -eq $true) {
  52. Write-Host "[$timestamp] " -NoNewline
  53. Write-Host "[WARN] " -NoNewline -ForegroundColor Yellow
  54. Write-Host $message
  55. }
  56. else {
  57. Write-Output "[$timestamp] [WARN] $message"
  58. }
  59. }
  60. # Error level logging, log an error but keep going, don't dump the stack or exit.
  61. function GPUStack.Log.Error {
  62. param (
  63. [Parameter(Position = 0)]
  64. [string]$message
  65. )
  66. $timestamp = Get-Timestamp
  67. if ($log_colorful -eq $true) {
  68. Write-Host "[$timestamp] " -NoNewline
  69. Write-Host "[ERRO] " -NoNewline -ForegroundColor Red
  70. Write-Host $message
  71. }
  72. else {
  73. Write-Output "[$timestamp] [ERRO] $message"
  74. }
  75. }
  76. # Fatal level logging, log an error but exit with 1, don't dump the stack or exit.
  77. function GPUStack.Log.Fatal {
  78. param (
  79. [Parameter(Position = 0)]
  80. [string]$message
  81. )
  82. $timestamp = Get-Timestamp
  83. if ($log_colorful -eq $true) {
  84. Write-Host "[$timestamp] " -NoNewline
  85. Write-Host "[FATA] " -NoNewline -ForegroundColor Red
  86. Write-Host $message
  87. }
  88. else {
  89. Write-Output "[$timestamp] [FATA] $message"
  90. }
  91. exit 1
  92. }
  93. # Panic level logging, dump the error stack and exit.
  94. function GPUStack.Log.Panic {
  95. param (
  96. [string]$message,
  97. [int]$code = 1
  98. )
  99. $timestamp = Get-Timestamp
  100. $formattedMessage = "[$timestamp] [FATA] $message"
  101. if ($log_colorful -eq $true) {
  102. Write-Error $formattedMessage
  103. } else {
  104. Write-Output $formattedMessage
  105. }
  106. $customStackTrace = [System.Diagnostics.StackTrace]::new()
  107. $customStackTrace.GetFrames() | ForEach-Object {
  108. Write-Error (" {0}: {1}" -f $_.GetFileName(), $_.GetFileLineNumber())
  109. }
  110. exit $code
  111. }
  112. function Get-Timestamp {
  113. return $(Get-Date -Format 'MMdd HH:mm:ss')
  114. }