#!/bin/bash
# Filename:      /usr/share/ngcp-ngcpcfg/functions/logs
# Purpose:       helper log functions for ngcpcfg
################################################################################

# console output including timestamps {{{
export timestamp_replacementchars='' # unset by default

console_output() {
  if [ -z "${TIME_FORMAT:-}" ] ; then
    printf -- "$*"
    return 0
  fi

  local timestamp="$(date "$TIME_FORMAT")"

  # indent depending on number of characters in date output
  export timestamp_replacementchars="$(printf -- "%s: " "$timestamp" | sed 's/./ /g')"
  printf -- "$timestamp ${HNAME:-}: $*"
}
# }}}

## logging functions {{{
log_info() {
  logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*"
  console_output "$*\n"
}

# info without ending newline
log_info_n() {
  logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*"
  console_output "$*"
}

log_warn() {
  logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*"
  console_output "Warning: $*\n"
}

log_warn_n() {
  logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*"
  console_output "Warning: $*"
}

log_error() {
  logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Error: $*"
  console_output "Error: $*\n" >&2
}

log_debug() {
  if [ -n "${DEBUG:-}" ] ; then
    logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Debug: $*"
    console_output "DEBUG: $*\n" >&2
  fi
}
## }}}
