blob: f8bec06a821f61d46195459614957fc92bbf1c53 [file] [log] [blame]
#! /bin/sh
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# This script prints the last part of a syslog-timestamped file to the
# stdout according to a time specification. By default the last day of
# msgs written to /var/log/messages is printed. Options:
# --last <time> collect most recent msgs up to <time> (default 1 day)
# --bytes N cap collected data at N bytes (default 80K)
#
# Note option order is important (TODO(sleffler) rewrite w/ shflags)
if [ "$1" = "--last" -o "$1" = "-l" ]; then
LAST="$2"; shift; shift
else
LAST='1 day'
fi
if [ "$1" = "--bytes" -o "$1" = "-c" ]; then
MAXDATA="$2"; shift; shift
else
MAXDATA='80K'
fi
LOGFILE=${1:-/var/log/messages}
# create list of files assuming normal log rotation
LOGFILES="${LOGFILE}"
for i in 1 2 3 4 5 6 7 8 9; do
file="${LOGFILE}".$i
[ -f "$file" ] || break
LOGFILES="${LOGFILES} $file"
done
WHEN="now - $LAST"
DATE=$(date -d "$WHEN" +'%FT%T.000000%:z')
tac $LOGFILES \
| awk -v DATE="$DATE" '{ if ($1 < DATE) exit; print }' \
| tac \
| tail -c $MAXDATA