![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
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; |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
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???
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Posts: 8
Rep Power: 0
![]() |
any help at all???
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jul 2005
Posts: 1
Rep Power: 0
![]() |
use IO:
ocketThen, you can do something like if ($socket->connected)
{
# go ahead and $socket->recv now
}
else {reconnect_me() }Kordaff |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|