5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
26 # Copyright 2007 Sun Microsystems, Inc. All rights reserved.
27 # Use is subject to license terms.
29 # ident "@(#)logapi.kshlib 1.2 07/03/14 SMI"
33 # This is a ksh function library. It is intended to be sourced into
34 # other ksh scripts and not executed directly.
37 . ${STF_SUITE}/include/stf.shlib
40 # Send a debug message to stderr, if $STF_DEBUG set.
44 [ -z "$STF_DEBUG" ] && return
54 _printline ASSERTION: "$@"
66 # Execute a positive test and exit $STF_FAIL is test fails
68 # $@ - command to execute
73 (( $? != 0 )) && log_fail
76 # Execute a command that must exit $1
78 # $@ - command to execute
81 typeset exitcode_wanted=$1
85 (( $? != $exitcode_wanted )) && log_fail
88 # Execute a negative test and exit $STF_FAIL if test passes
90 # $@ - command to execute
95 (( $? != 0 )) && log_fail
98 # Execute a command that should only be logged if it fails.
100 # $@ - command to execute
105 [ $status -eq 0 ] && return
106 _printerror "$@" "unexpectedly exited $status"
109 # Execute and print command with status where success equals non-zero result
110 # or output includes expected keyword
112 # $2-$@ - command to execute
114 # Summary: execute $@. Return 1 if any of the following hold:
115 # 1) The command exited 0, 127, 138, or 139
116 # 2) The command's stderr included "internal error" or
119 # return 0 if command fails, or the output contains the keyword expected,
125 typeset logfile="$TMPDIR/log.$$"
128 while [[ -e $logfile ]]; do
129 logfile="$logfile.$$"
134 out="/bin/cat $logfile"
137 if (( $status == 0 )); then
139 _printerror "$@" "unexpectedly exited $status"
141 elif (( $status == 127 )); then
143 _printerror "$@" "unexpectedly exited $status (File not found)"
144 # bus error - core dump
145 elif (( $status == 138 )); then
147 _printerror "$@" "unexpectedly exited $status (Bus Error)"
148 # segmentation violation - core dump
149 elif (( $status == 139 )); then
151 _printerror "$@" "unexpectedly exited $status (SEGV)"
153 $out | /usr/bin/egrep -i "internal error|assertion failed" \
155 # internal error or assertion failed
156 if (( $? == 0 )); then
158 _printerror "$@" "internal error or assertion failure" \
164 if (( $ret == 0 )); then
165 [[ -n $LOGAPI_DEBUG ]] && print $($out)
166 _printsuccess "$@" "exited $status"
169 _recursive_output $logfile "false"
173 # Execute and print command; unconditionally return its exit code.
174 # Useful for code that needs to do more specialized exit status filtering.
177 typeset logfile="$TMPDIR/log.$$"
179 while [[ -e $logfile ]]; do
180 logfile="$logfile.$$"
185 _printline "EXECUTED (exited $status): $@"
186 _recursive_output $logfile "false"
190 # Execute and print command with status where success equals zero result
192 # $@ command to execute
194 # Summary: run $@. return 1 if its exit status was nonzero or if it printed
195 # "internal error" or "assertion failed" to stderr.
196 # print stderr on failure or if LOGAPI_DEBUG is set.
198 # return command exit status
203 typeset logfile="$TMPDIR/log.$$"
205 while [[ -e $logfile ]]; do
206 logfile="$logfile.$$"
211 out="/bin/cat $logfile"
213 if (( $status != 0 )) ; then
215 _printerror "$@" "exited $status"
217 $out | /usr/bin/egrep -i "internal error|assertion failed" \
219 # internal error or assertion failed
220 if [[ $? -eq 0 ]]; then
222 _printerror "$@" "internal error or assertion failure" \
226 [[ -n $LOGAPI_DEBUG ]] && print $($out)
230 _recursive_output $logfile "false"
234 # Set an exit handler
236 # $@ - function(s) to perform on exit
247 # Perform cleanup and exit $STF_PASS
253 _endlog $STF_PASS "$@"
256 # Perform cleanup and exit $STF_FAIL
262 _endlog $STF_FAIL "$@"
265 # Perform cleanup and exit $STF_UNRESOLVED
269 function log_unresolved
271 _endlog $STF_UNRESOLVED "$@"
274 # Perform cleanup and exit $STF_NOTINUSE
278 function log_notinuse
280 _endlog $STF_NOTINUSE "$@"
283 # Perform cleanup and exit $STF_UNSUPPORTED
287 function log_unsupported
289 _endlog $STF_UNSUPPORTED "$@"
292 # Perform cleanup and exit $STF_UNTESTED
296 function log_untested
298 _endlog $STF_UNTESTED "$@"
301 # Perform cleanup and exit $STF_UNINITIATED
305 function log_uninitiated
307 _endlog $STF_UNINITIATED "$@"
310 # Perform cleanup and exit $STF_NORESULT
314 function log_noresult
316 _endlog $STF_NORESULT "$@"
319 # Perform cleanup and exit $STF_WARNING
325 _endlog $STF_WARNING "$@"
328 # Perform cleanup and exit $STF_TIMED_OUT
332 function log_timed_out
334 _endlog $STF_TIMED_OUT "$@"
337 # Perform cleanup and exit $STF_OTHER
343 _endlog $STF_OTHER "$@"
350 # Perform cleanup and exit
352 # Summary: Runs any cleanup routine registered with log_onexit. Prints a
353 # message and exits $1. Note: the _recursive_output does
354 # nothing, because the rest of this api guarantees that the
355 # logfile will not exist.
357 # $2-$n - message text
361 typeset logfile="$TMPDIR/log.$$"
362 _recursive_output $logfile
364 export STF_EXITCODE=$1
366 (( ${#@} > 0 )) && _printline "$@"
367 if [[ -n $_CLEANUP ]] ; then
368 typeset cleanup=$_CLEANUP
370 log_note "Performing local cleanup via log_onexit ($cleanup)"
376 # Output a formatted line
382 print `/bin/date +%H:%M:%S` "$@"
385 # Output an error message
391 _printline ERROR: "$@"
394 # Output a success message
398 function _printsuccess
400 _printline SUCCESS: "$@"
403 # Output logfiles recursively
406 # $2 - indicate whether output the start file itself, default as yes.
408 function _recursive_output #logfile
412 while [[ -e $logfile ]]; do
413 if [[ -z $2 || $logfile != $1 ]]; then
417 logfile="$logfile.$$"