# Automatic logging of all incoming traffic
# Saves to a gzipped file in ~/.mcl/logs/sessionName/CurrentDate

my $logopen = 0;
my $date = `date`;
my $filename = "med-default.gz";

if( $date =~ /(\w+)\s+(\w+)\s+(\w+)\s+/ ) {
    $filename = "med-$1$2$3.gz";
}

#print $logfilename;

print "${BGreen}Loading log.pl -> log file name $filename\n";


$ColorCode = "\xEA";

sub log_line {
    my ($line) = shift;
    $line =~ s/${ColorCode}.//gs;
	if( $logopen ) {
		print GZLOG $line, "\n"; 
	 }
}

sub log_output {
    log_line("$_");
}

sub log_input {
    log_line ( sprintf("\n>> %s\n", $_) );
}

sub stop_log {
    output_remove("log_output");
    userinput_remove("log_input");
    loselink_remove("stop_log");
	$logopen = 0;
    close (GZLOG);
}

sub cmd_logclose {
  print "${BGreen}Log Closed.\n";
  stop_log();
}

sub cmd_logopen {
    my $logdir = "/home/estiann/.mcl/logs/med/";
    system("mkdir -p $logdir");
    my $filename = $_;
    my $logfile = "$logdir/$filename";
   
    if (open (GZLOG, "|/bin/gzip -c >> $logfile")) {
		$logopen = 1;
        output_add("log_output");
	prompt_add("log_output");
        userinput_add("log_input");
        loselink_add("stop_log");
	print "${BGreen}Log Opened.\n";
    } else {
        print "Error opening log file $logdir/$filename: $!\n";
    }
}

1;


