/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. # This is the logger library
  15. # It can print stuff on STDIN, STDERR and send messages
  16. # to syslog.
  17. function check_logger()
  18. {
  19. if [[ -x /usr/bin/logger ]]; then
  20. logger=/usr/bin/logger
  21. else
  22. BM_LOGGER="false"
  23. fi
  24. }
  25. #########################################
  26. # The syslog() function should be called
  27. # for sending messages to syslog.
  28. #
  29. # Note that no messages will be sent to
  30. # syslog if BM_LOGGER is set to "false".
  31. #
  32. # ex:
  33. # syslog "info" "message to log"
  34. #
  35. #########################################
  36. function syslog()
  37. {
  38. if [[ "$BM_LOGGER" = "true" ]]; then
  39. level="$1"
  40. message="$2"
  41. $logger -t "backup-manager[$$]" -p "${BM_LOGGER_FACILITY}.${level}" -- "$level * $message"
  42. fi
  43. }
  44. # The meta function to log something to syslog, and
  45. # eventually print it to the appropriate STDOUT
  46. #
  47. # $@ should be the same args as for a echo stanza.
  48. #
  49. # $bm_log_level should be "info", "warning" or "error".
  50. function log()
  51. {
  52. # set the default log level if none given
  53. if [[ -z "$bm_log_level" ]]; then
  54. bm_log_level="info"
  55. fi
  56. # choose the good switch to read if needed
  57. case "$bm_log_level" in
  58. "debug")
  59. bm_log_switch=$verbosedebug
  60. ;;
  61. "info")
  62. bm_log_switch=$verbose
  63. ;;
  64. "warning")
  65. bm_log_switch=$warnings
  66. ;;
  67. # in the default case, we print stuff
  68. *)
  69. bm_log_switch="true"
  70. ;;
  71. esac
  72. log_buffer=""
  73. # if there's the -n switch, we buffer the message
  74. if [[ "$1" == "-n" ]]; then
  75. # output the message to STDOUT
  76. message=$(echo_translated "$@")
  77. if [[ "$bm_log_switch" == "true" ]]; then
  78. echo -n "${message}"
  79. fi
  80. BM_LOG_BUFFER="${log_buffer}${message}"
  81. else
  82. # output the message to STDOUT
  83. message=$(echo_translated "$@")
  84. if [[ "$bm_log_switch" == "true" ]]; then
  85. if [[ "$bm_log_level" == "debug" ]]; then
  86. echo "${message}" >&2
  87. else
  88. echo "${message}"
  89. fi
  90. bm_dbus_send_log_message "${bm_log_level}" "${message}"
  91. fi
  92. # log the message to syslog
  93. syslog $bm_log_level "${log_buffer}${message}"
  94. # we have now to flush the buffer
  95. BM_LOG_BUFFER=""
  96. fi
  97. }
  98. # That function is deprecated, soon we'll remove it.
  99. function __debug()
  100. {
  101. message="$1"
  102. echo "DEPRECATED DEBUG: $message" >&2
  103. }
  104. function debug()
  105. {
  106. if [[ "$BM_LOGGER_LEVEL" == "debug" ]]; then
  107. bm_log_level="debug"
  108. log "DEBUG: $@"
  109. fi
  110. }
  111. function info()
  112. {
  113. if [[ "$BM_LOGGER_LEVEL" == "debug" ]]\
  114. || [[ "$BM_LOGGER_LEVEL" == "info" ]]; then
  115. bm_log_level="info"
  116. log "$@"
  117. fi
  118. }
  119. function warning()
  120. {
  121. if [[ "$BM_LOGGER_LEVEL" == "debug" ]]\
  122. || [[ "$BM_LOGGER_LEVEL" == "info" ]]\
  123. || [[ "$BM_LOGGER_LEVEL" == "warning" ]]; then
  124. bm_log_level="warning"
  125. log "$@"
  126. fi
  127. }
  128. # Errors are always sent to syslog
  129. function error()
  130. {
  131. bm_log_level="error"
  132. log "$@"
  133. _exit 1
  134. }
  135. # that's the way backup-manager should exit.
  136. # need to remove the lock before, clean the mountpoint and
  137. # remove the logfile
  138. function _exit()
  139. {
  140. exit_code="$1"
  141. exit_context="$2"
  142. exit_reason="$3"
  143. if [[ "$exit_context" != "POST_COMMAND" ]]; then
  144. exec_post_command || error "Unable to exec post-command."
  145. fi
  146. umask $BM_UMASK >/dev/null
  147. info "Releasing lock"
  148. release_lock
  149. bm_dbus_send_progress 100 "Finished"
  150. bm_dbus_send_event "shutdown" "$@"
  151. if [[ -n "$exit_reason" ]]; then
  152. info "Exit reason: \$exit_reason"
  153. fi
  154. exit $exit_code
  155. }