How do I add a member function as e.g. Boost Log filter? - c++

I want to add a member function as a logging filter via:
boost::log::core::get()->set_filter(boost::phoenix::bind(&LoggerContext::checkIfShouldBeLogged, this, <WHICH_ARGUMENT_DO_I_PASS?>));
where the signature of the filtering function is
bool LoggerContext::checkIfShouldBeLogged(const boost::log::attribute_value_set &attributeValueSet);
just as described in the documentation.
I do not know which argument to pass from the function I am trying to add the filter in.
If I make that function static, I can simply omit the argument and set the filter like so:
boost::log::core::get()->set_filter(&LoggerContext::checkIfShouldBeLogged);
The argument seems to be passed magically.
Same goes for setting a custom formatter. The following works for a static function:
sink->set_formatter(&LoggerContext::fileFormatter);
with the signature
void LoggerContext::fileFormatter(boost::log::record_view const &rec, boost::log::formatting_ostream &strm);
But I again do not know where to get the arguments from in order to pass them, when using boost::phoenix::bind.

#Jorge Bellon: I just now read you comment, it gave me a good hint as to how this works.
The solution is to use the boost::phoenix placeholder. This works for both the formatter and the filter:
boost::log::core::get()->set_filter(boost::phoenix::bind(&LoggerContext::checkIfShouldBeLogged, this, boost::phoenix::placeholders::arg1));
sink->set_formatter(boost::phoenix::bind(&LoggerContext::fileFormatter, this, boost::phoenix::placeholders::arg1, boost::phoenix::placeholders::arg2));
Thank you for the tip!

Related

How to declare a variable only once in Gamemaker Step function?

Screenshot
I want the variable maxx to only update once when calling this script and all the other lines of code every frame , please help
You could either:
Declare the variable in Create
Structure the script so that it is called in Create to initialize it and then in Step to use it
(an optional argument or lack of arguments for a reasonable solution, event_type for a hack)
Use variable_instance_exists provided that you are not using an ancient version of GMS1

Pass argument to event handler in Ember handlebars helper

I'd like to reuse the same action for focusing an input field in and out, passing it e.g. true or false as an argument, instead of writing two actions. I can't work out how to pass arguments in, though; is it possible?
E.g. {{input focus-in="toggleSomething true" focus-out="toggleSomething false"}} is how I expected it might work, but no.
Unfortunately you can't pass an argument in like that, the only parameter passed through to the action is the current value of the text field.
You can see this if you look at the source for the focusIn function, which calls the local sendAction function that in turn calls Ember.Component.sendAction passing in the "value" variable.
Not sure if this is the correct use for this but I managed to pass argument values through to the action like this -
{{input focus-in=(action "toggleSomething" true) focus-out=(action "toggleSomething" false)}}
I am using Ember 1.13.0

How to check for a value's datatype inside django template?

I want to get the datatype of a value in django template. I am sure there is a way because when you do
value|date:"Y-m-d"
django template understands that the value is timestamp. How does django do it?
the "|date" you see is called a template filter and is a built-in function of django
you may want to create one of your own (here's how) that takes something as input and returns its datatype.
but, imo, it's not a best-practice, since a template should mostly be used to just display and format data. if you need an evaluation on the type i suggest you move that inside a view and return the result in the context, eventually

How can I call a filter with more than one argument?

I need to call a filter with more than one argument.
If a filter takes only one parameter, for example "cut", we can call it with
{{ somevariable|cut:"0" }}
But if I create a custom filter which takes two parameters, I cannot call it with correct syntax.
For answers, I ask this only:
I don't want to send one argument and parse it in my custom filter.
I also don't want to chain the filters.
I think calling with two arguments is legal because there is a default filter named urlizentrunc.
def urlizetrunc(value, limit, autoescape=None):
You cannot. The only work-arounds are to pass in one parameter and parse it into parts, or to have a variable external to the filter passed in.
The docs state that it cannot be done with custom filters. See this question for a more detailed explanation.
You cannot directly pass multiple parameters to non-custom filters, such as urlizetrunc either. urlizetrunc takes one parameter from the template. autoescape is set in by calling the autoescape tag with a parameter of "off" or "on". When you call urlizetrunc from the template, it passes in whatever value autoescape has been set to. You cannot pass in the value of autoescape directly from the template. See this question for a more detailed explanation.
You'll have to settle for taking one argument and then parsing it. The autoescape parameter is kind of special because it's there in cases your filter needs to know whether autoescaping is on or off. For more info, check out this link: https://docs.djangoproject.com/en/1.3/howto/custom-template-tags/#filters-and-auto-escaping
But parsing the argument in your custom filter isn't that hard, usually it's just doing argument.split(" ") or argument.split(",")

Passing named arguments to functions in Django templates

I have in my Django controller a function that is called as follows:
trip.driverTrip.filter(status='pending')
What it will be the equivalent of calling this in a template. If I just want to call the filter function, the following will suffice:
{{trip.driverTrip.filter}}
But is there a way to pass it arguments ?
There are no controllers in Django ... Do you mean a view ;) ?
The equivalent in a template would be :
{{ trip.driverTrip|filter:"pending" }}
However, for this to work, your function filter has to be registered as a template filter, and 'loaded' in your template. You cannot just call any function (or method) like this. Plus, if you do this, assuming that the preceding conditions are fulfilled it means that you pass trip.driverTrip as the first argument to filter, and "pending" is an additional argument.
Does this answer your question ?
django template system, calling a function inside a model explains, you can't do it directly, but it that site also suggests a workaround.