diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e374d0d --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2009-2011, Fred Palmer and contributors. +Copyright (c) 2017 Joe Cooper +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +Neither the name of Fred Palmer nor the names of its contributors may be used +to endorse or promote products derived from this software without specific +prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. diff --git a/slog.sh b/slog.sh new file mode 100644 index 0000000..adbd618 --- /dev/null +++ b/slog.sh @@ -0,0 +1,83 @@ +#!/bin/sh +#-------------------------------------------------------------------------------------------------- +# log4sh - Makes logging in POSIX shell scripting suck less +# Copyright (c) Fred Palmer +# POSIX version Copyright Joe Cooper +# Licensed under the MIT license +# http://github.com/swelljoe/log4sh +#-------------------------------------------------------------------------------------------------- +set -e # Fail on first error + +# Define $LOG_PATH in your script to log to a file, otherwise +# just writes to STDOUT. + +# Useful global variables that users may wish to reference +SCRIPT_ARGS="$@" +SCRIPT_NAME="$0" +SCRIPT_NAME="${SCRIPT_NAME#\./}" +SCRIPT_NAME="${SCRIPT_NAME##/*/}" +SCRIPT_BASE_DIR="$(cd "$( dirname "$0")" && pwd )" + +# Determines if we print colors or not +if [ $(tty -s) ]; then + readonly INTERACTIVE_MODE="off" +else + readonly INTERACTIVE_MODE="on" +fi + +#-------------------------------------------------------------------------------------------------- +# Begin Logging Section +if [ "${INTERACTIVE_MODE}" = "off" ] +then + # Then we don't care about log colors + readonly LOG_DEFAULT_COLOR="" + readonly LOG_ERROR_COLOR="" + readonly LOG_INFO_COLOR="" + readonly LOG_SUCCESS_COLOR="" + readonly LOG_WARN_COLOR="" + readonly LOG_DEBUG_COLOR="" +else + readonly LOG_DEFAULT_COLOR=$(tput sgr0) + readonly LOG_ERROR_COLOR=$(tput setaf 1) + readonly LOG_INFO_COLOR=$(tput sgr 0) + readonly LOG_SUCCESS_COLOR=$(tput setaf 2) + readonly LOG_WARN_COLOR=$(tput setaf 3) + readonly LOG_DEBUG_COLOR="\033[1;34m" +fi + +# This function scrubs the output of any control characters used in colorized output +# It's designed to be piped through with text that needs scrubbing. The scrubbed +# text will come out the other side! +prepare_log_for_nonterminal() { + # Essentially this strips all the control characters for log colors + sed "s/[[:cntrl:]]\[[0-9;]*m//g" +} + +log() { + local log_text="$1" + local log_level="$2" + local log_color="$3" + + # Default level to "info" + [ -z ${log_level} ] && log_level="INFO"; + [ -z ${log_color} ] && log_color="${LOG_INFO_COLOR}"; + + # STDOUT + printf "${log_color}[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${log_level}] ${log_text} ${LOG_DEFAULT_COLOR}\n"; + # LOG_PATH minus fancypants colors + if [ ! -z $LOG_PATH ]; then + printf "[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${log_level}] ${log_text}\n" >> $LOG_PATH; + fi + + return 0; +} + +log_info() { log "$@"; } +log_success() { log "$1" "SUCCESS" "${LOG_SUCCESS_COLOR}"; } +log_error() { log "$1" "ERROR" "${LOG_ERROR_COLOR}"; } +log_warning() { log "$1" "WARNING" "${LOG_WARN_COLOR}"; } +log_debug() { log "$1" "DEBUG" "${LOG_DEBUG_COLOR}"; } + +# End Logging Section +#-------------------------------------------------------------------------------------------------- + diff --git a/tests/.bashful.sh.swp b/tests/.bashful.sh.swp new file mode 100644 index 0000000..8b7848c Binary files /dev/null and b/tests/.bashful.sh.swp differ diff --git a/tests/sloggi.sh b/tests/sloggi.sh new file mode 100644 index 0000000..5228bee --- /dev/null +++ b/tests/sloggi.sh @@ -0,0 +1,38 @@ +#!/bin/sh +#-------------------------------------------------------------------------------------------------- +# slog - Simple logging for POSIX and bash shells +# Copyright (c) Fred Palmer, Joe Cooper +# Licensed under the MIT license +# http://github.com/swelljoe/slog +# (Original bash4log version: http://github.com/fredpalmer/log4bash) +#-------------------------------------------------------------------------------------------------- + +# XXX Output is not quite TAP, but should be. We could make a TAP mode for slog, or strip the +# non-tap output in the log. + +LOG_PATH="./my.log" +. ../slog.sh + +log "ok 1 - This is regular log message... "; + +log_info "ok 2 - So is this..."; + +log_success "ok 3 - Yeah!! Awesome Possum."; + +log_warning "ok 4 - Luke ... you turned off your targeting computer"; + +log_error "ok 5 - Whoops! I made a booboo"; + +if [ -e ./my.log ]; then + # Did the log get created with actual data? + grep -q Whoops "$LOG_PATH" + if [ $? -eq 0 ]; then + log_success "ok 6 - Log was created successfully! We'll remove it now." + rm "$LOG_PATH" + else + echo "not ok 6 - Log file was created, but doesn't contain the right data." + fi +else + echo "not ok 6 - Some tests failed." +fi +