Skip to content

Commit

Permalink
Implemented ANSI stripping for logs
Browse files Browse the repository at this point in the history
fixes #23

Change-Id: Ibec02e1efc28f9305cbf688ff018f4d68e055012
  • Loading branch information
ssbarnea committed Jul 3, 2017
1 parent acaee0f commit 81120e0
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 51 deletions.
38 changes: 36 additions & 2 deletions Jenkinsfile
Expand Up @@ -49,8 +49,42 @@ timestamps {
try {

sh "set > .envrc"
// run all test stages for the pipeline shared library
_testsuite

// start-of-unittests
stage('gitClean') {
gitClean()
}

stage("mkdtemp") {
def x = mkdtemp('cd-')
println "${x}"
}

stage("md5") {
def jobname = "${env.JOB_NAME}#${env.BUILD_NUMBER}"
def x = md5(jobname, 6)
println "md5('${jobname}', 6) => ${x}"
}

stage("sh2") {
withEnv(["MAX_LINES=2"]) {
// should display 1,2,4,5 (missing 3) and output.log
def result = sh2 script: "seq 5; exit 200", returnStatus: true
print "[${result}]"
if (result != 200) currentBuild.result = 'FAILURE'

// should generate output-1.log
sh2 "bash bin/ansitest"
// this should not generate a log file or limit the output due
// to returnStdout: true
result = sh2 script: "seq 5", returnStdout: true
print "[${result}]"
// normalize output
result = result.replaceAll('\\r\\n?', '\n').trim()
if (result != '1\n2\n3\n4\n5') currentBuild.result = 'FAILURE'
}
}
// end-of-unittests

} // end-try
catch (error) {
Expand Down
14 changes: 14 additions & 0 deletions bin/ansitest
@@ -0,0 +1,14 @@
#!/bin/bash
B=`tput bold` #BOLD
D=`tput dim` #DIM
U=`tput sgr 0 1` #UNDERLINE
U2=`tput smul` #UNDERLINE2
NOU=`tput rmul` #NO UNDERLINE
H=`tput smso` #HIGHLIGHT
X=`tput sgr0` #RESET
C='tput setaf ' #COLOR

for i in 0 1 2 3 4 5 6 7 ; do
c=`$C$i` && echo $c${B}Lorem ${U}ipsum$NOU $D dolor \
${U2}sit$NOU $c${H}amet$X.
done
45 changes: 0 additions & 45 deletions vars/_testsuite.groovy

This file was deleted.

23 changes: 19 additions & 4 deletions vars/sh2.groovy
Expand Up @@ -40,7 +40,18 @@ def call(Map cmd) {
cmd['timestamps'] = cmd['timestamps'] ?: false
cmd['ansiColor'] = cmd['ansiColor'] ?: true
cmd['returnStdout'] = cmd['returnStdout'] ?: false
cmd['compress'] = cmd['compress'] ?: false

def LOG_FILENAME = false
def LOG_FILENAME_SUFFIX = ".log"
def STRIP_ANSI = "sed 's/\\x1b\\[[0-9;]*m//g'"
def filters = [STRIP_ANSI]

if (cmd['compress']) {
filters.add("gzip -9 --stdout")
LOG_FILENAME_SUFFIX = ".log.gz"
}

if (! cmd['returnStdout']) {

// get log filename
Expand All @@ -51,7 +62,7 @@ def call(Map cmd) {
n=
set -C
until
file=$1${n:+-$n}.log.gz
file=$1${n:+-$n}''' + LOG_FILENAME_SUFFIX + '''
[[ ! -f "$file" ]]
do
((n++))
Expand All @@ -65,15 +76,15 @@ def call(Map cmd) {
awk -F '[^[:alnum:]]+' -v OFS=- \
'{$0=tolower($0); $1=$1; gsub(/^-|-$/, "")} 1')
printf "$(next_logfile $LOG_PREFIX)"
'''
printf "$(next_logfile $LOG_PREFIX)"'''


cmd['script'] = '''#!/bin/bash
# wrapper for limiting stdout output from commands
set -eo pipefail
( ''' + cmd['script'] + ''' ) 2>&1 | \
tee >(gzip -9 --stdout >> ''' + LOG_FILENAME + ''') | \
tee >(''' + filters.join('|') + ''' >> ''' + LOG_FILENAME + ''') | \
stdbuf -i0 -o0 -e0 awk -v offset=${MAX_LINES:-200} \
'{
if (NR <= offset) print;
Expand All @@ -91,6 +102,10 @@ def call(Map cmd) {
'''
}

if (env.DEBUG ?: true) {
echo cmd['script']
}

def error = false
try {
if (cmd['timestamps']) {
Expand Down

0 comments on commit 81120e0

Please sign in to comment.