LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-15-2017, 07:02 PM   #1
mr.simo
Member
 
Registered: Feb 2010
Posts: 78

Rep: Reputation: 0
C - preprocessor printf with fixed string __FUNCTION__ and __VA_ARGS__


Hi all,

I am just courious with an implementation of preprocessor printf.

When possible I like to define a custom printf in order to always print the function name given by the __FUNCTION__ macro, plus the string I have to print:

Code:
#define myprintf (...) { printf("[ %s ]" , __FUNCTION__); printf(__VA_ARGS__); printf("\n"); }
Is there a way to do it without using 3 separate printf ?
I tried with this one but his one is not valid:

Code:
#define myprintf(fmt, ...) printf("[ %s ] " fmt, __FUNCTION__, __VA_ARGS__);
because I cannot use with a single string without parameters. For example, the following:

Code:
myprintf("Hello world");
would not be compiled.
Thanks.
 
Old 03-15-2017, 07:55 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Does this help? https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
 
Old 03-15-2017, 08:05 PM   #3
rhamel
Member
 
Registered: Sep 2009
Location: Caribbean
Distribution: Slackware 15.0, Proxmox 7.4-17, FreeBSD 13.2
Posts: 70

Rep: Reputation: Disabled
Code:
#include <stdio.h>

#define eprintf(fmt,...) fprintf(stderr, "[%s] %s", __FUNCTION__, fmt, ##__VA_ARGS__)

int main()
{
  eprintf("hello world");
}
test.cpp lines 1-8/8 (END)
Code:
gcc test.cpp -o mytest
./mytest
[main] hello world
This is with
Quote:
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/i586-slackware-linux/5.3.0/lto-wrapper
Target: i586-slackware-linux
Configured with: ../gcc-5.3.0/configure --prefix=/usr --libdir=/usr/lib --mandir=/usr/man --infodir=/usr/info --enable-shared --enable-bootstrap --enable-languages=ada,c,c++,fortran,go,java,lto,objc --enable-threads=posix --enable-checking=release --enable-objc-gc --with-system-zlib --with-python-dir=/lib/python2.7/site-packages --enable-libstdcxx-dual-abi --with-default-libstdcxx-abi=gcc4-compatible --disable-libunwind-exceptions --enable-__cxa_atexit --enable-libssp --enable-lto --disable-install-libiberty --with-gnu-ld --verbose --enable-java-home --with-java-home=/usr/lib/jvm/jre --with-jvm-root-dir=/usr/lib/jvm --with-jvm-jar-dir=/usr/lib/jvm/jvm-exports --with-arch-directory=i386 --with-antlr-jar=/root/slackware-current/source/d/gcc/antlr-runtime-3.4.jar --enable-java-awt=gtk --disable-gtktest --with-arch=i586 --target=i586-slackware-linux --build=i586-slackware-linux --host=i586-slackware-linux
Thread model: posix
gcc version 5.3.0 (GCC)
 
1 members found this post helpful.
Old 03-15-2017, 08:06 PM   #4
rhamel
Member
 
Registered: Sep 2009
Location: Caribbean
Distribution: Slackware 15.0, Proxmox 7.4-17, FreeBSD 13.2
Posts: 70

Rep: Reputation: Disabled
BTW, that's from https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash: how to un-escape a string (printf %q and %b?) catkin Programming 7 01-10-2022 04:49 AM
need to echo / printf string containing date as parameter in cron v8625 Linux - Newbie 1 02-25-2011 06:31 PM
Minus sign in printf format string doesn't work. zebela Linux - Newbie 4 06-13-2008 01:32 AM
C string and printf question exvor Programming 7 12-10-2005 10:53 AM
std string class and printf The_Nerd Programming 3 06-28-2004 10:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration