Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   Writing to file (http://www.programmingforums.org/showthread.php?t=4338)

sonson Jun 7th, 2005 3:23 AM

Writing to file
 
Hey everyone! This is a suburb site that i discovered..it really helped me..
i just have a problem and hope someone would be able to help me...its pretty simple question..

ok what i have here in my script is a something that login to a port and reads logs from its and writes these logs into files which are created and stored in the system. i got 3/4 of script going and it works perfect by capturing the logs continously..what i need is to add some extra things....right now what is does is that ir writes to the file opeend contiously but what i want it to do is to create a file for each day so when it reaches 11.59pm..it creates a new files with the new date and starts storing the logs to it and so on ...hence when i want to access the file for a specific date at anytime i can do so...
any one can ehlp with that...what isma guseiing is that it will b a while loops which will creates a new file whenever it notices a chaneg in date ...

how do u do tht..any idea or scripts would b great..iam sure there is a script that would b written for that purpose already...thnks in advance,...

my script is the following..

#! /usr/bin/perl -w
# client1.pl - a client
#----------------

use strict;
use Socket;
use POSIX 'strftime';

# initialize host and port
my $host = shift || '172.xx.10.30';
my $port = shift || 5221;

my $proto = getprotobyname('tcp');

# get the port address
my $iaddr = inet_aton($host);
my $paddr = sockaddr_in($port, $iaddr);
# create the socket, connect to the port
socket(SOCKET, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCKET, $paddr) or die "connect: $!";

my @t = localtime(time) ;

open(OUTPUT_FILE, ">>/data2/elsonba/zizo/" . ($t[5]+1900) . "-" . ($t[4]+1) . "-$t[3].log");

select OUTPUT_FILE; $| = 1;

my $line;

while (<SOCKET>)
{
print OUTPUT_FILE "$_\n";
}
close SOCKET or die "close: $!";
close OUTPUT_FILE;

sykkn Jun 7th, 2005 9:45 AM

you are on the right track. Instead of generating the file name timestamp while opening the file ... create and store it to be used for the check you will do in you while loop ... then before writing ... generate another copy of the timestamp using the current time and compare the two ... if they differ, close the current log file and reopen with the new filename.

:


# I truncated a bit ...

my @t = localtime(time) ;
my $fileTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];
open(OUTPUT_FILE, ">>/data2/elsonba/zizo/" . $fileTimeStamp . ".log");

select OUTPUT_FILE; $| = 1;

my $line;

while (<SOCKET>)
{
    my @ct = localtime(time()); # current localtime
    my $currentTimeStamp = ($t[5]+1900) . "-" . ($t[4]+1) . "-" . $t[3];
    if($fileTimeStamp ne $currentTimeStamp)
    {
          $fileTimeStamp = $currentTimeStamp; # set file stamp to current for use in open and later checks
          close OUTPUT_FILE || warn($!);
          open(OUTPUT_FILE,">>/data2/elsonba/zizo/" . $fileTimeStamp . ".log") || die($!);
    }

    print OUTPUT_FILE "$_\n";
}
close SOCKET or die "close: $!";
close OUTPUT_FILE;


that's untested ... it should point you in the right direction.

sonson Jun 7th, 2005 4:14 PM

thanks alot sykkn for your help..just wanted to ask if i want to add something so that it reconnects automatically if it disconnects..where would i add checks for that???

sonson Jun 9th, 2005 5:09 PM

any help at all???

kordaff Jul 13th, 2005 3:52 PM

use IO::Socket

Then, you can do something like
:

if ($socket->connected)
  {
  # go ahead and $socket->recv now
  }
else {reconnect_me() }


Kordaff


All times are GMT -5. The time now is 4:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC