I’ve got following (simplified for example purpose) code and it works:
void log(const string type, const string msg, va_list argp)
{
fprintf(stdout, "[%s] ", type.c_str());
vfprintf(stdout, msg.c_str(), argp);
}
void log_err(const string msg, ...)
{
va_list argp;
va_start(argp, msg);
log("ERROR", msg, argp);
va_end(argp);
}
I would use it in this way:
log_err("test: %d", 5);
However if I would like to move this to class:
class Logger {
public:
Logger() {
//
};
void generic(const string type, const string msg, va_list argp) {
fprintf(stdout, "[%s] ", type.c_str());
vfprintf(stdout, msg.c_str(), argp);
};
void error(const string msg, ...) {
va_list argp;
va_start(argp, msg);
this->generic("ERROR", msg, argp);
va_end(argp);
};
};
Then I got Segmentation fault. I know that tricky thing like va macros could not work within class scopes but I want to know why.
Thanks in advice!
EDIT
Example usage:
Logger logger;
logger.error("test", 5);
Full source:
#include <string>
#include <cstdlib>
#include <cstdarg>
using namespace std;
void log(const string type, const string msg, va_list argp)
{
fprintf(stdout, "[%s] ", type.c_str());
vfprintf(stdout, msg.c_str(), argp);
}
void log_err(const string msg, ...)
{
va_list argp;
va_start(argp, msg);
log("ERROR", msg, argp);
va_end(argp);
}
class Logger {
public:
Logger() {
//
};
void generic(const string type, const string msg, va_list argp) {
fprintf(stdout, "[%s] ", type.c_str());
vfprintf(stdout, msg.c_str(), argp);
};
void error(const string msg, ...) {
va_list argp;
va_start(argp, msg);
this->generic("ERROR", msg, argp);
va_end(argp);
};
};
int main()
{
//log_err("test: %s\n", "str");
Logger logger;
logger.error("test %s", 5);
return 0;
}
I don’t want to make these methods static because in original use i have private file descriptors to which I’m writing log messages;
The bug is in this line:
The
%sformat specifier is for a C-style string.5is not a C-style string. Use:or: