src/mulle-bootstrap-logging.sh
c81c8721
 #! /usr/bin/env bash
d2100287
 #
 #   Copyright (c) 2015 Nat! - Mulle kybernetiK
 #   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 Mulle kybernetiK 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 HOLDER 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.
 #
8f77be37
 MULLE_BOOTSTRAP_LOGGING_SH="included"
d2100287
 
 
f67f6857
 MULLE_BOOTSTRAP_LOGGING_VERSION="3.0"
cce6dc3d
 
 #
 # WARNING! THIS FILE IS A LIBRARY USE BY OTHER PROJECTS
 #          DO NOT CASUALLY RENAME, REORGANIZE STUFF
 #
f1144ade
 log_printf()
 {
e3244671
    if [ -z "${MULLE_EXEKUTOR_LOG_DEVICE}" ]
f1144ade
    then
       printf "$@" >&2
    else
e3244671
       printf "$@" > "${MULLE_EXEKUTOR_LOG_DEVICE}"
75913c0b
    fi
f1144ade
 }
 
cce6dc3d
 
d2100287
 log_error()
 {
e400db11
    log_printf "${C_ERROR}${MULLE_EXECUTABLE_FAIL_PREFIX} error: %b${C_RESET}\n" "$*"
 }
 
 
 log_fail()
 {
    log_printf "${C_ERROR}${MULLE_EXECUTABLE_FAIL_PREFIX} fatal error: %b${C_RESET}\n" "$*"
d2100287
 }
 
 
 log_warning()
 {
f67f6857
    if [ "${MULLE_FLAG_LOG_TERSE}" != "YES" ]
d2100287
    then
e400db11
       log_printf "${C_WARNING}${MULLE_EXECUTABLE_FAIL_PREFIX} warning: %b${C_RESET}\n" "$*"
d2100287
    fi
 }
 
 
 log_info()
 {
f67f6857
    if [ "${MULLE_FLAG_LOG_TERSE}" != "YES" ]
d2100287
    then
75913c0b
       log_printf "${C_INFO}%b${C_RESET}\n" "$*"
d2100287
    fi
 }
 
 
 log_verbose()
 {
f67f6857
    if [ "${MULLE_FLAG_LOG_VERBOSE}" = "YES"  ]
d2100287
    then
75913c0b
       log_printf "${C_VERBOSE}%b${C_RESET}\n" "$*"
d2100287
    fi
 }
 
 
 log_fluff()
 {
f67f6857
    if [ "${MULLE_FLAG_LOG_FLUFF}" = "YES"  ]
d2100287
    then
75913c0b
       log_printf "${C_FLUFF}%b${C_RESET}\n" "$*"
d2100287
    fi
 }
 
 
4805e856
 # setting is like fluff but different color scheme
 log_setting()
 {
f67f6857
    if [ "${MULLE_FLAG_LOG_FLUFF}" = "YES"  ]
4805e856
    then
       log_printf "${C_SETTING}%b${C_RESET}\n" "$*"
    fi
 }
 
9339a58a
 # for debugging, not for user. same as fluff
 log_debug()
 {
    if [ "${MULLE_FLAG_LOG_DEBUG}" = "YES"  ]
    then
ed001110
       case "${UNAME}" in
          linux)
             log_printf "${C_BR_RED}$(date "+%s.%N") %b${C_RESET}\n" "$*"
          ;;
          *)
             log_printf "${C_BR_RED}$(date "+%s") %b${C_RESET}\n" "$*"
          ;;
       esac
9339a58a
    fi
 }
 
4805e856
 
d2100287
 log_trace()
 {
ee17b9c7
    case "${UNAME}" in
       linux)
          log_printf "${C_TRACE}$(date "+%s.%N") %b${C_RESET}\n" "$*"
          ;;
 
       *)
          log_printf "${C_TRACE}$(date "+%s") %b${C_RESET}\n" "$*"
       ;;
    esac
d2100287
 }
 
 
 log_trace2()
 {
ee17b9c7
    case "${UNAME}" in
       linux)
          log_printf "${C_TRACE2}$(date "+%s.%N") %b${C_RESET}\n" "$*"
          ;;
          
       *)
          log_printf "${C_TRACE2}$(date "+%s") %b${C_RESET}\n" "$*"
       ;;
    esac
d2100287
 }
 
 
 #
 # some common fail log functions
 #
