logging.lib #1

  • //
  • guest/
  • perforce_software/
  • sdp/
  • main/
  • Server/
  • Unix/
  • p4/
  • common/
  • lib/
  • logging.lib
  • View
  • Commits
  • Open Download .zip Download (2 KB)
# shellcheck shell=bash
           
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------

#==============================================================================
# This library contains runctions related to handling log files.

#------------------------------------------------------------------------------
# Function: get_old_log_timestamp ($log)
#
# Get the last modified timestamp of the old log in a cross-platform manner.
# If we don't get a correct value using 'stat' (which varies across the
# UNIX/Linux/MacOSX spectrum), use the current time as a fallback. In that
# case, the timestamp will reflect the time the log was moved rather than when
# it was last modified, but that's still reasonable.  The file timestamp will
# still have the correct last-modified time.
#------------------------------------------------------------------------------
function get_old_log_timestamp () {
   local log=${1:-}
   local oldLogTimestamp=
   [[ -n "$log" ]] || return

   if [[ "$(uname -s)" == "Darwin" ]]; then
      oldLogTimestamp=$(stat -L -f %Sm -t '%Y-%m-%d-%H%M%S' "$log" 2>/dev/null)
   else
      oldLogTimestamp="$(stat -L -c '%10y' "$log" | sed -e 's@[.].*$@@g' -e 's@:@@g' -e 's@ @-@g')"
   fi

   [[ "$oldLogTimestamp" =~ ^[2-9]{1}[0-9]{3}- ]] ||
      oldLogTimestamp=$(date +'%Y-%m-%d-%H%M%S')

   echo "$oldLogTimestamp"
}

#------------------------------------------------------------------------------
# Function: terminate
# shellcheck disable=SC2317,SC2154
function terminate ()
{
   # Disable signal trapping.
   trap - EXIT SIGINT SIGTERM

   dbg "ExitCode: $ErrorCount"

   [[ "$Log" == "off" ]] || msg "\\nLog is: $Log\\n${H1}"

   # With the trap removed, exit.
   exit "$ErrorCount"
}
# Change User Description Committed
#1 32135 C. Thomas Tyler Released SDP 2025.1.32133 (2025/10/29).
Copy Up using 'p4 copy -r -b perforce_software-sdp-dev'.
//guest/perforce_software/sdp/dev/Server/Unix/p4/common/lib/logging.lib
#2 32044 C. Thomas Tyler Slow Refactoring: "Moving" log_functions.sh from bin to lib directory,
and changing to .lib extension per coming SDP Coding Standard for bash.

The "slow refactoring" means for this change, the original log_functions.sh
will remain in the bin directory, and a new logging.lib file will be added
in the lib directory.  The removal of the file in bin will occur separately.
This is intended to balance allowing progress while preventing potential
disruption to customers who may have supplemental automation relying on the
current names.

New libs: ps.lib, logging.lib, run.lib

We may also allow legacy exceptions since customer-side custom scripts may
have dependencies on backup_functions.sh and other *.sh libraries in the bin
directory.

Unrelated minor fixes in p4verify.sh.
#1 31886 C. Thomas Tyler Refactoring non-trivial functions into a library utils.lib.

Added test script.