Add threadname to logging output

As the logging currently is, you can't see whether log messages came
from the netcam thread or the motion loop thread, and messages that
come from the ffmpeg thread appear to come from the main thread. This
makes debugging based on the log messages error prone at best.

As an added bonus any camera name is now included.
This commit is contained in:
Joseph Heenan
2016-10-03 08:46:00 +01:00
parent 8323227e9f
commit 80e78f559b

View File

@@ -191,22 +191,29 @@ void motion_log(int level, unsigned int type, int errno_flag, const char *fmt, .
*/
errno_save = errno;
#ifdef HAVE_PTHREAD_SETNAME_NP
char threadname[32] = "unknown";
pthread_getname_np(pthread_self(), threadname, sizeof(threadname));
#else /* HAVE_PTHREAD_SETNAME_NP */
const char *threadname = "";
#endif /* HAVE_PTHREAD_SETNAME_NP */
/*
* Prefix the message with the log level string, log type string,
* time and thread number. e.g. [1] [ERR] [ENC] [Apr 03 00:08:44] blah
*
*/
if (!log_mode) {
n = snprintf(buf, sizeof(buf), "[%d] [%s] [%s] [%s] ",
threadnr, get_log_level_str(level), get_log_type_str(type),
n = snprintf(buf, sizeof(buf), "[%d:%s] [%s] [%s] [%s] ",
threadnr, threadname, get_log_level_str(level), get_log_type_str(type),
str_time());
} else {
/*
* Prefix the message with the log level string, log type string
* and thread number. e.g. [1] [DBG] [TRK] blah
*/
n = snprintf(buf, sizeof(buf), "[%d] [%s] [%s] ",
threadnr, get_log_level_str(level), get_log_type_str(type));
n = snprintf(buf, sizeof(buf), "[%d:%s] [%s] [%s] ",
threadnr, threadname, get_log_level_str(level), get_log_type_str(type));
}
/* Next add the user's message. */