Skip to content

Commit

Permalink
fixes in new log system
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCarpintero authored and AngelCarpintero committed Apr 5, 2010
1 parent f746995 commit 07f9e36
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 74 deletions.
10 changes: 8 additions & 2 deletions conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ static void conf_cmdline(struct context *cnt, int thread)
case 'd':
/* No validation - just take what user gives. */
if (thread == -1)
debug_level = (unsigned int)atoi(optarg);
cnt->log_level = (unsigned int)atoi(optarg);
break;
case 'k':
if (thread == -1)
Expand Down Expand Up @@ -1819,6 +1819,7 @@ struct context **conf_load(struct context **cnt)
cnt[0]->conf_filename[0] = 0;
cnt[0]->pid_file[0] = 0;
cnt[0]->log_file[0] = 0;
cnt[0]->log_level = -1;

conf_cmdline(cnt[0], -1);

Expand Down Expand Up @@ -1884,16 +1885,21 @@ struct context **conf_load(struct context **cnt)
while (cnt[++i])
conf_cmdline(cnt[i], i);

/* if pid file was passed from Command-line copy to main thread conf struct. */
/* If pid file was passed from Command-line copy to main thread conf struct. */
if (cnt[0]->pid_file[0])
cnt[0]->conf.pid_file = mystrcpy(cnt[0]->conf.pid_file, cnt[0]->pid_file);

/* If log file was passed from Command-line copy to main thread conf struct. */
if (cnt[0]->log_file[0])
cnt[0]->conf.log_file = mystrcpy(cnt[0]->conf.log_file, cnt[0]->log_file);

/* If log type string was passed from Command-line copy to main thread conf struct. */
if (cnt[0]->log_type_str[0])
cnt[0]->conf.log_type_str = mystrcpy(cnt[0]->conf.log_type_str, cnt[0]->log_type_str);

/* if log level was passed from Command-line copy to main thread conf struct. */
if (cnt[0]->log_level != -1)
cnt[0]->conf.log_level = cnt[0]->log_level;

return cnt;
}
Expand Down
26 changes: 10 additions & 16 deletions logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ static FILE *logfile;
static unsigned int log_level = LEVEL_DEFAULT;
static unsigned int log_type = TYPE_DEFAULT;

static const char *log_type_str[] = {NULL, "STR", "ENC", "NET", "DBL", "EVT", "TRK", "VID", "ALL" };
static const char *log_level_str[] = {NULL, "EMG", "ALR", "CRT", "ERR", "WRN", "NTC", "INF", "DBG", "ALL" };
static const char *log_type_str[] = {NULL, "STR", "ENC", "NET", "DBL", "EVT", "TRK", "VID", "ALL"};
static const char *log_level_str[] = {"EMG", "ALR", "CRT", "ERR", "WRN", "NTC", "INF", "DBG", "ALL", NULL};


