Automatically prepend all stdout/stderr output with timestamp?

N

nobody

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!
 
R

Richard Heathfield

(e-mail address removed) said:
I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

Maybe you could just shove it through a pipe, something along these lines:

#include <stdio.h>
#include <time.h>

void printtime(FILE *fp)
{
char thetime[32] = "Unknown time and date";
time_t tt = time(NULL);
struct tm *ptm = localtime(&tt);
if(ptm != NULL)
{
strftime(thetime, sizeof thetime, "%Y/%m/%d %H:%M:%S | ", ptm);
}
fputs(thetime, fp);
}


int main(void)
{
int ch = 0;
int lookahead = 0;
lookahead = getchar();
if(lookahead != EOF)
{
printtime(stdout);
ungetc(lookahead, stdin);
}

while((ch = getchar()) != EOF)
{
putchar(ch);

if(ch == '\n')
{
lookahead = getchar();
if(lookahead != EOF)
{
printtime(stdout);
ungetc(lookahead, stdin);
}
}
}
return 0;
}
 
R

Roberto Waltman

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.
I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).
The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.
Also, the target platform is linux.
Any ideas? Thanks!

This is a slow day, as proven by
(1) Me having only five ideas so far, and
(2) Nobody else jumping on the wagon to tell
you that this is not doable in C / off
topic in comp.lang.c, etc.

In any case, this is what I can think of:

(A) Do not do it, at least not that way. Choose
one of the several logging services / libraries
available for Linux, make it a *documented*
part of the framework and tell people to use it
instead of the standard output streams.

(B) Do not do it, at least not that way. Create
your own versions of printf & family (with
different names,) that will add the timestamps,
make them a *documented* part of the framework
and tell people to use them instead of the
standard output streams.

(C) If you have control of the environment in
which the programs run, redirect stdout and stderr
into a separate process that will add the
timestamps before displaying the output.

(D) It may be possible to create your own
version of printf & family and force the programs
to use those by linking them ahead of the standard
libraries. (I do not recall if the standard Linux
linker will allow you to do this, while being
somehow tolerant of multiple definitions, etc.)
This may have undesirable side effects if other
code from the libraries also uses your functions.

(E) Create your own versions of the functions,
(like (B) but with the same names.) Modify the
source code of the standard C library to give
printf & friends new names. Link your programs
with the modified version of the library.

If I was responsible for this decision in one
of the projects I'm working on, I'll chose
either (A) or (B).

Hope this helps,
 
G

Guest

(2) Nobody else jumping on the wagon to tell
you that this is not doable in C / off
topic in comp.lang.c, etc.

Thank you for your response. I appreciate the suggestions.

What about my question was off-topic for this newsgroup? And which
newsgroup do you suggest would have been more appropriate?
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Write a program that creates a pipe, redirect I/O (dup) and fork to the
plug-in?.

Kind regards.
 
G

Gordon Burditt

I am writing a framework that other developers will write plug-ins
In general, C does not "automatically do".
You have to write code to do that.

Interception generally requires a football or wiretapping equipment.
Or, in the case of output streams, you can pipe it through something
that adds the timestamps.
This is a slow day, as proven by
(1) Me having only five ideas so far, and
(2) Nobody else jumping on the wagon to tell
you that this is not doable in C / off
topic in comp.lang.c, etc.

In any case, this is what I can think of:

(A) Do not do it, at least not that way. Choose
one of the several logging services / libraries
available for Linux, make it a *documented*
part of the framework and tell people to use it
instead of the standard output streams.
(B) Do not do it, at least not that way. Create
your own versions of printf & family (with
different names,) that will add the timestamps,
make them a *documented* part of the framework
and tell people to use them instead of the
standard output streams.

You might look at the documented interface for syslog (whether
or not you actually use syslog). It's not exactly the same
interface as printf(), but you might find some of the features
useful.
(C) If you have control of the environment in
which the programs run, redirect stdout and stderr
into a separate process that will add the
timestamps before displaying the output.

(D) It may be possible to create your own
version of printf & family and force the programs
to use those by linking them ahead of the standard
libraries. (I do not recall if the standard Linux
linker will allow you to do this, while being
somehow tolerant of multiple definitions, etc.)
This may have undesirable side effects if other
code from the libraries also uses your functions.

This may have undesireable side effects on your code.
A module that does something like:
sprintf(filename, "%s/.%src", getenv("HOME"), "foo");
and then opens said file might start producing messages like:
Jan 6 13:18:32 Error opening file "Jan 6 13:18:31 /home/george/.foorrc": file not found. sprintf() *is* part of the printf() family, right?
(E) Create your own versions of the functions,
(like (B) but with the same names.) Modify the
source code of the standard C library to give
printf & friends new names. Link your programs
with the modified version of the library.

Ditto with evil effects on sprintf().
If I was responsible for this decision in one
of the projects I'm working on, I'll chose
either (A) or (B).

I agree.

Gordon L. Burditt
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Write a program that redirect I/O (dup) and fork to the plug-in?.

Kind regards.
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Your program could create a pipe, redirect I/O (dup) and fork to the
plug-in?.

Kind regards.
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Write a program that redirect I/O (dup) and fork to the plug-in?.

Kind regards.
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Create a pipe, fork to plug-in and redirect I/O?

Kind regards.
 
T

tmp123

