LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 10-20-2005, 03:10 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
is a printf wrapper possible?


hiho@ll

is it possible to write a wrapper for printf

what i want:

myprintf("%s%d","test",5);

void myprintf(char *fmt,...){
var_list ap;
va_start(ap,fmt);

openlogfile();
printf("HMM ");

printf(fmt,ap); // << doesn't work, but how can i do this?

closelogfile();
}

in the above example (well, i tried without openlogfile, etc.) i got HM-1073745612

1. can it work?
2. how?
3. the next step i'm thinking about is a function, which calls another function depending on the argument
this means i want to write a wrapper for every possible function
this includes that one argument of my wrapper function is a function pointer!
void* mywrapper(void* (*func)(void*,...),...);
is this possible and how?

why i want to do this?
1. i'm interested if it works
2. maybe i can do some cool log functions
3. or my own memory management class (maybe )

thx@ll
 
Old 10-20-2005, 08:45 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Re: is a printf wrapper possible?

Quote:
Originally posted by Thinking
void myprintf(char *fmt,...){
var_list ap;
va_start(ap,fmt);

openlogfile();
printf("HMM ");

printf(fmt,ap); // << doesn't work, but how can i do this?

closelogfile();
}

in the above example (well, i tried without openlogfile, etc.) i got HM-1073745612
You use the wrong function for the printf; look at the vprintf functions.
Code:
void logit(char *filename, int verbose, int code, char *msg, ...)
{
va_list fmtargs;
char buffer[1024];

  va_start(fmtargs,msg);
  vsnprintf(buffer,sizeof(buffer)-1,msg,fmtargs);
  va_end(fmtargs);
  printf("%s",buffer);
  if(code!=0)
    printf("[%d]%s\n",errno,strerror(code));

  // do more stuff here (like write to file)
}
This is a stripped version of a logging function that I use and it prints correctly

call e.g. with logit("logfile",0,errno,"%s:%d\n",string_1,int_1);
 
Old 10-21-2005, 07:44 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
thx!!
seems to work

it's very long since i last viewed the man page of printf
 
  


Reply

Tags
programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How does printf work? Ephracis Programming 1 12-25-2004 03:06 AM
c printf question. fredgt Programming 5 09-09-2004 05:06 AM
More arguments in printf() AMMullan Programming 3 02-23-2004 02:29 PM
Using 'printf' on a 'struct' skywalker27182 Programming 10 12-20-2003 12:06 PM
printf new_user10 Programming 3 09-09-2003 11:12 PM


All times are GMT -5. The time now is 07:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration