# -*-perl-*-

# $Id: ShowLog.plm,v 1.9 2009/11/11 23:00:41 koczan Exp $

# Front-end to list atoms in the db

# $Log: ShowLog.plm,v $
# Revision 1.9  2009/11/11 23:00:41  koczan
# fixed the help output to be correct
#
# Revision 1.8  2003/06/09 14:45:12  thomas
# schema version of database
#
# Revision 1.7  2002/10/07 19:01:42  thomas
# speed showlog --follow command
#
# Revision 1.6  2002/08/20 21:59:56  thomas
# installed 0.3.0
#
# Revision 1.5  2002/08/14 17:13:08  thomas
# 0.2.9 release
#
# Revision 1.4  2002/07/31 15:17:11  thomas
# *** empty log message ***
#
# Revision 1.3  2002/07/31 14:51:10  thomas
# allow host, id, group, etc.
#
# Revision 1.2  2002/07/10 14:01:29  thomas
# sushi-0.2.4
#
# Revision 1.1  2002/07/08 15:56:35  thomas
# 0.2.3 release
#
# Revision 1.1.1.1  2002/05/21 14:13:35  thomas
# sushi - backup to disk
#

package Sushi::Command::ShowLog;

use strict;
use Getopt::Long;
use Time::localtime;
use Time::Local;

###############################################################################

sub main {
  my ($class) = shift;

  my $follow = 0;
  my $args = {};
  my $opts = {
	      opers => {
			serial => '>',
			timestamp => '>',
		       }
	     };
  my $res;

  Getopt::Long::Configure ("bundling");
  if (!($res = GetOptions (
			   'schedule_group|g=s', \$args->{schedule_group},
			   'type|t=s',           \$args->{type},
			   'name|n=s',           \$args->{name},
			   'id|i=s',             \$args->{id},
			   'host|h=s',           \$args->{host},
			   'pid|p=s',            \$args->{pid},
			   'timestamp|t=s',      \$args->{timestamp},
			   'like',               \$opts->{like},
			   'follow|f',           \$follow
			  ))) {
    $main::log->error (&help);
    return 0;
  }

  defined ($ARGV[0]) and $args->{host} = shift @ARGV;
  defined ($ARGV[0]) and $args->{pid}  = shift @ARGV;
  defined ($ARGV[0]) and $args->{timestamp} = shift @ARGV;

  if ($follow) {
    # Get the current maximum serial number
    $args->{serial} =
      $::sudbh->selectrow_array ("select nextval ('backend_log_serial_seq')");
    defined $args->{serial} and $args->{serial} -= 10;
    while (1) {
      printLog ($args, $opts);
      sleep 2;
    }
  } else {
    if (!defined $args->{timestamp}) {
      # Show log since midnight...
      $args->{timestamp} = timelocal (0,
				      0,
				      0,
				      localtime->mday(),
				      localtime->mon(),
				      localtime->year()
				     );
    }
    printLog ($args, $opts);
  }
  return 1;
}

###############################################################################

sub printLog {
  my $args = shift;
  my $opts = shift;

  my ($row, $line);
  foreach $row (sort { $a->{serial} <=> $b->{serial}}
		@{Sushi::GenDB::fetch (
				       $args,
				       $opts,
				       $::table_defs->{backend_log}
				      )}) {
    ($line = $row->{host}) =~ s/\.cs\.wisc\.edu$//;
    defined $row->{pid} and $line .= "($row->{pid})";
    defined $row->{timestamp} and $line .= ":" . Sushi::Misc::shortTime (Sushi::Misc::date2secs ($row->{timestamp}));
    defined $row->{schedule_group} and $line .= ":$row->{schedule_group}";
    defined $row->{type} and $line .= ":$row->{type}";
    defined $row->{name} and $line .= ":$row->{name}";
    print "$line: $row->{message}\n";
    $args->{serial} = $row->{serial};
  }
}

###############################################################################

sub shortHelp {
  return "Show Logs from sushid and sushiremote processes";
}

###############################################################################

sub help {
  my $help = <<__Eof__

showlog [(--host|-h) <host>] [(--pid|-p) <pid>] \\
\t[(--timestamp|-t) <timestamp>] [(--schedule_group|-g) <group>] \\
\t[(--type|-t) <type>] [(--name|-n) <atom name>] [(--id|-i) <atom id>] \\
\t[<host> [<pid> [<timestamp>]]]

By default, logs will be shown for all hosts and pids, beginning at midnight
of the current day.  If --follow is specified, 10 messages will be
replayed, followed by all subsequent information in 2-second intervals.

Logs are kept for approximately 7 months.

__Eof__
;
}

###############################################################################

1;
