3 #adopted for ALICE from:
4 #--------------------------------------------------------------------------------------------------
5 # log4bash - Makes logging in Bash scripting suck less
6 # Copyright (c) Fred Palmer
7 # Licensed under the MIT license
8 # http://github.com/fredpalmer/log4bash
9 #--------------------------------------------------------------------------------------------------
10 #Copyright (c) 2009-2011, Fred Palmer and contributors.
12 #Redistribution and use in source and binary forms, with or without
13 #modification, are permitted provided that the following conditions are met:
14 #* Redistributions of source code must retain the above copyright notice,
15 #this list of conditions and the following disclaimer.
16 #* Redistributions in binary form must reproduce the above copyright
17 #notice, this list of conditions and the following disclaimer in the
18 #documentation and/or other materials provided with the distribution.
19 #Neither the name of Fred Palmer nor the names of its contributors may be used
20 #to endorse or promote products derived from this software without specific
21 #prior written permission.
22 #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 #AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 #PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
26 #BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 #CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 #SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 #INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 #CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 #ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 #POSSIBILITY OF SUCH DAMAGE.
34 # Useful global variables that users may wish to reference
35 ALILOG_SCRIPT_ARGS="$@"
36 ALILOG_SCRIPT_NAME="$0"
37 ALILOG_SCRIPT_NAME="${SCRIPT_NAME#\./}"
38 ALILOG_SCRIPT_NAME="${SCRIPT_NAME##/*/}"
39 ALILOG_SCRIPT_BASE_DIR="$(cd "$( dirname "$0")" && pwd )"
41 # This should probably be the right way - didn't have time to experiment though
42 # declare -r ALILOG_INTERACTIVE_MODE="$([ tty --silent ] && echo on || echo off)"
43 # declare -r ALILOG_INTERACTIVE_MODE=$([ "$(uname)" == "Darwin" ] && echo "on" || echo "off")
44 declare -r ALILOG_INTERACTIVE_MODE="off"
46 #--------------------------------------------------------------------------------------------------
51 # This function is called in the event of an error.
52 # Scripts which source this script may override by defining their own "alilog_usage" function
54 echo -e "${HELP_TEXT}";
59 #--------------------------------------------------------------------------------------------------
61 #--------------------------------------------------------------------------------------------------
62 # Begin Logging Section
63 if [[ "${ALILOG_INTERACTIVE_MODE}" == "off" ]]
65 # Then we don't care about alilog colors
66 declare -r ALILOG_DEFAULT_COLOR=""
67 declare -r ALILOG_ERROR_COLOR=""
68 declare -r ALILOG_INFO_COLOR=""
69 declare -r ALILOG_SUCCESS_COLOR=""
70 declare -r ALILOG_WARN_COLOR=""
71 declare -r ALILOG_DEBUG_COLOR=""
73 declare -r ALILOG_DEFAULT_COLOR="\033[0m"
74 declare -r ALILOG_ERROR_COLOR="\033[1;31m"
75 declare -r ALILOG_INFO_COLOR="\033[1m"
76 declare -r ALILOG_SUCCESS_COLOR="\033[1;32m"
77 declare -r ALILOG_WARN_COLOR="\033[1;33m"
78 declare -r ALILOG_DEBUG_COLOR="\033[1;34m"
81 # This function scrubs the output of any control characters used in colorized output
82 # It's designed to be piped through with text that needs scrubbing. The scrubbed
83 # text will come out the other side!
84 prepare_alilog_for_nonterminal() {
85 # Essentially this strips all the control characters for alilog colors
86 sed "s/[[:cntrl:]]\[[0-9;]*m//g"
90 local alilog_text="$1"
91 local alilog_level="$2"
92 local alilog_color="$3"
94 # Default level to "info"
95 [[ -z ${alilog_level} ]] && alilog_level="INFO";
96 [[ -z ${alilog_color} ]] && alilog_color="${ALILOG_INFO_COLOR}";
98 echo -e "${alilog_color}[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${alilog_level}] ${alilog_text} ${ALILOG_DEFAULT_COLOR}";
102 alilog_info() { alilog "$@"; }
105 if type -P say >/dev/null
107 local easier_to_say="$1";
108 case "${easier_to_say}" in
110 easier_to_say="studio now dev ${easier_to_say#studionowdev}";
113 easier_to_say="studio now ${easier_to_say#studionow}";
116 say "${easier_to_say}";
121 alilog_success() { alilog "$1" "SUCCESS" "${ALILOG_SUCCESS_COLOR}"; }
122 alilog_error() { alilog "$1" "ERROR" "${ALILOG_ERROR_COLOR}"; alilog_speak "$1"; }
123 alilog_warning() { alilog "$1" "WARNING" "${ALILOG_WARN_COLOR}"; }
124 alilog_debug() { alilog "$1" "DEBUG" "${ALILOG_DEBUG_COLOR}"; }
126 if type -P figlet >/dev/null;
128 figlet -f computer -w 120 "$1";
139 # This function performs a campfire notification with the arguments passed to it
140 if [[ -z ${CAMPFIRE_API_AUTH_TOKEN} || -z ${CAMPFIRE_NOTIFICATION_URL} ]]
142 alilog_warning "CAMPFIRE_API_AUTH_TOKEN and CAMPFIRE_NOTIFICATION_URL must be set in order alilog to campfire."
146 local campfire_message="
149 \"type\":\"TextMessage\",
156 --user ${CAMPFIRE_API_AUTH_TOKEN}:X \
157 --header 'Content-Type: application/json' \
158 --data "${campfire_message}" \
159 ${CAMPFIRE_NOTIFICATION_URL}
164 # End Logging Section
165 #--------------------------------------------------------------------------------------------------