Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 7th, 2005, 3:23 AM   #1
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
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;
sonson is offline   Reply With Quote
Old Jun 7th, 2005, 9:45 AM   #2
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
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.
sykkn is offline   Reply With Quote
Old Jun 7th, 2005, 4:14 PM   #3
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
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 is offline   Reply With Quote
Old Jun 9th, 2005, 5:09 PM   #4
sonson
Newbie
 
Join Date: Jun 2005
Posts: 8
Rep Power: 0 sonson is on a distinguished road
any help at all???
sonson is offline   Reply With Quote
Old Jul 13th, 2005, 3:52 PM   #5
kordaff
Newbie
 
Join Date: Jul 2005
Posts: 1
Rep Power: 0 kordaff is on a distinguished road
use IO:ocket

Then, you can do something like
if ($socket->connected)
  {
  # go ahead and $socket->recv now
  }
else {reconnect_me() }

Kordaff
kordaff is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:17 PM.

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