PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/logger.sh

http://github.com/sukria/Backup-Manager
Shell | 172 lines | 109 code | 17 blank | 46 comment | 27 complexity | 161013b14de210d5de8571006a637bc7 MD5 | raw file
  1# Copyright � 2005-2016 The Backup Manager Authors
  2#
  3# See the AUTHORS file for details.
  4#
  5# This program is free software; you can redistribute it and/or
  6# modify it under the terms of the GNU General Public License
  7# as published by the Free Software Foundation; either version 2
  8# of the License, or (at your option) any later version.
  9#
 10# This program is distributed in the hope that it will be useful,
 11# but WITHOUT ANY WARRANTY; without even the implied warranty of
 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13# GNU General Public License for more details.
 14
 15# This is the logger library
 16# It can print stuff on STDIN, STDERR and send messages
 17# to syslog.
 18
 19function check_logger()
 20{
 21    if [[ -x /usr/bin/logger ]]; then
 22        logger=/usr/bin/logger
 23    else
 24        BM_LOGGER="false"
 25    fi
 26}
 27
 28#########################################
 29# The syslog() function should be called 
 30# for sending messages to syslog.
 31#
 32# Note that no messages will be sent to 
 33# syslog if BM_LOGGER is set to "false".
 34#
 35# ex:
 36#      syslog "info" "message to log"
 37#
 38#########################################
 39function syslog()
 40{
 41    if [[ "$BM_LOGGER" = "true" ]]; then  
 42        level="$1"
 43        message="$2"
 44        $logger -t "backup-manager[$$]" -p "${BM_LOGGER_FACILITY}.${level}" -- "$level * $message"  
 45    fi
 46}
 47
 48# The meta function to log something to syslog, and 
 49# eventually print it to the appropriate STDOUT
 50#
 51# $@ should be the same args as for a echo stanza.
 52# 
 53# $bm_log_level should be "info", "warning" or "error".
 54function log() 
 55{
 56    # set the default log level if none given
 57    if [[ -z "$bm_log_level" ]]; then
 58        bm_log_level="info"
 59    fi
 60    
 61    # choose the good switch to read if needed
 62    case "$bm_log_level" in
 63        "debug")
 64            bm_log_switch=$verbosedebug
 65        ;;
 66        "info")
 67            bm_log_switch=$verbose
 68        ;;
 69        "warning")
 70            bm_log_switch=$warnings
 71        ;;
 72        # in the default case, we print stuff
 73        *)
 74            bm_log_switch="true"
 75        ;;
 76    esac
 77    
 78    log_buffer=""
 79    # if there's the -n switch, we buffer the message 
 80    if [[ "$1" == "-n" ]]; then
 81        # output the message to STDOUT
 82        message=$(echo_translated "$@")
 83        if [[ "$bm_log_switch" == "true" ]]; then
 84            echo -n "${message}"
 85        fi
 86        BM_LOG_BUFFER="${log_buffer}${message}"
 87    
 88    else
 89        # output the message to STDOUT
 90        message=$(echo_translated "$@")
 91        if [[ "$bm_log_switch" == "true" ]]; then
 92            if [[ "$bm_log_level" == "debug" ]]; then
 93                echo "${message}" >&2
 94            else
 95                echo "${message}"
 96            fi
 97            bm_dbus_send_log_message "${bm_log_level}" "${message}"
 98        fi
 99        # log the message to syslog
100        syslog $bm_log_level "${log_buffer}${message}"
101        # we have now to flush the buffer
102        BM_LOG_BUFFER=""
103    fi
104}
105
106# That function is deprecated, soon we'll remove it.
107function __debug()
108{
109    message="$1"
110    echo "DEPRECATED DEBUG: $message" >&2
111}
112
113function debug()
114{
115    if [[ "$BM_LOGGER_LEVEL" == "debug" ]]; then
116        bm_log_level="debug"
117        log "DEBUG: $@"
118    fi
119}
120
121function info()
122{
123    if [[ "$BM_LOGGER_LEVEL" == "debug" ]]\
124    || [[ "$BM_LOGGER_LEVEL" == "info" ]]; then
125        bm_log_level="info"
126        log "$@"
127    fi
128}
129
130function warning()
131{
132    if [[ "$BM_LOGGER_LEVEL" == "debug" ]]\
133    || [[ "$BM_LOGGER_LEVEL" == "info" ]]\
134    || [[ "$BM_LOGGER_LEVEL" == "warning" ]]; then
135        bm_log_level="warning"
136        log "$@"
137    fi
138}
139
140# Errors are always sent to syslog
141function error()
142{
143    bm_log_level="error"
144    log "$@"
145    _exit 1
146}
147
148
149# that's the way backup-manager should exit.
150# need to remove the lock before, clean the mountpoint and 
151# remove the logfile
152function _exit()
153{
154	exit_code="$1"
155	exit_context="$2"
156	exit_reason="$3"
157
158    if [[ "$exit_context" != "POST_COMMAND" ]]; then
159		exec_post_command || error "Unable to exec post-command."
160	fi
161
162    umask $BM_UMASK >/dev/null
163    info "Releasing lock"
164    release_lock
165    bm_dbus_send_progress 100 "Finished"
166    bm_dbus_send_event "shutdown" "$@"
167
168	if [[ -n "$exit_reason" ]]; then 
169		info "Exit reason: \$exit_reason"
170	fi
171    exit $exit_code
172}