ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I had a similar thing I wanted to in the past. And here was my answer... My function only did string formatting, so I just had to format the string, and pass it to the other function and continue on.
Code:
void logIt(const char* s, ...)
{
va_list va_args;
va_start(va_args, s);
char sz[MAX];
_vsnprintf(sz, MAX, s, va_args);
// do something...
printf("Log: %s\n", sz); // I'll just print it...
}
void send(const char* s, ...)
{
va_list va_args;
va_start(va_args, s);
char sz[MAX];
_vsnprintf(sz, MAX, s, va_args);
// at this point, sz is the formatted string
logIt(sz); // this will just send the formatted string as is...
// this will send another formatted string and one of the
//params is the formated one we just did
logIt("MyObject::send - %s", sz);
// do something...
SendToNetwork(sz);
}
OOO dear me dreaded va_args!!
Please explain what it is you are trying to do and I will attempt to show you a C++ version which does not loose type safety.
OOO dear me dreaded va_args!!
Please explain what it is you are trying to do and I will attempt to show you a C++ version which does not loose type safety.
I am building an internal message system (for logging, output, etc). And I want to use printf-style formatting.
Cause I really don't like the C++ way with << and stuff. It looks ugly and it is not as elegant and short as the printf-style. I just prefer the old ways.
I am building an internal message system (for logging, output, etc). And I want to use printf-style formatting.
I had almost the exact same question for almost the exact same problem several months ago. What I did was have the logging function take a const char*, then several "log message" functions (based on the event) that would take certain arguments based on the message, would snprintf them, then use the log function on that string. It works very well and has the added benefit of centralizing log messages, which allows you to have translations of your program.
ta0kira
The main logging functions I use add a lot more info, though, like time and pid automatically. This also lets you design a library with no specific logging format and have the using program define its own logging function.
With this setup I can do exactly what I wanted to: I can use printf-like logging from both the main class and the rest of the children. Just do something like:
message("Main Class", "Could not open %s", filename);
or
message("There was no errors");
And the message would be sent to the logfile, stdout/err or the GUI depending on the current settings. Where all stuff like timestamping, formatting, setting lower case or capitalize, handling multiliners, etc is done in only one place for the whole project.
It's wonderful, and it works. And in the Qt GUI I can use QString::vsprintf(), just hoping there's something as good available in GTK for when I'm gonna write the GTK GUI.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.