From dc27136cbed4437ce33ef5a453e655eb76aae27f Mon Sep 17 00:00:00 2001 From: AngelCarpintero Date: Mon, 7 Jul 2008 13:53:42 +0000 Subject: [PATCH] Update log adding function name , remove setup_mode as a login level. Added servo_position() and check limits to avoid overflow maxN , minN --- conf.c | 24 ++- configure | 18 +- event.c | 38 +++-- ffmpeg.c | 44 ++--- motion.c | 233 ++++++++++++++------------ motion.h | 1 + netcam.c | 435 ++++++++++++++++++++++++++++--------------------- netcam_ftp.c | 44 ++--- picture.c | 32 ++-- rotate.c | 33 ++-- stream.c | 41 +++-- track.c | 85 ++++++---- video.c | 115 ++++++------- video.h | 2 +- video2.c | 142 ++++++++-------- video_common.c | 26 +-- webhttpd.c | 94 +++++------ 17 files changed, 775 insertions(+), 632 deletions(-) diff --git a/conf.c b/conf.c index 3a077e9f..cc75393b 100644 --- a/conf.c +++ b/conf.c @@ -1459,7 +1459,7 @@ struct context **conf_cmdparse(struct context **cnt, const char *cmd, const char } /* We reached the end of config_params without finding a matching option */ - motion_log(LOG_ERR, 0, "Unknown config option \"%s\"", cmd); + motion_log(LOG_ERR, 0, "%s: Unknown config option \"%s\"", __FUNCTION__, cmd); return cnt; } @@ -1550,7 +1550,8 @@ void conf_print(struct context **cnt) FILE *conffile; for (thread = 0; cnt[thread]; thread++) { - motion_log(LOG_INFO, 0, "Writing config file to %s", cnt[thread]->conf_filename); + motion_log(LOG_INFO, 0, "%s: Writing config file to %s", + __FUNCTION__, cnt[thread]->conf_filename); conffile = myfopen(cnt[thread]->conf_filename, "w"); if (!conffile) continue; @@ -1658,9 +1659,10 @@ struct context ** conf_load (struct context **cnt) if (!fp) { /* Commandline didn't work, try current dir */ char *path = NULL; if (cnt[0]->conf_filename[0]) - motion_log(-1, 1, "Configfile %s not found - trying defaults.", filename); + motion_log(-1, 1, "%s: Configfile %s not found - trying defaults.", + __FUNCTION__, filename); if ( (path = get_current_dir_name()) == NULL) { - motion_log(LOG_ERR, 1, "Error get_current_dir_name"); + motion_log(LOG_ERR, 1, "%s: Error get_current_dir_name", __FUNCTION__); exit(-1); } snprintf(filename, PATH_MAX, "%s/motion.conf", path); @@ -1674,18 +1676,21 @@ struct context ** conf_load (struct context **cnt) snprintf(filename, PATH_MAX, "%s/motion.conf", sysconfdir); fp = fopen(filename, "r"); if (!fp) /* there is no config file.... use defaults */ - motion_log(-1, 1, "could not open configfile %s", filename); + motion_log(-1, 1, "%s: could not open configfile %s", + __FUNCTION__, filename); } } /* Now we process the motion.conf config file and close it*/ if (fp) { strcpy(cnt[0]->conf_filename, filename); - motion_log(LOG_INFO, 0, "Processing thread 0 - config file %s", filename); + motion_log(LOG_INFO, 0, "%s: Processing thread 0 - config file %s", + __FUNCTION__, filename); cnt = conf_process(cnt, fp); fclose(fp); } else { - motion_log(LOG_INFO, 0, "Not config file to process using default values"); + motion_log(LOG_INFO, 0, "%s: Not config file to process using default values", + __FUNCTION__); } @@ -2032,7 +2037,8 @@ static struct context **config_thread(struct context **cnt, const char *str, fp = fopen(str, "r"); if (!fp) { - motion_log(LOG_ERR, 1, "Thread config file %s not found", str); + motion_log(LOG_ERR, 1, "%s: Thread config file %s not found", + __FUNCTION__, str); return cnt; } @@ -2066,7 +2072,7 @@ static struct context **config_thread(struct context **cnt, const char *str, /* process the thread's config file and notify user on console */ strcpy(cnt[i]->conf_filename, str); - motion_log(LOG_INFO, 0, "Processing config file %s", str); + motion_log(LOG_INFO, 0, "%s: Processing config file %s", __FUNCTION__, str); conf_process(cnt+i, fp); /* Finally we close the thread config file */ diff --git a/configure b/configure index 41f1af88..2188d13f 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.61 for motion trunk-r374. +# Generated by GNU Autoconf 2.61 for motion trunk-r375. # # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, # 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. @@ -572,8 +572,8 @@ SHELL=${CONFIG_SHELL-/bin/sh} # Identity of this package. PACKAGE_NAME='motion' PACKAGE_TARNAME='motion' -PACKAGE_VERSION='trunk-r374' -PACKAGE_STRING='motion trunk-r374' +PACKAGE_VERSION='trunk-r375' +PACKAGE_STRING='motion trunk-r375' PACKAGE_BUGREPORT='' ac_unique_file="motion.c" @@ -1177,7 +1177,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures motion trunk-r374 to adapt to many kinds of systems. +\`configure' configures motion trunk-r375 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1238,7 +1238,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of motion trunk-r374:";; + short | recursive ) echo "Configuration of motion trunk-r375:";; esac cat <<\_ACEOF @@ -1374,7 +1374,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -motion configure trunk-r374 +motion configure trunk-r375 generated by GNU Autoconf 2.61 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, @@ -1388,7 +1388,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by motion $as_me trunk-r374, which was +It was created by motion $as_me trunk-r375, which was generated by GNU Autoconf 2.61. Invocation command line was $ $0 $@ @@ -8666,7 +8666,7 @@ exec 6>&1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by motion $as_me trunk-r374, which was +This file was extended by motion $as_me trunk-r375, which was generated by GNU Autoconf 2.61. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -8715,7 +8715,7 @@ Report bugs to ." _ACEOF cat >>$CONFIG_STATUS <<_ACEOF ac_cs_version="\\ -motion config.status trunk-r374 +motion config.status trunk-r375 configured by $0, generated by GNU Autoconf 2.61, with options \\"`echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\" diff --git a/event.c b/event.c index da2255b9..95877b72 100644 --- a/event.c +++ b/event.c @@ -47,12 +47,12 @@ static void exec_command(struct context *cnt, char *command, char *filename, int execl("/bin/sh", "sh", "-c", stamp, " &", NULL); /* if above function succeeds the program never reach here */ - motion_log(LOG_ERR, 1, "Unable to start external command '%s'", stamp); + motion_log(LOG_ERR, 1, "%s: Unable to start external command '%s'", __FUNCTION__, stamp); exit(1); } - else if (cnt->conf.setup_mode) - motion_log(-1, 0, "Executing external command '%s'", stamp); + else if (debug_level >= CAMERA_VERBOSE) + motion_log(-1, 0, "%s: Executing external command '%s'", __FUNCTION__, stamp); } /* @@ -63,7 +63,8 @@ static void event_newfile(struct context *cnt ATTRIBUTE_UNUSED, int type ATTRIBUTE_UNUSED, unsigned char *dummy ATTRIBUTE_UNUSED, char *filename, void *ftype, struct tm *tm ATTRIBUTE_UNUSED) { - motion_log(-1, 0, "File of type %ld saved to: %s", (unsigned long)ftype, filename); + motion_log(-1, 0, "%s: File of type %ld saved to: %s", __FUNCTION__, + (unsigned long)ftype, filename); } @@ -130,17 +131,20 @@ static void event_sqlnewfile(struct context *cnt, int type ATTRIBUTE_UNUSED, if (mysql_query(cnt->database, sqlquery) != 0){ int error_code = mysql_errno(cnt->database); - motion_log(LOG_ERR, 1, "Mysql query failed %s error code %d", - mysql_error(cnt->database), error_code); + motion_log(LOG_ERR, 1, "%s: Mysql query failed %s error code %d", + __FUNCTION__, mysql_error(cnt->database), error_code); /* Try to reconnect ONCE if fails continue and discard this sql query */ if (error_code >= 2000){ cnt->database = (MYSQL *) mymalloc(sizeof(MYSQL)); mysql_init(cnt->database); if (!mysql_real_connect(cnt->database, cnt->conf.database_host, cnt->conf.database_user, cnt->conf.database_password, cnt->conf.database_dbname, 0, NULL, 0)) { - motion_log(LOG_ERR, 0, "Cannot reconnect to MySQL database %s on host %s with user %s", - cnt->conf.database_dbname, cnt->conf.database_host, cnt->conf.database_user); - motion_log(LOG_ERR, 0, "MySQL error was %s", mysql_error(cnt->database)); + motion_log(LOG_ERR, 0, "%s: Cannot reconnect to MySQL database " + "%s on host %s with user %s MySQL error was %s", + __FUNCTION__, cnt->conf.database_dbname, + cnt->conf.database_host, cnt->conf.database_user, + mysql_error(cnt->database)); + }else mysql_query(cnt->database, sqlquery); } } @@ -154,7 +158,7 @@ static void event_sqlnewfile(struct context *cnt, int type ATTRIBUTE_UNUSED, res = PQexec(cnt->database_pg, sqlquery); if (PQresultStatus(res) != PGRES_COMMAND_OK) { - motion_log(LOG_ERR, 1, "PGSQL query failed"); + motion_log(LOG_ERR, 1, "%s: PGSQL query failed", __FUNCTION__); PQclear(res); } } @@ -216,7 +220,7 @@ static void event_vid_putpipe(struct context *cnt, int type ATTRIBUTE_UNUSED, { if (*(int *)devpipe >= 0) { if (vid_putpipe(*(int *)devpipe, img, cnt->imgs.size) == -1) - motion_log(LOG_ERR, 1, "Failed to put image into video pipe"); + motion_log(LOG_ERR, 1, "%s: Failed to put image into video pipe", __FUNCTION__); } } #endif /* BSD */ @@ -310,7 +314,8 @@ static void event_image_snapshot(struct context *cnt, int type ATTRIBUTE_UNUSED, snprintf(linkpath, PATH_MAX, "%s/lastsnap.%s", cnt->conf.filepath, imageext(cnt)); remove(linkpath); if (symlink(filename, linkpath)) { - motion_log(LOG_ERR, 1, "Could not create symbolic link [%s]", filename); + motion_log(LOG_ERR, 1, "%s: Could not create symbolic link [%s]", + __FUNCTION__, filename); return; } } else { @@ -402,7 +407,8 @@ static void event_ffmpeg_newfile(struct context *cnt, int type ATTRIBUTE_UNUSED, ffmpeg_open((char *)cnt->conf.ffmpeg_video_codec, cnt->newfilename, y, u, v, cnt->imgs.width, cnt->imgs.height, cnt->movie_fps, cnt->conf.ffmpeg_bps, cnt->conf.ffmpeg_vbr)) == NULL) { - motion_log(LOG_ERR, 1, "ffopen_open error creating (new) file [%s]", cnt->newfilename); + motion_log(LOG_ERR, 1, "s%: ffopen_open error creating (new) file [%s]", + __FUNCTION__, cnt->newfilename); cnt->finish = 1; return; } @@ -427,7 +433,8 @@ static void event_ffmpeg_newfile(struct context *cnt, int type ATTRIBUTE_UNUSED, ffmpeg_open((char *)cnt->conf.ffmpeg_video_codec, cnt->motionfilename, y, u, v, cnt->imgs.width, cnt->imgs.height, cnt->movie_fps, cnt->conf.ffmpeg_bps, cnt->conf.ffmpeg_vbr)) == NULL){ - motion_log(LOG_ERR, 1, "ffopen_open error creating (motion) file [%s]", cnt->motionfilename); + motion_log(LOG_ERR, 1, "%s: ffopen_open error creating (motion) file [%s]", + __FUNCTION__, cnt->motionfilename); cnt->finish = 1; return; } @@ -478,7 +485,8 @@ static void event_ffmpeg_timelapse(struct context *cnt, ffmpeg_open((char *)TIMELAPSE_CODEC, cnt->timelapsefilename, y, u, v, cnt->imgs.width, cnt->imgs.height, 24, cnt->conf.ffmpeg_bps, cnt->conf.ffmpeg_vbr)) == NULL) { - motion_log(LOG_ERR, 1, "ffopen_open error creating (timelapse) file [%s]", cnt->timelapsefilename); + motion_log(LOG_ERR, 1, "%s: ffopen_open error creating (timelapse) file [%s]", + __FUNCTION__, cnt->timelapsefilename); cnt->finish = 1; return; } diff --git a/ffmpeg.c b/ffmpeg.c index 093dab46..f5fd8a32 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -206,7 +206,8 @@ static int mpeg1_write_trailer(AVFormatContext *s) /* ffmpeg_init initializes for libavformat. */ void ffmpeg_init() { - motion_log(LOG_INFO, 0, "ffmpeg LIBAVCODEC_BUILD %d LIBAVFORMAT_BUILD %d", LIBAVCODEC_BUILD, LIBAVFORMAT_BUILD); + motion_log(LOG_INFO, 0, "%s: ffmpeg LIBAVCODEC_BUILD %d LIBAVFORMAT_BUILD %d", + __FUNCTION__, LIBAVCODEC_BUILD, LIBAVFORMAT_BUILD); av_register_all(); #if LIBAVCODEC_BUILD > 4680 @@ -255,7 +256,8 @@ static AVOutputFormat *get_oformat(const char *codec, char *filename) } #ifdef FFMPEG_NO_NONSTD_MPEG1 } else if (strcmp(codec, "mpeg1") == 0) { - motion_log(LOG_ERR, 0, "*** mpeg1 support for normal videos has been disabled ***"); + motion_log(LOG_ERR, 0, "%s: *** mpeg1 support for normal videos has been disabled ***", + __FUNCTION__); return NULL; #endif } else if (strcmp(codec, "mpeg4") == 0) { @@ -286,12 +288,13 @@ static AVOutputFormat *get_oformat(const char *codec, char *filename) ext = ".mov"; of = guess_format("mov", NULL, NULL); } else { - motion_log(LOG_ERR, 0, "ffmpeg_video_codec option value %s is not supported", codec); + motion_log(LOG_ERR, 0, "%s: ffmpeg_video_codec option value %s is not supported", + __FUNCTION__, codec); return NULL; } if (!of) { - motion_log(LOG_ERR, 0, "Could not guess format for %s", codec); + motion_log(LOG_ERR, 0, "%s: Could not guess format for %s", __FUNCTION__, codec); return NULL; } @@ -331,7 +334,7 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, /* allocation the output media context */ ffmpeg->oc = av_mallocz(sizeof(AVFormatContext)); if (!ffmpeg->oc) { - motion_log(LOG_ERR, 1, "Memory error while allocating output media context"); + motion_log(LOG_ERR, 1, "%s: Memory error while allocating output media context", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -350,13 +353,13 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, if (ffmpeg->oc->oformat->video_codec != CODEC_ID_NONE) { ffmpeg->video_st = av_new_stream(ffmpeg->oc, 0); if (!ffmpeg->video_st) { - motion_log(LOG_ERR, 1, "av_new_stream - could not alloc stream"); + motion_log(LOG_ERR, 1, "%s: av_new_stream - could not alloc stream", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } } else { /* We did not get a proper video codec. */ - motion_log(LOG_ERR, 0, "Failed to obtain a proper video codec"); + motion_log(LOG_ERR, 0, "%s: Failed to obtain a proper video codec", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -404,7 +407,8 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, /* set the output parameters (must be done even if no parameters). */ if (av_set_parameters(ffmpeg->oc, NULL) < 0) { - motion_log(LOG_ERR, 0, "ffmpeg av_set_parameters error: Invalid output format parameters"); + motion_log(LOG_ERR, 0, "%s: av_set_parameters error: Invalid output format parameters", + __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -416,7 +420,7 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, codec and allocate the necessary encode buffers */ codec = avcodec_find_encoder(c->codec_id); if (!codec) { - motion_log(LOG_ERR, 1, "Codec not found"); + motion_log(LOG_ERR, 1, "%s: Codec not found", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -431,7 +435,7 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, if (avcodec_open(c, codec) < 0) { /* Release the lock. */ pthread_mutex_unlock(&global_lock); - motion_log(LOG_ERR, 1, "avcodec_open - could not open codec"); + motion_log(LOG_ERR, 1, "%s: avcodec_open - could not open codec", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -452,7 +456,7 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, /* allocate the encoded raw picture */ ffmpeg->picture = avcodec_alloc_frame(); if (!ffmpeg->picture) { - motion_log(LOG_ERR, 1, "avcodec_alloc_frame - could not alloc frame"); + motion_log(LOG_ERR, 1, "%s: avcodec_alloc_frame - could not alloc frame", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -494,20 +498,22 @@ struct ffmpeg *ffmpeg_open(char *ffmpeg_video_codec, char *filename, /* and retry opening the file (use file_proto) */ if (url_fopen(&ffmpeg->oc->pb, file_proto, URL_WRONLY) < 0) { - motion_log(LOG_ERR, 1, "url_fopen - error opening file %s", filename); + motion_log(LOG_ERR, 1, "%s: url_fopen - error opening file %s", + __FUNCTION__, filename); ffmpeg_cleanups(ffmpeg); return (NULL); } /* Permission denied */ } else if (errno == EACCES) { motion_log(LOG_ERR, 1, - "url_fopen - error opening file %s" - " ... check access rights to target directory", filename); + "%s: url_fopen - error opening file %s" + " ... check access rights to target directory", + __FUNCTION__, filename); /* create path for file... */ ffmpeg_cleanups(ffmpeg); return (NULL); } else { - motion_log(LOG_ERR, 1, "Error opening file %s", filename); + motion_log(LOG_ERR, 1, "%s: Error opening file %s", __FUNCTION__, filename); ffmpeg_cleanups(ffmpeg); return (NULL); } @@ -673,7 +679,7 @@ int ffmpeg_put_frame(struct ffmpeg *ffmpeg, AVFrame *pic) } if (ret != 0) { - motion_log(LOG_ERR, 1, "Error while writing video frame"); + motion_log(LOG_ERR, 1, "%s: Error while writing video frame", __FUNCTION__); ffmpeg_cleanups(ffmpeg); return (-1); } @@ -695,7 +701,7 @@ AVFrame *ffmpeg_prepare_frame(struct ffmpeg *ffmpeg, unsigned char *y, picture = avcodec_alloc_frame(); if (!picture) { - motion_log(LOG_ERR, 1, "Could not alloc frame"); + motion_log(LOG_ERR, 1, "%s: Could not alloc frame", __FUNCTION__); return NULL; } @@ -735,7 +741,7 @@ void ffmpeg_deinterlace(unsigned char *img, int width, int height) picture = avcodec_alloc_frame(); if (!picture) { - motion_log(LOG_ERR, 1, "Could not alloc frame"); + motion_log(LOG_ERR, 1, "%s: Could not alloc frame", __FUNCTION__); return; } @@ -777,7 +783,7 @@ void ffmpeg_avcodec_log(void *ignoreme ATTRIBUTE_UNUSED, int errno_flag, const c vsnprintf(buf, sizeof(buf), fmt, vl); /* If the debug_level is correct then send the message to the motion logging routine. */ - motion_log(LOG_ERR, 0, "ffmpeg_avcodec_log: %s - flag %d", buf, errno_flag); + motion_log(LOG_ERR, 0, "%s: %s - flag %d", __FUNCTION__, buf, errno_flag); } } diff --git a/motion.c b/motion.c index d430fa8f..a04f3c07 100644 --- a/motion.c +++ b/motion.c @@ -105,7 +105,8 @@ static void image_ring_resize(struct context *cnt, int new_size) smallest = cnt->imgs.image_ring_size; } if (cnt->imgs.image_ring_in == smallest - 1 || smallest == 0) { - motion_log(LOG_INFO, 0, "Resizing pre_capture buffer to %d items", new_size); + motion_log(LOG_INFO, 0, "%s: Resizing pre_capture buffer to %d items", + __FUNCTION__, new_size); /* Create memory for new ring buffer */ struct image_data *tmp; @@ -356,9 +357,9 @@ static void motion_remove_pid(void) { if ((cnt_list[0]->daemon) && (cnt_list[0]->conf.pid_file) && (restart == 0)) { if (!unlink(cnt_list[0]->conf.pid_file)) - motion_log(LOG_INFO, 0, "Removed process id file (pid file)."); + motion_log(LOG_INFO, 0, "%s: Removed process id file (pid file).", __FUNCTION__); else - motion_log(LOG_INFO, 1, "Error removing pid file"); + motion_log(LOG_INFO, 1, "%s: Error removing pid file", __FUNCTION__); } } @@ -420,8 +421,9 @@ static void motion_detected(struct context *cnt, int dev, struct image_data *img /* EVENT_FIRSTMOTION triggers on_event_start_command and event_ffmpeg_newfile */ event(cnt, EVENT_FIRSTMOTION, img->image, NULL, NULL, &img->timestamp_tm); - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Motion detected - starting event %d", cnt->event_nr); + if (debug_level >= CAMERA_INFO) + motion_log(-1, 0, "%s: Motion detected - starting event %d", + __FUNCTION__, cnt->event_nr); /* always save first motion frame as preview-shot, may be changed to an other one later */ if (cnt->new_img & (NEWIMG_FIRST | NEWIMG_BEST | NEWIMG_CENTER)) { @@ -523,7 +525,8 @@ static void process_image_ring(struct context *cnt, unsigned int max_images) int frames = cnt->movie_fps - (cnt->movie_last_shot + 1); if (frames > 0) { char tmp[15]; - motion_log(LOG_DEBUG, 0, "Added %d fillerframes into movie", frames ); + motion_log(LOG_DEBUG, 0, "%s: Added %d fillerframes into movie", + __FUNCTION__, frames ); sprintf(tmp, "Fillerframes %d", frames); draw_text(cnt->imgs.image_ring[cnt->imgs.image_ring_out].image, 10, 40, cnt->imgs.width, tmp, cnt->conf.text_double); @@ -626,7 +629,8 @@ static int motion_init(struct context *cnt) cnt->detecting_motion = 0; cnt->makemovie = 0; - motion_log(LOG_DEBUG, 0, "Thread %d started", (unsigned long)pthread_getspecific(tls_key_threadnr)); + motion_log(LOG_DEBUG, 0, "%s: Thread %d started", + __FUNCTION__, (unsigned long)pthread_getspecific(tls_key_threadnr)); if (!cnt->conf.filepath) cnt->conf.filepath = mystrdup("."); @@ -639,8 +643,9 @@ static int motion_init(struct context *cnt) * file options. */ if (cnt->video_dev < 0) { - motion_log(LOG_ERR, 0, "Could not fetch initial image from camera"); - motion_log(LOG_ERR, 0, "Motion continues using width and height from config file(s)"); + motion_log(LOG_ERR, 0, "%s: Could not fetch initial image from camera\n" + "Motion continues using width and height from config file(s)", + __FUNCTION__); cnt->imgs.width = cnt->conf.width; cnt->imgs.height = cnt->conf.height; cnt->imgs.size = cnt->conf.width * cnt->conf.height * 3 / 2; @@ -697,7 +702,7 @@ static int motion_init(struct context *cnt) memset(cnt->imgs.image_virgin, 0x80, cnt->imgs.size); /* initialize to grey */ draw_text(cnt->imgs.image_virgin, 10, 20, cnt->imgs.width, "Error capturing first image", cnt->conf.text_double); - motion_log(LOG_ERR, 0, "Error capturing first image"); + motion_log(LOG_ERR, 0, "%s: Error capturing first image", __FUNCTION__); } } @@ -708,26 +713,26 @@ static int motion_init(struct context *cnt) #if (!defined(BSD)) /* open video loopback devices if enabled */ if (cnt->conf.vidpipe) { - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Opening video loopback device for normal pictures"); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Opening video loopback device for normal pictures", __FUNCTION__); else - motion_log(LOG_INFO, 0, "Opening video loopback device for normal pictures"); + motion_log(LOG_INFO, 0, "%s: Opening video loopback device for normal pictures", __FUNCTION__); /* vid_startpipe should get the output dimensions */ cnt->pipe = vid_startpipe(cnt->conf.vidpipe, cnt->imgs.width, cnt->imgs.height, cnt->imgs.type); if (cnt->pipe < 0) { - motion_log(LOG_ERR, 0, "Failed to open video loopback"); + motion_log(LOG_ERR, 0, "%s: Failed to open video loopback", __FUNCTION__); return -1; } } if (cnt->conf.motionvidpipe) { - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Opening video loopback device for motion pictures"); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Opening video loopback device for motion pictures", __FUNCTION__); else - motion_log(LOG_INFO, 0, "Opening video loopback device for motion pictures"); + motion_log(LOG_INFO, 0, "%s: Opening video loopback device for motion pictures", __FUNCTION__); /* vid_startpipe should get the output dimensions */ cnt->mpipe = vid_startpipe(cnt->conf.motionvidpipe, cnt->imgs.width, cnt->imgs.height, cnt->imgs.type); if (cnt->mpipe < 0) { - motion_log(LOG_ERR, 0, "Failed to open video loopback"); + motion_log(LOG_ERR, 0, "%s: Failed to open video loopback", __FUNCTION__); return -1; } } @@ -743,9 +748,10 @@ static int motion_init(struct context *cnt) mysql_init(cnt->database); if (!mysql_real_connect(cnt->database, cnt->conf.database_host, cnt->conf.database_user, cnt->conf.database_password, cnt->conf.database_dbname, 0, NULL, 0)) { - motion_log(LOG_ERR, 0, "Cannot connect to MySQL database %s on host %s with user %s", - cnt->conf.database_dbname, cnt->conf.database_host, cnt->conf.database_user); - motion_log(LOG_ERR, 0, "MySQL error was %s", mysql_error(cnt->database)); + motion_log(LOG_ERR, 0, "%s: Cannot connect to MySQL database %s on host %s with user %s", + __FUNCTION__, cnt->conf.database_dbname, cnt->conf.database_host, + cnt->conf.database_user); + motion_log(LOG_ERR, 0, "%s: MySQL error was %s", __FUNCTION__, mysql_error(cnt->database)); return -2; } #if (defined(MYSQL_VERSION_ID)) && (MYSQL_VERSION_ID > 50012) @@ -772,8 +778,8 @@ static int motion_init(struct context *cnt) cnt->database_pg = PQconnectdb(connstring); if (PQstatus(cnt->database_pg) == CONNECTION_BAD) { - motion_log(LOG_ERR, 0, "Connection to PostgreSQL database '%s' failed: %s", - cnt->conf.database_dbname, PQerrorMessage(cnt->database_pg)); + motion_log(LOG_ERR, 0, "%s: Connection to PostgreSQL database '%s' failed: %s", + __FUNCTION__, cnt->conf.database_dbname, PQerrorMessage(cnt->database_pg)); return -2; } } @@ -800,18 +806,22 @@ static int motion_init(struct context *cnt) cnt->imgs.mask = get_pgm(picture, cnt->imgs.width, cnt->imgs.height); fclose(picture); } else { - motion_log(LOG_ERR, 1, "Error opening mask file %s", cnt->conf.mask_file); + motion_log(LOG_ERR, 1, "%s: Error opening mask file %s", + __FUNCTION__, cnt->conf.mask_file); /* Try to write an empty mask file to make it easier for the user to edit it */ put_fixed_mask(cnt, cnt->conf.mask_file); } if (!cnt->imgs.mask) { - motion_log(LOG_ERR, 0, "Failed to read mask image. Mask feature disabled."); + motion_log(LOG_ERR, 0, "%s: Failed to read mask image. Mask feature disabled.", + __FUNCTION__); } else { - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Maskfile \"%s\" loaded.", cnt->conf.mask_file); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Maskfile \"%s\" loaded.", + __FUNCTION__, cnt->conf.mask_file); else - motion_log(LOG_INFO, 0, "Maskfile \"%s\" loaded.", cnt->conf.mask_file); + motion_log(LOG_INFO, 0, "%s: Maskfile \"%s\" loaded.", + __FUNCTION__, cnt->conf.mask_file); } } else cnt->imgs.mask = NULL; @@ -830,9 +840,11 @@ static int motion_init(struct context *cnt) /* Initialize stream server if stream port is specified to not 0 */ if (cnt->conf.stream_port) { if ( stream_init(cnt) == -1 ) { - motion_log(LOG_ERR, 1, "Problem enabling motion-stream server in port %d", cnt->conf.stream_port); + motion_log(LOG_ERR, 1, "%s: Problem enabling motion-stream server in port %d", + __FUNCTION__, cnt->conf.stream_port); cnt->finish = 1; - }else motion_log(LOG_DEBUG, 0, "Started motion-stream server in port %d", cnt->conf.stream_port); + }else motion_log(LOG_DEBUG, 0, "%s: Started motion-stream server in port %d", + __FUNCTION__, cnt->conf.stream_port); } /* Prevent first few frames from triggering motion... */ @@ -861,7 +873,7 @@ static void motion_cleanup(struct context *cnt) event(cnt, EVENT_STOP, NULL, NULL, NULL, NULL); if (cnt->video_dev >= 0){ - motion_log(LOG_DEBUG, 0, "Calling vid_close() from motion_cleanup"); + motion_log(LOG_DEBUG, 0, "%s: Calling vid_close() from motion_cleanup", __FUNCTION__); vid_close(cnt); } @@ -1127,18 +1139,18 @@ static void *motion_loop(void *arg) if (cnt->video_dev < 0 && cnt->currenttime % 10 == 0 && cnt->shots == 0) { motion_log(LOG_ERR, 0, - "Retrying until successful connection with camera"); + "%s: Retrying until successful connection with camera", __FUNCTION__); cnt->video_dev = vid_start(cnt); /* if the netcam has different dimensions than in the config file * we need to restart Motion to re-allocate all the buffers */ if (cnt->imgs.width != cnt->conf.width || cnt->imgs.height != cnt->conf.height) { - motion_log(LOG_ERR, 0, "Camera has finally become available"); - motion_log(LOG_ERR, 0, "Camera image has different width and height " - "from what is in the config file. You should fix that"); - motion_log(LOG_ERR, 0, "Restarting Motion thread to reinitialize all " - "image buffers to new picture dimensions"); + motion_log(LOG_ERR, 0, "%s: Camera has finally become available\n" + "Camera image has different width and height" + "from what is in the config file. You should fix that\n" + "Restarting Motion thread to reinitialize all " + "image buffers to new picture dimensions", __FUNCTION__); cnt->conf.width = cnt->imgs.width; cnt->conf.height = cnt->imgs.height; /* Break out of main loop terminating thread @@ -1170,7 +1182,7 @@ static void *motion_loop(void *arg) /* If all is well reset missing_frame_counter */ if (cnt->missing_frame_counter >= MISSING_FRAMES_TIMEOUT * cnt->conf.frame_limit) { /* If we previously logged starting a grey image, now log video re-start */ - motion_log(LOG_ERR, 0, "Video signal re-acquired"); + motion_log(LOG_ERR, 0, "%s: Video signal re-acquired", __FUNCTION__); // event for re-acquired video signal can be called here } cnt->missing_frame_counter = 0; @@ -1199,7 +1211,8 @@ static void *motion_loop(void *arg) // FATAL ERROR - leave the thread by breaking out of the main loop } else if (vid_return_code < 0) { /* Fatal error - Close video device */ - motion_log(LOG_ERR, 0, "Video device fatal error - Closing video device"); + motion_log(LOG_ERR, 0, "%s: Video device fatal error - Closing video device", + __FUNCTION__); vid_close(cnt); /* Use virgin image, if we are not able to open it again next loop * a gray image with message is applied @@ -1217,7 +1230,7 @@ static void *motion_loop(void *arg) } else { if (debug_level >= CAMERA_VERBOSE) - motion_log(-1, 0, "vid_return_code %d", vid_return_code); + motion_log(-1, 0, "%s: vid_return_code %d", __FUNCTION__, vid_return_code); /* Netcams that change dimensions while Motion is running will * require that Motion restarts to reinitialize all the many @@ -1225,8 +1238,8 @@ static void *motion_loop(void *arg) * other way */ if (vid_return_code == NETCAM_RESTART_ERROR) { - motion_log(LOG_ERR, 0, "Restarting Motion thread to reinitialize all " - "image buffers"); + motion_log(LOG_ERR, 0, "%s: Restarting Motion thread to reinitialize all " + "image buffers", __FUNCTION__); /* Break out of main loop terminating thread * watchdog will start us again * Set lost_connection flag on */ @@ -1268,7 +1281,8 @@ static void *motion_loop(void *arg) /* Write error message only once */ if (cnt->missing_frame_counter == MISSING_FRAMES_TIMEOUT * cnt->conf.frame_limit) { - motion_log(LOG_ERR, 0, "Video signal lost - Adding grey image"); + motion_log(LOG_ERR, 0, "%s: Video signal lost - Adding grey image", + __FUNCTION__); // Event for lost video signal can be called from here event(cnt, EVENT_CAMERA_LOST, NULL, NULL, NULL, cnt->currenttime_tm); @@ -1278,7 +1292,8 @@ static void *motion_loop(void *arg) * Only try this when a device is open */ if ( (cnt->video_dev > 0) && (cnt->missing_frame_counter == (MISSING_FRAMES_TIMEOUT * 4) * cnt->conf.frame_limit) ) { - motion_log(LOG_ERR, 0, "Video signal still lost - Trying to close video device"); + motion_log(LOG_ERR, 0, "%s: Video signal still lost - " + "Trying to close video device",__FUNCTION__); vid_close(cnt); } } @@ -1314,8 +1329,8 @@ static void *motion_loop(void *arg) */ if (cnt->conf.lightswitch && !cnt->lost_connection) { if (alg_lightswitch(cnt, cnt->current_image->diffs)) { - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Lightswitch detected"); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Lightswitch detected", __FUNCTION__); if (cnt->moved < 5) cnt->moved = 5; cnt->current_image->diffs = 0; @@ -1335,8 +1350,8 @@ static void *motion_loop(void *arg) cnt->current_image->diffs = alg_switchfilter(cnt, cnt->current_image->diffs, cnt->current_image->image); if (cnt->current_image->diffs <= cnt->threshold) { cnt->current_image->diffs = 0; - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Switchfilter detected"); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Switchfilter detected", __FUNCTION__); } } @@ -1427,8 +1442,8 @@ static void *motion_loop(void *arg) alg_update_reference_frame(cnt, RESET_REF_FRAME); cnt->current_image->diffs = 0; cnt->lightswitch_framecounter = 0; - if (cnt->conf.setup_mode) - motion_log(-1, 0, "micro-lightswitch!"); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: micro-lightswitch!", __FUNCTION__); } else { alg_update_reference_frame(cnt, UPDATE_REF_FRAME); } @@ -1481,7 +1496,7 @@ static void *motion_loop(void *arg) /* Add changed pixels to motion-images (for stream) in setup_mode and always overlay smartmask (not only when motion is detected) */ - if (cnt->conf.setup_mode) { + if (debug_level >= CAMERA_DEBUG) { char tmp[PATH_MAX]; sprintf(tmp, "D:%5d L:%3d N:%3d", cnt->current_image->diffs, cnt->current_image->total_labels, cnt->noise); draw_text(cnt->imgs.out, cnt->imgs.width - 10, cnt->imgs.height - 30 * text_size_factor, @@ -1596,7 +1611,7 @@ static void *motion_loop(void *arg) event(cnt, EVENT_AREA_DETECTED, NULL, NULL, NULL, cnt->currenttime_tm); area_once = cnt->event_nr; /* Fire script only once per event */ - if (cnt->conf.setup_mode) + if (debug_level >= CAMERA_DEBUG) motion_log(-1, 0, "Motion in area %d detected.\n", z+1); break; } @@ -1634,8 +1649,9 @@ static void *motion_loop(void *arg) if (cnt->track.type) cnt->moved = track_center(cnt, cnt->video_dev, 0, 0, 0); - if (cnt->conf.setup_mode) - motion_log(-1, 0, "End of event %d", cnt->event_nr); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: End of event %d", + __FUNCTION__, cnt->event_nr); cnt->makemovie = 0; /* Reset post capture */ @@ -1658,7 +1674,7 @@ static void *motion_loop(void *arg) /***** MOTION LOOP - SETUP MODE CONSOLE OUTPUT SECTION *****/ /* If setup_mode enabled output some numbers to console */ - if (cnt->conf.setup_mode){ + if (debug_level >= CAMERA_DEBUG){ char msg[1024] = "\0"; char part[100]; @@ -1757,9 +1773,10 @@ static void *motion_loop(void *arg) /* If invalid we report in syslog once and continue in manual mode */ else { - motion_log(LOG_ERR, 0, "Invalid timelapse_mode argument '%s'", - cnt->conf.timelapse_mode); - motion_log(LOG_ERR, 0, "Defaulting to manual timelapse mode"); + motion_log(LOG_ERR, 0, "%s: Invalid timelapse_mode argument '%s'", + __FUNCTION__, cnt->conf.timelapse_mode); + motion_log(LOG_ERR, 0, "%:s Defaulting to manual timelapse mode", + __FUNCTION__); conf_cmdparse(&cnt, (char *)"ffmpeg_timelapse_mode",(char *)"manual"); } } @@ -1925,7 +1942,7 @@ err: free(rolling_average_data); cnt->lost_connection = 1; - motion_log(-1, 0, "Thread exiting"); + motion_log(-1, 0, "%s: Thread exiting", __FUNCTION__); motion_cleanup(cnt); @@ -1973,7 +1990,7 @@ static void become_daemon(void) /* fork */ if (fork()) { - motion_log(-1, 0, "Motion going to daemon mode"); + motion_log(-1, 0, "%s: Motion going to daemon mode", __FUNCTION__); exit(0); } @@ -1989,8 +2006,8 @@ static void become_daemon(void) (void)fprintf(pidf, "%d\n", getpid()); fclose(pidf); } else { - motion_log(LOG_ERR, 1, "Exit motion, cannot create process id file (pid file) %s", - cnt_list[0]->conf.pid_file); + motion_log(LOG_ERR, 1, "%s: Exit motion, cannot create process id file (pid file) %s", + __FUNCTION__, cnt_list[0]->conf.pid_file); exit(0); } } @@ -1998,7 +2015,7 @@ static void become_daemon(void) /* changing dir to root enables people to unmount a disk without having to stop Motion */ if (chdir("/")) { - motion_log(LOG_ERR, 1, "Could not change directory"); + motion_log(LOG_ERR, 1, "%s: Could not change directory", __FUNCTION__); } #if (defined(BSD)) @@ -2031,8 +2048,8 @@ static void become_daemon(void) /* Now it is safe to add the PID creation to the logs */ if ( pidf ) - motion_log(LOG_INFO, 0, "Created process id file %s. Process ID is %d", - cnt_list[0]->conf.pid_file, getpid()); + motion_log(LOG_INFO, 0, "%s: Created process id file %s. Process ID is %d", + __FUNCTION__, cnt_list[0]->conf.pid_file, getpid()); sigaction(SIGTTOU, &sig_ign_action, NULL); sigaction(SIGTTIN, &sig_ign_action, NULL); @@ -2131,7 +2148,7 @@ static void motion_startup(int daemonize, int argc, char *argv[]) */ cntlist_create(argc, argv); - motion_log(LOG_INFO, 0, "Motion "VERSION" Started"); + motion_log(LOG_INFO, 0, "%s: Motion "VERSION" Started", __FUNCTION__); initialize_chars(); @@ -2141,7 +2158,7 @@ static void motion_startup(int daemonize, int argc, char *argv[]) */ if (cnt_list[0]->daemon && cnt_list[0]->conf.setup_mode == 0) { become_daemon(); - motion_log(LOG_INFO, 0, "Motion running as daemon process"); + motion_log(LOG_INFO, 0, "%s: Motion running as daemon process", __FUNCTION__); } } @@ -2214,9 +2231,10 @@ static void start_motion_thread(struct context *cnt, pthread_attr_t *thread_attr /* Compare against the control port. */ if (cnt_list[0]->conf.webcontrol_port == cnt->conf.stream_port) { motion_log(LOG_ERR, 0, - "Stream port number %d for thread %d conflicts with the control port", - cnt->conf.stream_port, cnt->threadnr); - motion_log(LOG_ERR, 0, "Stream feature for thread %d is disabled.", cnt->threadnr); + "%s: Stream port number %d for thread %d conflicts with the control port", + __FUNCTION__, cnt->conf.stream_port, cnt->threadnr); + motion_log(LOG_ERR, 0, "%s: Stream feature for thread %d is disabled.", + __FUNCTION__, cnt->threadnr); cnt->conf.stream_port = 0; } @@ -2226,10 +2244,11 @@ static void start_motion_thread(struct context *cnt, pthread_attr_t *thread_attr continue; if (cnt_list[i]->conf.stream_port == cnt->conf.stream_port) { motion_log(LOG_ERR, 0, - "Stream port number %d for thread %d conflicts with thread %d", - cnt->conf.stream_port, cnt->threadnr, cnt_list[i]->threadnr); + "%s: Stream port number %d for thread %d conflicts with thread %d", + __FUNCTION__, cnt->conf.stream_port, cnt->threadnr, cnt_list[i]->threadnr); motion_log(LOG_ERR, 0, - "Stream feature for thread %d is disabled.", cnt->threadnr); + "%s: Stream feature for thread %d is disabled.", + __FUNCTION__, cnt->threadnr); cnt->conf.stream_port = 0; } } @@ -2297,7 +2316,7 @@ int main (int argc, char **argv) * optimize motion detection and stuff. */ if(cnt_list[0]->conf.setup_mode) - motion_log(-1, 0, "Motion running in setup mode."); + motion_log(-1, 0, "%s: Motion running in setup mode.", __FUNCTION__); /* Create and a thread attribute for the threads we spawn later on. * PTHREAD_CREATE_DETACHED means to create threads detached, i.e. @@ -2317,7 +2336,7 @@ int main (int argc, char **argv) */ motion_shutdown(); restart = 0; /* only one reset for now */ - motion_log(LOG_INFO, 0, "motion restarted"); + motion_log(LOG_INFO, 0, "%s: motion restarted", __FUNCTION__); #ifndef WITHOUT_V4L SLEEP(5, 0); // maybe some cameras needs less time #endif @@ -2333,17 +2352,19 @@ int main (int argc, char **argv) cnt_list[i]->threadnr = i ? i : 1; if ( strcmp(cnt_list[i]->conf_filename, "") ) - motion_log(LOG_INFO, 0, "Thread %d is from %s", cnt_list[i]->threadnr, cnt_list[i]->conf_filename ); + motion_log(LOG_INFO, 0, "%s: Thread %d is from %s", + __FUNCTION__, cnt_list[i]->threadnr, cnt_list[i]->conf_filename ); - if (cnt_list[0]->conf.setup_mode) { - motion_log(-1, 0, "Thread %d is device: %s input %d", cnt_list[i]->threadnr, - cnt_list[i]->conf.netcam_url ? cnt_list[i]->conf.netcam_url : cnt_list[i]->conf.video_device, - cnt_list[i]->conf.netcam_url ? -1 : cnt_list[i]->conf.input - ); + if (debug_level >= CAMERA_DEBUG) { + motion_log(-1, 0, "%s: Thread %d is device: %s input %d", __FUNCTION__, + cnt_list[i]->threadnr, cnt_list[i]->conf.netcam_url ? + cnt_list[i]->conf.netcam_url : cnt_list[i]->conf.video_device, + cnt_list[i]->conf.netcam_url ? -1 : cnt_list[i]->conf.input); } - if (cnt_list[0]->conf.setup_mode) - motion_log(LOG_ERR, 0, "Stream port %d", cnt_list[i]->conf.stream_port); + if (debug_level >= CAMERA_DEBUG) + motion_log(LOG_ERR, 0, "%s: Stream port %d", + __FUNCTION__, cnt_list[i]->conf.stream_port); start_motion_thread(cnt_list[i], &thread_attr); } @@ -2354,8 +2375,8 @@ int main (int argc, char **argv) if (cnt_list[0]->conf.webcontrol_port) pthread_create(&thread_id, &thread_attr, &motion_web_control, cnt_list); - if (cnt_list[0]->conf.setup_mode) - motion_log(-1, 0, "Waiting for threads to finish, pid: %d", getpid()); + if (debug_level >= CAMERA_DEBUG) + motion_log(-1, 0, "%s: Waiting for threads to finish, pid: %d", __FUNCTION__, getpid()); /* Crude way of waiting for all threads to finish - check the thread * counter (because we cannot do join on the detached threads). @@ -2374,27 +2395,28 @@ int main (int argc, char **argv) if ( ((motion_threads_running == 0 ) && finish ) || ((motion_threads_running == 0 ) && (threads_running == 0)) ){ if (debug_level >= CAMERA_DEBUG) - motion_log(LOG_INFO, 0, "DEBUG-1 threads_running %d motion_threads_running %d , finish %d", - threads_running, motion_threads_running, finish); + motion_log(LOG_INFO, 0, "%s: DEBUG-1 threads_running %d motion_threads_running %d , finish %d", + __FUNCTION__, threads_running, motion_threads_running, finish); break; } for (i = (cnt_list[1] != NULL ? 1 : 0); cnt_list[i]; i++) { /* Check if threads wants to be restarted */ if ( (!cnt_list[i]->running) && (cnt_list[i]->restart) ) { - motion_log(LOG_INFO, 0, "Motion thread %d restart", cnt_list[i]->threadnr); + motion_log(LOG_INFO, 0, "%s: Motion thread %d restart", + __FUNCTION__, cnt_list[i]->threadnr); start_motion_thread(cnt_list[i], &thread_attr); } if (cnt_list[i]->watchdog > WATCHDOG_OFF) { cnt_list[i]->watchdog--; if (cnt_list[i]->watchdog == 0) { - motion_log(LOG_ERR, 0, "Thread %d - Watchdog timeout, trying to do a graceful restart", - cnt_list[i]->threadnr); + motion_log(LOG_ERR, 0, "%s: Thread %d - Watchdog timeout, trying to do " + "a graceful restart", __FUNCTION__, cnt_list[i]->threadnr); cnt_list[i]->finish = 1; } if (cnt_list[i]->watchdog == -60) { - motion_log(LOG_ERR, 0, "Thread %d - Watchdog timeout, did NOT restart graceful," - "killing it!", cnt_list[i]->threadnr); + motion_log(LOG_ERR, 0, "%s: Thread %d - Watchdog timeout, did NOT restart graceful," + "killing it!", __FUNCTION__, cnt_list[i]->threadnr); pthread_cancel(cnt_list[i]->thread_id); pthread_mutex_lock(&global_lock); threads_running--; @@ -2406,14 +2428,14 @@ int main (int argc, char **argv) } } if (debug_level >= CAMERA_DEBUG) - motion_log(LOG_INFO, 0, "DEBUG-2 threads_running %d motion_threads_running %d finish %d", - threads_running, motion_threads_running, finish); + motion_log(LOG_INFO, 0, "%s: DEBUG-2 threads_running %d motion_threads_running %d finish %d", + __FUNCTION__, threads_running, motion_threads_running, finish); } /* Reset end main loop flag */ finish = 0; - if (cnt_list[0]->conf.setup_mode) - motion_log(LOG_DEBUG, 0, "Threads finished"); + if (debug_level >= CAMERA_DEBUG) + motion_log(LOG_DEBUG, 0, "%s: Threads finished", __FUNCTION__); /* Rest for a while if we're supposed to restart. */ if (restart) @@ -2424,7 +2446,7 @@ int main (int argc, char **argv) // Be sure that http control exits fine cnt_list[0]->finish = 1; SLEEP(1, 0); - motion_log(LOG_INFO, 0, "Motion terminating"); + motion_log(LOG_INFO, 0, "%s: Motion terminating", __FUNCTION__); /* Perform final cleanup. */ pthread_key_delete(tls_key_threadnr); @@ -2458,7 +2480,8 @@ void * mymalloc(size_t nbytes) { void *dummy = malloc(nbytes); if (!dummy) { - motion_log(LOG_EMERG, 1, "Could not allocate %llu bytes of memory!", (unsigned long long)nbytes); + motion_log(LOG_EMERG, 1, "%s: Could not allocate %llu bytes of memory!", + __FUNCTION__, (unsigned long long)nbytes); motion_remove_pid(); exit(1); } @@ -2489,14 +2512,14 @@ void *myrealloc(void *ptr, size_t size, const char *desc) if (size == 0) { free(ptr); motion_log(LOG_WARNING, 0, - "Warning! Function %s tries to resize memoryblock at %p to 0 bytes!", - desc, ptr); + "%s: Warning! Function %s tries to resize memoryblock at %p to 0 bytes!", + __FUNCTION__, desc, ptr); } else { dummy = realloc(ptr, size); if (!dummy) { motion_log(LOG_EMERG, 0, - "Could not resize memory-block at offset %p to %llu bytes (function %s)!", - ptr, (unsigned long long)size, desc); + "%s: Could not resize memory-block at offset %p to %llu bytes (function %s)!", + __FUNCTION__, ptr, (unsigned long long)size, desc); motion_remove_pid(); exit(1); } @@ -2536,7 +2559,8 @@ int create_path(const char *path) buffer[start-path] = 0x00; if (mkdir(buffer, mode) == -1 && errno != EEXIST) { - motion_log(LOG_ERR, 1, "Problem creating directory %s", buffer); + motion_log(LOG_ERR, 1, "%s: Problem creating directory %s", + __FUNCTION__, buffer); free(buffer); return -1; } @@ -2590,7 +2614,8 @@ FILE * myfopen(const char *path, const char *mode) * 1: there was an other error while trying to open the file for the first time * 2: could still not open the file after the path was created */ - motion_log(LOG_ERR, 1, "Error opening file %s with mode %s", path, mode); + motion_log(LOG_ERR, 1, "%s: Error opening file %s with mode %s", + __FUNCTION__, path, mode); return NULL; } diff --git a/motion.h b/motion.h index c55b2d0e..289b68a5 100644 --- a/motion.h +++ b/motion.h @@ -105,6 +105,7 @@ #define CAMERA_WARNINGS 3 /* warnings only */ #define TRACK_DEBUG 4 /* track debug */ #define CAMERA_INFO 5 /* info debug */ +#define CAMERA_VIDEO 6 /* debug video not verbose */ #define CAMERA_DEBUG 7 /* debug but not verbose */ #define CAMERA_VERBOSE 8 /* verbose level */ #define CAMERA_ALL 9 /* everything */ diff --git a/netcam.c b/netcam.c index 7e920789..94259b69 100644 --- a/netcam.c +++ b/netcam.c @@ -59,7 +59,7 @@ * are also printed when NETCAM_DEBUG is set. Set the following #if to * 1 to enable it, or 0 (normal setting) to disable it. */ -#define SETUP ((cnt->conf.setup_mode) || (debug_level >= CAMERA_INFO)) +#define SETUP (debug_level >= CAMERA_INFO) tfile_context *file_new_context(void); @@ -165,7 +165,7 @@ static void netcam_url_parse(struct url_t *parse_url, const char *text_url) "([^/:]|[-.a-z0-9]*)(:([0-9]*))?($|(/[^:][/-_.a-z0-9]+))"; if (debug_level > CAMERA_DEBUG) - motion_log(-1, 0, "Entry netcam_url_parse data %s", text_url ); + motion_log(-1, 0, "%s: Entry netcam_url_parse data %s", __FUNCTION__, text_url); memset(parse_url, 0, sizeof(struct url_t)); /* @@ -180,7 +180,7 @@ static void netcam_url_parse(struct url_t *parse_url, const char *text_url) for (i = 0; i < 10; i++) { if ((s = netcam_url_match(matches[i], text_url)) != NULL) { if (debug_level > CAMERA_DEBUG) - motion_log(-1, 0, "Parse case %d data %s", i, s ); + motion_log(-1, 0, "%s: Parse case %d data %s", __FUNCTION__, i, s ); switch (i) { case 1: parse_url->service = s; @@ -457,12 +457,14 @@ static int netcam_read_next_header(netcam_context_ptr netcam) /* Header reported as not-OK, check to see if it's null */ if (strlen(header) == 0 ) { if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "Error reading image header, streaming mode (1). Null header."); + motion_log(LOG_DEBUG, 0, "%s: Error reading image header, " + "streaming mode (1). Null header.", __FUNCTION__); } else { /* Header is not null. Output it in case it's a new camera with unknown headers. */ if (debug_level > CAMERA_INFO) - motion_log(LOG_ERR, 0, "Error reading image header, streaming mode (1). " - "Unknown header '%s'", header ); + motion_log(LOG_ERR, 0, "%s: Error reading image header, " + "streaming mode (1). Unknown header '%s'", + __FUNCTION__, header ); } free(header); @@ -481,7 +483,7 @@ static int netcam_read_next_header(netcam_context_ptr netcam) retval = header_get(netcam, &header, HG_NONE); if (retval != HG_OK) { - motion_log(LOG_ERR, 0, "Error reading image header (2)"); + motion_log(LOG_ERR, 0, "%s: Error reading image header (2)", __FUNCTION__); free(header); return -1; } @@ -491,7 +493,7 @@ static int netcam_read_next_header(netcam_context_ptr netcam) if ((retval = netcam_check_content_type(header)) >= 0) { if (retval != 1) { - motion_log(LOG_ERR, 0, "Header not JPEG"); + motion_log(LOG_ERR, 0, "%s: Header not JPEG", __FUNCTION__); free(header); return -1; } @@ -506,7 +508,7 @@ static int netcam_read_next_header(netcam_context_ptr netcam) } if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Found image header record"); + motion_log(-1, 0, "%s: Found image header record", __FUNCTION__); free(header); return 0; @@ -543,12 +545,12 @@ static int netcam_read_first_header(netcam_context_ptr netcam) int closeflag = 0; /* If we have seen a Connection: close header from cam */ char *header; char *boundary; - struct context *cnt = netcam->cnt; /* for conf debug_level */ + //struct context *cnt = netcam->cnt; /* for conf debug_level */ /* Send the initial command to the camera */ if (send(netcam->sock, netcam->connect_request, strlen(netcam->connect_request), 0) < 0) { - motion_log(LOG_ERR, 1, "Error sending 'connect' request"); + motion_log(LOG_ERR, 1, "%s: Error sending 'connect' request", __FUNCTION__); return -1; } @@ -578,18 +580,18 @@ static int netcam_read_first_header(netcam_context_ptr netcam) ret = header_get(netcam, &header, HG_NONE); if (debug_level > CAMERA_INFO) /* Changed criterion and moved up from below to catch headers that cause returns */ - motion_log(LOG_DEBUG, 0, "Received first header ('%s')", header); + motion_log(LOG_DEBUG, 0, "%s: Received first header ('%s')", __FUNCTION__, header); if (ret != HG_OK) { if (debug_level > CAMERA_INFO) - motion_log(LOG_ERR, 0, "Error reading first header (%s)", header); + motion_log(LOG_ERR, 0, "%s: Error reading first header (%s)", __FUNCTION__, header); free(header); return -1; } if (firstflag) { if ((ret = http_result_code(header)) != 200) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "HTTP Result code %d", ret); + motion_log(-1, 0, "%s: HTTP Result code %d", __FUNCTION__, ret); free(header); if (netcam->connect_keepalive) { /* Cannot unset netcam->cnt->conf.netcam_keepalive as it is assigned const */ @@ -597,8 +599,8 @@ static int netcam_read_first_header(netcam_context_ptr netcam) /* This message is logged as Information as it would be useful to know */ /* if your netcam often returns bad HTTP result codes */ netcam->connect_keepalive = 0; - motion_log(LOG_INFO, 0, "Removed netcam Keep-Alive flag" - "due to apparent closed HTTP connection."); + motion_log(LOG_INFO, 0, "%s: Removed netcam Keep-Alive flag" + "due to apparent closed HTTP connection.", __FUNCTION__); } return ret; } @@ -623,16 +625,18 @@ static int netcam_read_first_header(netcam_context_ptr netcam) case 1: /* not streaming */ if (SETUP) { if (netcam->connect_keepalive) - motion_log(LOG_DEBUG, 0, "Non-streaming camera (keep-alive set)"); + motion_log(LOG_DEBUG, 0, "%s: Non-streaming camera " + "(keep-alive set)", __FUNCTION__); else - motion_log(LOG_DEBUG, 0, "Non-streaming camera (keep-alive not set)"); + motion_log(LOG_DEBUG, 0, "%s: Non-streaming camera " + "(keep-alive not set)", __FUNCTION__); } netcam->caps.streaming = NCS_UNSUPPORTED; break; case 2: /* streaming */ if (SETUP) - motion_log(LOG_DEBUG, 0, "Streaming camera"); + motion_log(LOG_DEBUG, 0, "%s: Streaming camera", __FUNCTION__); netcam->caps.streaming = NCS_MULTIPART; @@ -655,26 +659,26 @@ static int netcam_read_first_header(netcam_context_ptr netcam) netcam->boundary_length = strlen(netcam->boundary); if (SETUP) { - motion_log(LOG_DEBUG, 0, "Boundary string [%s]", - netcam->boundary); + motion_log(LOG_DEBUG, 0, "%s: Boundary string [%s]", + __FUNCTION__, netcam->boundary); } } break; case 3: /* MJPG-Block style streaming */ if (SETUP) - motion_log(LOG_DEBUG, 0, "Streaming camera probably using MJPG-blocks," - " consider using mjpg:// netcam_url."); + motion_log(LOG_DEBUG, 0, "%s: Streaming camera probably using MJPG-blocks," + " consider using mjpg:// netcam_url.", __FUNCTION__); break; default:{ /* error */ - motion_log(LOG_ERR, 0, "Unrecognized content type"); + motion_log(LOG_ERR, 0, "%s: Unrecognized content type", __FUNCTION__); free(header); return -1; } } } else if ((ret = (int) netcam_check_content_length(header)) >= 0) { if (SETUP) - motion_log(LOG_DEBUG, 0, "Content-length present"); + motion_log(LOG_DEBUG, 0, "%s: Content-length present", __FUNCTION__); netcam->caps.content_length = 1; /* set flag */ netcam->receiving->content_length = ret; @@ -689,7 +693,7 @@ static int netcam_read_first_header(netcam_context_ptr netcam) closeflag = TRUE; /* This flag is acted upon below */ if (debug_level > CAMERA_INFO) /* Changed criterion and moved up from below to catch headers that cause returns */ - motion_log(LOG_DEBUG, 0, "Found Conn:close header ('%s')", header); + motion_log(LOG_DEBUG, 0, "%s: Found Conn: close header ('%s')", __FUNCTION__, header); } free(header); } @@ -708,8 +712,9 @@ static int netcam_read_first_header(netcam_context_ptr netcam) * did not see a Keep-Alive field returned from netcam and a Close field. * Not quite sure what the correct course of action is here. In for testing. */ - motion_log(LOG_INFO, 0, "Info: Both 'Connection: Keep-Alive' and 'Connection: close' " - "header received. Motion continues unchanged."); + motion_log(LOG_INFO, 0, "%s: Info: Both 'Connection: Keep-Alive' and " + "'Connection: close' header received. Motion continues unchanged.", + __FUNCTION__); } else { /* aliveflag && !closeflag * @@ -719,7 +724,8 @@ static int netcam_read_first_header(netcam_context_ptr netcam) */ if (debug_level > CAMERA_INFO) - motion_log(LOG_INFO, 0, "Info: Received a Keep-Alive field in this set of headers."); + motion_log(LOG_INFO, 0, "%s: Info: Received a Keep-Alive field in this" + "set of headers.", __FUNCTION__); } } else { /* !aliveflag */ if (!closeflag) { @@ -728,8 +734,8 @@ static int netcam_read_first_header(netcam_context_ptr netcam) * did not see a Keep-Alive field returned from netcam nor a Close field. * Not quite sure what the correct course of action is here. In for testing. */ - motion_log(LOG_INFO, 0, "Info: No 'Connection: Keep-Alive' nor 'Connection: close' " - "header received. Motion continues unchanged."); + motion_log(LOG_INFO, 0, "%s: Info: No 'Connection: Keep-Alive' nor 'Connection: close' " + "header received. Motion continues unchanged.", __FUNCTION__); }else{ /* !aliveflag & closeflag * If not a streaming cam, and keepalive is set, and the flag shows we @@ -754,13 +760,13 @@ static int netcam_read_first_header(netcam_context_ptr netcam) */ if (!netcam->keepalive_thisconn) { netcam->connect_keepalive = FALSE; /* No further attempts at keep-alive */ - motion_log(LOG_INFO, 0, "Removed netcam Keep-Alive flag because 'Connection: close' " - "header received. Netcam does not support Keep-Alive. Motion " - "continues in non-Keep-Alive."); + motion_log(LOG_INFO, 0, "%s: Removed netcam Keep-Alive flag because " + "'Connection: close' header received. Netcam does not support " + "Keep-Alive. Motion continues in non-Keep-Alive.", __FUNCTION__); } else { netcam->keepalive_timeup = TRUE; /* We will close and re-open keep-alive */ motion_log(LOG_INFO, 0, "Keep-Alive has reached end of valid period. Motion will close " - "netcam, then resume Keep-Alive with a new socket."); + "netcam, then resume Keep-Alive with a new socket.", __FUNCTION__); } } } @@ -784,7 +790,7 @@ static void netcam_disconnect(netcam_context_ptr netcam) { if (netcam->sock > 0) { if (close(netcam->sock) < 0) - motion_log(LOG_ERR, 1, "netcam_disconnect"); + motion_log(LOG_ERR, 1, "%s: netcam_disconnect", __FUNCTION__); netcam->sock = -1; } @@ -822,66 +828,70 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) /* Assure any previous connection has been closed - IF we are not in keepalive */ if (!netcam->connect_keepalive) { - if (debug_level > CAMERA_INFO ) - motion_log(LOG_DEBUG, 0, "netcam_connect, disconnecting netcam since keep-alive not set." ); + if (debug_level > CAMERA_INFO) + motion_log(LOG_DEBUG, 0, "%s: disconnecting netcam " + "since keep-alive not set.", __FUNCTION__); netcam_disconnect(netcam); /* create a new socket */ if ((netcam->sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { - motion_log(LOG_ERR, 1, "netcam_connect with no keepalive, attempt to create socket failed."); + motion_log(LOG_ERR, 1, "%s: with no keepalive, attempt " + "to create socket failed.", __FUNCTION__); return -1; } - if (debug_level > CAMERA_INFO ) - motion_log(LOG_DEBUG, 0, "netcam_connect with no keepalive, new socket created fd %d", netcam->sock); + if (debug_level > CAMERA_INFO) + motion_log(LOG_DEBUG, 0, "%s: with no keepalive, " + "new socket created fd %d", __FUNCTION__, netcam->sock); } else if (netcam->sock == -1) { /* We are in keepalive mode, check for invalid socket */ /* Must be first time, or closed, create a new socket */ if ((netcam->sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { - motion_log(LOG_ERR, 1, "netcam_connect with keepalive set, invalid socket." - "This could be the first time. Creating a new one failed."); + motion_log(LOG_ERR, 1, "%s: with keepalive set, invalid socket." + "This could be the first time. Creating a new one failed.", + __FUNCTION__); return -1; } - if (debug_level > CAMERA_INFO ) - motion_log(LOG_DEBUG, 0, "netcam_connect with keepalive set, invalid socket." - "This could be first time, created a new one with fd %d", netcam->sock); + if (debug_level > CAMERA_INFO) + motion_log(LOG_DEBUG, 0, "%s: with keepalive set, invalid socket." + "This could be first time, created a new one with fd %d", + __FUNCTION__, netcam->sock); /* Record that this connection has not yet received a Keep-Alive header */ netcam->keepalive_thisconn = FALSE; /* Check the socket status for the keepalive option */ if (getsockopt(netcam->sock, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) { - motion_log(LOG_ERR, 1, "netcam_connect : getsockopt()"); + motion_log(LOG_ERR, 1, "%s: getsockopt()", __FUNCTION__); return -1; } - if (debug_level > CAMERA_INFO) { - if (optval == 1) - motion_log(LOG_DEBUG, 0, "netcam_connect: SO_KEEPALIVE is ON"); - else - motion_log(LOG_DEBUG, 0, "netcam_connect: SO_KEEPALIVE is OFF"); - } + if (debug_level > CAMERA_INFO) + motion_log(LOG_DEBUG, 0, "%s: SO_KEEPALIVE is %s", __FUNCTION__, optval ? "ON":"OFF"); /* Set the option active */ optval = 1; optlen = sizeof(optval); - if(setsockopt(netcam->sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { - motion_log(LOG_ERR, 1, "netcam_connect : setsockopt()"); + if (setsockopt(netcam->sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { + motion_log(LOG_ERR, 1, "%s: setsockopt()", __FUNCTION__); return -1; } if (debug_level > CAMERA_INFO ) - motion_log(LOG_DEBUG, 0, "netcam_connect: SO_KEEPALIVE set on socket."); + motion_log(LOG_DEBUG, 0, "%s: SO_KEEPALIVE set on socket.", __FUNCTION__); } else if (debug_level > CAMERA_INFO ) { - motion_log(LOG_DEBUG, 0, "netcam_connect re-using socket %d since keepalive is set.", netcam->sock); + motion_log(LOG_DEBUG, 0, "%s: re-using socket %d since keepalive is set.", + __FUNCTION__, netcam->sock); } /* lookup the hostname given in the netcam URL */ if ((ret = getaddrinfo(netcam->connect_host, NULL, NULL, &res)) != 0) { if (!err_flag) - motion_log(LOG_ERR, 0, "getaddrinfo() failed (%s): %s", - netcam->connect_host, gai_strerror(ret)); + motion_log(LOG_ERR, 0, "%s: getaddrinfo() failed (%s): %s", + __FUNCTION__, netcam->connect_host, gai_strerror(ret)); + if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "netcam_connect disconnecting netcam (1)"); + motion_log(LOG_DEBUG, 0, "%s: disconnecting netcam (1)", __FUNCTION__); + netcam_disconnect(netcam); return -1; } @@ -903,14 +913,14 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) */ if ((saveflags = fcntl(netcam->sock, F_GETFL, 0)) < 0) { - motion_log(LOG_ERR, 1, "fcntl(1) on socket"); + motion_log(LOG_ERR, 1, "%s: fcntl(1) on socket", __FUNCTION__); netcam_disconnect(netcam); return -1; } /* Set the socket non-blocking */ if (fcntl(netcam->sock, F_SETFL, saveflags | O_NONBLOCK) < 0) { - motion_log(LOG_ERR, 1, "fcntl(2) on socket"); + motion_log(LOG_ERR, 1, "%s: fcntl(2) on socket", __FUNCTION__); netcam_disconnect(netcam); return -1; } @@ -923,9 +933,13 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) /* If the connect failed with anything except EINPROGRESS, error */ if ((ret < 0) && (back_err != EINPROGRESS)) { if (!err_flag) - motion_log(LOG_ERR, 1, "connect() failed (%d)", back_err); + motion_log(LOG_ERR, 1, "%s: connect() failed (%d)", + __FUNCTION__, back_err); + if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "netcam_connect disconnecting netcam (4)"); + motion_log(LOG_DEBUG, 0, "%s: disconnecting netcam (4)", + __FUNCTION__); + netcam_disconnect(netcam); return -1; } @@ -939,9 +953,11 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) if (ret == 0) { /* 0 means timeout */ if (!err_flag) - motion_log(LOG_ERR, 0, "timeout on connect()"); + motion_log(LOG_ERR, 0, "%s: timeout on connect()", __FUNCTION__); + if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "netcam_connect disconnecting netcam (2)"); + motion_log(LOG_DEBUG, 0, "%s: disconnecting netcam (2)", __FUNCTION__); + netcam_disconnect(netcam); return -1; } @@ -954,7 +970,7 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) len = sizeof(ret); if (getsockopt(netcam->sock, SOL_SOCKET, SO_ERROR, &ret, &len) < 0) { - motion_log(LOG_ERR, 0, "getsockopt after connect"); + motion_log(LOG_ERR, 0, "%s: getsockopt after connect", __FUNCTION__); netcam_disconnect(netcam); return -1; } @@ -962,9 +978,11 @@ static int netcam_connect(netcam_context_ptr netcam, int err_flag) /* If the return code is anything except 0, error on connect */ if (ret) { if (!err_flag) - motion_log(LOG_ERR, 1, "connect returned error"); + motion_log(LOG_ERR, 1, "%s: connect returned error", __FUNCTION__); + if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "netcam_connect disconnecting netcam (3)"); + motion_log(LOG_DEBUG, 0, "%s: disconnecting netcam (3)", __FUNCTION__); + netcam_disconnect(netcam); return -1; } @@ -1007,8 +1025,8 @@ static void netcam_check_buffsize(netcam_buff_ptr buff, size_t numbytes) new_size = buff->size + real_alloc; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "expanding buffer from [%d/%d] to [%d/%d] bytes.", - (int) buff->used, (int) buff->size, + motion_log(-1, 0, "%s: expanding buffer from [%d/%d] to [%d/%d] bytes.", + __FUNCTION__, (int) buff->used, (int) buff->size, (int) buff->used, new_size); buff->ptr = myrealloc(buff->ptr, new_size, @@ -1215,9 +1233,9 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) if (debug_level > CAMERA_INFO) { motion_log(-1, 0, - "Potential split boundary - " + "%s: Potential split boundary - " "%d chars flushed, %d " - "re-positioned", ix, + "re-positioned", __FUNCTION__, ix, (int) netcam->response->buffer_left); } @@ -1231,7 +1249,8 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) netcam->response->buffer_left); if (retval <= 0) { /* this is a fatal error */ - motion_log(LOG_ERR, 1, "recv() fail after boundary string"); + motion_log(LOG_ERR, 1, "%s: recv() fail after boundary string", + __FUNCTION__); return -1; } @@ -1267,7 +1286,7 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) * the new 'receiving' */ if (gettimeofday(&curtime, NULL) < 0) { - motion_log(LOG_ERR, 1, "gettimeofday in netcam_read_jpeg"); + motion_log(LOG_ERR, 1, "%s: gettimeofday in netcam_read_jpeg", __FUNCTION__); } netcam->receiving->image_time = curtime; @@ -1283,7 +1302,8 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) (curtime.tv_usec- netcam->last_image.tv_usec)) / 10.0; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Calculated frame time %f", netcam->av_frame_time); + motion_log(-1, 0, "%s: Calculated frame time %f", + __FUNCTION__, netcam->av_frame_time); } netcam->last_image = curtime; @@ -1305,10 +1325,13 @@ static int netcam_read_html_jpeg(netcam_context_ptr netcam) if (netcam->caps.streaming == NCS_UNSUPPORTED) { if (!netcam->connect_keepalive) { if (debug_level > CAMERA_INFO ) - motion_log(LOG_DEBUG, 0, "netcam_read_html_jpeg disconnecting netcam since keep-alive not set." ); + motion_log(LOG_DEBUG, 0, "%s: netcam_read_html_jpeg disconnecting " + "netcam since keep-alive not set.", __FUNCTION__); + netcam_disconnect(netcam); } else if (debug_level > CAMERA_INFO ) { - motion_log(LOG_DEBUG, 0, "netcam_read_html_jpeg leaving netcam connected." ); + motion_log(LOG_DEBUG, 0, "%s: netcam_read_html_jpeg leaving netcam connected.", + __FUNCTION__); } } @@ -1350,9 +1373,12 @@ static int netcam_http_request(netcam_context_ptr netcam) * (Not always true now Keep-Alive is implemented) */ if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_http_request: about to try to connect, time #%d", ix ); + motion_log(-1, 0, "%s: about to try to connect, time #%d", + __FUNCTION__, ix ); + if (netcam_connect(netcam, 0) != 0) { - motion_log(LOG_ERR, 0, "Failed to open camera - check your config and that netcamera is online"); + motion_log(LOG_ERR, 0, "Failed to open camera - check your config " + "and that netcamera is online", __FUNCTION__); /* Fatal error on startup */ ix = MAX_HEADER_RETRIES; @@ -1362,11 +1388,12 @@ static int netcam_http_request(netcam_context_ptr netcam) if (netcam_read_first_header(netcam) >= 0) break; - motion_log(LOG_ERR, 0, "Error reading first header - re-trying"); + motion_log(LOG_ERR, 0, "%s: Error reading first header - re-trying", __FUNCTION__); } if (ix == MAX_HEADER_RETRIES) { - motion_log(LOG_ERR, 0, "Failed to read first camera header - giving up for now"); + motion_log(LOG_ERR, 0, "%s: Failed to read first camera header " + "- giving up for now", __FUNCTION__); return -1; } @@ -1395,10 +1422,10 @@ static int netcam_mjpg_buffer_refill(netcam_context_ptr netcam) while (1) { retval = rbuf_read_bufferful(netcam); if (retval <= 0) { /* If we got 0, we timeoutted. */ - motion_log(-1, 0, "netcam_mjpg_buffer_refill: Read error, trying to reconnect.."); + motion_log(-1, 0, "%s: Read error, trying to reconnect..", __FUNCTION__); /* We may have lost the connexion */ if (netcam_http_request(netcam) < 0) { - motion_log(-1, 0, "netcam_mjpg_buffer_refill: lost the cam."); + motion_log(-1, 0, "%s: lost the cam.", __FUNCTION__); return -1; /* We REALLY lost the cam... bail out for now. */ } } @@ -1409,7 +1436,8 @@ static int netcam_mjpg_buffer_refill(netcam_context_ptr netcam) netcam->response->buffer_left = retval; netcam->response->buffer_pos = netcam->response->buffer; - motion_log(LOG_DEBUG, 0, "Refilled buffer with [%d] bytes from the network.", retval); + motion_log(LOG_DEBUG, 0, "%s: Refilled buffer with [%d] bytes from the network.", + __FUNCTION__, retval); return retval; } @@ -1477,7 +1505,8 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) read_bytes += retval; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Read [%d/%d] header bytes.", read_bytes, sizeof(mh)); + motion_log(-1, 0, "%s: Read [%d/%d] header bytes.", + __FUNCTION__, read_bytes, sizeof(mh)); /* If we don't have received a full header, refill our buffer. */ @@ -1490,7 +1519,8 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) /* Now check the validity of our header. */ if (strncmp(mh.mh_magic, MJPG_MH_MAGIC, MJPG_MH_MAGIC_SIZE)) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Invalid header received, reconnecting"); + motion_log(-1, 0, "%s: Invalid header received, reconnecting", + __FUNCTION__); /* @@ -1513,28 +1543,30 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) mh.mh_chunksize - read_bytes); read_bytes += retval; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Read [%d/%d] chunk bytes, [%d/%d] total", - read_bytes, mh.mh_chunksize, buffer->used + read_bytes, - mh.mh_framesize); + motion_log(-1, 0, "%s: Read [%d/%d] chunk bytes, [%d/%d] total", + __FUNCTION__, read_bytes, mh.mh_chunksize, + buffer->used + read_bytes, mh.mh_framesize); if (retval < (int)(mh.mh_chunksize - read_bytes)){ /* motion_log(-1, 0, "Chunk incomplete, going to refill."); */ - if (netcam_mjpg_buffer_refill(netcam) < 0){ + if (netcam_mjpg_buffer_refill(netcam) < 0) return -1; - } + } } buffer->used += read_bytes; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Chunk complete, buffer used [%d] bytes.", buffer->used); + motion_log(-1, 0, "%s: Chunk complete, buffer used [%d] bytes.", + __FUNCTION__, buffer->used); /* Is our JPG image complete ? */ if (mh.mh_framesize == buffer->used) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Image complete, buffer used [%d] bytes.", buffer->used); + motion_log(-1, 0, "%s: Image complete, buffer used [%d] bytes.", + __FUNCTION__, buffer->used); break; } @@ -1546,9 +1578,9 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) * as 'latest', and make the buffer previously in 'latest' become * the new 'receiving' */ - if (gettimeofday(&curtime, NULL) < 0) { - motion_log(LOG_ERR, 1, "gettimeofday in netcam_read_jpeg"); - } + if (gettimeofday(&curtime, NULL) < 0) + motion_log(LOG_ERR, 1, "%s: gettimeofday in netcam_read_jpeg", __FUNCTION__); + netcam->receiving->image_time = curtime; @@ -1563,7 +1595,8 @@ static int netcam_read_mjpg_jpeg(netcam_context_ptr netcam) (curtime.tv_usec- netcam->last_image.tv_usec)) / 10.0; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Calculated frame time %f", netcam->av_frame_time); + motion_log(-1, 0, "%s: Calculated frame time %f", + __FUNCTION__, netcam->av_frame_time); } netcam->last_image = curtime; @@ -1606,7 +1639,8 @@ static int netcam_read_ftp_jpeg(netcam_context_ptr netcam) /* Request the image from the remote server */ if (ftp_get_socket(netcam->ftp) <= 0) { - motion_log(LOG_ERR, 0, "ftp_get_socket failed in netcam_read_jpeg"); + motion_log(LOG_ERR, 0, "%s: ftp_get_socket failed in netcam_read_jpeg", + __FUNCTION__); return -1; } @@ -1622,9 +1656,9 @@ static int netcam_read_ftp_jpeg(netcam_context_ptr netcam) buffer->used += len; } while (len > 0); - if (gettimeofday(&curtime, NULL) < 0) { - motion_log(LOG_ERR, 1, "gettimeofday in netcam_read_jpeg"); - } + if (gettimeofday(&curtime, NULL) < 0) + motion_log(LOG_ERR, 1, "%s: gettimeofday in netcam_read_jpeg", __FUNCTION__); + netcam->receiving->image_time = curtime; /* @@ -1640,7 +1674,8 @@ static int netcam_read_ftp_jpeg(netcam_context_ptr netcam) / 10.0; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Calculated frame time %f", netcam->av_frame_time); + motion_log(-1, 0, "%s: Calculated frame time %f", + __FUNCTION__, netcam->av_frame_time); } netcam->last_image = curtime; @@ -1682,7 +1717,7 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) { int loop_counter = 0; if (debug_level > CAMERA_VERBOSE) { - motion_log(-1, 0, "Begin %s", __FUNCTION__); + motion_log(-1, 0, "%s: Begin", __FUNCTION__); } netcam_buff_ptr buffer; int len; @@ -1698,22 +1733,23 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) do { if( stat( netcam->file->path, &statbuf) ) { - motion_log(-1, 0, "stat(%s) error", netcam->file->path ); + motion_log(-1, 0, "%s: stat(%s) error", __FUNCTION__, netcam->file->path ); return -1; } - if (debug_level > CAMERA_VERBOSE) { - motion_log(-1, 0, "statbuf.st_mtime[%d] != last_st_mtime[%d]", statbuf.st_mtime, netcam->file->last_st_mtime); - } + if (debug_level > CAMERA_VERBOSE) + motion_log(-1, 0, "%s: statbuf.st_mtime[%d] != last_st_mtime[%d]", + __FUNCTION__, statbuf.st_mtime, netcam->file->last_st_mtime); + if( loop_counter>((POLLING_TIMEOUT*1000*1000)/(POLLING_TIME/1000)) ) { //its waits POLLING_TIMEOUT - motion_log(-1, 0, "waiting new file image timeout" ); + motion_log(-1, 0, "%s: waiting new file image timeout", __FUNCTION__); return -1; } - if (debug_level > CAMERA_VERBOSE) { - motion_log(-1, 0, "delay waiting new file image "); - } + if (debug_level > CAMERA_VERBOSE) + motion_log(-1, 0, "%s: delay waiting new file image ", __FUNCTION__); + //SLEEP(netcam->timeout.tv_sec, netcam->timeout.tv_usec*1000 ); //its waits 5seconds - READ_TIMEOUT SLEEP( 0, POLLING_TIME ); // its waits 500ms @@ -1723,25 +1759,27 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) } while(statbuf.st_mtime == netcam->file->last_st_mtime); netcam->file->last_st_mtime = statbuf.st_mtime; - if (debug_level > CAMERA_INFO) { - motion_log(LOG_INFO, 0, "processing new file image - st_mtime " - "%d", netcam->file->last_st_mtime ); - } - + + if (debug_level > CAMERA_INFO) + motion_log(LOG_INFO, 0, "%s: processing new file image - st_mtime " + "%d", __FUNCTION__, netcam->file->last_st_mtime ); + /* Assure there's enough room in the buffer */ - while( buffer->size < (size_t)statbuf.st_size ) { + while( buffer->size < (size_t)statbuf.st_size ) netcam_check_buffsize(buffer, statbuf.st_size ); - } + /* Do the read */ netcam->file->control_file_desc = open( netcam->file->path, O_RDONLY ); if( netcam->file->control_file_desc < 0 ) { - motion_log(-1, 0, "open(%s) error:%d", netcam->file->path, netcam->file->control_file_desc ); + motion_log(-1, 0, "%s: open(%s) error:%d", __FUNCTION__, + netcam->file->path, netcam->file->control_file_desc ); return -1; } if ((len = read(netcam->file->control_file_desc, buffer->ptr + buffer->used, statbuf.st_size)) < 0) { - motion_log(-1, 0, "read(%s) error:%d", netcam->file->control_file_desc, len ); + motion_log(-1, 0, "%s: read(%s) error:%d", __FUNCTION__, + netcam->file->control_file_desc, len ); return -1; } @@ -1749,9 +1787,9 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) close( netcam->file->control_file_desc ); - if (gettimeofday(&curtime, NULL) < 0) { - motion_log(LOG_ERR, 1, "gettimeofday in netcam_read_jpeg"); - } + if (gettimeofday(&curtime, NULL) < 0) + motion_log(LOG_ERR, 1, "%s: gettimeofday in netcam_read_jpeg", __FUNCTION__); + netcam->receiving->image_time = curtime; /* @@ -1767,7 +1805,8 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) / 10.0; if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "Calculated frame time %f", netcam->av_frame_time); + motion_log(-1, 0, "%s: Calculated frame time %f", __FUNCTION__, + netcam->av_frame_time); } netcam->last_image = curtime; @@ -1793,9 +1832,9 @@ static int netcam_read_file_jpeg(netcam_context_ptr netcam) pthread_mutex_unlock(&netcam->mutex); - if (debug_level > CAMERA_VERBOSE) { - motion_log(-1, 0, "End %s", __FUNCTION__); - } + if (debug_level > CAMERA_VERBOSE) + motion_log(-1, 0, "%s: End", __FUNCTION__); + return 0; } @@ -1840,7 +1879,8 @@ static int netcam_setup_file(netcam_context_ptr netcam, struct url_t *url) url->path = NULL; if (debug_level > CAMERA_INFO) - motion_log(LOG_INFO, 0, "netcam_setup_file: netcam->file->path %s", netcam->file->path); + motion_log(LOG_INFO, 0, "%s: netcam->file->path %s", + __FUNCTION__, netcam->file->path); netcam_url_free(url); @@ -1874,7 +1914,8 @@ static void *netcam_handler_loop(void *arg) pthread_setspecific(tls_key_threadnr, (void *)((unsigned long)cnt->threadnr)); if (SETUP) - motion_log(LOG_INFO, 0, "Camera handler thread [%d] started", netcam->threadnr); + motion_log(LOG_INFO, 0, "%s: Camera handler thread [%d] started", + __FUNCTION__, netcam->threadnr); /* * The logic of our loop is very simple. If this is a non- @@ -1894,8 +1935,9 @@ static void *netcam_handler_loop(void *arg) if (!netcam->connect_keepalive || (netcam->connect_keepalive && netcam->keepalive_timeup)) { /* If keepalive flag set but time up, time to close this socket */ if (netcam->connect_keepalive && netcam->keepalive_timeup) { - motion_log(LOG_INFO, 0, "Closing netcam socket as Keep-Alive time is up " - "(camera sent Close field). A reconnect should happen."); + motion_log(LOG_INFO, 0, "%s: Closing netcam socket as Keep-Alive" + "time is up (camera sent Close field). A reconnect should happen.", + __FUNCTION__); netcam_disconnect(netcam); netcam->keepalive_timeup = FALSE; } @@ -1903,7 +1945,7 @@ static void *netcam_handler_loop(void *arg) if (netcam_connect(netcam, open_error) < 0) { if (!open_error) { /* log first error */ motion_log(LOG_ERR, 0, - "re-opening camera (non-streaming)"); + "%s: re-opening camera (non-streaming)", __FUNCTION__); open_error = 1; } /* need to have a dynamic delay here */ @@ -1912,16 +1954,18 @@ static void *netcam_handler_loop(void *arg) } if (open_error) { /* log re-connection */ motion_log(LOG_ERR, 0, - "camera re-connected"); + "%s: camera re-connected", __FUNCTION__); open_error = 0; } } /* Send our request and look at the response */ if ((retval = netcam_read_first_header(netcam)) != 1) { if (retval > 0) { - motion_log(LOG_ERR, 0, "Unrecognized image header (%d)", retval); + motion_log(LOG_ERR, 0, "%s: Unrecognized image header (%d)", + __FUNCTION__, retval); } else if (retval != -1) { - motion_log(LOG_ERR, 0, "Error in header (%d)", retval); + motion_log(LOG_ERR, 0, "%s: Error in header (%d)", + __FUNCTION__, retval); } /* need to have a dynamic delay here */ continue; @@ -1931,7 +1975,7 @@ static void *netcam_handler_loop(void *arg) if (netcam_connect(netcam, open_error) < 0) { if (!open_error) { /* log first error */ motion_log(LOG_ERR, 0, - "re-opening camera (streaming)"); + "%s: re-opening camera (streaming)", __FUNCTION__); open_error = 1; } SLEEP(5, 0); @@ -1941,11 +1985,11 @@ static void *netcam_handler_loop(void *arg) if ((retval = netcam_read_first_header(netcam) != 2)) { if (retval > 0) { motion_log(LOG_ERR, 0, - "Unrecognized image header (%d)", + "%s: Unrecognized image header (%d)", __FUNCTION__, retval); } else if (retval != -1) { motion_log(LOG_ERR, 0, - "Error in header (%d)", retval); + "%s: Error in header (%d)", __FUNCTION__, retval); } /* FIXME need some limit */ continue; @@ -1953,7 +1997,7 @@ static void *netcam_handler_loop(void *arg) } if (open_error) { /* log re-connection */ motion_log(LOG_ERR, 0, - "camera re-connected"); + "%s: camera re-connected", __FUNCTION__); open_error = 0; } } else if (netcam->caps.streaming == NCS_BLOCK) { /* MJPG-Block streaming */ @@ -1964,13 +2008,13 @@ static void *netcam_handler_loop(void *arg) } } if (netcam->get_image(netcam) < 0) { - motion_log(LOG_ERR, 0, "Error getting jpeg image"); + motion_log(LOG_ERR, 0, "%s: Error getting jpeg image", __FUNCTION__); /* if FTP connection, attempt to re-connect to server */ if (netcam->ftp) { close(netcam->ftp->control_file_desc); - if (ftp_connect(netcam) < 0) { - motion_log(LOG_ERR, 0, "Trying to re-connect"); - } + if (ftp_connect(netcam) < 0) + motion_log(LOG_ERR, 0, "%s: Trying to re-connect", __FUNCTION__); + } continue; } @@ -2017,7 +2061,7 @@ static void *netcam_handler_loop(void *arg) pthread_mutex_unlock(&global_lock); /* log out a termination message */ - motion_log(LOG_INFO, 0, "netcam camera handler: finish set, exiting"); + motion_log(LOG_INFO, 0, "%s: netcam camera handler: finish set, exiting", __FUNCTION__); /* setting netcam->thread_id to zero shows netcam_cleanup we're done */ netcam->thread_id = 0; @@ -2060,9 +2104,9 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) memset(netcam->response, 0, sizeof(struct rbuf)); if (debug_level > CAMERA_INFO) - motion_log(LOG_INFO, 0, "netcam_http_build_url: Netcam has flags: HTTP/1.0: %s HTTP/1.1: %s Keep-Alive %s.", - netcam->connect_http_10 ? "1":"0", netcam->connect_http_11 ? "1":"0", - netcam->connect_keepalive ? "ON":"OFF"); + motion_log(LOG_INFO, 0, "%s: Netcam has flags: HTTP/1.0: %s HTTP/1.1: %s Keep-Alive %s.", + __FUNCTION__, netcam->connect_http_10 ? "1":"0", netcam->connect_http_11 ? "1":"0", + netcam->connect_keepalive ? "ON":"OFF"); /* * The network camera may require a username and password. If @@ -2134,8 +2178,8 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) sprintf((char *)ptr, "http://%s%s", url->host, url->path); netcam->connect_keepalive = 0; /* Disable Keepalive if proxy */ if (debug_level > CAMERA_INFO) - motion_log(LOG_DEBUG, 0, "Removed netcam_keepalive flag due to proxy set." - "Proxy is incompatible with Keep-Alive."); + motion_log(LOG_DEBUG, 0, "%s: Removed netcam_keepalive flag due to proxy set." + "Proxy is incompatible with Keep-Alive.", __FUNCTION__); } else { /* if no proxy, set as netcam_url path */ ptr = url->path; @@ -2197,10 +2241,9 @@ static int netcam_http_build_url(netcam_context_ptr netcam, struct url_t *url) free((void *)ptr); netcam_url_free(url); /* Cleanup the url data */ - if (debug_level > CAMERA_INFO) { - motion_log(-1, 0, "Camera connect string is ''%s''", netcam->connect_request); - motion_log(-1, 0, "End of camera connect string."); - } + if (debug_level > CAMERA_INFO) + motion_log(-1, 0, "%s: Camera connect string is ''%s'' End of camera connect string.", + __FUNCTION__, netcam->connect_request); return 0; } @@ -2240,14 +2283,15 @@ static int netcam_setup_html(netcam_context_ptr netcam, struct url_t *url) */ if (netcam->caps.streaming == NCS_MULTIPART) { if (netcam_read_next_header(netcam) < 0) { - motion_log(LOG_ERR, 0, - "Failed to read first stream header - giving up for now"); + motion_log(LOG_ERR, 0, "%s: Failed to read first stream header - " + "giving up for now", __FUNCTION__); return -1; } } if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_setup_html: connected, going on to read image."); + motion_log(-1, 0, "%s: connected, going on to read image.", __FUNCTION__); + netcam->get_image = netcam_read_html_jpeg; return 0; } @@ -2290,7 +2334,8 @@ static int netcam_setup_mjpg(netcam_context_ptr netcam, struct url_t *url) * header, so don't move anymore, initialization complete. */ if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_setup_mjpg: connected, going on to read and decode MJPG chunks."); + motion_log(-1, 0, "%s: connected, going on to read and decode MJPG chunks.", __FUNCTION__); + netcam->get_image = netcam_read_mjpg_jpeg; return 0; @@ -2342,7 +2387,7 @@ static int netcam_setup_ftp(netcam_context_ptr netcam, struct url_t *url) } if (ftp_send_type(netcam->ftp, 'I') < 0) { - motion_log(LOG_ERR, 0, "Error sending TYPE I to ftp server"); + motion_log(LOG_ERR, 0, "%s: Error sending TYPE I to ftp server", __FUNCTION__); return -1; } @@ -2480,8 +2525,9 @@ void netcam_cleanup(netcam_context_ptr netcam, int init_retry_flag) * Although this shouldn't happen, if it *does* happen we will * log it (just for the programmer's information) */ - motion_log(-1, 0, "No response from camera " - "handler - it must have already died"); + motion_log(-1, 0, "%s: No response from camera " + "handler - it must have already died", + __FUNCTION__); pthread_mutex_lock(&global_lock); threads_running--; pthread_mutex_unlock(&global_lock); @@ -2568,9 +2614,10 @@ int netcam_next(struct context *cnt, unsigned char *image) netcam = cnt->netcam; if (!netcam->latest->used) { - if (debug_level) { - motion_log(LOG_INFO, 0, "netcam_next called with no data in buffer"); - } + if (debug_level) + motion_log(LOG_INFO, 0, "%s: netcam_next called with no data in buffer", + __FUNCTION__); + return NETCAM_NOTHING_NEW_ERROR; } @@ -2592,9 +2639,9 @@ int netcam_next(struct context *cnt, unsigned char *image) * approach is to just return a NULL (failed) to the caller (an * error message has already been produced by the libjpeg routines) */ - if (setjmp(netcam->setjmp_buffer)) { + if (setjmp(netcam->setjmp_buffer)) return NETCAM_GENERAL_ERROR | NETCAM_JPEG_CONV_ERROR; - } + /* If there was no error, process the latest image buffer */ return netcam_proc_jpeg(netcam, image); } @@ -2623,11 +2670,11 @@ int netcam_start(struct context *cnt) struct url_t url; /* for parsing netcam URL */ if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "entered netcam_start()"); + motion_log(-1, 0, "%s: entered netcam_start()", __FUNCTION__); memset(&url, 0, sizeof(url)); if (SETUP) - motion_log(LOG_INFO, 0, "Camera thread starting..."); + motion_log(LOG_INFO, 0, "%s: Camera thread starting...", __FUNCTION__); /* * Create a new netcam_context for this camera @@ -2674,14 +2721,15 @@ int netcam_start(struct context *cnt) netcam_url_parse(&url, cnt->conf.netcam_proxy); if (!url.host) { - motion_log(LOG_ERR, 0, "Invalid netcam_proxy (%s)", - cnt->conf.netcam_proxy); + motion_log(LOG_ERR, 0, "%s: Invalid netcam_proxy (%s)", + __FUNCTION__, cnt->conf.netcam_proxy); netcam_url_free(&url); return -1; } if (url.userpass) { - motion_log(LOG_ERR, 0, "Username/password not allowed on a proxy URL"); + motion_log(LOG_ERR, 0, "%s: Username/password not allowed on a proxy URL", + __FUNCTION__); netcam_url_free(&url); return -1; } @@ -2703,7 +2751,8 @@ int netcam_start(struct context *cnt) netcam_url_parse(&url, cnt->conf.netcam_url); if (!url.host) { - motion_log(LOG_ERR, 0, "Invalid netcam_url (%s)", cnt->conf.netcam_url); + motion_log(LOG_ERR, 0, "%s: Invalid netcam_url (%s)", + __FUNCTION__, cnt->conf.netcam_url); netcam_url_free(&url); return -1; } @@ -2736,34 +2785,38 @@ int netcam_start(struct context *cnt) netcam->connect_keepalive = TRUE; /* HTTP 1.1 has keepalive by default */ } if (debug_level > CAMERA_INFO) - motion_log(LOG_INFO, 0, "netcam_start: Netcam_http parameter '%s' converts to flags: HTTP/1.0:" - "%s HTTP/1.1: %s Keep-Alive %s.", cnt->conf.netcam_keepalive, - netcam->connect_http_10 ? "1":"0", netcam->connect_http_11 ? "1":"0", - netcam->connect_keepalive ? "ON":"OFF"); + motion_log(LOG_INFO, 0, "%s: Netcam_http parameter '%s' converts to flags: HTTP/1.0:" + "%s HTTP/1.1: %s Keep-Alive %s.", __FUNCTION__, cnt->conf.netcam_keepalive, + netcam->connect_http_10 ? "1":"0", netcam->connect_http_11 ? "1":"0", + netcam->connect_keepalive ? "ON":"OFF"); /* Initialise the netcam socket to -1 to trigger a connection by the keep-alive logic */ netcam->sock = -1; if ((url.service) && (!strcmp(url.service, "http")) ) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_start: now calling netcam_setup_html()"); + motion_log(-1, 0, "%s: now calling netcam_setup_html()", __FUNCTION__); + retval = netcam_setup_html(netcam, &url); } else if ((url.service) && (!strcmp(url.service, "ftp")) ) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_start: now calling netcam_setup_ftp"); + motion_log(-1, 0, "%s:: now calling netcam_setup_ftp", __FUNCTION__); + retval = netcam_setup_ftp(netcam, &url); } else if ((url.service) && (!strcmp(url.service, "file")) ) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_start: now calling netcam_setup_file()"); + motion_log(-1, 0, "%s: now calling netcam_setup_file()", __FUNCTION__); + retval = netcam_setup_file(netcam, &url); } else if ((url.service) && (!strcmp(url.service, "mjpg")) ) { if (debug_level > CAMERA_INFO) - motion_log(-1, 0, "netcam_start: now calling netcam_setup_mjpg()"); + motion_log(-1, 0, "%s: now calling netcam_setup_mjpg()", __FUNCTION__); + strcpy(url.service, "http"); /* Put back a real URL service. */ retval = netcam_setup_mjpg(netcam, &url); } else { - motion_log(LOG_ERR, 0, "Invalid netcam service '%s' - " - "must be http, ftp, mjpg or file.", url.service); + motion_log(LOG_ERR, 0, "%s: Invalid netcam service '%s' - " + "must be http, ftp, mjpg or file.", __FUNCTION__, url.service); netcam_url_free(&url); return -1; } @@ -2779,7 +2832,8 @@ int netcam_start(struct context *cnt) * these to set the required image buffer(s) in our netcam_struct. */ if ((retval = netcam->get_image(netcam)) != 0) { - motion_log(LOG_ERR, 0, "Failed trying to read first image - retval:%d", retval ); + motion_log(LOG_ERR, 0, "%s: Failed trying to read first image - retval:%d", + __FUNCTION__, retval); return -1; } @@ -2789,8 +2843,8 @@ int netcam_start(struct context *cnt) * occurs during startup, we will just abandon this attempt. */ if (setjmp(netcam->setjmp_buffer)) { - motion_log(LOG_ERR, 0, "libjpeg decompression failure " - "on first frame - giving up!"); + motion_log(LOG_ERR, 0, "%s: libjpeg decompression failure " + "on first frame - giving up!", __FUNCTION__); return -1; } @@ -2802,14 +2856,14 @@ int netcam_start(struct context *cnt) * multiple of 16. So we check for this. */ if (netcam->width % 16) { - motion_log(LOG_ERR, 0, "netcam image width (%d) is not modulo 16", - netcam->width); + motion_log(LOG_ERR, 0, "%s: netcam image width (%d) is not modulo 16", + __FUNCTION__, netcam->width); return -1; } if (netcam->height % 16) { - motion_log(LOG_ERR, 0, "netcam image height (%d) is not modulo 16", - netcam->height); + motion_log(LOG_ERR, 0, "%s: netcam image height (%d) is not modulo 16", + __FUNCTION__, netcam->height); return -1; } @@ -2832,7 +2886,8 @@ int netcam_start(struct context *cnt) if ((retval = pthread_create(&netcam->thread_id, &handler_attribute, &netcam_handler_loop, netcam)) < 0) { - motion_log(LOG_ERR, 1, "Starting camera handler thread [%d]", netcam->threadnr); + motion_log(LOG_ERR, 1, "%s: Starting camera handler thread [%d]", + __FUNCTION__, netcam->threadnr); return -1; } diff --git a/netcam_ftp.c b/netcam_ftp.c index 5af9d1c1..0c701838 100644 --- a/netcam_ftp.c +++ b/netcam_ftp.c @@ -172,7 +172,7 @@ static int ftp_get_more(ftp_context_pointer ctxt) { if ((len = recv(ctxt->control_file_desc, &ctxt->control_buffer[ctxt->control_buffer_index], size, 0)) < 0) { - motion_log(LOG_ERR, 1, "recv failed in ftp_get_more"); + motion_log(LOG_ERR, 1, "%s: recv failed in ftp_get_more", __FUNCTION__); close(ctxt->control_file_desc); ctxt->control_file_desc = -1; return(-1); @@ -265,7 +265,7 @@ static int ftp_send_user(ftp_context_pointer ctxt) { len = strlen(buf); res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_send_user"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_send_user", __FUNCTION__); return(res); } return(0); @@ -288,7 +288,7 @@ static int ftp_send_passwd(ftp_context_pointer ctxt) { len = strlen(buf); res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_send_passwd"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_send_passwd", __FUNCTION__); return(res); } return(0); @@ -318,7 +318,7 @@ static int ftp_quit(ftp_context_pointer ctxt) { len = strlen(buf); res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_quit"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_quit", __FUNCTION__); return(res); } return(0); @@ -362,13 +362,13 @@ int ftp_connect(netcam_context_ptr netcam) { hp = gethostbyname (netcam->connect_host); if (hp == NULL) { - motion_log(LOG_ERR, 1, "gethostbyname failed in ftp_connect"); + motion_log(LOG_ERR, 1, "%s: gethostbyname failed in ftp_connect", __FUNCTION__); return (-1); } if ((unsigned int) hp->h_length > sizeof(((struct sockaddr_in *)&ctxt->ftp_address)->sin_addr)) { - motion_log(LOG_ERR, 1, "gethostbyname address mismatch " - "in ftp_connect"); + motion_log(LOG_ERR, 1, "%s: gethostbyname address mismatch " + "in ftp_connect", __FUNCTION__); return (-1); } @@ -382,7 +382,7 @@ int ftp_connect(netcam_context_ptr netcam) { addrlen = sizeof (struct sockaddr_in); if (ctxt->control_file_desc < 0) { - motion_log(LOG_ERR, 1, "socket failed"); + motion_log(LOG_ERR, 1, "%s: socket failed", __FUNCTION__); return(-1); } @@ -391,7 +391,7 @@ int ftp_connect(netcam_context_ptr netcam) { */ if (connect(ctxt->control_file_desc, (struct sockaddr *) &ctxt->ftp_address, addrlen) < 0) { - motion_log(LOG_ERR, 1, "Failed to create a connection"); + motion_log(LOG_ERR, 1, "%s: Failed to create a connection", __FUNCTION__); close(ctxt->control_file_desc); ctxt->control_file_desc = -1; return(-1); @@ -442,7 +442,7 @@ int ftp_connect(netcam_context_ptr netcam) { case 2: break; case 3: - motion_log(LOG_ERR, 0, "FTP server asking for ACCT on anonymous"); + motion_log(LOG_ERR, 0, "%s: FTP server asking for ACCT on anonymous", __FUNCTION__); case 1: case 4: case 5: @@ -487,13 +487,13 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { memset (&data_address, 0, sizeof(data_address)); ctxt->data_file_desc = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ctxt->data_file_desc < 0) { - motion_log(LOG_ERR, 1, "socket failed"); + motion_log(LOG_ERR, 1, "%s: socket failed", __FUNCTION__); return (-1); } on = 1; if (setsockopt(ctxt->data_file_desc, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) { - motion_log(LOG_ERR, 1, "setting socket option SO_REUSEADDR"); + motion_log(LOG_ERR, 1, "%s: setting socket option SO_REUSEADDR", __FUNCTION__); return -1; } @@ -506,7 +506,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { len = strlen (buf); res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_get_connection"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_get_connection", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return(res); @@ -533,7 +533,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { cur++; if (sscanf (cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2], &temp[3], &temp[4], &temp[5]) != 6) { - motion_log(LOG_ERR, 0, "Invalid answer to PASV"); + motion_log(LOG_ERR, 0, "%s: Invalid answer to PASV", __FUNCTION__); if (ctxt->data_file_desc != -1) { close (ctxt->data_file_desc); ctxt->data_file_desc = -1; @@ -548,7 +548,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { /* Now try to connect to the data port */ if (connect(ctxt->data_file_desc, (struct sockaddr *) &data_address, data_address_length) < 0) { - motion_log(LOG_ERR, 1, "Failed to create a data connection"); + motion_log(LOG_ERR, 1, "%s: Failed to create a data connection", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return (-1); @@ -567,7 +567,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { /* bind to the socket - should give us a unique port */ if (bind(ctxt->data_file_desc, (struct sockaddr *) &data_address, data_address_length) < 0) { - motion_log(LOG_ERR, 1, "bind failed"); + motion_log(LOG_ERR, 1, "%s: bind failed", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return (-1); @@ -579,7 +579,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { /* set up a 'listen' on the port to get the server's connection */ if (listen(ctxt->data_file_desc, 1) < 0) { - motion_log(LOG_ERR, 1, "listen failed"); + motion_log(LOG_ERR, 1, "%s: listen failed", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return (-1); @@ -599,7 +599,7 @@ static int ftp_get_connection(ftp_context_pointer ctxt) { res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_get_connection"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_get_connection", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return(res); @@ -707,7 +707,7 @@ int ftp_get_socket(ftp_context_pointer ctxt) { res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_get_socket"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_get_socket", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return(res); @@ -732,7 +732,7 @@ int ftp_get_socket(ftp_context_pointer ctxt) { if ((acfd = accept(ctxt->data_file_desc, (struct sockaddr *)&data_address, &data_address_length)) < 0) { - motion_log(LOG_ERR, 1, "accept in ftp_get_socket"); + motion_log(LOG_ERR, 1, "%s: accept in ftp_get_socket", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return -1; @@ -768,7 +768,7 @@ int ftp_send_type(ftp_context_pointer ctxt, char type) { res = send(ctxt->control_file_desc, buf, len, 0); if (res < 0) { - motion_log(LOG_ERR, 1, "send failed in ftp_get_socket"); + motion_log(LOG_ERR, 1, "%s: send failed in ftp_get_socket", __FUNCTION__); close(ctxt->data_file_desc); ctxt->data_file_desc = -1; return(res); @@ -817,7 +817,7 @@ int ftp_read(ftp_context_pointer ctxt, void *dest, int len) { if (len <= 0) { if (len < 0) - motion_log(LOG_ERR, 1, "recv failed in ftp_read"); + motion_log(LOG_ERR, 1, "%s: recv failed in ftp_read", __FUNCTION__); ftp_close_connection(ctxt); } diff --git a/picture.c b/picture.c index 569cdec8..efc182c1 100644 --- a/picture.c +++ b/picture.c @@ -519,14 +519,15 @@ void put_picture(struct context *cnt, char *file, unsigned char *image, int ftyp /* Report to syslog - suggest solution if the problem is access rights to target dir */ if (errno == EACCES) { motion_log(LOG_ERR, 1, - "Can't write picture to file %s - check access rights to target directory", file); - motion_log(LOG_ERR, 1, "Thread is going to finish due to this fatal error"); + "%s: Can't write picture to file %s - check access rights to target directory" + "Thread is going to finish due to this fatal error", + __FUNCTION__, file); cnt->finish = 1; cnt->restart = 0; return; } else { /* If target dir is temporarily unavailable we may survive */ - motion_log(LOG_ERR, 1, "Can't write picture to file %s", file); + motion_log(LOG_ERR, 1, "%s: Can't write picture to file %s", __FUNCTION__, file); return; } } @@ -546,12 +547,13 @@ unsigned char *get_pgm(FILE *picture, int width, int height) line[255] = 0; if (!fgets(line, 255, picture)) { - motion_log(LOG_ERR, 1, "Could not read from ppm file"); + motion_log(LOG_ERR, 1, "%s: Could not read from ppm file", __FUNCTION__); return NULL; } if (strncmp(line, "P5", 2)) { - motion_log(LOG_ERR, 1, "This is not a ppm file, starts with '%s'", line); + motion_log(LOG_ERR, 1, "%s: This is not a ppm file, starts with '%s'", + __FUNCTION__, line); return NULL; } @@ -563,12 +565,13 @@ unsigned char *get_pgm(FILE *picture, int width, int height) /* check size */ if (sscanf(line, "%d %d", &x, &y) != 2) { - motion_log(LOG_ERR, 1, "Failed reading size in pgm file"); + motion_log(LOG_ERR, 1, "%s: Failed reading size in pgm file", __FUNCTION__); return NULL; } if (x != width || y != height) { - motion_log(LOG_ERR, 1, "Wrong image size %dx%d should be %dx%d", x, y, width, height); + motion_log(LOG_ERR, 1, "%s: Wrong image size %dx%d should be %dx%d", + __FUNCTION__, x, y, width, height); return NULL; } @@ -579,7 +582,7 @@ unsigned char *get_pgm(FILE *picture, int width, int height) return NULL; if (sscanf(line, "%d", &maxval) != 1) { - motion_log(LOG_ERR, 1, "Failed reading maximum value in pgm file"); + motion_log(LOG_ERR, 1, "%s: Failed reading maximum value in pgm file", __FUNCTION__); return NULL; } @@ -589,7 +592,7 @@ unsigned char *get_pgm(FILE *picture, int width, int height) for (y = 0; y < height; y++) { if ((int)fread(&image[y * width], 1, width, picture) != width) - motion_log(LOG_ERR, 1, "Failed reading image data from pgm file"); + motion_log(LOG_ERR, 1, "%s: Failed reading image data from pgm file", __FUNCTION__); for (x = 0; x < width; x++) { image[y * width + x] = (int)image[y * width + x] * 255 / maxval; @@ -612,10 +615,11 @@ void put_fixed_mask(struct context *cnt, const char *file) /* Report to syslog - suggest solution if the problem is access rights to target dir */ if (errno == EACCES) { motion_log(LOG_ERR, 1, - "can't write mask file %s - check access rights to target directory", file); + "%s: can't write mask file %s - check access rights to target directory", + __FUNCTION__, file); } else { /* If target dir is temporarily unavailable we may survive */ - motion_log(LOG_ERR, 1, "can't write mask file %s", file); + motion_log(LOG_ERR, 1, "%s: can't write mask file %s", __FUNCTION__, file); } return; } @@ -628,14 +632,14 @@ void put_fixed_mask(struct context *cnt, const char *file) /* write pgm image data at once */ if ((int)fwrite(cnt->imgs.out, cnt->conf.width, cnt->conf.height, picture) != cnt->conf.height) { - motion_log(LOG_ERR, 1, "Failed writing default mask as pgm file"); + motion_log(LOG_ERR, 1, "%s: Failed writing default mask as pgm file", __FUNCTION__); return; } fclose(picture); - motion_log(LOG_ERR, 0, "Creating empty mask %s",cnt->conf.mask_file); - motion_log(LOG_ERR, 0, "Please edit this file and re-run motion to enable mask feature"); + motion_log(LOG_ERR, 0, "%s: Creating empty mask %s\nPlease edit this file and " + "re-run motion to enable mask feature", __FUNCTION__, cnt->conf.mask_file); } /* save preview_shot */ diff --git a/rotate.c b/rotate.c index c7889c59..fb7755ba 100644 --- a/rotate.c +++ b/rotate.c @@ -237,8 +237,8 @@ void rotate_init(struct context *cnt) * we have a value that is safe from changes caused by motion-control. */ if((cnt->conf.rotate_deg % 90) > 0) { - motion_log(LOG_ERR, 0, "Config option \"rotate\" not a multiple of 90: %d", - cnt->conf.rotate_deg); + motion_log(LOG_ERR, 0, "%s: Config option \"rotate\" not a multiple of 90: %d", + __FUNCTION__, cnt->conf.rotate_deg); cnt->conf.rotate_deg = 0; /* disable rotation */ cnt->rotate_data.degrees = 0; /* force return below */ } else { @@ -268,37 +268,35 @@ void rotate_init(struct context *cnt) /* If we're not rotating, let's exit once we have setup the capture dimensions * and output dimensions properly. */ - if(cnt->rotate_data.degrees == 0) { + if(cnt->rotate_data.degrees == 0) return; - } - switch(cnt->imgs.type) - { - case VIDEO_PALETTE_YUV420P: + switch(cnt->imgs.type) { + + case VIDEO_PALETTE_YUV420P: /* For YUV 4:2:0 planar, the memory block used for 90/270 degrees * rotation needs to be width x height x 1.5 bytes large. */ - size = cnt->imgs.width * cnt->imgs.height * 3 / 2; + size = cnt->imgs.width * cnt->imgs.height * 3 / 2; break; - case VIDEO_PALETTE_GREY: + case VIDEO_PALETTE_GREY: /* For greyscale, the memory block used for 90/270 degrees rotation * needs to be width x height bytes large. */ - size = cnt->imgs.width * cnt->imgs.height; + size = cnt->imgs.width * cnt->imgs.height; break; - default: - cnt->rotate_data.degrees = 0; - motion_log(LOG_ERR, 0, "Unsupported palette (%d), rotation is disabled", - cnt->imgs.type); + default: + cnt->rotate_data.degrees = 0; + motion_log(LOG_ERR, 0, "%s: Unsupported palette (%d), rotation is disabled", + __FUNCTION__, cnt->imgs.type); return; } /* Allocate memory if rotating 90 or 270 degrees, because those rotations * cannot be performed in-place (they can, but it would be too slow). */ - if((cnt->rotate_data.degrees == 90) || (cnt->rotate_data.degrees == 270)) { + if((cnt->rotate_data.degrees == 90) || (cnt->rotate_data.degrees == 270)) cnt->rotate_data.temp_buf = mymalloc(size); - } } /** @@ -314,9 +312,8 @@ void rotate_init(struct context *cnt) */ void rotate_deinit(struct context *cnt) { - if(cnt->rotate_data.temp_buf) { + if(cnt->rotate_data.temp_buf) free(cnt->rotate_data.temp_buf); - } } /** diff --git a/stream.c b/stream.c index 016436f4..16ab6696 100644 --- a/stream.c +++ b/stream.c @@ -51,7 +51,8 @@ int http_bindsock(int port, int local) optval = getaddrinfo(local ? "localhost" : NULL, portnumber, &hints, &res); if (optval != 0) { - motion_log(LOG_ERR, 1, "getaddrinfo() for motion-stream socket failed: %s", gai_strerror(optval)); + motion_log(LOG_ERR, 1, "%s: getaddrinfo() for motion-stream socket failed: %s", + __FUNCTION__, gai_strerror(optval)); freeaddrinfo(res); return -1; } @@ -70,33 +71,33 @@ int http_bindsock(int port, int local) /* Reuse Address */ setsockopt(sl, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof( int ) ); - motion_log(LOG_INFO, 0, "motion-stream testing : %s addr: %s port: %s", - res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); + motion_log(LOG_INFO, 0, "%s: motion-stream testing : %s addr: %s port: %s", + __FUNCTION__, res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); if (bind(sl, res->ai_addr, res->ai_addrlen) == 0){ - motion_log(LOG_INFO, 0, "motion-stream Bound : %s addr: %s port: %s", - res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); + motion_log(LOG_INFO, 0, "%s: motion-stream Bound : %s addr: %s port: %s", + __FUNCTION__, res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); break; } - motion_log(LOG_ERR, 1, "motion-stream bind() failed, retrying "); + motion_log(LOG_ERR, 1, "%s: motion-stream bind() failed, retrying", __FUNCTION__); close(sl); sl = -1; } - motion_log(LOG_ERR, 1, "motion-stream socket failed, retrying"); + motion_log(LOG_ERR, 1, "%s: motion-stream socket failed, retrying", __FUNCTION__); res = res->ai_next; } freeaddrinfo(ressave); if (sl < 0) { - motion_log(LOG_ERR, 1, "motion-stream creating socket/bind ERROR"); + motion_log(LOG_ERR, 1, "%s: motion-stream creating socket/bind ERROR", __FUNCTION__); return -1; } if (listen(sl, DEF_MAXWEBQUEUE) == -1) { - motion_log(LOG_ERR, 1, "motion-stream listen() ERROR"); + motion_log(LOG_ERR, 1, "%s: motion-stream listen() ERROR", __FUNCTION__); close(sl); sl = -1; } @@ -118,7 +119,7 @@ static int http_acceptsock(int sl) return sc; } - motion_log(LOG_ERR, 1, "motion-stream accept()"); + motion_log(LOG_ERR, 1, "%s: motion-stream accept()", __FUNCTION__); return -1; } @@ -254,7 +255,7 @@ static void stream_add_client(struct stream *list, int sc) new->socket = sc; if ((new->tmpbuffer = stream_tmpbuffer(sizeof(header))) == NULL) { - motion_log(LOG_ERR, 1, "Error creating tmpbuffer in stream_add_client"); + motion_log(LOG_ERR, 1, "%s: Error creating tmpbuffer in stream_add_client", __FUNCTION__); } else { memcpy(new->tmpbuffer->ptr, header, sizeof(header)-1); new->tmpbuffer->size = sizeof(header)-1; @@ -333,10 +334,12 @@ void stream_stop(struct context *cnt) struct stream *list; struct stream *next = cnt->stream.next; - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Closing motion-stream listen socket & active motion-stream sockets"); + if (debug_level >= CAMERA_VERBOSE) + motion_log(-1, 0, "%s: Closing motion-stream listen socket" + " & active motion-stream sockets", __FUNCTION__); else - motion_log(LOG_INFO, 0, "Closing motion-stream listen socket & active motion-stream sockets"); + motion_log(LOG_INFO, 0, "%s: Closing motion-stream listen socket" + " & active motion-stream sockets", __FUNCTION__); close(cnt->stream.socket); cnt->stream.socket = -1; @@ -354,10 +357,12 @@ void stream_stop(struct context *cnt) free(list); } - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Closed motion-stream listen socket & active motion-stream sockets"); + if (debug_level >= CAMERA_VERBOSE) + motion_log(-1, 0, "%s: Closed motion-stream listen socket" + " & active motion-stream sockets", __FUNCTION__); else - motion_log(LOG_INFO, 0, "Closed motion-stream listen socket & active motion-stream sockets"); + motion_log(LOG_INFO, 0, "%s: Closed motion-stream listen socket" + " & active motion-stream sockets", __FUNCTION__); } /* stream_put is the starting point of the stream loop. It is called from @@ -463,7 +468,7 @@ void stream_put(struct context *cnt, unsigned char *image) */ stream_add_write(&cnt->stream, tmpbuffer, cnt->conf.stream_maxrate); } else { - motion_log(LOG_ERR, 1, "Error creating tmpbuffer"); + motion_log(LOG_ERR, 1, "%s: Error creating tmpbuffer", __FUNCTION__); } } diff --git a/track.c b/track.c index 52656565..13720340 100644 --- a/track.c +++ b/track.c @@ -41,6 +41,7 @@ struct trackoptions track_template = { /* Add your own center and move functions here: */ +static unsigned short int servo_position(struct context *cnt, short unsigned int motor); static unsigned short int servo_center(struct context *cnt, int xoff, int yoff ATTRIBUTE_UNUSED); static unsigned short int stepper_center(struct context *cnt, int xoff, int yoff ATTRIBUTE_UNUSED); @@ -232,12 +233,15 @@ static unsigned short int stepper_move(struct context *cnt, struct coord *cent, unsigned short int command = 0, data = 0; if (cnt->track.dev < 0) { - motion_log(LOG_INFO, 0, "No device %s started yet , trying stepper_center()", cnt->track.port); - if (!stepper_center(cnt, 0, 0)){ + motion_log(LOG_INFO, 0, "%s: No device %s started yet , trying stepper_center()", + __FUNCTION__, cnt->track.port); + + if (!stepper_center(cnt, 0, 0)) { motion_log(LOG_ERR, 1, "%s: failed to initialize stepper device on %s , fd [%i].", __FUNCTION__, cnt->track.port, cnt->track.dev); return 0; } + motion_log(LOG_INFO, 0, "%s: succeed , device started %s , fd [%i]", __FUNCTION__, cnt->track.port, cnt->track.dev); } @@ -354,6 +358,18 @@ static unsigned short int servo_command(struct context *cnt, unsigned short int return buffer[0]; } + +static unsigned short int servo_position(struct context *cnt, short unsigned int motor) +{ + unsigned short int ret = 0; + + ret = servo_command(cnt, motor, SERVO_COMMAND_POSITION, 0); + + return ret; +} + + + /* * servo_move() * @@ -382,9 +398,10 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st if (manual) { int offset; + int position; if (cent->x) { - + position = servo_position(cnt, cnt->track.motorx); offset = cent->x * cnt->track.stepsize; @@ -394,14 +411,14 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st else command = SERVO_COMMAND_RIGHT_N; - // TODO: need to get position to avoid overflow limits - /*if ((offset > cnt->track.maxx) || (offset < cnt->track.minx)){ - motion_log(LOG_INFO, 0, "%s: x %d value out of range!", - __FUNCTION__, offset); - return 0; - }*/ + data = abs(offset); + + if ((data + position > cnt->track.maxx) || (position - offset < cnt->track.minx)) { + motion_log(LOG_INFO, 0, "%s: x %d value out of range! (%d - %d)", + __FUNCTION__, data, cnt->track.minx, cnt->track.maxx); + return 0; + } - data = abs(offset); /* Set Speed , TODO : it should be done only when speed changes */ servo_command(cnt, cnt->track.motorx, SERVO_COMMAND_SPEED, cnt->track.speed); @@ -410,7 +427,7 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st if (cent->y) { - + position = servo_position(cnt, cnt->track.motory); offset = cent->y * cnt->track.stepsize; if ( (cnt->track.motory_reverse && (offset > 0)) || @@ -419,14 +436,14 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st else command = SERVO_COMMAND_DOWN_N; - // TODO: need to get position to avoid overflow limits - /*if ((offset > cnt->track.maxy) || (offset < cnt->track.miny)) { - motion_log(LOG_INFO, 0, "%s: y %d value out of range!", - __FUNCTION__, offset); - return 0; - }*/ - data = abs(offset); + + if ((data + position > cnt->track.maxy) || (position - offset < cnt->track.miny)) { + motion_log(LOG_INFO, 0, "%s: y %d value out of range! (%d - %d)", + __FUNCTION__, data, cnt->track.miny, cnt->track.maxy); + return 0; + } + /* Set Speed , TODO : it should be done only when speed changes */ servo_command(cnt, cnt->track.motory, SERVO_COMMAND_SPEED, cnt->track.speed); @@ -434,6 +451,7 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st } } else { + int position; /***** x-axis *****/ @@ -462,13 +480,15 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st data = data * cnt->track.stepsize / imgs->width; if (data && command) { + // TODO: need to get position to avoid overflow limits - /* - if (data > cnt->track.maxx) - data = cnt->track.maxx; - else if (data < cnt->track.minx) - data = cnt->track.minx; - */ + position = servo_position(cnt, cnt->track.motorx); + + if ((position + data > cnt->track.maxx) || (position - data < cnt->track.minx)) { + motion_log(LOG_INFO, 0, "%s: x %d value out of range! (%d - %d)", + __FUNCTION__, data, cnt->track.minx, cnt->track.maxx); + return 0; + } /* Set Speed , TODO : it should be done only when speed changes */ @@ -508,15 +528,16 @@ static unsigned short int servo_move(struct context *cnt, struct coord *cent, st data = data * cnt->track.stepsize / imgs->height; if (data && command) { + // TODO: need to get position to avoid overflow limits - /* - if (data > cnt->track.maxy) - data = cnt->track.maxy; - else if (data < cnt->track.miny) - data = cnt->track.miny; - */ - - + position = servo_position(cnt, cnt->track.motory); + + if ((position + data > cnt->track.maxy) || (position - data < cnt->track.miny)) { + motion_log(LOG_INFO, 0, "%s: y %d value out of range! (%d - %d)", + __FUNCTION__, data, cnt->track.miny, cnt->track.maxy); + return 0; + } + /* Set Speed , TODO : it should be done only when speed changes */ servo_command(cnt, cnt->track.motory, SERVO_COMMAND_SPEED, cnt->track.speed); servo_command(cnt, cnt->track.motory, command, data); diff --git a/video.c b/video.c index 466be0c6..9db3987f 100644 --- a/video.c +++ b/video.c @@ -29,7 +29,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) if (cnt->conf.contrast && cnt->conf.contrast != viddev->contrast) { if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); make_change = 1; vid_pic.contrast = cnt->conf.contrast * 256; @@ -40,7 +40,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) if (!make_change) { if (ioctl(dev, VIDIOCGPICT, &vid_pic)==-1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); } make_change = 1; @@ -52,7 +52,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) if (!make_change) { if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); } make_change = 1; @@ -66,7 +66,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) /* If we already read the VIDIOGPICT - we should not do it again */ if (!make_change) { if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); } vid_pic.brightness = viddev->brightness * 256; @@ -77,7 +77,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) if (cnt->conf.brightness && cnt->conf.brightness != viddev->brightness) { if (!make_change) { if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); } make_change = 1; @@ -88,7 +88,7 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) if (make_change) { if (ioctl(dev, VIDIOCSPICT, &vid_pic) == -1) - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSPICT)", __FUNCTION__); } } @@ -99,8 +99,8 @@ static void v4l_picture_controls(struct context *cnt, struct video_dev *viddev) */ -unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int width, int height, - int input, int norm, unsigned long freq, int tuner_number) +unsigned char *v4l_start(struct video_dev *viddev, int width, int height,int input, + int norm, unsigned long freq, int tuner_number) { int dev = viddev->fd; struct video_capability vid_caps; @@ -111,7 +111,7 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt void *map; if (ioctl (dev, VIDIOCGCAP, &vid_caps) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGCAP)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGCAP)", __FUNCTION__); return (NULL); } @@ -123,12 +123,12 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt vid_chnl.channel = input; if (ioctl (dev, VIDIOCGCHAN, &vid_chnl) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGCHAN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGCHAN)", __FUNCTION__); } else { vid_chnl.channel = input; vid_chnl.norm = norm; if (ioctl (dev, VIDIOCSCHAN, &vid_chnl) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSCHAN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSCHAN)", __FUNCTION__); return (NULL); } } @@ -138,7 +138,7 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt memset(&vid_tuner, 0, sizeof(struct video_tuner)); vid_tuner.tuner = tuner_number; if (ioctl (dev, VIDIOCGTUNER, &vid_tuner) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGTUNER)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGTUNER)", __FUNCTION__); } else { if (vid_tuner.flags & VIDEO_TUNER_LOW) { freq = freq*16; /* steps of 1/16 KHz */ @@ -146,18 +146,17 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt freq = (freq*10)/625; } if (ioctl(dev, VIDIOCSFREQ, &freq) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSFREQ)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSFREQ)", __FUNCTION__); return (NULL); } - if (cnt->conf.setup_mode) - motion_log(-1, 0, "Frequency set"); + if (debug_level >= CAMERA_VERBOSE) + motion_log(-1, 0, "%s: Frequency set", __FUNCTION__); } } if (ioctl (dev, VIDIOCGMBUF, &vid_buf) == -1) { - motion_log(LOG_ERR, 0, "ioctl(VIDIOCGMBUF) - Error device does not support memory map"); - motion_log(LOG_ERR, 0, "V4L capturing using read is deprecated!"); - motion_log(LOG_ERR, 0, "Motion only supports mmap."); + motion_log(LOG_ERR, 0, "%s: ioctl(VIDIOCGMBUF) - Error device does not support memory map\n", + "V4L capturing using read is deprecated!\nMotion only supports mmap.", __FUNCTION__); return NULL; } else { map = mmap(0, vid_buf.size, PROT_READ|PROT_WRITE, MAP_SHARED, dev, 0); @@ -172,7 +171,7 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt } if (MAP_FAILED == map) { - motion_log(LOG_ERR, 1, "MAP_FAILED"); + motion_log(LOG_ERR, 1, "%s: MAP_FAILED", __FUNCTION__); return (NULL); } viddev->v4l_curbuffer = 0; @@ -181,29 +180,33 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt vid_mmap.width = width; vid_mmap.height = height; if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_DEBUG, 1, "Failed with YUV420P, trying YUV422 palette"); + motion_log(LOG_DEBUG, 1, "%s: Failed with YUV420P, trying YUV422 palette", + __FUNCTION__); viddev->v4l_fmt = VIDEO_PALETTE_YUV422; vid_mmap.format = viddev->v4l_fmt; /* Try again... */ if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_DEBUG, 1, "Failed with YUV422, trying YUYV palette"); + motion_log(LOG_DEBUG, 1, "%s: Failed with YUV422, trying YUYV palette", + __FUNCTION__); viddev->v4l_fmt = VIDEO_PALETTE_YUYV; vid_mmap.format = viddev->v4l_fmt; if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_DEBUG, 1, "Failed with YUYV, trying RGB24 palette"); + motion_log(LOG_DEBUG, 1, "%s: Failed with YUYV, trying RGB24 palette", + __FUNCTION__); viddev->v4l_fmt = VIDEO_PALETTE_RGB24; vid_mmap.format = viddev->v4l_fmt; /* Try again... */ if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_DEBUG, 1, "Failed with RGB24, trying GREYSCALE palette"); + motion_log(LOG_DEBUG, 1, "%s: Failed with RGB24, trying" + "GREYSCALE palette", __FUNCTION__); viddev->v4l_fmt = VIDEO_PALETTE_GREY; vid_mmap.format = viddev->v4l_fmt; /* Try one last time... */ if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_ERR, 1, "Failed with all supported palettes " - "- giving up"); + motion_log(LOG_ERR, 1, "%s: Failed with all supported palettes " + "- giving up", __FUNCTION__); return (NULL); } } @@ -214,24 +217,24 @@ unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int widt switch (viddev->v4l_fmt) { case VIDEO_PALETTE_YUV420P: - viddev->v4l_bufsize = (width*height*3)/2; - motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUV420P palette"); + viddev->v4l_bufsize = (width * height * 3) / 2; + motion_log(LOG_DEBUG, 0, "%s: Using VIDEO_PALETTE_YUV420P palette", __FUNCTION__); break; case VIDEO_PALETTE_YUV422: - viddev->v4l_bufsize = (width*height*2); - motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUV422 palette"); + viddev->v4l_bufsize = (width * height * 2); + motion_log(LOG_DEBUG, 0, "%s: Using VIDEO_PALETTE_YUV422 palette", __FUNCTION__); break; case VIDEO_PALETTE_YUYV: - viddev->v4l_bufsize = (width*height*2); - motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_YUYV palette"); + viddev->v4l_bufsize = (width * height * 2); + motion_log(LOG_DEBUG, 0, "%s: Using VIDEO_PALETTE_YUYV palette", __FUNCTION__); break; case VIDEO_PALETTE_RGB24: - viddev->v4l_bufsize = (width*height*3); - motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_RGB24 palette"); + viddev->v4l_bufsize = (width * height * 3); + motion_log(LOG_DEBUG, 0, "%s: Using VIDEO_PALETTE_RGB24 palette", __FUNCTION__); break; case VIDEO_PALETTE_GREY: - viddev->v4l_bufsize = width*height; - motion_log(LOG_DEBUG, 0, "Using VIDEO_PALETTE_GREY palette"); + viddev->v4l_bufsize = width * height; + motion_log(LOG_DEBUG, 0, "%s: Using VIDEO_PALETTE_GREY palette", __FUNCTION__); break; } return map; @@ -286,7 +289,7 @@ int v4l_next(struct video_dev *viddev, unsigned char *map, int width, int height vid_mmap.frame = viddev->v4l_curbuffer; if (ioctl(dev, VIDIOCMCAPTURE, &vid_mmap) == -1) { - motion_log(LOG_ERR, 1, "mcapture error in proc %d", getpid()); + motion_log(LOG_ERR, 1, "%s: mcapture error in proc %d", __FUNCTION__, getpid()); sigprocmask (SIG_UNBLOCK, &old, NULL); return V4L_FATAL_ERROR; } @@ -294,7 +297,7 @@ int v4l_next(struct video_dev *viddev, unsigned char *map, int width, int height vid_mmap.frame = frame; if (ioctl(dev, VIDIOCSYNC, &vid_mmap.frame) == -1) { - motion_log(LOG_ERR, 1, "sync error in proc %d", getpid()); + motion_log(LOG_ERR, 1, "%s: sync error in proc %d", __FUNCTION__, getpid()); sigprocmask (SIG_UNBLOCK, &old, NULL); } @@ -332,7 +335,7 @@ void v4l_set_input(struct context *cnt, struct video_dev *viddev, unsigned char vid_tuner.tuner = tuner_number; if (ioctl (dev, VIDIOCGTUNER, &vid_tuner) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGTUNER)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGTUNER)", __FUNCTION__); } else { if (vid_tuner.flags & VIDEO_TUNER_LOW) { frequnits = freq*16; /* steps of 1/16 KHz */ @@ -340,7 +343,7 @@ void v4l_set_input(struct context *cnt, struct video_dev *viddev, unsigned char frequnits = (freq*10)/625; } if (ioctl(dev, VIDIOCSFREQ, &frequnits) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSFREQ)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSFREQ)", __FUNCTION__); return; } } @@ -350,12 +353,12 @@ void v4l_set_input(struct context *cnt, struct video_dev *viddev, unsigned char vid_chnl.channel = input; if (ioctl (dev, VIDIOCGCHAN, &vid_chnl) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGCHAN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGCHAN)", __FUNCTION__); } else { vid_chnl.channel = input; vid_chnl.norm = norm; if (ioctl (dev, VIDIOCSCHAN, &vid_chnl) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSCHAN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSCHAN)", __FUNCTION__); return; } } @@ -384,13 +387,13 @@ static int v4l_open_vidpipe(void) struct utsname uts; if (uname(&uts) < 0) { - motion_log(LOG_ERR, 1, "Unable to execute uname"); + motion_log(LOG_ERR, 1, "%s: Unable to execute uname", __FUNCTION__); return -1; } major = strtok(uts.release, "."); minor = strtok(NULL, "."); if ((major == NULL) || (minor == NULL) || (strcmp(major, "2"))) { - motion_log(LOG_ERR, 1, "Unable to decipher OS version"); + motion_log(LOG_ERR, 1, "%s: Unable to decipher OS version", __FUNCTION__); return -1; } if (strcmp(minor, "5") < 0) { @@ -403,13 +406,14 @@ static int v4l_open_vidpipe(void) vloopbacks = fopen("/proc/video/vloopback/vloopbacks", "r"); if (!vloopbacks) { - motion_log(LOG_ERR, 1, "Failed to open '/proc/video/vloopback/vloopbacks'"); + motion_log(LOG_ERR, 1, "%s: Failed to open '/proc/video/vloopback/vloopbacks'", + __FUNCTION__); return -1; } /* Read vloopback version*/ if (!fgets(buffer, 255, vloopbacks)) { - motion_log(LOG_ERR, 1, "Unable to read vloopback version"); + motion_log(LOG_ERR, 1, "%s: Unable to read vloopback version", __FUNCTION__); return -1; } @@ -418,7 +422,8 @@ static int v4l_open_vidpipe(void) /* Read explanation line */ if (!fgets(buffer, 255, vloopbacks)) { - motion_log(LOG_ERR, 1, "Unable to read vloopback explanation line"); + motion_log(LOG_ERR, 1, "%s: Unable to read vloopback explanation line", + __FUNCTION__); return -1; } @@ -434,8 +439,8 @@ static int v4l_open_vidpipe(void) snprintf(pipepath, 255, "/dev/%s", input); pipe_fd = open(pipepath, O_RDWR); if (pipe_fd >= 0) { - motion_log(-1, 0, "\tInput: /dev/%s", input); - motion_log(-1, 0, "\tOutput: /dev/%s", output); + motion_log(-1, 0, "%s: \tInput: /dev/%s \tOutput: /dev/%s", + __FUNCTION__, input, output); break; } } @@ -453,7 +458,7 @@ static int v4l_open_vidpipe(void) int tnum; if ((dir = opendir(prefix)) == NULL) { - motion_log(LOG_ERR, 1, "Failed to open '%s'", prefix); + motion_log(LOG_ERR, 1, "%s: Failed to open '%s'", __FUNCTION__, prefix); return -1; } while ((dirp = readdir(dir)) != NULL) { @@ -501,7 +506,7 @@ static int v4l_open_vidpipe(void) } closedir(dir); if (pipe_fd >= 0) - motion_log(-1, 0, "Opened input of %s", pipepath); + motion_log(-1, 0, "%s: Opened input of %s", __FUNCTION__, pipepath); } return pipe_fd; } @@ -521,22 +526,22 @@ static int v4l_startpipe(const char *dev_name, int width, int height, int type) return(-1); if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGPICT)", __FUNCTION__); return(-1); } vid_pic.palette = type; if (ioctl(dev, VIDIOCSPICT, &vid_pic) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSPICT)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSPICT)", __FUNCTION__); return(-1); } if (ioctl(dev, VIDIOCGWIN, &vid_win) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCGWIN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCGWIN)", __FUNCTION__); return(-1); } vid_win.height = height; vid_win.width = width; if (ioctl(dev, VIDIOCSWIN, &vid_win) == -1) { - motion_log(LOG_ERR, 1, "ioctl (VIDIOCSWIN)"); + motion_log(LOG_ERR, 1, "%s: ioctl (VIDIOCSWIN)", __FUNCTION__); return(-1); } return dev; @@ -550,7 +555,7 @@ static int v4l_putpipe (int dev, unsigned char *image, int size) int vid_startpipe(const char *dev_name, int width, int height, int type) { - return v4l_startpipe( dev_name, width, height, type); + return v4l_startpipe(dev_name, width, height, type); } int vid_putpipe (int dev, unsigned char *image, int size) diff --git a/video.h b/video.h index 37f85879..13f47a39 100644 --- a/video.h +++ b/video.h @@ -90,7 +90,7 @@ void mjpegtoyuv420p(unsigned char *map, unsigned char *cap_map, int width, int h /* video functions, video.c */ int vid_startpipe(const char *dev_name, int width, int height, int); int vid_putpipe(int dev, unsigned char *image, int); -unsigned char *v4l_start(struct context *cnt, struct video_dev *viddev, int width, int height, +unsigned char *v4l_start(struct video_dev *viddev, int width, int height, int input, int norm, unsigned long freq, int tuner_number); void v4l_set_input(struct context *cnt, struct video_dev *viddev, unsigned char *map, int width, int height, int input, int norm, int skip, unsigned long freq, int tuner_number); diff --git a/video2.c b/video2.c index c3eccc96..69bba3fd 100644 --- a/video2.c +++ b/video2.c @@ -149,14 +149,15 @@ static int v4l2_get_capability(src_v4l2_t * s) { if (xioctl(s->fd, VIDIOC_QUERYCAP, &s->cap) < 0) { - motion_log(LOG_ERR, 0, "Not a V4L2 device?"); + motion_log(LOG_ERR, 0, "%s: Not a V4L2 device?", __FUNCTION__); return (-1); } - motion_log(LOG_INFO, 0, "cap.driver: \"%s\"", s->cap.driver); - motion_log(LOG_INFO, 0, "cap.card: \"%s\"", s->cap.card); - motion_log(LOG_INFO, 0, "cap.bus_info: \"%s\"", s->cap.bus_info); - motion_log(LOG_INFO, 0, "cap.capabilities=0x%08X", s->cap.capabilities); + motion_log(LOG_INFO, 0, "%s: \n------------------------\ncap.driver: \"%s\"\n" + "cap.card: \"%s\"\n" + "cap.bus_info: \"%s\"\n" + "cap.capabilities=0x%08X\n------------------------", __FUNCTION__, + s->cap.driver, s->cap.card, s->cap.bus_info, s->cap.capabilities); if (s->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) motion_log(LOG_INFO, 0, "- VIDEO_CAPTURE"); @@ -184,7 +185,7 @@ static int v4l2_get_capability(src_v4l2_t * s) motion_log(LOG_INFO, 0, "- TIMEPERFRAME"); if (!s->cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) { - motion_log(LOG_ERR, 0, "Device does not support capturing."); + motion_log(LOG_ERR, 0, "%s: Device does not support capturing.", __FUNCTION__); return (-1); } @@ -204,31 +205,31 @@ static int v4l2_select_input(src_v4l2_t * s, int in, int norm, unsigned long fre memset (&input, 0, sizeof (input)); input.index = in; if (xioctl(s->fd, VIDIOC_ENUMINPUT, &input) == -1) { - motion_log(LOG_ERR, 0, "Unable to query input %d.", in); - motion_log(LOG_ERR, 0, "VIDIOC_ENUMINPUT: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Unable to query input %d.\nVIDIOC_ENUMINPUT: %s", + __FUNCTION__, in, strerror(errno)); return (-1); } - if (debug_level > 5) + if (debug_level >= CAMERA_VIDEO) motion_log(LOG_INFO, 0, "%s: name = \"%s\", type 0x%08X, status %08x", __FUNCTION__, input.name, input.type, input.status); - if ((input.type & V4L2_INPUT_TYPE_TUNER) && (debug_level > 5)) + if ((input.type & V4L2_INPUT_TYPE_TUNER) && (debug_level >= CAMERA_VIDEO)) motion_log(LOG_INFO, 0, "- TUNER"); - if ((input.type & V4L2_INPUT_TYPE_CAMERA) && (debug_level > 5)) + if ((input.type & V4L2_INPUT_TYPE_CAMERA) && (debug_level >= CAMERA_VIDEO)) motion_log(LOG_INFO, 0, "- CAMERA"); if (xioctl(s->fd, VIDIOC_S_INPUT, &in) == -1) { - motion_log(LOG_ERR, 0, "Error selecting input %d", in); - motion_log(LOG_ERR, 0, "VIDIOC_S_INPUT: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error selecting input %d\nVIDIOC_S_INPUT: %s", + __FUNCTION__, in, strerror(errno)); return (-1); } /* Set video standard usually webcams doesn't support the ioctl or return V4L2_STD_UNKNOWN */ if (xioctl(s->fd, VIDIOC_G_STD, &std_id) == -1) { - if (debug_level > 5) - motion_log(LOG_INFO, 0, "Device doesn't support VIDIOC_G_STD "); + if (debug_level >= CAMERA_VIDEO) + motion_log(LOG_INFO, 0, "%s: Device doesn't support VIDIOC_G_STD", __FUNCTION__); std_id = 0; // V4L2_STD_UNKNOWN = 0 } @@ -237,9 +238,9 @@ static int v4l2_select_input(src_v4l2_t * s, int in, int norm, unsigned long fre standard.index = 0; while (xioctl(s->fd, VIDIOC_ENUMSTD, &standard) == 0) { - if ((standard.id & std_id) && (debug_level > 5)) { + if ((standard.id & std_id) && (debug_level >= CAMERA_VIDEO)) motion_log(LOG_INFO, 0, "- video standard %s", standard.name); - } + standard.index++; } @@ -254,10 +255,10 @@ static int v4l2_select_input(src_v4l2_t * s, int in, int norm, unsigned long fre std_id = V4L2_STD_PAL; } - if (xioctl(s->fd, VIDIOC_S_STD, &std_id) == -1) { - motion_log(LOG_ERR, 0, "Error selecting standard method %d", std_id); - motion_log(LOG_ERR, 0, "VIDIOC_S_STD: %s", strerror(errno)); - } + if (xioctl(s->fd, VIDIOC_S_STD, &std_id) == -1) + motion_log(LOG_ERR, 0, "%s: Error selecting standard method %d\nVIDIOC_S_STD: %s", + __FUNCTION__, std_id, strerror(errno)); + } /* If this input is attached to a tuner, set the frequency. */ @@ -271,7 +272,7 @@ static int v4l2_select_input(src_v4l2_t * s, int in, int norm, unsigned long fre tuner.index = input.tuner; if (xioctl(s->fd, VIDIOC_G_TUNER, &tuner) == -1) { - motion_log(LOG_ERR, 0, "VIDIOC_G_TUNER: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: VIDIOC_G_TUNER: %s", __FUNCTION__,strerror(errno)); return (0); } @@ -282,7 +283,7 @@ static int v4l2_select_input(src_v4l2_t * s, int in, int norm, unsigned long fre freq.frequency = (freq_ / 1000) * 16; if (xioctl(s->fd, VIDIOC_S_FREQUENCY, &freq) == -1) { - motion_log(LOG_ERR, 0, "VIDIOC_S_FREQUENCY: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: VIDIOC_S_FREQUENCY: %s", __FUNCTION__,strerror(errno)); return (0); } } @@ -314,7 +315,7 @@ static int v4l2_set_pix_format(struct context *cnt, src_v4l2_t * s, int *width, fmt.index = v4l2_pal = 0; fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - motion_log(LOG_INFO, 0, "Supported palettes:"); + motion_log(LOG_INFO, 0, "%s: Supported palettes:", __FUNCTION__); while (xioctl(s->fd, VIDIOC_ENUM_FMT, &fmt) != -1) { short int i; @@ -357,51 +358,56 @@ static int v4l2_set_pix_format(struct context *cnt, src_v4l2_t * s, int *width, s->fmt.fmt.pix.field = V4L2_FIELD_ANY; if (xioctl(s->fd, VIDIOC_TRY_FMT, &s->fmt) != -1 && s->fmt.fmt.pix.pixelformat == pixformat) { - motion_log(LOG_INFO, 0, "index_format %d Test palette %c%c%c%c (%dx%d)", - index_format, pixformat >> 0, pixformat >> 8, - pixformat >> 16, pixformat >> 24, *width, *height); + motion_log(LOG_INFO, 0, "%s: index_format %d Test palette %c%c%c%c (%dx%d)", + __FUNCTION__, index_format, pixformat >> 0, pixformat >> 8, + pixformat >> 16, pixformat >> 24, *width, *height); if (s->fmt.fmt.pix.width != (unsigned int) *width || s->fmt.fmt.pix.height != (unsigned int) *height) { - motion_log(LOG_INFO, 0, "Adjusting resolution from %ix%i to %ix%i.", *width, *height, - s->fmt.fmt.pix.width, s->fmt.fmt.pix.height); + + motion_log(LOG_INFO, 0, "%s: Adjusting resolution from %ix%i to %ix%i.", + __FUNCTION__, *width, *height, s->fmt.fmt.pix.width, s->fmt.fmt.pix.height); + *width = s->fmt.fmt.pix.width; *height = s->fmt.fmt.pix.height; } if (xioctl(s->fd, VIDIOC_S_FMT, &s->fmt) == -1) { - motion_log(LOG_ERR, 0, "Error setting pixel format."); - motion_log(LOG_ERR, 0, "VIDIOC_S_FMT: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error setting pixel format.\nVIDIOC_S_FMT: %s", + __FUNCTION__, strerror(errno)); return (-1); } - motion_log(LOG_INFO, 0, "Using palette %c%c%c%c (%dx%d) bytesperlines %d sizeimage %d colorspace %08x", - pixformat >> 0, pixformat >> 8, pixformat >> 16, pixformat >> 24, *width, *height, - s->fmt.fmt.pix.bytesperline, s->fmt.fmt.pix.sizeimage, s->fmt.fmt. pix.colorspace); + motion_log(LOG_INFO, 0, "%s: Using palette %c%c%c%c (%dx%d) bytesperlines %d sizeimage " + "%d colorspace %08x", __FUNCTION__, pixformat >> 0, pixformat >> 8, pixformat >> 16, + pixformat >> 24, *width, *height, s->fmt.fmt.pix.bytesperline, s->fmt.fmt.pix.sizeimage, + s->fmt.fmt. pix.colorspace); /* TODO: Review when it has been tested */ if (pixformat == V4L2_PIX_FMT_MJPEG) { struct v4l2_jpegcompression v4l2_jpeg; if (xioctl(s->fd, VIDIOC_G_JPEGCOMP, &v4l2_jpeg) == -1) { - motion_log(LOG_ERR, 0, "VIDIOC_G_JPEGCOMP not supported but it should"); + motion_log(LOG_ERR, 0, "%s: VIDIOC_G_JPEGCOMP not supported but it should", + __FUNCTION__); } else { v4l2_jpeg.jpeg_markers |= V4L2_JPEG_MARKER_DHT; if (xioctl(s->fd, VIDIOC_S_JPEGCOMP, &v4l2_jpeg) == -1) - motion_log(LOG_ERR, 0, "VIDIOC_S_JPEGCOMP %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: VIDIOC_S_JPEGCOMP %s", + __FUNCTION__, strerror(errno)); } } return 0; } - motion_log(LOG_ERR, 0, "VIDIOC_TRY_FMT failed for format %c%c%c%c (%s).", pixformat >> 0, - pixformat >> 8, pixformat >> 16, pixformat >> 24, strerror(errno)); + motion_log(LOG_ERR, 0, "%s: VIDIOC_TRY_FMT failed for format %c%c%c%c (%s).", __FUNCTION__, + pixformat >> 0, pixformat >> 8, pixformat >> 16, pixformat >> 24, strerror(errno)); return -1; } - motion_log(LOG_ERR, 0, "Unable to find a compatible palette format."); + motion_log(LOG_ERR, 0, "%s: Unable to find a compatible palette format.", __FUNCTION__); return (-1); } @@ -437,16 +443,15 @@ static int v4l2_set_mmap(src_v4l2_t * s) s->req.memory = V4L2_MEMORY_MMAP; if (xioctl(s->fd, VIDIOC_REQBUFS, &s->req) == -1) { - motion_log(LOG_ERR, 1, "Error requesting buffers for memory map."); - motion_log(LOG_ERR, 0, "VIDIOC_REQBUFS: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error requesting buffers for memory map.\nVIDIOC_REQBUFS: %s", + __FUNCTION__, strerror(errno)); return (-1); } - motion_log(LOG_DEBUG, 0, "mmap information:"); - motion_log(LOG_DEBUG, 0, "frames=%d", s->req.count); + motion_log(LOG_DEBUG, 0, "%s: mmap information: frames=%d", __FUNCTION__, s->req.count); if (s->req.count < MIN_MMAP_BUFFERS) { - motion_log(LOG_ERR, 0, "Insufficient buffer memory."); + motion_log(LOG_ERR, 0, "%s: Insufficient buffer memory.", __FUNCTION__); return (-1); } @@ -466,8 +471,8 @@ static int v4l2_set_mmap(src_v4l2_t * s) buf.index = b; if (xioctl(s->fd, VIDIOC_QUERYBUF, &buf) == -1) { - motion_log(LOG_ERR, 0, "Error querying buffer %i", b); - motion_log(LOG_ERR, 0, "VIDIOC_QUERYBUF: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error querying buffer %i\nVIDIOC_QUERYBUF: %s", + __FUNCTION__, b, strerror(errno)); free(s->buffers); return (-1); } @@ -476,13 +481,13 @@ static int v4l2_set_mmap(src_v4l2_t * s) s->buffers[b].ptr = mmap(NULL, buf.length, PROT_READ | PROT_WRITE, MAP_SHARED, s->fd, buf.m.offset); if (s->buffers[b].ptr == MAP_FAILED) { - motion_log(LOG_ERR, 0, "Error mapping buffer %i", b); - motion_log(LOG_ERR, 0, "mmap: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error mapping buffer %i\nmmap: %s", + __FUNCTION__, b, strerror(errno)); free(s->buffers); return (-1); } - motion_log(LOG_DEBUG, 0, "%i length=%d", b, buf.length); + motion_log(LOG_DEBUG, 0, "%s: %i length=%d", __FUNCTION__, b, buf.length); } s->map = -1; @@ -495,7 +500,7 @@ static int v4l2_set_mmap(src_v4l2_t * s) s->buf.index = b; if (xioctl(s->fd, VIDIOC_QBUF, &s->buf) == -1) { - motion_log(LOG_ERR, 0, "VIDIOC_QBUF: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: VIDIOC_QBUF: %s", __FUNCTION__, strerror(errno)); return (-1); } } @@ -503,8 +508,8 @@ static int v4l2_set_mmap(src_v4l2_t * s) type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (xioctl(s->fd, VIDIOC_STREAMON, &type) == -1) { - motion_log(LOG_ERR, 0, "Error starting stream."); - motion_log(LOG_ERR, 0, "VIDIOC_STREAMON: %s", strerror(errno)); + motion_log(LOG_ERR, 0, "%s: Error starting stream.\nVIDIOC_STREAMON: %s", + __FUNCTION__, strerror(errno)); return (-1); } @@ -545,15 +550,15 @@ static int v4l2_scan_controls(src_v4l2_t * s) memcpy(ctrl, &queryctrl, sizeof(struct v4l2_queryctrl)); - motion_log(LOG_INFO, 0, "found control 0x%08x, \"%s\", range %d,%d %s", ctrl->id, - ctrl->name, ctrl->minimum, ctrl->maximum, - ctrl->flags & V4L2_CTRL_FLAG_DISABLED ? "!DISABLED!" : ""); + motion_log(LOG_INFO, 0, "%s: found control 0x%08x, \"%s\", range %d,%d %s", + __FUNCTION__,ctrl->id, ctrl->name, ctrl->minimum, ctrl->maximum, + ctrl->flags & V4L2_CTRL_FLAG_DISABLED ? "!DISABLED!" : ""); memset (&control, 0, sizeof (control)); control.id = queried_ctrls[i]; xioctl(s->fd, VIDIOC_G_CTRL, &control); - motion_log(LOG_INFO, 0, "\t\"%s\", default %d, current %d", ctrl->name, - ctrl->default_value, control.value); + motion_log(LOG_INFO, 0, "%s: \t\"%s\", default %d, current %d", __FUNCTION__, + ctrl->name, ctrl->default_value, control.value); ctrl++; } @@ -593,14 +598,13 @@ static int v4l2_set_control(src_v4l2_t * s, u32 cid, int value) break; default: - motion_log(LOG_ERR, 0, "%s: control type not supported yet"); + motion_log(LOG_ERR, 0, "%s: control type not supported yet", __FUNCTION__); return -1; } - if (debug_level > 5) - motion_log(LOG_INFO, 0, "setting control \"%s\" to %d (ret %d %s) %s", ctrl->name, - value, ret, ret ? strerror(errno) : "", - ctrl->flags & V4L2_CTRL_FLAG_DISABLED ? - "Control is DISABLED!" : ""); + if (debug_level >= CAMERA_VIDEO) + motion_log(LOG_INFO, 0, "%s: setting control \"%s\" to %d (ret %d %s) %s", + __FUNCTION__, ctrl->name, value, ret, ret ? strerror(errno) : "", + ctrl->flags & V4L2_CTRL_FLAG_DISABLED ? "Control is DISABLED!" : ""); return 0; } @@ -741,9 +745,9 @@ void v4l2_set_input(struct context *cnt, struct video_dev *viddev, unsigned char { src_v4l2_t *s = (src_v4l2_t *) viddev->v4l2_private; unsigned int counter = 0; - if (debug_level > 5) - motion_log(LOG_DEBUG, 0, "set_input_skip_frame switch_time=%ld:%ld", - switchTime.tv_sec, switchTime.tv_usec); + if (debug_level >= CAMERA_VIDEO) + motion_log(LOG_DEBUG, 0, "%s: set_input_skip_frame switch_time=%ld:%ld", + __FUNCTION__, switchTime.tv_sec, switchTime.tv_usec); /* Avoid hang using the number of mmap buffers */ while(counter < s->req.count) @@ -754,9 +758,9 @@ void v4l2_set_input(struct context *cnt, struct video_dev *viddev, unsigned char if (s->buf.timestamp.tv_sec > switchTime.tv_sec || (s->buf.timestamp.tv_sec == switchTime.tv_sec && s->buf.timestamp.tv_usec > switchTime.tv_usec)) break; - if (debug_level > 5) - motion_log(LOG_DEBUG, 0, "got frame before switch timestamp=%ld:%ld", - s->buf.timestamp.tv_sec, s->buf.timestamp.tv_usec); + if (debug_level >= CAMERA_VIDEO) + motion_log(LOG_DEBUG, 0, "%s: got frame before switch timestamp=%ld:%ld", + __FUNCTION__, s->buf.timestamp.tv_sec, s->buf.timestamp.tv_usec); } } diff --git a/video_common.c b/video_common.c index 221efb48..c3bf21ba 100644 --- a/video_common.c +++ b/video_common.c @@ -552,7 +552,7 @@ void vid_close(struct context *cnt) /* Cleanup the netcam part */ if(cnt->netcam) { - motion_log(LOG_DEBUG, 0, "vid_close: calling netcam_cleanup"); + motion_log(LOG_DEBUG, 0, "%s: calling netcam_cleanup", __FUNCTION__); netcam_cleanup(cnt->netcam, 0); cnt->netcam = NULL; return; @@ -574,12 +574,12 @@ void vid_close(struct context *cnt) cnt->video_dev = -1; if (dev == NULL) { - motion_log(LOG_ERR, 0, "vid_close: Unable to find video device"); + motion_log(LOG_ERR, 0, "%s: Unable to find video device", __FUNCTION__); return; } if( --dev->usage_count == 0) { - motion_log(LOG_INFO, 0, "Closing video device %s", dev->video_device); + motion_log(LOG_INFO, 0, "%s: Closing video device %s", __FUNCTION__, dev->video_device); #ifdef MOTION_V4L2 if (dev->v4l2) { v4l2_close(dev); @@ -605,7 +605,8 @@ void vid_close(struct context *cnt) pthread_mutex_destroy(&dev->mutex); free(dev); } else { - motion_log(LOG_INFO, 0, "Still %d users of video device %s, so we don't close it now", dev->usage_count, dev->video_device); + motion_log(LOG_INFO, 0, "%s: Still %d users of video device %s, so we don't close it now", + __FUNCTION__, dev->usage_count, dev->video_device); /* There is still at least one thread using this device * If we own it, release it */ @@ -659,12 +660,14 @@ static int vid_v4lx_start(struct context *cnt) * for this first. */ if (conf->width % 16) { - motion_log(LOG_ERR, 0, "config image width (%d) is not modulo 16", conf->width); + motion_log(LOG_ERR, 0, "%s: config image width (%d) is not modulo 16", + __FUNCTION__, conf->width); return -1; } if (conf->height % 16) { - motion_log(LOG_ERR, 0, "config image height (%d) is not modulo 16", conf->height); + motion_log(LOG_ERR, 0, "%s: config image height (%d) is not modulo 16", + __FUNCTION__, conf->height); return -1; } @@ -722,7 +725,8 @@ static int vid_v4lx_start(struct context *cnt) fd = open(dev->video_device, O_RDWR); if (fd < 0) { - motion_log(LOG_ERR, 1, "Failed to open video device %s", conf->video_device); + motion_log(LOG_ERR, 1, "%s: Failed to open video device %s", + __FUNCTION__, conf->video_device); free(dev); pthread_mutex_unlock(&vid_mutex); return -1; @@ -761,7 +765,7 @@ static int vid_v4lx_start(struct context *cnt) dev->height = height; #endif - if (!v4l_start(cnt, dev, width, height, input, norm, frequency, tuner_number)) { + if (!v4l_start(dev, width, height, input, norm, frequency, tuner_number)) { close(dev->fd); pthread_mutexattr_destroy(&dev->attr); pthread_mutex_destroy(&dev->mutex); @@ -775,9 +779,9 @@ static int vid_v4lx_start(struct context *cnt) } #endif if (dev->v4l2 == 0) { - motion_log(-1, 0, "Using V4L1"); + motion_log(-1, 0, "%s: Using V4L1", __FUNCTION__); } else { - motion_log(-1, 0, "Using V4L2"); + motion_log(-1, 0, "%s: Using V4L2", __FUNCTION__); /* Update width & height because could be changed in v4l2_start () */ width = dev->width; height = dev->height; @@ -850,7 +854,7 @@ int vid_start(struct context *cnt) } #ifdef WITHOUT_V4L else - motion_log(LOG_ERR, 0, "You must setup netcam_url"); + motion_log(LOG_ERR, 0, "%s: You must setup netcam_url", __FUNCTION__); #else else dev = vid_v4lx_start(cnt); diff --git a/webhttpd.c b/webhttpd.c index dde794c5..b65a91d4 100644 --- a/webhttpd.c +++ b/webhttpd.c @@ -189,7 +189,6 @@ static const char *request_auth_response_template= "WWW-Authenticate: Basic realm=\"Motion Security Access\"\r\n"; - static ssize_t write_nonblock(int fd, const void *buf, size_t size) { ssize_t nwrite = -1; @@ -211,7 +210,6 @@ static ssize_t write_nonblock(int fd, const void *buf, size_t size) } return nwrite; - } @@ -234,20 +232,18 @@ static ssize_t read_nonblock(int fd ,void *buf, ssize_t size) } } } + return nread; - } - - static void send_template_ini_client(int client_socket, const char *template) { ssize_t nwrite = 0; nwrite = write_nonblock(client_socket, ok_response, strlen (ok_response)); nwrite += write_nonblock(client_socket, template, strlen(template)); if (nwrite != (ssize_t)(strlen(ok_response) + strlen(template))) - motion_log(LOG_ERR, 1, "httpd send_template_ini_client"); + motion_log(LOG_ERR, 1, "%s: failure write", __FUNCTION__); } static void send_template_ini_client_raw(int client_socket) @@ -255,7 +251,7 @@ static void send_template_ini_client_raw(int client_socket) ssize_t nwrite = 0; nwrite = write_nonblock(client_socket, ok_response_raw, strlen (ok_response_raw)); if (nwrite != (ssize_t)strlen(ok_response_raw)) - motion_log(LOG_ERR, 1, "httpd send_template_ini_client_raw"); + motion_log(LOG_ERR, 1, "%s: failure write", __FUNCTION__); } static void send_template(int client_socket, char *res) @@ -263,7 +259,7 @@ static void send_template(int client_socket, char *res) ssize_t nwrite = 0; nwrite = write_nonblock(client_socket, res, strlen(res)); if ( nwrite != (ssize_t)strlen(res)) - motion_log(LOG_ERR, 1, "httpd send_template failure write"); + motion_log(LOG_ERR, 1, "%s: failure write", __FUNCTION__); } static void send_template_raw(int client_socket, char *res) @@ -322,7 +318,6 @@ static char *replace(const char *str, const char *old, const char *new) /* This function decode the values from GET request following the http RFC. */ - static void url_decode(char *urlencoded, size_t length) { char *data = urlencoded; @@ -369,7 +364,7 @@ static void url_decode(char *urlencoded, size_t length) static unsigned short int config(char *pointer, char *res, unsigned short int length_uri, unsigned short int thread, int client_socket, void *userdata) { - char question; + char question='\0'; char command[256] = {'\0'}; unsigned short int i; struct context **cnt = userdata; @@ -979,7 +974,7 @@ static unsigned short int action(char *pointer, char *res, unsigned short int le /*call restart*/ if (thread == 0) { - motion_log(LOG_DEBUG, 0, "httpd restart"); + motion_log(LOG_DEBUG, 0, "%s: httpd restart", __FUNCTION__); kill(getpid(),SIGHUP); if (cnt[0]->conf.webcontrol_html_output) { send_template_ini_client(client_socket, ini_template); @@ -993,7 +988,7 @@ static unsigned short int action(char *pointer, char *res, unsigned short int le } return 0; // to restart } else { - motion_log(LOG_DEBUG, 0, "httpd restart thread %d", thread); + motion_log(LOG_DEBUG, 0, "%s: httpd restart thread %d", __FUNCTION__, thread); if (cnt[thread]->running) { cnt[thread]->makemovie = 1; cnt[thread]->finish = 1; @@ -1024,7 +1019,7 @@ static unsigned short int action(char *pointer, char *res, unsigned short int le /*call quit*/ if (thread == 0) { - motion_log(LOG_DEBUG, 0, "httpd quit"); + motion_log(LOG_DEBUG, 0, "%s: httpd quit", __FUNCTION__); kill(getpid(),SIGQUIT); if (cnt[0]->conf.webcontrol_html_output) { send_template_ini_client(client_socket, ini_template); @@ -1038,7 +1033,7 @@ static unsigned short int action(char *pointer, char *res, unsigned short int le } return 0; // to quit } else { - motion_log(LOG_DEBUG, 0, "httpd quit thread %d", thread); + motion_log(LOG_DEBUG, 0, "%s: httpd quit thread %d", __FUNCTION__, thread); cnt[thread]->restart = 0; cnt[thread]->makemovie = 1; cnt[thread]->finish = 1; @@ -1272,7 +1267,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len if (( question != '=' ) || (command[0] == '\0')) { /* no valid syntax */ - motion_log(LOG_WARNING, 0, "httpd debug race 1"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 1", __FUNCTION__); if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); else @@ -1322,7 +1317,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len } } else { /* no valid syntax */ - motion_log(LOG_WARNING, 0, "httpd debug race 2"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 2", __FUNCTION__); if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); else @@ -1333,7 +1328,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len /* first value check for error */ if ( !warningkill ) { - motion_log(LOG_WARNING, 0, "httpd debug race 3"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 3", __FUNCTION__); /* error value */ if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, error_value, NULL); @@ -1486,7 +1481,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len warningkill = sscanf(pointer, "%c%255[a-z]" , &question, command); if ( ( question != '&' ) || (command[0] == '\0') ) { - motion_log(LOG_WARNING, 0, "httpd debug race 4"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 4", __FUNCTION__); if ( strstr(pointer, "&")) { if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, error_value, NULL); @@ -1510,7 +1505,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len pointer = pointer + 3; length_uri = length_uri - 3; if ( (pan) || (!tilt) || (X) || (Y) ) { - motion_log(LOG_WARNING, 0, "httpd debug race 5"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 5", __FUNCTION__); /* no valid syntax */ if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); @@ -1526,7 +1521,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len length_uri = length_uri - 4; if ( (tilt) || (!pan) || (X) || (Y) ) { /* no valid syntax */ - motion_log(LOG_WARNING, 0, "httpd debug race 6"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 6", __FUNCTION__); if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); else @@ -1540,7 +1535,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len pointer++; length_uri--; if ( (X) || (!Y) || (pan) || (tilt) ) { - motion_log(LOG_WARNING, 0, "httpd debug race 7"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 7", __FUNCTION__); /* no valid syntax */ if (cnt[0]->conf.webcontrol_html_output) @@ -1556,7 +1551,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len pointer++; length_uri--; if ( (Y) || (!X) || (pan) || (tilt) ) { - motion_log(LOG_WARNING, 0, "httpd debug race 8"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 8", __FUNCTION__); /* no valid syntax */ if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); @@ -1567,7 +1562,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len Y = 2; warningkill = sscanf(pointer, "%c%10[-0-9]" , &question, y_value); } else { - motion_log(LOG_WARNING, 0, "httpd debug race 9"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 9", __FUNCTION__); /* no valid syntax */ if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); @@ -1579,7 +1574,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len /* Second value check */ if ( ( warningkill < 2 ) && (question != '=') ) { - motion_log(LOG_WARNING, 0, "httpd debug race 10"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 10", __FUNCTION__); /* no valid syntax */ if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, not_valid_syntax, NULL); @@ -1587,7 +1582,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len response_client(client_socket, not_valid_syntax_raw, NULL); return 1; }else if (( question == '=') && ( warningkill == 1)) { - motion_log(LOG_WARNING, 0, "httpd debug race 11"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 11", __FUNCTION__); if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, error_value, NULL); else @@ -1616,7 +1611,7 @@ static unsigned short int track(char *pointer, char *res, unsigned short int len if (length_uri != 0) { - motion_log(LOG_WARNING, 0, "httpd debug race 12"); + motion_log(LOG_WARNING, 0, "%s: httpd debug race 12", __FUNCTION__); if (cnt[0]->conf.webcontrol_html_output) response_client(client_socket, error_value, NULL); else @@ -2191,7 +2186,7 @@ static unsigned short int read_client(int client_socket, void *userdata, char *a nread = read_nonblock(client_socket, buffer, length); if (nread <= 0) { - motion_log(LOG_ERR, 1, "motion-httpd First Read Error"); + motion_log(LOG_ERR, 1, "%s: motion-httpd First Read Error", __FUNCTION__); pthread_mutex_unlock(&httpd_mutex); return 1; } @@ -2227,7 +2222,8 @@ static unsigned short int read_client(int client_socket, void *userdata, char *a nread += readb; if (nread > length) { - motion_log(LOG_ERR, 1, "motion-httpd End buffer reached waiting for buffer ending"); + motion_log(LOG_ERR, 1, "%s: motion-httpd End buffer reached waiting " + "for buffer ending", __FUNCTION__); break; } buffer[nread] = '\0'; @@ -2236,7 +2232,7 @@ static unsigned short int read_client(int client_socket, void *userdata, char *a /* Make sure the last read didn't fail. If it did, there's a problem with the connection, so give up. */ if (nread == -1) { - motion_log(LOG_ERR, 1, "motion-httpd READ give up!"); + motion_log(LOG_ERR, 1, "%s: motion-httpd READ give up!", __FUNCTION__); pthread_mutex_unlock(&httpd_mutex); return 1; } @@ -2390,7 +2386,8 @@ void httpd_run(struct context **cnt) /* check != 0 to allow FreeBSD compatibility */ if (val != 0) { - motion_log(LOG_ERR, 1, "getaddrinfo() for httpd socket failed: %s", gai_strerror(val)); + motion_log(LOG_ERR, 1, "%s: getaddrinfo() for httpd socket failed: %s", + __FUNCTION__, gai_strerror(val)); freeaddrinfo(res); return; } @@ -2404,8 +2401,8 @@ void httpd_run(struct context **cnt) getnameinfo(res->ai_addr, res->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); - motion_log(LOG_INFO, 0, "motion-httpd testing : %s addr: %s port: %s", - res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); + motion_log(LOG_INFO, 0, "%s: motion-httpd testing : %s addr: %s port: %s", + __FUNCTION__, res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); if (sd >= 0) { val = 1; @@ -2413,36 +2410,40 @@ void httpd_run(struct context **cnt) setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof( int ) ); if (bind(sd, res->ai_addr, res->ai_addrlen) == 0) { - motion_log(LOG_INFO, 0, "motion-httpd Bound : %s addr: %s port: %s", - res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); + motion_log(LOG_INFO, 0, "%s: motion-httpd Bound : %s addr: %s port: %s", + __FUNCTION__, res->ai_family == AF_INET ? "IPV4":"IPV6", hbuf, sbuf); break; } - motion_log(LOG_ERR, 1, "motion-httpd failed bind() interface %s / port %s, retrying", hbuf, sbuf); + motion_log(LOG_ERR, 1, "%s: motion-httpd failed bind() interface %s / port %s, retrying", + __FUNCTION__, hbuf, sbuf); close(sd); sd = -1; } - motion_log(LOG_ERR, 1, "motion-httpd socket failed interface %s / port %s, retrying", hbuf, sbuf); + motion_log(LOG_ERR, 1, "%s: motion-httpd socket failed interface %s / port %s, retrying", + __FUNCTION__, hbuf, sbuf); res = res->ai_next; } freeaddrinfo(ressave); if (sd < 0) { - motion_log(LOG_ERR, 1, "motion-httpd ERROR bind() [interface %s port %s]", hbuf, sbuf); + motion_log(LOG_ERR, 1, "%s: motion-httpd ERROR bind() [interface %s port %s]", + __FUNCTION__, hbuf, sbuf); return; } if (listen(sd,5) == -1) { - motion_log(LOG_ERR, 1, "motion-httpd ERROR listen() [interface %s port %s]", hbuf, sbuf); + motion_log(LOG_ERR, 1, "%s: motion-httpd ERROR listen() [interface %s port %s]", + __FUNCTION__, hbuf, sbuf); close(sd); return; } - motion_log(LOG_DEBUG, 0, "motion-httpd/"VERSION" running, accepting connections"); - motion_log(LOG_DEBUG, 0, "motion-httpd: waiting for data on %s port TCP %s", hbuf, sbuf); + motion_log(LOG_DEBUG, 0, "%s: motion-httpd/"VERSION" running, accepting connections", __FUNCTION__); + motion_log(LOG_DEBUG, 0, "%s: motion-httpd: waiting for data on %s port TCP %s", __FUNCTION__, hbuf, sbuf); - if (cnt[0]->conf.webcontrol_authentication != NULL ) { + if (cnt[0]->conf.webcontrol_authentication != NULL) { char *userpass = NULL; size_t auth_size = strlen(cnt[0]->conf.webcontrol_authentication); @@ -2461,13 +2462,13 @@ void httpd_run(struct context **cnt) if (client_socket_fd < 0) { if ((!cnt[0]) || (cnt[0]->finish)) { - motion_log(-1, 0, "motion-httpd - Finishing"); + motion_log(-1, 0, "%s: motion-httpd - Finishing", __FUNCTION__); closehttpd = 1; } } else { /* Get the Client request */ client_sent_quit_message = read_client (client_socket_fd, cnt, authentication); - motion_log(-1, 0, "motion-httpd - Read from client"); + motion_log(-1, 0, "%s: motion-httpd - Read from client", __FUNCTION__); /* Close Connection */ if (client_socket_fd) @@ -2476,9 +2477,10 @@ void httpd_run(struct context **cnt) } - if (authentication != NULL) free(authentication); + if (authentication != NULL) + free(authentication); close(sd); - motion_log(LOG_DEBUG, 0, "motion-httpd Closing"); + motion_log(LOG_DEBUG, 0, "%s: motion-httpd Closing", __FUNCTION__); pthread_mutex_destroy(&httpd_mutex); } @@ -2486,6 +2488,6 @@ void *motion_web_control(void *arg) { struct context **cnt = arg; httpd_run(cnt); - motion_log(LOG_DEBUG, 0, "motion-httpd thread exit"); + motion_log(LOG_DEBUG, 0, "%s: motion-httpd thread exit", __FUNCTION__); pthread_exit(NULL); }