#!/bin/bash
#  core/logging - Script Logging Support

set -e

lib_load 'core/file'


#;;;;;
#; Library Settings

logging=${logging:-false}

logdir=${logdir:-/home/zwelch/src/mcf/mcsh-release/install/log/mcsh}


######
# Library Configuration

log_config_init() {
	# $logging - Enables logging when set to `true`.  Can be set at runtime.
	lib_setting_vars logging

	# $logdir - Path to package log file directory
	lib_setting_vars -ro logdir
}


######
# Output Support

# log_pipe() - Passes stdin to stdout, saving a copy to file ($1).
log_pipe() {
	has_args 1 "$@"
	logging=false pretend=false file_mkdir "$1"
	tee -a "$1"
}


######
# Log Files

# log_filename() - Returns log filename given its base name ($1).
log_filename() {
	echo "$logdir/${1:-main}.log}"
}

# log_rotate() - Rotates log file ($1), saving `n` ($2) old files.
# $1 - Log file name
# $2 - Integer number of log files to save.
log_rotate() {
	has_args 2 "$@"
	local log=$1
	local dst=$2

	if [ $dst -eq 0 ]; then
		if [ -f "$log" ]; then
			run mv "$log" "$log.$dst"
			run gzip "$log.$dst"
		fi
		return
	fi

	local src
	src=$(($dst - 1))
	if [ -f "$log.$src.gz" ]; then
		run mv "$log.$src.gz" "$log.$dst.gz"
	fi

	log_rotate "$log" "$src"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Wed Jun 28 07:39:41 PDT 2017 by mcsh d14 v0.20.0.