[9f29ba6] | 1 | #!/bin/bash |
---|
| 2 | #/** |
---|
| 3 | #@file System.lib |
---|
[9f57de01] | 4 | #@brief Librería o clase System |
---|
[9f29ba6] | 5 | #@class System |
---|
| 6 | #@brief Funciones básicas del sistema. |
---|
| 7 | #@version 0.9 |
---|
| 8 | #@warning License: GNU GPLv3+ |
---|
| 9 | #*/ |
---|
| 10 | |
---|
[2e15649] | 11 | |
---|
[9f29ba6] | 12 | #/** |
---|
[cfeabbf] | 13 | # ogEcho [loglevel] message... |
---|
[9f57de01] | 14 | #@brief Muestra mensajes en consola y lo registra en fichero de incidencias. |
---|
| 15 | #@arg \c str_loglevel nivel de registro de incidencias. |
---|
| 16 | #@arg \c str_message mensaje (puede recibir más de 1 parámetro. |
---|
| 17 | #@return Mensaje mostrado. |
---|
[aae34f6] | 18 | #@note El nivel de ayuda \c (help) no se registra en el fichero de incidencias. |
---|
[cfeabbf] | 19 | #@version 0.9 - Primera versión para OpenGNSys |
---|
[aae34f6] | 20 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[9f57de01] | 21 | #@date 2009-07-23 |
---|
[9f29ba6] | 22 | #*/ |
---|
[9f57de01] | 23 | function ogEcho () { |
---|
[9f29ba6] | 24 | |
---|
[59f9ad2] | 25 | # Variables locales |
---|
[cfeabbf] | 26 | local LOGLEVEL LOGFILE DATETIME |
---|
[9f57de01] | 27 | |
---|
[59f9ad2] | 28 | #/// Selección del nivel de registro (opcional). |
---|
[9f57de01] | 29 | case "$1" in |
---|
[d071d9b] | 30 | help) shift ;; |
---|
| 31 | info) LOGLEVEL=$1; shift ;; |
---|
| 32 | warning) LOGLEVEL=$1; shift ;; |
---|
| 33 | error) LOGLEVEL=$1; shift ;; |
---|
| 34 | *) ;; |
---|
[9f57de01] | 35 | esac |
---|
| 36 | |
---|
| 37 | #/// Registro de incidencias. |
---|
[cfeabbf] | 38 | #[ -w "$OGLOGFILE" ] && LOGFILE="-f $OGLOGFILE" |
---|
| 39 | #DATETIME=$(date +"%F %T") # pendiente en cliente Initrd |
---|
| 40 | if [ -n "$LOGLEVEL" ]; then |
---|
| 41 | logger -s -t "OpenGNSys $LOGLEVEL" $LOGFILE "$DATETIME $*" |
---|
| 42 | else |
---|
| 43 | echo "$*" |
---|
[9f57de01] | 44 | fi |
---|
[9f29ba6] | 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | #/** |
---|
[a5df9b9] | 49 | # ogRaiseError errcode ["errmessage" ...] |
---|
[9f57de01] | 50 | #@brief Devuelve el mensaje y el código de error correspondiente. |
---|
| 51 | #@arg \c int_errcode código de error. |
---|
[a5df9b9] | 52 | #@arg \c str_errmessage mensajes complementarios de error. |
---|
[9f57de01] | 53 | #@return Mensaje de error. |
---|
| 54 | #@warning No definidas |
---|
[aae34f6] | 55 | #@note Mensajes internacionales del fichero de idiomas. |
---|
| 56 | #@version 0.9 - Primera versión para OpenGNSys. |
---|
| 57 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
[9f57de01] | 58 | #@date 2009-07-21 |
---|
[9f29ba6] | 59 | #*/ |
---|
[9f57de01] | 60 | function ogRaiseError () { |
---|
[9f29ba6] | 61 | |
---|
[59f9ad2] | 62 | # Variables locales |
---|
[a5df9b9] | 63 | local MSG CODE |
---|
[2e15649] | 64 | |
---|
[5dbb046] | 65 | #/// Obtener código y mensaje de error. |
---|
[a5df9b9] | 66 | CODE=$1 |
---|
| 67 | case "$CODE" in |
---|
[be81649] | 68 | $OG_ERR_FORMAT) MSG="$MSG_ERR_FORMAT $*" ;; |
---|
[a5df9b9] | 69 | $OG_ERR_NOTFOUND) MSG="$MSG_ERR_NOTFOUND \"$2\"" ;; |
---|
[aae34f6] | 70 | $OG_ERR_PARTITION) MSG="$MSG_ERR_PARTITION \"$2\"" ;; |
---|
[a79dd508] | 71 | $OG_ERR_LOCKED) MSG="$MSG_ERR_LOCKED \"$2\"" ;; |
---|
[5ceca9c] | 72 | $OG_ERR_IMAGE) MSG="$MSG_ERR_IMAGE \"$2\"" ;; |
---|
[be81649] | 73 | $OG_ERR_NOTOS) MSG="$MSG_ERR_NOTOS \"$2\"" ;; |
---|
| 74 | $OG_ERR_NOTEXEC) MSG="$MSG_ERR_NOTEXEC \"$2\"" ;; |
---|
[cfeabbf] | 75 | *) MSG="$MSG_ERR_GENERIC"; CODE=$OG_ERR_GENERIC ;; |
---|
[2e15649] | 76 | esac |
---|
| 77 | |
---|
[5dbb046] | 78 | #/// Mostrar mensaje de error y salir con el código indicado. |
---|
[aae34f6] | 79 | ogEcho error "${FUNCNAME[1]}: $MSG" >&2 |
---|
[a5df9b9] | 80 | return $CODE |
---|
[9f29ba6] | 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
[aae34f6] | 84 | #/** |
---|
| 85 | # ogHelp ["function" ["format" ["example" ... ]]] |
---|
| 86 | #@brief Muestra mensaje de ayuda para una función determinda. |
---|
| 87 | #@arg \c str_function Nombre de la función. |
---|
| 88 | #@arg \c str_format Formato de ejecución de la función. |
---|
| 89 | #@arg \c str_example Ejemplo de ejecución de la función. |
---|
| 90 | #@return Salida de ayuda. |
---|
| 91 | #@note Si no se indican parámetros, la función se toma de la variable \c $FUNCNAME |
---|
| 92 | #@note La descripción de la función se toma de la variable compuesta por \c MSG_FUNC_$función incluida en el fichero de idiomas. |
---|
| 93 | #@note Pueden especificarse varios mensajes con ejemplos. |
---|
| 94 | #@version 0.9 - Primera versión para OpenGNSys. |
---|
| 95 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
| 96 | #@date 2009-07-27 |
---|
| 97 | #*/ |
---|
| 98 | function ogHelp () { |
---|
| 99 | |
---|
[59f9ad2] | 100 | # Variables locales. |
---|
[aae34f6] | 101 | local FUNC MSG |
---|
| 102 | |
---|
| 103 | #/// Mostrar función, descripción y formato. |
---|
| 104 | FUNC="${1:-${FUNCNAME[${#FUNCNAME[*]}-1]}}" |
---|
| 105 | MSG="MSG_HELP_$FUNC" |
---|
| 106 | ogEcho help "$MSG_FUNCTION $FUNC: ${!MSG}" |
---|
| 107 | [ -n "$2" ] && ogEcho help " $MSG_FORMAT: $2" |
---|
| 108 | #/// Mostrar ejemplos. |
---|
| 109 | shift 2 |
---|
| 110 | while [ $# -gt 0 ]; do |
---|
| 111 | ogEcho help " $MSG_EXAMPLE: $1" |
---|
| 112 | shift |
---|
| 113 | done |
---|
| 114 | } |
---|
| 115 | |
---|