Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ee917c8778
Fetching contributors…

Cannot retrieve contributors at this time

file 132 lines (111 sloc) 3.77 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#!/usr/bin/perl
# Copyright (C) 2010-2011 by Derek Hoagland <grickit@gmail.com>
# This file is part of Gambot.
#
# Gambot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gambot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gambot. If not, see <http://www.gnu.org/licenses/>.

###This file handles logging and read/write operations on filehandles.

use strict;
use warnings;

my %back_buffers;

sub pipe_status {
  no warnings 'unopened';
  my $pipe = shift;
  fcntl($pipe, F_SETFL(), O_NONBLOCK());
  my $bytes_read = sysread($pipe,my $buffer,1,0);
  if (defined $bytes_read) {
    if ($bytes_read == 0) {
      return 'dead';
    }
    else {
      if(defined $back_buffers{$pipe}) { $back_buffers{$pipe} = $buffer . $back_buffers{$pipe}; }
      else { $back_buffers{$pipe} = $buffer; }
      return 'ready';
    }
  }
  else {
    return 'later';
  }
}

sub read_lines {
  my ($pipe, $buffer) = (shift,'');
  fcntl($pipe, F_SETFL(), O_NONBLOCK());

  if($back_buffers{$pipe}) {
    $buffer = $back_buffers{$pipe};
  }

  while(my $bytes_read = sysread($pipe,$buffer,1024,length($buffer))) { 1; }
  my @lines = split(/[\r\n]+/,$buffer);

  if($buffer !~ /[\r\n]+$/) { $back_buffers{$pipe} = pop(@lines); }
  else { delete $back_buffers{$pipe}; }

  return @lines;
}



sub generate_timestamps {
  my ($sec,$min,$hour,$mday,$mon,$year,undef,undef,undef) = localtime(time);
  $mon += 1;
  $year += 1900;
  $hour = sprintf("%02d", $hour);
  $min = sprintf("%02d", $min);
  $sec = sprintf("%02d", $sec);
  my $datestamp = "$year-$mon-$mday";
  my $timestamp = "$hour:$min:$sec";
  return $datestamp, $timestamp;
}

sub log_output {
  my ($prefix, $datestamp, $timestamp, $message) = @_;

  unless (&value_get('core','unlogged')) {
    my $filename = &value_get('config','log_directory') . '/' . &value_get('config','base_nick') . "-$datestamp.txt";
    open my $logfile, '>>' . $filename
      or print 'Unable to open logfile "' . $filename . "\".\n" . "Does that directory structure exist?\n";
    print $logfile "$prefix $timestamp $message\015\012";
    close $logfile;
  }
}

sub stdout_output {
  my ($prefix, $timestamp, $message) = @_;
  print "$prefix $timestamp $message\n";
}


#Always logged and displayed
sub error_output {
  my $message = shift;
  my ($datestamp, $timestamp) = &generate_timestamps();
  log_output('BOTERROR',$datestamp,$timestamp,$message);
  stdout_output('BOTERROR',$timestamp,$message);
}

#Always logged and displayed
sub event_output {
  my $message = shift;
  my ($datestamp, $timestamp) = &generate_timestamps();
  log_output('BOTEVENT',$datestamp,$timestamp,$message);
  stdout_output('BOTEVENT',$timestamp,$message);
}

#Always logged, but only displayed if --verbose
sub normal_output {
  my ($prefix,$message) = @_;
  my ($datestamp, $timestamp) = &generate_timestamps();
  log_output($prefix,$datestamp,$timestamp,$message);
  if (&value_get('core','verbose')) {
    stdout_output($prefix,$timestamp,$message);
  }
}

#Logged if --debug. Displayed if --verbose and --debug
sub debug_output {
  my $message = shift;
  my ($datestamp, $timestamp) = &generate_timestamps();
  if (&value_get('core','debug')) {
    log_output('BOTDEBUG',$datestamp,$timestamp,$message);
    if(&value_get('core','verbose')) {
      stdout_output('BOTDEBUG',$timestamp,$message);
    }
  }
}

1;
Something went wrong with that request. Please try again.