Is there a simple api (without loop) to check if varargs contain some value (char*)?
If there is no simple API , how should i iterate without any args count?
My method should be able to filter the log base on configuration IP ,so i need to check if one of the args is 10.3.2.2 (some configuration IP).
void Logger::Log(const char *format, ...) {
lock_guard<mutex> guard(mtx_);
if (!file_) {
file_ = fopen("application.log", "w");
}
time_t current = time(0);
tm *ptm = localtime(¤t);
stringstream ss;
ss << "\n[" << ptm->tm_min << ":" << ptm->tm_sec << "]";
fprintf(file_, ss.str().data());
va_list argptr;
va_start(argptr, format);
vfprintf(file_, format, argptr);
va_end(argptr);
fflush(file_);
}
looking on this lesson , show only iterate with given args length and types.
format and than check contain is expensive and logger should be able to support in highly performance demands.
my target is to implement simple logger method. so you will call it like this:
Log("trafic from ip %s to ip %s", "10.1.1.1","1.1.1.1")
and lots of other calls . in the filter i want to check if one of the parameter value is “10.1.1.1” (for example) , so don’t log it.
There isn’t any syntactic sugar to do this because there is no universal way to know the types of the varargs. Without knowing the types, you can’t get their values. So you’d have to write code to do this based on how your functions knows what types its arguments are.
You want to do this:
Assemble the entire log message in the
stringstream.Check the log message for the desired string.
If you find the string in there, make sure it is not preceded or followed by a digit.
If you found the string and it’s not preceded or followed by a digit, log it.
You need that last check because “foo bar 12.3.4.5 baz” contains “2.3.4.5” as does “foo 2.3.4.51 bar”. So if you find the string and it’s followed or preceded by a digit, you still want to log the message.