[2] | 1 | #!/bin/ksh |
---|
| 2 | |
---|
| 3 | #************************************************************** |
---|
| 4 | # Author: Patrick Brockmann, Martial Mancip |
---|
[373] | 5 | # Contact: Patrick.Brockmann__at__cea.fr Martial.Mancip__at__ipsl.jussieu.fr |
---|
| 6 | # $Revision:: $ Revision of last commit |
---|
| 7 | # $Author:: $ Author of last commit |
---|
| 8 | # $Date:: $ Date of last commit |
---|
[2] | 9 | # IPSL (2006) |
---|
| 10 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 11 | # |
---|
| 12 | #************************************************************** |
---|
| 13 | |
---|
| 14 | #================================================== |
---|
| 15 | # The documentation of this file can be automatically generated |
---|
| 16 | # if you use the prefix #D- for comments to be extracted. |
---|
| 17 | # Extract with command: cat lib* | grep "^#D-" | cut -c "4-" |
---|
| 18 | #================================================== |
---|
| 19 | |
---|
| 20 | #================================================== |
---|
| 21 | # Add high level verbosity |
---|
| 22 | typeset -i Verbosity=${Verbosity:=3} |
---|
| 23 | |
---|
| 24 | #================================================== |
---|
| 25 | # DEBUG_debug |
---|
| 26 | # Add low level verbosity |
---|
[98] | 27 | typeset DEBUG_debug=${DEBUG_debug:=false} |
---|
[2] | 28 | |
---|
| 29 | if ( $DEBUG_debug ) ; then |
---|
| 30 | if [ -f stack ] ; |
---|
| 31 | then |
---|
| 32 | echo "Stack of an libIGCM job :" >> stack |
---|
| 33 | else |
---|
| 34 | echo "Stack of an libIGCM job :" > stack |
---|
| 35 | fi |
---|
| 36 | fi |
---|
| 37 | |
---|
| 38 | #================================================== |
---|
| 39 | # NULL_STR |
---|
| 40 | # Default null string |
---|
| 41 | typeset -r NULL_STR="_0_" |
---|
| 42 | |
---|
| 43 | #================================================== |
---|
| 44 | # libIGCM_CurrentTag |
---|
| 45 | # Current libIGCM tag, check compatibilty with *.card |
---|
| 46 | typeset -r libIGCM_CurrentTag="1.0" |
---|
| 47 | |
---|
| 48 | #================================================== |
---|
| 49 | # Exit Flag (internal debug) |
---|
| 50 | # When true, end the master loop AFTER SAVES FILES |
---|
| 51 | ExitFlag=false |
---|
| 52 | |
---|
| 53 | #================================================== |
---|
| 54 | # Declare a stack of functions calls |
---|
| 55 | |
---|
| 56 | # insert last argument of the Stack |
---|
[54] | 57 | #set -A IGCM_debug_Stack ${NULL_STR} |
---|
| 58 | #set -A IGCM_debug_StackArgs ${NULL_STR} |
---|
[59] | 59 | unset IGCM_debug_Stack |
---|
| 60 | unset IGCM_debug_StackArgs |
---|
[54] | 61 | IGCM_debug_Stack[0]=${NULL_STR} |
---|
| 62 | IGCM_debug_StackArgs[0]=${NULL_STR} |
---|
[2] | 63 | IGCM_debug_LenStack=0 |
---|
| 64 | |
---|
| 65 | #D-#================================================================== |
---|
| 66 | #D-function IGCM_debug_CallStack |
---|
| 67 | #D-* Purpose: Echo the Stack |
---|
| 68 | #D- |
---|
| 69 | function IGCM_debug_CallStack { |
---|
| 70 | echo |
---|
| 71 | echo "!!!!!!!!!!!!!!!!!!!!!!!!!!" |
---|
| 72 | echo "!! IGCM_debug_CallStack !!" |
---|
| 73 | echo "!------------------------!" |
---|
| 74 | echo |
---|
| 75 | if ( $DEBUG_debug ) ; then |
---|
| 76 | # La pile d'appels est affichée de la plus vieille à la plus récente |
---|
| 77 | # (c'est donc l'inverse de la norme d'affichage). |
---|
| 78 | typeset i decal |
---|
| 79 | (( i=0 )) |
---|
| 80 | until [ $i -eq ${IGCM_debug_LenStack} ]; do |
---|
| 81 | decal=0 |
---|
| 82 | until [ $decal -eq ${i} ]; do |
---|
| 83 | printf -- ' ' |
---|
| 84 | let $(( decal=decal+1 )) |
---|
| 85 | done |
---|
| 86 | echo "$i - ${IGCM_debug_Stack[$(( $IGCM_debug_LenStack-$i-1 ))]}"\ |
---|
| 87 | "(${IGCM_debug_StackArgs[$(( $IGCM_debug_LenStack-$i-1 ))]})" |
---|
| 88 | let $((i=i+1)) |
---|
| 89 | done |
---|
[157] | 90 | echo "!------------------------!" |
---|
[2] | 91 | fi |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | #D-#================================================================== |
---|
| 95 | #D-function IGCM_debug_PushStack |
---|
| 96 | #D-* Purpose: Push a function name in the stack |
---|
| 97 | #D- |
---|
| 98 | function IGCM_debug_PushStack { |
---|
| 99 | if ( $DEBUG_debug ) ; then |
---|
| 100 | typeset decal |
---|
| 101 | echo >> stack |
---|
| 102 | decal=0 |
---|
| 103 | while [ $decal -lt ${IGCM_debug_LenStack} ]; do |
---|
| 104 | # printf -- ' ' >> stack |
---|
| 105 | printf ' ' >> stack |
---|
| 106 | let $(( decal=decal+1 )) |
---|
| 107 | done |
---|
| 108 | echo "> ${IGCM_debug_LenStack} : ${@}" >> stack |
---|
| 109 | |
---|
| 110 | # We add function call name on beginning of the stack |
---|
[264] | 111 | set +A IGCM_debug_Stack -- ${1} ${IGCM_debug_Stack[*]} |
---|
[2] | 112 | |
---|
| 113 | # We include the "null" Args in the beginning of the StackArgs |
---|
| 114 | set +A IGCM_debug_StackArgs ${NULL_STR} ${IGCM_debug_StackArgs[*]} |
---|
| 115 | # Then, we shift StackArgs tabular |
---|
| 116 | if [ $# -gt 1 ]; then |
---|
| 117 | shift; |
---|
[264] | 118 | IGCM_debug_StackArgs[0]=$(echo ${@} | sed -e "s/\ /,/g") >&2 |
---|
[2] | 119 | fi |
---|
| 120 | (( IGCM_debug_LenStack = IGCM_debug_LenStack + 1 )) |
---|
| 121 | |
---|
| 122 | # If you want to print CallStack each time : |
---|
| 123 | # IGCM_debug_CallStack |
---|
| 124 | fi |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | #D-#================================================================== |
---|
| 128 | #D-function IGCM_debug_PopStack |
---|
| 129 | #D-* Purpose: Pop a function name in the stack |
---|
| 130 | #D- |
---|
| 131 | function IGCM_debug_PopStack { |
---|
| 132 | if ( $DEBUG_debug ) ; then |
---|
[36] | 133 | if ( ${ExitFlag} ) ; then |
---|
| 134 | echo '!!! ExitFlag has been activated !!!' >> stack |
---|
| 135 | fi |
---|
[2] | 136 | if [ "${IGCM_debug_Stack[0]}" = "${1}" ]; then |
---|
| 137 | let $(( IGCM_debug_LenStack = IGCM_debug_LenStack - 1 )) |
---|
| 138 | |
---|
| 139 | IGCM_debug_Stack[0]="" |
---|
| 140 | IGCM_debug_StackArgs[0]="" |
---|
| 141 | |
---|
[264] | 142 | set +A tmpStack -- ${IGCM_debug_Stack[*]} |
---|
| 143 | unset IGCM_debug_Stack |
---|
| 144 | set +A IGCM_debug_Stack -- ${tmpStack[*]} |
---|
| 145 | |
---|
| 146 | set +A tmpStack -- ${IGCM_debug_StackArgs[*]} |
---|
| 147 | unset IGCM_debug_StackArgs |
---|
| 148 | set +A IGCM_debug_StackArgs -- ${tmpStack[*]} |
---|
| 149 | unset tmpStack |
---|
[2] | 150 | else |
---|
| 151 | echo 'IGCM_debug_Exit : stack is corrupted ! LenStack =' ${IGCM_debug_LenStack} |
---|
| 152 | IGCM_debug_Exit $@ |
---|
| 153 | fi |
---|
| 154 | typeset decal=0 |
---|
| 155 | while [ $decal -lt ${IGCM_debug_LenStack} ]; do |
---|
| 156 | # printf -- ' ' >> stack |
---|
| 157 | printf ' ' >> stack |
---|
| 158 | (( decal=decal+1 )) |
---|
| 159 | done |
---|
| 160 | echo "< ${IGCM_debug_LenStack} : ${@}" >> stack |
---|
| 161 | |
---|
| 162 | if [ ${IGCM_debug_LenStack} = 0 ]; then |
---|
| 163 | # Reset array only when necessary |
---|
| 164 | #echo |
---|
| 165 | #IGCM_debug_Print 3 "Clean stack array" |
---|
| 166 | #echo |
---|
[54] | 167 | #set -A IGCM_debug_Stack ${NULL_STR} |
---|
| 168 | #set -A IGCM_debug_StackArgs ${NULL_STR} |
---|
[264] | 169 | unset IGCM_debug_Stack |
---|
| 170 | unset IGCM_debug_StackArgs |
---|
[54] | 171 | IGCM_debug_Stack[0]=${NULL_STR} |
---|
| 172 | IGCM_debug_StackArgs[0]=${NULL_STR} |
---|
[2] | 173 | fi |
---|
| 174 | |
---|
| 175 | fi |
---|
| 176 | |
---|
| 177 | #IGCM_debug_CallStack |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | #D-#================================================================== |
---|
| 181 | #D-function IGCM_debug_Exit |
---|
| 182 | #D-* Purpose: Print Call Stack and set ExitFlag to true |
---|
| 183 | #D- |
---|
| 184 | function IGCM_debug_Exit { |
---|
| 185 | IGCM_debug_PushStack "IGCM_debug_Exit" |
---|
| 186 | echo "IGCM_debug_Exit : " "${@}" |
---|
| 187 | IGCM_debug_CallStack |
---|
[108] | 188 | ExitFlag=true |
---|
[2] | 189 | IGCM_debug_PopStack "IGCM_debug_Exit" |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | #D-#================================================== |
---|
| 193 | #D-function IGCM_debug_Verif_Exit |
---|
| 194 | #D-* Purpose: exit with number 1 if ExitFlag is true |
---|
| 195 | #D- |
---|
| 196 | function IGCM_debug_Verif_Exit { |
---|
| 197 | if ( ${ExitFlag} ) ; then |
---|
[409] | 198 | # Plan to send an email here with IGCM_sys_SendMail |
---|
[2] | 199 | IGCM_card_WriteOption ${SUBMIT_DIR}/run.card Configuration PeriodState "Fatal" |
---|
| 200 | echo "IGCM_debug_Verif_Exit : Something wrong append." |
---|
| 201 | echo " EXIT THE JOB." |
---|
| 202 | echo |
---|
| 203 | if ( $DEBUG_debug ) ; then |
---|
[113] | 204 | IGCM_sys_Cp stack ${SUBMIT_DIR}/stack_error |
---|
[2] | 205 | echo "Your files on ${R_OUT} :" |
---|
| 206 | IGCM_sys_Tree ${R_SAVE} |
---|
| 207 | echo |
---|
| 208 | fi |
---|
| 209 | date |
---|
| 210 | exit 1 |
---|
| 211 | fi |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | #D-#================================================== |
---|
| 215 | #D-function IGCM_debug_Verif_Exit_Post |
---|
| 216 | #D-* Purpose: exit with number 1 if ExitFlag is true for Post-treatment |
---|
| 217 | #D- |
---|
| 218 | function IGCM_debug_Verif_Exit_Post { |
---|
| 219 | if ( ${ExitFlag} ) ; then |
---|
| 220 | echo "IGCM_debug_Verif_Exit_Post : Something wrong append." |
---|
[409] | 221 | # If SpaceName is PROD then we stop if post_processing fails |
---|
| 222 | # Plan to send an email here with IGCM_sys_SendMail |
---|
| 223 | if [ X${config_UserChoices_SpaceName} = XPROD ] ; then |
---|
| 224 | echo " EXIT THE JOB." |
---|
| 225 | echo |
---|
| 226 | if ( $DEBUG_debug ) ; then |
---|
| 227 | IGCM_sys_Cp stack ${SUBMIT_DIR}/stack_error |
---|
| 228 | echo |
---|
| 229 | fi |
---|
| 230 | date |
---|
| 231 | exit 1 |
---|
| 232 | else |
---|
| 233 | echo " BUT WE DO NOT EXIT THE JOB." |
---|
| 234 | echo |
---|
| 235 | date |
---|
| 236 | fi |
---|
[2] | 237 | fi |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | #D-#================================================================== |
---|
| 241 | #D-function IGCM_debug_Print |
---|
| 242 | #D-* Purpose: Print arguments according to a level of verbosity. |
---|
| 243 | #D- |
---|
| 244 | function IGCM_debug_Print |
---|
| 245 | { |
---|
| 246 | typeset level=$1 |
---|
| 247 | shift |
---|
| 248 | |
---|
[286] | 249 | if [ X"${1}" = X"-e" ]; then |
---|
| 250 | typeset cmd_echo="echo -e" |
---|
| 251 | shift |
---|
| 252 | else |
---|
| 253 | typeset cmd_echo="echo" |
---|
| 254 | fi |
---|
| 255 | |
---|
[2] | 256 | if [ ${level} -le ${Verbosity} ] ; then |
---|
| 257 | typeset i |
---|
| 258 | case "${level}" in |
---|
| 259 | 1) for i in "$@" ; do |
---|
[286] | 260 | ${cmd_echo} "--Debug1-->" ${i} |
---|
[2] | 261 | done ;; |
---|
| 262 | 2) for i in "$@" ; do |
---|
[286] | 263 | ${cmd_echo} "--------Debug2-->" ${i} |
---|
[2] | 264 | done ;; |
---|
| 265 | 3) for i in "$@" ; do |
---|
[286] | 266 | ${cmd_echo} "--------------Debug3-->" ${i} |
---|
[2] | 267 | done ;; |
---|
| 268 | esac |
---|
| 269 | fi |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | #D-#================================================================== |
---|
| 273 | #D-function IGCM_debug_PrintVariables |
---|
| 274 | #D-* Purpose: Print arguments when match a pattern |
---|
| 275 | #D- according to a level of verbosity. |
---|
| 276 | function IGCM_debug_PrintVariables |
---|
| 277 | { |
---|
| 278 | typeset level=$1 |
---|
| 279 | shift |
---|
| 280 | |
---|
[54] | 281 | list=$( set | grep $1 | sed -e "s/'//g" ) |
---|
| 282 | |
---|
| 283 | if [ "X${list}" != X ] ; then |
---|
[2] | 284 | IGCM_debug_Print ${level} ${list} |
---|
| 285 | fi |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | #D-#================================================================== |
---|
| 289 | #D-function IGCM_debug_Check |
---|
| 290 | #D- * Purpose: Check the present file by comparison with a reference file |
---|
| 291 | function IGCM_debug_Check |
---|
| 292 | { |
---|
| 293 | #--------------------- |
---|
| 294 | if [ ! -n "${libIGCM}" ] ; then |
---|
| 295 | echo "Check libIGCM_debug ..........................................[ FAILED ]" |
---|
| 296 | echo "--Error--> libIGCM variable is not defined" |
---|
| 297 | exit 2 |
---|
| 298 | fi |
---|
| 299 | |
---|
| 300 | #--------------------- |
---|
| 301 | if [ ! -n "${Verbosity}" ] ; then |
---|
| 302 | echo "Check libIGCM_debug ..........................................[ FAILED ]" |
---|
| 303 | echo "--Error--> Verbosity variable is not defined" |
---|
| 304 | exit 3 |
---|
| 305 | fi |
---|
| 306 | |
---|
| 307 | #--------------------- |
---|
| 308 | ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh > IGCM_debug_Test.ref.failed 2>&1 |
---|
| 309 | sleep 2 |
---|
| 310 | |
---|
| 311 | if diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref > /dev/null 2>&1 ; then |
---|
| 312 | echo "Check libIGCM_debug ..............................................[ OK ]" |
---|
| 313 | rm -f IGCM_debug_Test.ref.failed |
---|
| 314 | else |
---|
| 315 | echo "Check libIGCM_debug ..........................................[ FAILED ]" |
---|
| 316 | echo "--Error--> Execution of ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ksh" |
---|
| 317 | echo " has produced the file IGCM_debug_Test.ref.failed" |
---|
| 318 | echo " Please analyse differences with the reference file by typing:" |
---|
| 319 | echo " diff IGCM_debug_Test.ref.failed ${libIGCM}/libIGCM_debug/IGCM_debug_Test.ref" |
---|
| 320 | echo " Report errors to the author: Patrick.Brockmann@cea.fr" |
---|
| 321 | exit 4 |
---|
| 322 | fi |
---|
| 323 | #--------------------- |
---|
| 324 | } |
---|