/**
Expand All @@ -34,7 +34,6 @@ int get_log_type(const char *type)
unsigned int maxtype = sizeof(log_type_str)/sizeof(const char *);

for (i = 1;i < maxtype; i++) {
/* printf("Debug %s\n", log_type_str[i]); */
if (!strncasecmp(type, log_type_str[i], 3)) {
ret = i;
break;
Expand Down Expand Up @@ -74,7 +73,7 @@ void set_log_type(unsigned int type)
*/
const char* get_log_level_str(unsigned int level)
{
return log_level_str[SHOW_LEVEL_VALUE(level)];
return log_level_str[level];
}

/**
Expand Down Expand Up @@ -170,14 +169,10 @@ void motion_log(int level, unsigned int type, int errno_flag, const char *fmt, .
if ((unsigned int)level > log_level)
return;

//printf("Level %d <= Log_level %d ", level, log_level);

/* Exit if type is greater than log_type */
if (type > log_type)
return;

//printf("Type %d <= Log_type %d\n---------\n", type, log_type);

/*
* If pthread_getspecific fails (e.g., because the thread's TLS doesn't
* contain anything for thread number, it returns NULL which casts to zero,
Expand All @@ -193,21 +188,20 @@ void motion_log(int level, unsigned int type, int errno_flag, const char *fmt, .

/*
* Prefix the message with the log level string, log type string,
* time and thread number. i.ex [ERR] [ENC] [Apr 03 00:08:44] [1] blah
* time and thread number. i.ex [1] [ERR] [ENC] [Apr 03 00:08:44] blah
*
*/
if (!log_mode) {
n = snprintf(buf, sizeof(buf), "[%s] [%s] [%d] [%s]",
get_log_level_str(level), get_log_type_str(type),
threadnr, str_time());
n = snprintf(buf, sizeof(buf), "[%d] [%s] [%s] [%s] ",
threadnr, get_log_level_str(level), get_log_type_str(type),
str_time());
} else {
/*
* Prefix the message with the log level string, log type string
* and thread number. i.ex [DBG] [TRK] [1] blah
* and thread number. i.ex [1] [DBG] [TRK] blah
*/
n = snprintf(buf, sizeof(buf), "[%s] [%s] [%d]",
get_log_level_str(level), get_log_type_str(type),
threadnr);
n = snprintf(buf, sizeof(buf), "[%d] [%s] [%s] ",
threadnr, get_log_level_str(level), get_log_type_str(type));
}

/* Next add the user's message */
Expand Down
6 changes: 2 additions & 4 deletions logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
#define SHOW_ERRNO 1 /* Flag to show message associated to errno */

/* Log levels */
#define LOG_CONSOLE -1
#define LOG_ALL 8
#define CONSOLE LOG_CONSOLE /* -1 */
#define LOG_ALL 9
#define EMG LOG_EMERG /* syslog 0 motion 1 */
#define ALR LOG_ALERT /* syslog 1 motion 2 */
#define CRT LOG_CRIT /* syslog 2 motion 3 */
Expand All @@ -34,7 +32,7 @@
#define NTC LOG_NOTICE /* syslog 5 motion 6 */
#define INF LOG_INFO /* syslog 6 motion 7 */
#define DBG LOG_DEBUG /* syslog 7 motion 8 */
#define ALL LOG_ALL /* syslog 8 motion 9 */
#define ALL LOG_ALL /* syslog 7 motion 9 */
#define LEVEL_DEFAULT ERR /* syslog 3 motion 4 default */
#define SHOW_LEVEL_VALUE(x) (x+1)

Expand Down
51 changes: 22 additions & 29 deletions motion.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ struct context **cnt_list = NULL;
*/
volatile int threads_running = 0;

/*
* debug_level is for developers, normally used to control which
* types of messages get output.
*/
unsigned int debug_level;
unsigned int debug_type;

/* Set this when we want main to end or restart */
volatile unsigned int finish = 0;

Expand Down Expand Up @@ -540,7 +533,7 @@ static void process_image_ring(struct context *cnt, unsigned int max_images)
cnt->current_image = &cnt->imgs.image_ring[cnt->imgs.image_ring_out];

if (cnt->imgs.image_ring[cnt->imgs.image_ring_out].shot < cnt->conf.frame_limit) {
if (debug_level >= DBG) {
if (cnt->log_level >= DBG) {
char tmp[32];
const char *t;

Expand Down Expand Up @@ -583,7 +576,7 @@ static void process_image_ring(struct context *cnt, unsigned int max_images)
* we don't know how many frames there is in first sec
*/
if (cnt->movie_last_shot >= 0) {
if (debug_level >= DBG) {
if (cnt_list[0]->log_level >= DBG) {
int frames = cnt->movie_fps - (cnt->movie_last_shot + 1);
if (frames > 0) {
char tmp[15];
Expand Down Expand Up @@ -841,7 +834,7 @@ static int motion_init(struct context *cnt)
MOTION_LOG(ERR, TYPE_DB, NO_ERRNO, "%s: 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(ERR, TYPE_DB, NO_ERRNO, "%s: MySQL error was %s", mysql_error(cnt->database));
MOTION_LOG(ERR, TYPE_DB, NO_ERRNO, "%s: MySQL error was %s", mysql_error(cnt->database));
return -2;
}
#if (defined(MYSQL_VERSION_ID)) && (MYSQL_VERSION_ID > 50012)
Expand Down Expand Up @@ -2273,7 +2266,7 @@ static void become_daemon(void)
myfclose(pidf);
} else {
MOTION_LOG(EMG, TYPE_ALL, SHOW_ERRNO, "%s: Exit motion, cannot create process"
" id file (pid file) %s", cnt_list[0]->conf.pid_file);
" id file (pid file) %s", cnt_list[0]->conf.pid_file);
if (ptr_logfile)
myfclose(ptr_logfile);
exit(0);
Expand Down Expand Up @@ -2421,17 +2414,17 @@ static void motion_startup(int daemonize, int argc, char *argv[])
*/
cntlist_create(argc, argv);

debug_level = cnt_list[0]->conf.log_level;

if ((cnt_list[0]->conf.log_level > ALL) ||
(cnt_list[0]->conf.log_level == 0)) {
cnt_list[0]->conf.log_level = LEVEL_DEFAULT;
debug_level = cnt_list[0]->conf.log_level;
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Using default log level (%s) (%d)",
get_log_level_str(debug_level), SHOW_LEVEL_VALUE(debug_level));
cnt_list[0]->log_level = cnt_list[0]->conf.log_level;
MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Using default log level (%s) (%d)",
get_log_level_str(cnt_list[0]->log_level), SHOW_LEVEL_VALUE(cnt_list[0]->log_level));
} else {
cnt_list[0]->log_level = cnt_list[0]->conf.log_level - 1; // Let's make syslog compatible
}

set_log_level(debug_level);
set_log_level(cnt_list[0]->log_level);

MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Motion "VERSION" Started");

Expand All @@ -2441,7 +2434,7 @@ static void motion_startup(int daemonize, int argc, char *argv[])

if (ptr_logfile) {
set_log_mode(LOGMODE_SYSLOG);
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Logging to file (%s)",
MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Logging to file (%s)",
cnt_list[0]->conf.log_file);
set_log_mode(LOGMODE_FILE);
} else {
Expand All @@ -2450,19 +2443,19 @@ static void motion_startup(int daemonize, int argc, char *argv[])
exit(0);
}
} else {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Logging to syslog");
MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Logging to syslog");
}

if (!(debug_type = get_log_type(cnt_list[0]->conf.log_type_str))) {
debug_type = TYPE_DEFAULT;
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Using default log type (%s)",
get_log_type_str(debug_type));
if (!(cnt_list[0]->log_type = get_log_type(cnt_list[0]->conf.log_type_str))) {
cnt_list[0]->log_type = TYPE_DEFAULT;
MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Using default log type (%s)",
get_log_type_str(cnt_list[0]->log_type));
}

MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Using log type (%s) log level (%s)",
get_log_type_str(debug_type), get_log_level_str(debug_level));
MOTION_LOG(EMG, TYPE_ALL, NO_ERRNO, "%s: Using log type (%s) log level (%s)",
get_log_type_str(cnt_list[0]->log_type), get_log_level_str(cnt_list[0]->log_level));

set_log_type(debug_type);
set_log_type(cnt_list[0]->log_type);

initialize_chars();

Expand Down Expand Up @@ -2722,7 +2715,7 @@ int main (int argc, char **argv)
if (((motion_threads_running == 0) && finish) ||
((motion_threads_running == 0) && (threads_running == 0))) {
MOTION_LOG(DBG, TYPE_ALL, NO_ERRNO, "%s: DEBUG-1 threads_running %d motion_threads_running %d "
", finish %d", threads_running, motion_threads_running, finish);
", finish %d", threads_running, motion_threads_running, finish);
break;
}