ea74a237
 
ed001110
 stacktrace()
 {
    local i=1
ea74a237
    local line
 
    while line="`caller $i`"
    do
       log_info "$i: #${line}"
       ((i++))
    done
ed001110
 }
ea74a237
 
ed001110
 
 _bail()
 {
0921857b
 #   should kill process group...
 #   kills calling shell too though
 #   kill 0
9339a58a
 
8cb0beed
 #   if [ ! -z "${MULLE_EXECUTABLE_PID}" ]
 #   then
 #      kill -INT "${MULLE_EXECUTABLE_PID}"  # kill myself (especially, if executing in subshell)
 #      if [ $$ -ne ${MULLE_EXECUTABLE_PID} ]
 #      then
 #         kill -INT $$  # actually useful
 #      fi
 #   fi
ea74a237
 
0921857b
    exit 1
d2100287
 }
 
 
cbad10ec
 
 fail()
 {
cbe9e1a7
    if [ ! -z "$*" ]
    then
       log_fail "$*"
    fi
ed001110
    _bail
 }
 
 
d2100287
 internal_fail()
 {
e400db11
    log_printf "${C_ERROR}${MULLE_EXECUTABLE_FAIL_PREFIX} *** internal error ***: %b${C_RESET}\n" "$*"
ed001110
    stacktrace
    _bail
d2100287
 }
8f77be37
 
 
fcf4306a
 #
 # here because often needed :-/
0b9e9e49
 # this puts a space between items
fcf4306a
 #
 concat()
 {
    local i
    local s
 
    for i in "$@"
    do
       if [ -z "${i}" ]
       then
          continue
       fi
 
       if [ -z "${s}" ]
       then
          s="${i}"
       else
          s="${s} ${i}"
       fi
    done
 
    echo "${s}"
 }
 
 
8f77be37
 # Escape sequence and resets, should use tput here instead of ANSI
 logging_initialize()
 {
fcf4306a
    DEFAULT_IFS="${IFS}" # as early as possible
 
8f77be37
    #
    # need this for scripts also
    #
cce6dc3d
    if [ -z "${UNAME}" ]
8f77be37
    then
8f951e19
       UNAME="`uname | cut -d_ -f1 | sed 's/64$//' | tr 'A-Z' 'a-z'`"
8f77be37
    fi
 
    if [ "${MULLE_BOOTSTRAP_NO_COLOR}" != "YES" ]
    then
       case "${UNAME}" in
          *)
             C_RESET="\033[0m"
 
             # Useable Foreground colours, for black/white white/black
             C_RED="\033[0;31m"     C_GREEN="\033[0;32m"
             C_BLUE="\033[0;34m"    C_MAGENTA="\033[0;35m"
             C_CYAN="\033[0;36m"
 
             C_BR_RED="\033[0;91m"
             C_BOLD="\033[1m"
             C_FAINT="\033[2m"
 
             C_RESET_BOLD="${C_RESET}${C_BOLD}"
ed001110
             trap 'printf "${C_RESET}" >&2 ; exit 1' TERM INT
8f77be37
             ;;
       esac
    fi
 
 
    C_ERROR="${C_RED}${C_BOLD}"
578dcc01
    C_WARNING="${C_RED}${C_BOLD}"
8f77be37
    C_INFO="${C_CYAN}${C_BOLD}"
    C_VERBOSE="${C_GREEN}${C_BOLD}"
    C_FLUFF="${C_GREEN}${C_BOLD}"
4805e856
    C_SETTING="${C_GREEN}${C_FAINT}"
8f77be37
    C_TRACE="${C_FLUFF}${C_FAINT}"
    C_TRACE2="${C_RESET}${C_FAINT}"
bba581f8
 
    if [ ! -z "${MULLE_BOOTSTRAP_LIBEXEC_TRACE}" ]
    then
       local exedir
       local exedirpath
 
       exedir="`dirname "${BASH_SOURCE}"`"
8f951e19
       exedirpath="`( cd "${exedir}" ; pwd -P )`" || fail "failed to get pwd"
4eff2e57
       echo "${MULLE_EXECUTABLE} libexec: ${exedirpath}" >&2
bba581f8
    fi
8f77be37
 }
ec2b9bc2
 
bba581f8
 logging_initialize "$@"
0921857b
 
 :