decltype in lambda


decltype is to calculate type from object expression
Demonstrated by the lambda used to initialize std::set
decltype is required since that’s the only way to access the type of the lambda when the lambda is embedded.

lambda is no more than syntactic shortcut to functor.

void TestLambdaInSet()
{
	bool bGreat = true;
	auto cmp = [=, &bGreat](int a, int b)
	{
		if (bGreat)
			return a > b;
		else
			return a < b;
	};
	std::set myset(cmp);
	myset.insert(1); myset.insert(2);
	printf("first item should be greatest since bGreat = true. got %d", *myset.begin());
	//output is 2 as expected
	bGreat = false;
	myset.clear();
	myset.insert(1); myset.insert(2);
	printf("first item should be smallest since bGreat = false. got %d", *myset.begin());
	//output is 1 as expected
}

void main()
{
	TestLambdaInSet();
}

Reduce web server log size


How to exclude log messages based on IP and request type, so that access.log doesn’t take too much space too soon.
As a public facing streaming server, nginx server’s access.log grows fast for three reasons:
1. due to HLS streaming, most requests are about .ts segment files, which we don’t want to log.
2. some requests are from web crawlers such as Googlebot
3. some IPs request files that we have no idea what they are about, which need to be ignored.

Excluding certain messages from logs/access.log is configured in nginx.conf.
This statement below in nginx.conf says: log only if user defined variable $loggable is true.
access_log logs/access.log combined if=$loggable;
This statement can be in http{} or server{} or location{}.

To set $loggable, we have statements below in. e.g. the server{} section:
set $loggable 1;
if ( $ext_in_log = 0 ) {
set $loggable 0;
}
if ( $bot_in_log = 0 ) {
set $loggable 0;
}
if ( $ip_in_log = 0 ) {
set $loggable 0;
}
which says: if these three user defined variables, ext_in_log, bot_in_log, ip_in_log, are true, set $loggable to 0.

To set these three variables:
check files extension, .ts, to decide if the request is about a video segment.
map $request $ext_in_log {
~*(.*?)\.ts.* 0;
default 1;
}
check if the request is from web crawlers.
map $http_user_agent $bot_in_log {
~*Googlebot 0;
~*bingbot 0;
default 1;
}
check if the request is from certain IPs
geo $remote_addr $ip_in_log {
208.71.208.0/22 0;
172.105.77.67 0;
default 1;
}
Here is a sample nginx.conf file: http://riowing.net/p/nginx.conf

functor vs lamba


Demonstrated by std::sort
Comparing to function pointers, lambda can access all variables in enclosing scope.
Comparing to function pointers, functor have state, with help from data members in the functor class.

bool MyCompare(const int& a, const int& b)
{
	return a > b;
}

class MyComparer
{
public:
	MyComparer(bool bCompareByAbsValue)
	{
		mbCompareByAbsValue = bCompareByAbsValue;
	}
	bool operator()(const int& a, const int& b)
	{
		if (mbCompareByAbsValue)
			return abs(a) > abs(b);
		else
			return a > b;
	}
private:
	bool mbCompareByAbsValue;
};
void output(string by, int * aInt, size_t nLen)
{
	cout << "sort by " << by << " descending order: " << endl;
	for (int i = 0; i < nLen; i++)
		cout << aInt[i] << " ";
	cout << endl;

}
void TestFunctionPtrFunctorLambda()
{
	const int LENGTH = 3;
	int nLen = LENGTH;
	int aInt[LENGTH] = {2, -3, 1};
	int * pInt = aInt;
	std::sort(pInt, pInt + nLen, MyCompare);
	output("function pointer", aInt, nLen);

	bool bCompareByAbsValue = true;
	std::sort(pInt, pInt + nLen,
		[bCompareByAbsValue](int a, int b) {
		if (bCompareByAbsValue)
			return abs(a) > abs(b);
		else
			return a > b;
	});
	output("lambda, absolute value", aInt, nLen);

	std::sort(pInt, pInt + nLen, MyComparer(bCompareByAbsValue));
	output("functor, absolute value", aInt, nLen);
}

output:
sort by function pointer descending order:
2 1 -3
sort by lambda, absolute value descending order:
-3 2 1
sort by functor, absolute value descending order:
-3 2 1

gdb for embedded device


Using gdb to debug this streaming device: MStar MSO9380 ARM Cortex-A53
On device, by telnet:
# wdog 0 # turn off watch dog
# gdbserver :5555 –attach $PROCESS_ID_MYAPP # part of debug build of rootfs

On host: Linux desktop:
$ arm-linux-gdb $FILE_FULLPATH_MYAPP # the file of PROCESS_ID_MYAPP with symbols

Inside (gdb):
set pagination off # reduce output
set print pretty on # print with indentation
set sysroot /mypath/rootfs #location of .so files, alias of solib-absolute-prefix
set solib-search-path /mypath/rootfs1:/mypath/rootfs2
target remote 10.15.22.22:5555 #connect to gdbserver
b My.cpp:57 #break at line 57
c #continue to run