public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* fprintf control from Environment variable.
@ 2012-10-09  5:22 naveen yadav
  2012-10-09  7:18 ` Kalle Olavi Niemitalo
  2012-10-09 13:39 ` Ian Lance Taylor
  0 siblings, 2 replies; 3+ messages in thread
From: naveen yadav @ 2012-10-09  5:22 UTC (permalink / raw)
  To: glibc-bugs, gcc-help

Dear All,

I want to control the print message being sent to stdout and stderr
using printf(), vprintf() and fprintf() function call for better
performance.

For this I have added following code in vfprintf() function
  static int Verbose = 0;
  if(stream == stdout || stream == stderr)
  {
      if (Verbose == 0)
     Verbose = getenv ("PRINT") ? 1 : 2;
      if (Verbose == 1)
     return 0;
  }
 Thus, if  PRINT is being set as an environment variable then print
messages being controlled and not getting displayed.
I checked that printf, vprintf and fprintf all calls vfprintf() function.

In this, I am facing issue that print messages send through fprintf()
call are not getting controlled and still coming on stdout or stderr.

I checked that for fprintf() call if stream is other then stdout or
stderr then code flows comes to vfprintf() function and if stream is
stdout or stderr then code flow is different and fwrite() function is
getting called.
Please find obj-dump for fprintf() call
  fprintf(stdout, "fprintf stdout!\n");
   8048662:       a1 80 9b 04 08          mov    0x8049b80,%eax
   8048667:       89 c2                   mov    %eax,%edx
   8048669:       b8 54 88 04 08          mov    $0x8048854,%eax
   804866e:       89 54 24 0c             mov    %edx,0xc(%esp)
   8048672:       c7 44 24 08 10 00 00    movl   $0x10,0x8(%esp)
   8048679:       00
   804867a:       c7 44 24 04 01 00 00    movl   $0x1,0x4(%esp)
   8048681:       00
   8048682:       89 04 24                mov    %eax,(%esp)
   8048685:       e8 76 fd ff ff          call   8048400 <fwrite@plt>

Please let me know the way using which print messages invoked using
fprintf() call can be controlled if send to stdout ot stderr stream.

Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: fprintf control from Environment variable.
  2012-10-09  5:22 fprintf control from Environment variable naveen yadav
@ 2012-10-09  7:18 ` Kalle Olavi Niemitalo
  2012-10-09 13:39 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Kalle Olavi Niemitalo @ 2012-10-09  7:18 UTC (permalink / raw)
  To: naveen yadav; +Cc: gcc-help

naveen yadav <yad.naveen@gmail.com> writes:

> I checked that for fprintf() call if stream is other then stdout or
> stderr then code flows comes to vfprintf() function and if stream is
> stdout or stderr then code flow is different and fwrite() function is
> getting called.

Use gcc -fno-builtin-fprintf to disable this transformation.
However, that option also makes GCC not warn about calls like
fprintf(stdout, "hahaha%s\n") where the format string does not
match the argument list.  You can restore those warnings by
declaring fprintf with __attribute__((format(printf, 2, 3))).
<stdio.h> of glibc apparently does not do that.

According to gcc/builtins.c (avoid_folding_inline_builtin),
another way to disable the transformation would be to declare
fprintf as an inline __attribute__((always_inline)) function.
However, that causes an error if compiling with gcc -O2.
If you only declare fprintf without a function body:

  In file included from test-builtin-fprintf.c:1:0:
  /usr/include/stdio.h:353:12: error: inlining failed in call to always_inline ‘fprintf’: function body not available
  test-builtin-fprintf.c:24:10: error: called from here

If you define fprintf with a function body:

  In file included from test-builtin-fprintf.c:1:0:
  test-builtin-fprintf.c: In function ‘fprintf’:
  /usr/include/stdio.h:353:12: error: function ‘fprintf’ can never be inlined because it uses variable argument lists

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: fprintf control from Environment variable.
  2012-10-09  5:22 fprintf control from Environment variable naveen yadav
  2012-10-09  7:18 ` Kalle Olavi Niemitalo
@ 2012-10-09 13:39 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2012-10-09 13:39 UTC (permalink / raw)
  To: naveen yadav; +Cc: glibc-bugs, gcc-help

On Mon, Oct 8, 2012 at 10:22 PM, naveen yadav <yad.naveen@gmail.com> wrote:
>
> I want to control the print message being sent to stdout and stderr
> using printf(), vprintf() and fprintf() function call for better
> performance.
>
> For this I have added following code in vfprintf() function
>   static int Verbose = 0;
>   if(stream == stdout || stream == stderr)
>   {
>       if (Verbose == 0)
>      Verbose = getenv ("PRINT") ? 1 : 2;
>       if (Verbose == 1)
>      return 0;
>   }

That seems really unwise.  Do this kind of thing in your application,
not in your C library.  If for some reason you truly must do it in
your C library, do it at level of write, not at the level of printf.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-10-09 13:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-09  5:22 fprintf control from Environment variable naveen yadav
2012-10-09  7:18 ` Kalle Olavi Niemitalo
2012-10-09 13:39 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).