Expand All @@ -2739,13 +2732,13 @@ int main (int argc, char **argv)

if (cnt_list[i]->watchdog == 0) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Thread %d - Watchdog timeout, trying to do "
"a graceful restart", cnt_list[i]->threadnr);
"a graceful restart", cnt_list[i]->threadnr);
cnt_list[i]->finish = 1;
}

if (cnt_list[i]->watchdog == -60) {
MOTION_LOG(ERR, TYPE_ALL, NO_ERRNO, "%s: Thread %d - Watchdog timeout, did NOT restart graceful,"
"killing it!", cnt_list[i]->threadnr);
"killing it!", cnt_list[i]->threadnr);
pthread_cancel(cnt_list[i]->thread_id);
pthread_mutex_lock(&global_lock);
threads_running--;
Expand Down
4 changes: 2 additions & 2 deletions motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ struct context {
char pid_file[PATH_MAX];
char log_file[PATH_MAX];
char log_type_str[6];
int log_level;
unsigned int log_type;

struct config conf;
struct images imgs;
Expand Down Expand Up @@ -437,8 +439,6 @@ struct context {

extern pthread_mutex_t global_lock;
extern volatile int threads_running;
extern unsigned int debug_level;
extern unsigned int debug_type;
extern FILE *ptr_logfile;

/* TLS keys below */
Expand Down
Loading

0 comments on commit 07f9e36

Please sign in to comment.