]> CyberLeo.Net >> Repos - CDN/shlib.git/blob - lib/sh/log.sh
Add explicit copyright to all library files
[CDN/shlib.git] / lib / sh / log.sh
1 # Include for convenience log functions
2 # Set _log_to_stderr to send messages to stderr as well, for logging
3 : <<"END_OF_COPYRIGHT"
4
5 Copyright (c) 2000-2012, CyberLeo
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11     * Redistributions of the source code must retain the above copyright
12       notice, this list of conditions, and the following disclaimer.
13     * Redistributions in binary form must reproduce the above copyright
14       notice, this list of conditions, and the following disclaimer in the
15       documentation and/or other materials provided with the distribution.
16     * Neither the name of the organization nor the names of its contributors
17       may be used to endorse or promote products derived from this software
18       without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILTY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 END_OF_COPYRIGHT
32
33 if [ -z "${__log_sh_loaded}" ]
34 then
35   __log_sh_loaded=yes
36   
37   want ansi
38
39   logf() {
40     printf "${@}"
41     [ -n "${_log_to_stderr}" ] && printf "${@}" >&2
42     return 0
43   }
44
45   note() {
46     printf " ${a_cyan}*${a_normal} "
47     echo "${*}"
48     [ -n "${_log_to_stderr}" ] && echo "* note: ${*}" >&2
49     return 0
50   }
51
52   log() {
53     printf " ${a_green}*${a_normal} "
54     echo "${*}"
55     [ -n "${_log_to_stderr}" ] && echo "* log: ${*}" >&2
56     return 0
57   }
58   alias meh=log
59
60   warn() {
61     printf " ${a_yellow}*${a_normal} "
62     echo "${*}"
63     [ -n "${_log_to_stderr}" ] && echo "* warn: ${*}" >&2
64     return 0
65   }
66   alias omg=warn
67
68   err() {
69     printf " ${a_red}*${a_normal} "
70     echo "${*}"
71     [ -n "${_log_to_stderr}" ] && echo "* err: ${*}" >&2
72     exit 1
73   }
74   alias wtf=err
75
76   chk() {
77     _res=$?
78     if [ $# -gt 0 ]
79     then
80       out=$("${@}")
81       _res=$?
82     fi
83     if [ ${_res} -gt 0 ]
84     then
85       [ -n "${*}" ] && echo " ${a_red}*${a_normal} in '$(pwd)': cmd '${*}' failed with status ${_res}"
86       [ -n "${out}" ] && printf " ${a_red}*${a_normal} Output:\n${out}\n"
87       echo ""
88       kill $$
89     fi
90   }
91 fi