Browse code

Implement logging of screen windows.

- Fixes bug 945231.

Change-Id: Ie26555d3e99905ba053510f6b08822b1043aa974

Chmouel Boudjnah authored on 2012/03/05 21:42:48
Showing 1 changed files
... ...
@@ -465,18 +465,22 @@ APACHE_GROUP=${APACHE_GROUP:-$APACHE_USER}
465 465
 # Set LOGFILE to turn on logging
466 466
 # We append '.xxxxxxxx' to the given name to maintain history
467 467
 # where xxxxxxxx is a representation of the date the file was created
468
+if [[ -n "$LOGFILE" || -n "$SCREEN_LOGDIR" ]]; then
469
+    LOGDAYS=${LOGDAYS:-7}
470
+    TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
471
+    CURRENT_LOG_TIME=$(date "+$TIMESTAMP_FORMAT")
472
+fi
473
+
468 474
 if [[ -n "$LOGFILE" ]]; then
469 475
     # First clean up old log files.  Use the user-specified LOGFILE
470 476
     # as the template to search for, appending '.*' to match the date
471 477
     # we added on earlier runs.
472
-    LOGDAYS=${LOGDAYS:-7}
473 478
     LOGDIR=$(dirname "$LOGFILE")
474 479
     LOGNAME=$(basename "$LOGFILE")
475 480
     mkdir -p $LOGDIR
476 481
     find $LOGDIR -maxdepth 1 -name $LOGNAME.\* -mtime +$LOGDAYS -exec rm {} \;
477 482
 
478
-    TIMESTAMP_FORMAT=${TIMESTAMP_FORMAT:-"%F-%H%M%S"}
479
-    LOGFILE=$LOGFILE.$(date "+$TIMESTAMP_FORMAT")
483
+    LOGFILE=$LOGFILE.${CURRENT_LOG_TIME}
480 484
     # Redirect stdout/stderr to tee to write the log file
481 485
     exec 1> >( tee "${LOGFILE}" ) 2>&1
482 486
     echo "stack.sh log $LOGFILE"
... ...
@@ -484,6 +488,23 @@ if [[ -n "$LOGFILE" ]]; then
484 484
     ln -sf $LOGFILE $LOGDIR/$LOGNAME
485 485
 fi
486 486
 
487
+# Set up logging of screen windows
488
+# Set SCREEN_LOGDIR to turn on logging of screen windows to the
489
+# directory specified in SCREEN_LOGDIR, we will log to the the file
490
+# screen-$SERVICE_NAME-$TIMESTAMP.log in that dir and have a link
491
+# screen-$SERVICE_NAME.log to the latest log file.
492
+# Logs are kept for as long specified in LOGDAYS.
493
+if [[ -n "$SCREEN_LOGDIR" ]]; then
494
+
495
+    # We make sure the directory is created.
496
+    if [[ -d "$SCREEN_LOGDIR" ]]; then
497
+        # We cleanup the old logs
498
+        find $SCREEN_LOGDIR -maxdepth 1 -name screen-\*.log -mtime +$LOGDAYS -exec rm {} \;
499
+    else
500
+        mkdir -p $SCREEN_LOGDIR
501
+    fi
502
+fi
503
+
487 504
 # So that errors don't compound we exit on any errors so you see only the
488 505
 # first error that occurred.
489 506
 trap failed ERR
... ...
@@ -1357,6 +1378,12 @@ function screen_it {
1357 1357
         # creating a new window in screen and then sends characters, so if
1358 1358
         # bash isn't running by the time we send the command, nothing happens
1359 1359
         sleep 1.5
1360
+
1361
+        if [[ -n ${SCREEN_LOGDIR} ]]; then
1362
+            screen -S stack -p $1 -X logfile ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log
1363
+            screen -S stack -p $1 -X log on
1364
+            ln -sf ${SCREEN_LOGDIR}/screen-${1}.${CURRENT_LOG_TIME}.log ${SCREEN_LOGDIR}/screen-${1}.log
1365
+        fi
1360 1366
         screen -S stack -p $1 -X stuff "$2$NL"
1361 1367
     fi
1362 1368
 }