<# FPS:Containers:Common:Private:Update-Log:ACTIVE #>

 

function Update-Log {

 

<#

.SYNOPSIS

Module: Common

Private CmdLet: Update-Log

 

The method used here is to copy the existing log to a scratch location, add the data, then copy it back to the transcript location.  When

the scratch location is in existence, the transcript is locked, and the CmdLet will wait up to 20 seconds (in two second intervals) in order to

claim the scratch area via it's own copy.  It will return true if the log was successful and false if not.

 

Input:

The base requirement for use is that the following elements are set appropriately

$Module -eq "module name"

$Element -eq "CmdLet or subsection name"

$Buffer -ne "[null]"

 

$Buffer can be multiple buffered messages split by "|".  For each line the format {sev}*{text} is nessessary.

 

$LogFile -eq [Valid fully qualified log file]

 

.EXAMPLE

$Sev, $Module, $Element, $Buffer = "Info", "Common", "Update-Log", "This is a test log entry"

if (Push-NeededFor Update-Log) {. (Get-ActiveFileNm -PS1 Update-Log) }

 

Update-Log -Sev $Sev -Module $Module -Element $Element -Buffer $Buffer -LogFile (Mount-Path($drcLoc_Logs))

 

.CC

 

[2024.11.08 JD] Corrected $tStmp to use a dynamic timestamp instead of the login timestamp.

[2024.09.25 JD] Original code release

 

.NOTES

By default, data is appended after the last character. If you want to append the data on a new line in the text document, use `n.

 

Here are some other special characters that can be used with the add-content cmdlet.

`0 -- Null

`a -- Alert

`b -- Backspace

`n -- New line

`r -- Carriage return

`t -- Horizontal tab

`' -- Single quote

`" -- Double quote

 

#>

 

    [CmdletBinding()]

    param(

    [Parameter(Position=0, Mandatory=$true)]  [string]$Sev, # Info, Warn, Error - or buffered (optional, can be anything)

    [Parameter(Position=1, Mandatory=$true)]  [string]$Module,

    [Parameter(Position=2, Mandatory=$true)]  [string]$Element,

    [Parameter(Position=3, Mandatory=$true)]  [string]$Buffer,

    [Parameter(Position=4, Mandatory=$true)]  [string]$LogFile,

    [Parameter(Position=5, Mandatory=$false)] [switch]$Buffered

    )

 

    #region Initialization

 

    $overrideSrc, $overrideSnippet = '','' # Initialize override variables

    . (Get-ActiveFileNm -PS1 Initialize-Common-Locations)

    . (Get-ActiveFileNm -PS1 Load-Common-Dependencies) # No-op - Currently no dependencies

    . (Get-ActiveFileNm -PS1 SN-SetDateFormats) # Refresh the date timestamps each time through

 

    $scratchFile = $cmnScratchLog+'\'+$DDMMMYY+'-UpdateLog-'+$NowTicks+'.txt'

 

    $tStmp = (get-date).ToString('MM-dd-yyyy hh:mm:ss')

    #endregion

 

    # The scratch file, given its format, should always be unique.  But.... If the scratch file exists,

    # try 10 times to see if it is gone.  If not, then abort at the end

    $maxTries = 10

    while (Test-Path $scratchFile) {

           Start-Sleep -s 2

           $tries++

           if ($tries -gt $maxTries) { return $false }

    }

    if (Test-Path $LogFile) {

        Copy-Item $LogFile -Destination $scratchFile

    } else {

        New-Item $scratchFile -type file | Out-Null

    }

 

    if ($PSBoundParameters.Buffered -eq $true) {

        $spBuffer = $Buffer.split("|")

        foreach ($buffLn in $spBuffer) {

            $spBuff = $buffLn.split("*")

            if ($spBuff[0] -eq "Error") {

                $formatBuffer = $spBuff[0]+":"+$tStmp+" ... ("+$Module+":"+$Element+") "+$spBuff[1]

            } else {

                $formatBuffer = $spBuff[0]+"::"+$tStmp+" ... ("+$Module+":"+$Element+") "+$spBuff[1]

            }

 

            Add-Content $scratchFile $formatBuffer

 

        }

 

    } else {

        # Adjust the columns because Info and Warn are shorter than Error

        if ($Sev -eq "Error") {

            $formatBuffer = $Sev+":"+$tStmp+" ... ("+$Module+":"+$Element+") $Buffer"

        } else {

            $formatBuffer = $Sev+"::"+$tStmp+" ... ("+$Module+":"+$Element+") $Buffer"

        }

   

        Add-Content $scratchFile $formatBuffer

    }

 

    if (Test-Path $LogFile) { Remove-Item $LogFile }

    Copy-Item $scratchFile -Destination $LogFile

    Remove-Item $scratchFile

 

    if (Test-Path $scratchFile) { Remove-Item $scratchFile }

 

 }