how to split STDOUT into 2 or more output streams
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how to split STDOUT into 2 or more output streams




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how to split STDOUT into 2 or more output streams  
Liang


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-10-05 10:55 PM

I want to log a process and its children's output into a log file, while
printing the output on STDOUT.

By the way, there's no 'tee' available in the target environment.

One approach is to 'printf' twice the output, one for STDOUT, another for
the log file. But this makes the program ugly.

Another approach is to 'fork' the process, and let the parent act as the
'tee'.

If I don't want to use 'fork', is there any other way to implement the
requirement?

Thanks,
Liang







[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-10-05 10:55 PM

>I want to log a process and its children's output into a log file, while
>printing the output on STDOUT.
>
>By the way, there's no 'tee' available in the target environment.
>
>One approach is to 'printf' twice the output, one for STDOUT, another for
>the log file. But this makes the program ugly.

It is possible to change all the printf() calls (unless some of them
are hidden in the C library) and other output to call printf2(),
which is a varargs function you write that calls vfprintf(), twice.
That limits the ugliness to a small piece of code.

>Another approach is to 'fork' the process, and let the parent act as the
>'tee'.

Assuming you DO have pipes available, is there any reason you can't
write your own version of tee?

Assuming you DO NOT have pipes available, how does fork()ing the
process accomplish what you want?  Run it twice to get two sets
of output?

>If I don't want to use 'fork', is there any other way to implement the
>requirement?

Is it OK for the shell to use fork()?

program | my_own_version_of_tee logfile

Gordon L. Burditt





[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Bill Marcum


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-10-05 10:55 PM

On Wed, 10 Aug 2005 22:54:47 +0800, Liang
<leo2002chen@hotmail.com> wrote:
> I want to log a process and its children's output into a log file, while
> printing the output on STDOUT.
>
> By the way, there's no 'tee' available in the target environment.
>
What OS is it, and why are you asking in comp.unix.programmer?


--
Tonight you will pay the wages of sin; Don't forget to leave a tip.





[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Vikram


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 08:13 AM

The simplest way of doing on most of the unix systems is to use "tee"
command which can replicate the standard output.

tee command can be used in the following way

# unixcmd | tee logile

-Vikram






[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 08:13 AM

"Liang" <leo2002chen@hotmail.com> writes:
> I want to log a process and its children's output into a log file, while
> printing the output on STDOUT.
>
> By the way, there's no 'tee' available in the target environment.
>
> One approach is to 'printf' twice the output, one for STDOUT, another for
> the log file. But this makes the program ugly.
>
> Another approach is to 'fork' the process, and let the parent act as the
> 'tee'.
>
> If I don't want to use 'fork', is there any other way to implement the
> requirement?

Why would you not want to fork?
pgm | tee
forks a tee process anyways.

So you don't have tee on this computer?  Why don't you write one? It's trivi
al.

What would be ugly?  Didn't you learn about functional abstraction
when you learned programming?


FILE* file=fopen("/var/log/file","a");
FILE* remote=fdopen(socket,"a");

log_add_output(stdout);
log_add_output(file);
log_add_output(remote);

/* and then you just write: */

logf("Message to be logged with data %s",data);

/* and logf takes care of logging out to all the log streams. */


--
__Pascal Bourguignon__                     http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.





[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Vitale Ferruccio


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 08:13 AM

Hi Liang,

according to me, first thing you should consider is avoiding a race conditio
n
between main process and the children, while they're trying to write into th
e
log file.
You could:
- open a file, share its file descriptor between forks and associate a
semaphore
to it, so that every process could write in this file if the
semaphore is released
- the second type of output could be redirected to syslog or stdout via
a printf

Pseudo-code example:
#define	LOG_INIT()	{ INIT_THE_SEMAPHORE; OPEN_SYSLOG }
#define	LOG_STR(fmt, str...)	{ LOCK_SEMAPHORE; LOG_IT; \
WRITE_TO_SYSLOG; UNLOCK_SEMAPHORE };

I hope this could help you.

Regards,
Ferruccio

On 2005-08-10 16:54:47 +0200, "Liang" <leo2002chen@hotmail.com> said:

> I want to log a process and its children's output into a log file, while
> printing the output on STDOUT.
>
> By the way, there's no 'tee' available in the target environment.
>
> One approach is to 'printf' twice the output, one for STDOUT, another for
> the log file. But this makes the program ugly.
>
> Another approach is to 'fork' the process, and let the parent act as the
> 'tee'.
>
> If I don't want to use 'fork', is there any other way to implement the
> requirement?
>
> Thanks,
> Liang


--
/dev/zero
Unix Power @ Your Service
http://www.devzero.it






[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Liang


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 12:50 PM

it's POSIX UNIX.
 
> What OS is it, and why are you asking in comp.unix.programmer?
>
>
> --
> Tonight you will pay the wages of sin; Don't forget to leave a tip.







[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Liang


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 12:50 PM


"Gordon Burditt" <gordonb.d18ae@burditt.org> wrote in message
news:11fk9f5jia27j08@corp.supernews.com... 
>
> It is possible to change all the printf() calls (unless some of them
> are hidden in the C library) and other output to call printf2(),
> which is a varargs function you write that calls vfprintf(), twice.
> That limits the ugliness to a small piece of code.

oh, not applicable. Because it will start other programs.

> 
>
> Assuming you DO have pipes available, is there any reason you can't
> write your own version of tee?
>
> Assuming you DO NOT have pipes available, how does fork()ing the
> process accomplish what you want?  Run it twice to get two sets
> of output?

oh, no, don't want to run it twice. Just want to run once, but split the
output to 2 streams.
One implementation may look like:


create pipe;
if(0==fork()){
# children
close STDOUT and STDERR
dup STDOUT and STDERR from pipe;
exec();
}
# parent
close STDIN
dup STDIN from pipe
while( getchar STDIN ){
put char;
}

But, I want to know if there's other solution that does not need 'fork'?



> 
>
> Is it OK for the shell to use fork()?
>
> program | my_own_version_of_tee logfile






>
> Gordon L. Burditt







[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Liang


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 10:55 PM

thank Ferruccio, you reminded me.

BRs,
Liang


"Vitale Ferruccio" <ferruccio@devzero.it> wrote in message
 news:2005081109575616807%ferruccio@devze
roit...
> Hi Liang,
>
> according to me, first thing you should consider is avoiding a race
condition
> between main process and the children, while they're trying to write into
the
> log file.
> You could:
> - open a file, share its file descriptor between forks and associate a
> semaphore
>   to it, so that every process could write in this file if the
> semaphore is released
> - the second type of output could be redirected to syslog or stdout via
> a printf
>
> Pseudo-code example:
> #define LOG_INIT() { INIT_THE_SEMAPHORE; OPEN_SYSLOG }
> #define LOG_STR(fmt, str...) { LOCK_SEMAPHORE; LOG_IT; \
> WRITE_TO_SYSLOG; UNLOCK_SEMAPHORE };
>
> I hope this could help you.
>
> Regards,
> Ferruccio
>
> On 2005-08-10 16:54:47 +0200, "Liang" <leo2002chen@hotmail.com> said:
> 
for[vbcol=seagreen] 
>
>
> --
>               /dev/zero
> Unix Power @ Your Service
>     http://www.devzero.it
>







[ Post a follow-up to this message ]



    Re: how to split STDOUT into 2 or more output streams  
Gordon Burditt


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-11-05 10:55 PM

>> Assuming you DO have pipes available, is there any reason you can't 
>
>oh, no, don't want to run it twice. Just want to run once, but split the
>output to 2 streams.
>One implementation may look like:
>
>
>create pipe;
>if(0==fork()){
>    # children
>    close STDOUT and STDERR
>    dup STDOUT and STDERR from pipe;
>    exec();
>}
># parent
>close STDIN
>dup STDIN from pipe
>while( getchar STDIN ){
>    put char;
>}

Explain to me why this outputs on two streams.  It seems to me obvious
that it doesn't, unless "put char;" refers to TWO calls to fputc() or
some other output function, one on stdout and one to a log file.

>But, I want to know if there's other solution that does not need 'fork'?

I want to know WHY you don't want to use fork()?

If you can't modify the programs doing the output, you're stuck doing
it in another process.  Starting that process is going to involve SOME
process doing a fork().


I want to remove some screws, but I don't want to use a screwdriver,
because that's too slow, and I've got a pipe wrench, and I don't
care that removing screws with a pipe wrench is 100 times slower
than using a screwdriver.

Gordon L. Burditt





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:26 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register