#!/bin/bash # # 日志模块 # Constants RESET='\033[0m' ## 普通黑 RED='\033[38;5;1m' ## 姨妈红 GREEN='\033[38;5;2m' ## 水鸭青 YELLOW='\033[38;5;3m' ## 鸭屎黄 MAGENTA='\033[38;5;5m' ## 基佬紫 CYAN='\033[38;5;6m' ## 天依蓝 SplitLine="--------------------------------------------------------------------------------" # Functions ######################## # Print to STDERR # Arguments: # Message to print # Returns: # None ######################### stderr_print() { # 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it local bool="${BITNAMI_QUIET:-false}" # comparison is performed without regard to the case of alphabetic characters shopt -s nocasematch if ! [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then printf "%b\\n" "${*}" >&2 fi } ######################## # Log message # Arguments: # Message to log # Returns: # None ######################### log() { stderr_print "${CYAN}${MODULE:-} ${MAGENTA}$(date "+%Y-%m-%d %H:%M:%S.%2N ")${RESET}${*}" } ######################## # Log an 'info' message # Arguments: # Message to log # Returns: # None ######################### info() { log "${GREEN}INFO ${RESET} ==> ${*}" } ######################## # Log message # Arguments: # Message to log # Returns: # None ######################### warn() { log "${YELLOW}WARN ${RESET} ==> ${*}" } ######################## # Log an 'error' message # Arguments: # Message to log # Returns: # None ######################### error() { log "${RED}ERROR${RESET} ==> ${*}" } ######################## # Log a 'debug' message # Globals: # BITNAMI_DEBUG # Arguments: # None # Returns: # None ######################### debug() { # 'is_boolean_yes' is defined in libvalidations.sh, but depends on this file so we cannot source it local bool="${BITNAMI_DEBUG:-true}" # comparison is performed without regard to the case of alphabetic characters shopt -s nocasematch if [[ "$bool" = 1 || "$bool" =~ ^(yes|true)$ ]]; then log "${MAGENTA}DEBUG${RESET} ==> ${*}" fi } ######################## # Indent a string # Arguments: # $1 - string # $2 - number of indentation characters (default: 4) # $3 - indentation character (default: " ") # Returns: # None ######################### indent() { local string="${1:-}" local num="${2:?missing num}" local char="${3:-" "}" # Build the indentation unit string local indent_unit="" for ((i = 0; i < num; i++)); do indent_unit="${indent_unit}${char}" done # shellcheck disable=SC2001 # Complex regex, see https://github.com/koalaman/shellcheck/wiki/SC2001#exceptions echo "$string" | sed "s/^/${indent_unit}/" } RED_COLOR="31m" ## 姨妈红 GREEN_COLOR="32m" ## 水鸭青 YELLOW_COLOR="33m" ## 鸭屎黄 PURPLE_COLOR="35m" ## 基佬紫 BLUE_COLOR="36m" ## 天依蓝 BlinkGreen_COLOR="32;5m" ## 闪烁的水鸭青 BlinkRed_COLOR="31;5m" ## 闪烁的姨妈红 BlickPurple_COLOR="35;5m" ## 闪烁的基佬紫 BackRed_COLOR="41m" ## 背景红色 ######## 颜色函数方法很精妙 ############ colorEcho() { echo -e "\033[${1}${@:2}\033[0m" 1>&2 } colorEchoGreen() { echo -e "\033[32m${@:1}\033[0m" 1>&2 } colorEchoBlue() { echo -e "\033[36m${@:1}\033[0m" 1>&2 } colorEchoYellow() { echo -e "\033[33m${@:1}\033[0m" 1>&2 } colorEchoRed() { echo -e "\033[31m${@:1}\033[0m" 1>&2 } colorEchoPurple() { echo -e "\033[35m${@:1}\033[0m" 1>&2 } SplitLine(){ echo "" } SplitBlue(){ colorEchoBlue ${SplitLine} echo "" } SplitGreen(){ colorEchoGreen ${SplitLine} echo "" } SplitRed(){ colorEchoRed ${SplitLine} echo "" } SplitPurple(){ colorEchoPurple ${SplitLine} echo "" } FunctionStart() { colorEcho ${PURPLE_COLOR} ${SplitLine} if [[ $# -gt 0 ]] then colorEchoBlue " $1 " fi echo "" } FunctionSuccess() { colorEcho ${GREEN_COLOR} ${SplitLine} echo "" } FunctionEnd() { colorEcho ${BlinkGreen_COLOR} ${SplitLine} echo "" }