I am writing a framework that other developers will write plug-ins
for. I would like for one of the features of the framework to be to
intercept all text written to stdout/stderr and prepend timestamps on
each line.

I would like for this to work for all the printf-line functions
(fprintf, etc...) as well as C++ I/O streams (cout and cerr).

The key here is that I would like to get these timestamps on the lines
of text written to stdout/stderr without requiring any code
change/recompilation of the plug-ins.

Also, the target platform is linux.

Any ideas? Thanks!

Create a pipe, fork to plug-in and redirect I/O?

Kind regards.
 
C

Chris Hulbert

Roberto said:
This is a slow day, as proven by
(1) Me having only five ideas so far, and
(2) Nobody else jumping on the wagon to tell
you that this is not doable in C / off
topic in comp.lang.c, etc.

In any case, this is what I can think of:

(A) Do not do it, at least not that way. Choose
one of the several logging services / libraries
available for Linux, make it a *documented*
part of the framework and tell people to use it
instead of the standard output streams.

(B) Do not do it, at least not that way. Create
your own versions of printf & family (with
different names,) that will add the timestamps,
make them a *documented* part of the framework
and tell people to use them instead of the
standard output streams.

(C) If you have control of the environment in
which the programs run, redirect stdout and stderr
into a separate process that will add the
timestamps before displaying the output.

(D) It may be possible to create your own
version of printf & family and force the programs
to use those by linking them ahead of the standard
libraries. (I do not recall if the standard Linux
linker will allow you to do this, while being
somehow tolerant of multiple definitions, etc.)
This may have undesirable side effects if other
code from the libraries also uses your functions.

Off Topic:
The GNU linker has a --wrap option that allows you to wrap symbols
through __wrap_SYMBOL. In this case, you could define __wrap_printf as
your version of printf and prepend the timestamp to the format string.
I have used this feature to force memory allocation routines to use
Matlab's memory allocation routines in Matlab MEX files in the past.
The nice part about the wrap option is that even for libraries where
all you have are compiled libraries/etc even those symbols get wrapped
to your routine. I do however agree with the other posters that the
preferable solution would be tell developers who are writing plugins to
use your custom printf or fprintf routines. The benefit about that
solution is you're not using system-dependent features and hence more
portable.

As for other newsgroups that would be more on-topic, for linux you can
try comp.unix.programmer or possibly comp.os.linux.development.apps.
 
C

Chris Hulbert

Roberto said:
This is a slow day, as proven by
(1) Me having only five ideas so far, and
(2) Nobody else jumping on the wagon to tell
you that this is not doable in C / off
topic in comp.lang.c, etc.

In any case, this is what I can think of:

(A) Do not do it, at least not that way. Choose
one of the several logging services / libraries
available for Linux, make it a *documented*
part of the framework and tell people to use it
instead of the standard output streams.

(B) Do not do it, at least not that way. Create
your own versions of printf & family (with
different names,) that will add the timestamps,
make them a *documented* part of the framework
and tell people to use them instead of the
standard output streams.

(C) If you have control of the environment in
which the programs run, redirect stdout and stderr
into a separate process that will add the
timestamps before displaying the output.

(D) It may be possible to create your own
version of printf & family and force the programs
to use those by linking them ahead of the standard
libraries. (I do not recall if the standard Linux
linker will allow you to do this, while being
somehow tolerant of multiple definitions, etc.)
This may have undesirable side effects if other
code from the libraries also uses your functions.

Off Topic:
The GNU linker has a --wrap option that allows you to wrap symbols
through __wrap_SYMBOL. In this case, you could define __wrap_printf as
your version of printf and prepend the timestamp to the format string.
I have used this feature to force memory allocation routines to use
Matlab's memory allocation routines in Matlab MEX files in the past.
The nice part about the wrap option is that even for libraries where
all you have are compiled libraries/etc even those symbols get wrapped
to your routine. I do however agree with the other posters that the
preferable solution would be tell developers who are writing plugins to
use your custom printf or fprintf routines. The benefit about that
solution is you're not using system-dependent features and hence more
portable.

As for other newsgroups that would be more on-topic, for linux you can
try comp.unix.programmer or possibly comp.os.linux.development.apps.
 
T

tmp123

Kenny said:
Yes, of course (obviously), but (as always):

Not portable. Can't discuss it here. Blah, blah, blah.


Are you taken "pipe"... like function names or like concepts?

Kind regards.
 
T

tmp123

Kenny said:
Yes, of course (obviously), but (as always):

Not portable. Can't discuss it here. Blah, blah, blah.

Are you taken "pipe"... like function names or like concepts?

Kind regards.
 
T

tmp123

Kenny McCormack ha escrito:
Yes, of course (obviously), but (as always):

Not portable. Can't discuss it here. Blah, blah, blah.

Are you taken "pipe"... like function names or like concepts?

Kind regards.
 
F

Flash Gordon

tmp123 said:
Gordon Burditt wrote:

Please leave in the attributions for the text you are acutally quoting.
I don't believe Gordon Burditt wrote any of the below.
Create a pipe, fork and dup stdio?

That is completely non-portable and won't work with a number of very
common platforms, since they don't support what you are suggesting.
What, in the post, suggests that a platform that supports such things is
being used? Even if functions of those names do exist they might have
different semantics.

Also, why are you so determined to give people off topic answers without
even having the curtsey to state that they are platform specific and
should be discussed else where?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top