]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWGPP/scripts/alilog4bash.sh
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGPP / scripts / alilog4bash.sh
CommitLineData
70a21d36 1#!/usr/bin/env bash
2#
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#--------------------------------------------------------------------------------------------------
3600ebef 10#Copyright (c) 2009-2011, Fred Palmer and contributors.
11#All rights reserved.
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.
70a21d36 33
34# Useful global variables that users may wish to reference
35ALILOG_SCRIPT_ARGS="$@"
36ALILOG_SCRIPT_NAME="$0"
37ALILOG_SCRIPT_NAME="${SCRIPT_NAME#\./}"
38ALILOG_SCRIPT_NAME="${SCRIPT_NAME##/*/}"
39ALILOG_SCRIPT_BASE_DIR="$(cd "$( dirname "$0")" && pwd )"
40
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")
44declare -r ALILOG_INTERACTIVE_MODE="off"
45
46#--------------------------------------------------------------------------------------------------
47# Begin Help Section
48
49ALILOG_HELP_TEXT=""
50
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
53alilog_usage() {
54 echo -e "${HELP_TEXT}";
55 return 1;
56}
57
58# End Help Section
59#--------------------------------------------------------------------------------------------------
60
61#--------------------------------------------------------------------------------------------------
62# Begin Logging Section
63if [[ "${ALILOG_INTERACTIVE_MODE}" == "off" ]]
64then
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=""
72else
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"
79fi
80
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!
84prepare_alilog_for_nonterminal() {
85 # Essentially this strips all the control characters for alilog colors
86 sed "s/[[:cntrl:]]\[[0-9;]*m//g"
87}
88
89alilog() {
90 local alilog_text="$1"
91 local alilog_level="$2"
92 local alilog_color="$3"
93
94 # Default level to "info"
95 [[ -z ${alilog_level} ]] && alilog_level="INFO";
96 [[ -z ${alilog_color} ]] && alilog_color="${ALILOG_INFO_COLOR}";
97
98 echo -e "${alilog_color}[$(date +"%Y-%m-%d %H:%M:%S %Z")] [${alilog_level}] ${alilog_text} ${ALILOG_DEFAULT_COLOR}";
99 return 0;
100}
101
102alilog_info() { alilog "$@"; }
103
104alilog_speak() {
105 if type -P say >/dev/null
106 then
107 local easier_to_say="$1";
108 case "${easier_to_say}" in
109 studionowdev*)
110 easier_to_say="studio now dev ${easier_to_say#studionowdev}";
111 ;;
112 studionow*)
113 easier_to_say="studio now ${easier_to_say#studionow}";
114 ;;
115 esac
116 say "${easier_to_say}";
117 fi
118 return 0;
119}
120
121alilog_success() { alilog "$1" "SUCCESS" "${ALILOG_SUCCESS_COLOR}"; }
122alilog_error() { alilog "$1" "ERROR" "${ALILOG_ERROR_COLOR}"; alilog_speak "$1"; }
123alilog_warning() { alilog "$1" "WARNING" "${ALILOG_WARN_COLOR}"; }
124alilog_debug() { alilog "$1" "DEBUG" "${ALILOG_DEBUG_COLOR}"; }
125alilog_captains() {
126 if type -P figlet >/dev/null;
127 then
128 figlet -f computer -w 120 "$1";
129 else
130 alilog "$1";
131 fi
132
133 alilog_speak "$1";
134
135 return 0;
136}
137
138alilog_campfire() {
139 # This function performs a campfire notification with the arguments passed to it
140 if [[ -z ${CAMPFIRE_API_AUTH_TOKEN} || -z ${CAMPFIRE_NOTIFICATION_URL} ]]
141 then
142 alilog_warning "CAMPFIRE_API_AUTH_TOKEN and CAMPFIRE_NOTIFICATION_URL must be set in order alilog to campfire."
143 return 1;
144 fi
145
146 local campfire_message="
147 {
148 \"message\": {
149 \"type\":\"TextMessage\",
150 \"body\":\"$@\"
151 }
152 }"
153
154 curl \
155 --write-out "\r\n" \
156 --user ${CAMPFIRE_API_AUTH_TOKEN}:X \
157 --header 'Content-Type: application/json' \
158 --data "${campfire_message}" \
159 ${CAMPFIRE_NOTIFICATION_URL}
160
161 return $?;
162}
163
164# End Logging Section
165#--------------------------------------------------------------------------------------------------
166