From 21474658a066cfd15da521bbf4dc7665940c48a0 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 2 Feb 2021 09:13:59 +0100 Subject: [PATCH] Adding possibility for logging messages --- bin/start-openfortivpn | 2 ++ lib/functions.rc | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/bin/start-openfortivpn b/bin/start-openfortivpn index eaf4a56..5e762af 100755 --- a/bin/start-openfortivpn +++ b/bin/start-openfortivpn @@ -88,6 +88,8 @@ get_options() { check_for_root + LOGFILE="/var/log/$( basename $0 ).log" + } #------------------------------------------------------------------------------ diff --git a/lib/functions.rc b/lib/functions.rc index 536c692..38e4ab3 100644 --- a/lib/functions.rc +++ b/lib/functions.rc @@ -10,7 +10,7 @@ BLUE="" CYAN="" NORMAL="" -VERSION="0.3.8" +VERSION="0.3.9" STD_SHORT_OPTIONS="sdvhV" STD_LONG_OPTIONS="simulate,debug,verbose,nocolor,help,version" @@ -30,6 +30,8 @@ DEBUG="n" DO_ASK="n" SIMULATE="n" +LOGFILE= + declare -a REMAINING_ARGS=() declare -a REMAINING_OPTS=() @@ -266,22 +268,38 @@ debug() { if [[ "${VERBOSE}" != "y" ]] ; then return 0 fi - echo -e " * [$(my_date)] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2 + local ts=$(my_date) + if [[ -n "${LOGFILE}" ]] ; then + echo "[${ts}] [${BASE_NAME}DEBUG]: $@" >>"${LOGFILE}" + fi + echo -e " * [${ts}] [${BASE_NAME}:${CYAN}DEBUG${NORMAL}]: $@" >&2 } #------------------------------------------------------------------------------ info() { - echo -e " ${GREEN}*${NORMAL} [$(my_date)] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >&2 + local ts=$(my_date) + if [[ -n "${LOGFILE}" ]] ; then + echo "[${ts}] [${BASE_NAME}INFO] : $@" >>"${LOGFILE}" + fi + echo -e " ${GREEN}*${NORMAL} [${ts}] [${BASE_NAME}:${GREEN}INFO${NORMAL}] : $@" >&2 } #------------------------------------------------------------------------------ warn() { - echo -e " ${YELLOW}*${NORMAL} [$(my_date)] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 + local ts=$(my_date) + if [[ -n "${LOGFILE}" ]] ; then + echo "[${ts}] [${BASE_NAME}WARN] : $@" >>"${LOGFILE}" + fi + echo -e " ${YELLOW}*${NORMAL} [${ts}] [${BASE_NAME}:${YELLOW}WARN${NORMAL}] : $@" >&2 } #------------------------------------------------------------------------------ error() { - echo -e " ${RED}*${NORMAL} [$(my_date)] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2 + local ts=$(my_date) + if [[ -n "${LOGFILE}" ]] ; then + echo "[${ts}] [${BASE_NAME}ERROR]: $@" >>"${LOGFILE}" + fi + echo -e " ${RED}*${NORMAL} [${ts}] [${BASE_NAME}:${RED}ERROR${NORMAL}]: $@" >&2 } #------------------------------------------------------------------------------ -- 2.39.5