log_manager.cc

Commented why spread-down logging is disabled. 
	In MaxScale start, log paths are printed in their complete form to make it easier to copy-paste the names.

gateway.c
	Replaced print_signal_set_error with more general-purpose print_log_n_stderr which prints non-formatted messages to error log and to stderr to the point MaxScale switches to run in daemon process. After that only error log is printed.

skyge_utils.cc
	polish
This commit is contained in:
vraatikka
2013-11-25 10:49:45 +02:00
parent 4f14c65040
commit 3b0d6c23ad
3 changed files with 212 additions and 207 deletions

View File

@ -683,6 +683,10 @@ static int logmanager_write_log(
wp[timestamp_len-1+str_len-1]='\n'; wp[timestamp_len-1+str_len-1]='\n';
blockbuf_unregister(bb); blockbuf_unregister(bb);
/**
* disable because cross-blockbuffer locking either causes deadlock
* or run out of memory blocks.
*/
if (spread_down && false) { if (spread_down && false) {
/** /**
* Write to target log. If spread_down == true, then * Write to target log. If spread_down == true, then
@ -1472,18 +1476,20 @@ static bool fnames_conf_init(
ss_dfprintf(stderr, "\n");*/ ss_dfprintf(stderr, "\n");*/
fprintf(stderr, fprintf(stderr,
"Log directory :\t%s\n" "Error log :\t%s/%s1%s\n"
"Error log :\t%s1%s\n" "Message log :\t%s/%s1%s\n"
"Message log :\t%s1%s\n" "Trace log :\t%s/%s1%s\n"
"Trace log :\t%s1%s\n" "Debug log :\t%s/%s1%s\n\n",
"Debug log :\t%s1%s\n\n",
fn->fn_logpath, fn->fn_logpath,
fn->fn_err_prefix, fn->fn_err_prefix,
fn->fn_err_suffix, fn->fn_err_suffix,
fn->fn_logpath,
fn->fn_msg_prefix, fn->fn_msg_prefix,
fn->fn_msg_suffix, fn->fn_msg_suffix,
fn->fn_logpath,
fn->fn_trace_prefix, fn->fn_trace_prefix,
fn->fn_trace_suffix, fn->fn_trace_suffix,
fn->fn_logpath,
fn->fn_debug_prefix, fn->fn_debug_prefix,
fn->fn_debug_suffix); fn->fn_debug_suffix);

View File

@ -326,24 +326,57 @@ return_succp:
return succp; return succp;
} }
static void print_signal_set_error(
int sig, /**
int eno) * @node Provides error printing for non-formatted error strings.
*
* Parameters:
* @param do_log - in, use
* is printing to log enabled
*
* @param do_stderr - in, use
* is printing to stderr enabled
*
* @param logerr - in, use
* string to be printed to log
*
* @param fprerr - in, use
* string to be printed to stderr
*
* @param eno - in, use
* errno, if it is set, zero, otherwise
*
* @return void
*
*/
static void print_log_n_stderr(
bool do_log, /**<! is printing to log enabled */
bool do_stderr,/**<! is printing to stderr enabled */
char* logerr, /**<! string to be printed to log */
char* fprerr, /**<! string to be printed to stderr */
int eno) /**<! errno, if it is set, zero, otherwise */
{ {
fprintf(stderr, char* log_start = "Error :";
"*\n* Error : Failed to set signal handler for %s due " char* fpr_start = "*\n* Error :";
"%d, %s.\n* " char* fpr_end = "\n*\n";
"Exiting.\n*\n",
strsignal(sig), if (do_log) {
eno, skygw_log_write_flush(LOGFILE_ERROR,
strerror(eno)); "%s %s %s %s",
skygw_log_write_flush( log_start,
LOGFILE_ERROR, logerr,
"Error : Failed to set signal handler for %s due " eno == 0 ? "" : "error :",
"%d, %s. Exiting.", eno == 0 ? "" : strerror(eno));
strsignal(sig), }
eno, if (do_stderr) {
strerror(eno)); fprintf(stderr,
"%s %s %s %s %s",
fpr_start,
fprerr,
eno == 0 ? "" : "error :",
eno == 0 ? "" : strerror(eno),
fpr_end);
}
} }
@ -360,13 +393,23 @@ static void print_signal_set_error(
* @return 0 in success, 1 otherwise * @return 0 in success, 1 otherwise
* *
* *
* @details (write detailed description here) * @details Logging and error printing:
* ---
* What is printed to the terminal is something that the user can understand,
* and/or something what the user can do for. For example, fix configuration.
* More detailed messages are printed to error log, and optionally to trace
* and debug log.
*
* As soon as process switches to daemon process, stderr printing is stopped.
* This is not obvious solution because stderr is often directed to somewhere,
* but currently this is the case.
*
* vraa 25.11.13
* *
*/ */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int rc = 0; int rc = 0;
int daemon_mode = 1;
int l; int l;
int i; int i;
int n; int n;
@ -384,7 +427,17 @@ int main(int argc, char **argv)
sigset_t sigset; sigset_t sigset;
sigset_t sigpipe_mask; sigset_t sigpipe_mask;
sigset_t saved_mask; sigset_t saved_mask;
/**
* If MaxScale is started to run in daemon process the value is true.
*/
bool daemon_mode = true;
void (*exitfunp[4])(void) = {skygw_logmanager_exit,
datadir_cleanup,
write_footer,
NULL};
sigemptyset(&sigpipe_mask); sigemptyset(&sigpipe_mask);
sigaddset(&sigpipe_mask, SIGPIPE); sigaddset(&sigpipe_mask, SIGPIPE);
@ -398,38 +451,22 @@ int main(int argc, char **argv)
fail_accept_errno = 0; fail_accept_errno = 0;
#endif #endif
file_write_header(stderr); file_write_header(stderr);
/**
l = atexit(skygw_logmanager_exit); * Register functions which are called at exit.
/*l=1;/**/ */
if (l != 0) { for (i=0; exitfunp[i] != NULL; i++)
fprintf(stderr, {
"*\n* Error : Failed to register exit function for " l = atexit(*exitfunp);
"%s.\n* Exiting.\n*\n",
program_invocation_short_name); if (l != 0)
rc = 1; {
goto return_main; char* fprerr = "Failed to register exit functions for MaxScale";
print_log_n_stderr(false, true, NULL, fprerr, 0);
rc = 1;
goto return_main;
}
} }
l = atexit(datadir_cleanup);
/*l=1;/**/
if (l != 0) {
fprintf(stderr,
"*\n* Error : Failed to register exit function for "
"%s. Exiting.\n*\n",
program_invocation_short_name);
rc = 1;
goto return_main;
}
l = atexit(write_footer);
/*l=1;/**/
if (l != 0) {
fprintf(stderr,
"*\n* Error : Failed to register exit function for "
"%s.\n* Exiting.\n*\n",
program_invocation_short_name);
rc = 1;
goto return_main;
}
for (n = 0; n < argc; n++) for (n = 0; n < argc; n++)
{ {
int r = strcmp(argv[n], "-d"); int r = strcmp(argv[n], "-d");
@ -437,7 +474,7 @@ int main(int argc, char **argv)
if (r == 0) if (r == 0)
{ {
/** Debug mode, maxscale runs in this same process */ /** Debug mode, maxscale runs in this same process */
daemon_mode = 0; daemon_mode = false;
} }
/** /**
* 1. Resolve config file location from command-line argument. * 1. Resolve config file location from command-line argument.
@ -453,19 +490,12 @@ int main(int argc, char **argv)
/*s=arg_limit; /**/ /*s=arg_limit; /**/
if (s == arg_limit) if (s == arg_limit)
{ {
fprintf(stderr, char* logerr = "Unable to find the MaxScale "
"*\n* Error : Unable to find the MaxScale " "configuration file MaxScale.cnf."
"configuration file MaxScale.cnf.\n" " Either install one in /etc/ , "
"* Either install one in /etc/ , "
"$MAXSCALE_HOME/etc/ , or specify the file " "$MAXSCALE_HOME/etc/ , or specify the file "
"with the -c option.\n* Exiting.\n*\n"); "with the -c option. Exiting.";
skygw_log_write_flush( print_log_n_stderr(true, true, logerr, logerr, 0);
LOGFILE_ERROR,
"Error : Unable to find the MaxScale "
"configuration file, either install one "
"in /etc/, "
"$MAXSCALE_HOME/etc/ "
"or use the -c option. Exiting.");
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
@ -473,22 +503,26 @@ int main(int argc, char **argv)
} }
} }
if (daemon_mode == 0) if (!daemon_mode)
{ {
fprintf(stderr, fprintf(stderr,
"Info : MaxScale will be run in the terminal process.\n\n"); "Info : MaxScale will be run in the terminal process.\n See "
"the log from the following log files.\n\n");
} }
else else
{ {
/** /**
* Maxscale must be daemonized before opening files, initializing * Maxscale must be daemonized before opening files, initializing
* embedded MariaDB and in general, as early as possible. * embedded MariaDB and in general, as early as possible.
*/ */
int r; int r;
int eno = 0; int eno = 0;
char* fprerr = "Failed to initialize set the signal "
"set for MaxScale. Exiting.";
fprintf(stderr, fprintf(stderr,
"Info : MaxScale will be run in a daemon process.\n\n"); "Info : MaxScale will be run in a daemon process.\n\tSee "
"the log from the following log files.\n\n");
r = sigfillset(&sigset); r = sigfillset(&sigset);
/*r=1;/**/ /*r=1;/**/
@ -496,13 +530,7 @@ int main(int argc, char **argv)
{ {
eno = errno; eno = errno;
errno = 0; errno = 0;
skygw_log_write_flush( print_log_n_stderr(true, true, fprerr, fprerr, eno);
LOGFILE_ERROR,
"Error : Failed to initialize set the signal "
"set for %s due %d, %s. Exiting.",
program_invocation_short_name,
eno,
strerror(eno));
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
@ -510,16 +538,11 @@ int main(int argc, char **argv)
/*r=1;/**/ /*r=1;/**/
if (r != 0) if (r != 0)
{ {
char* logerr = "Failed to delete signal SIGHUP from the "
"signal set of MaxScale. Exiting.";
eno = errno; eno = errno;
errno = 0; errno = 0;
skygw_log_write_flush( print_log_n_stderr(true, true, fprerr, logerr, eno);
LOGFILE_ERROR,
"Error : Failed to delete signal %s from the "
"signal set of %s due to %d, %s. Exiting.",
strsignal(SIGHUP),
program_invocation_short_name,
eno,
strerror(eno));
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
@ -527,101 +550,86 @@ int main(int argc, char **argv)
/*r=1;/**/ /*r=1;/**/
if (r != 0) if (r != 0)
{ {
char* logerr = "Failed to delete signal SIGTERM from the "
"signal set of MaxScale. Exiting.";
eno = errno; eno = errno;
errno = 0; errno = 0;
skygw_log_write_flush( print_log_n_stderr(true, true, fprerr, logerr, eno);
LOGFILE_ERROR,
"Error : Failed to delete signal %s from the "
"signal set of %s due to %d, %s. Exiting.",
strsignal(SIGTERM),
program_invocation_short_name,
eno,
strerror(eno));
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
r = sigprocmask(SIG_SETMASK, &sigset, NULL); r = sigprocmask(SIG_SETMASK, &sigset, NULL);
/*r=1;/**/ /*r=1;/**/
if (r != 0) { if (r != 0) {
char* logerr = "Failed to set the signal set for MaxScale."
" Exiting.";
eno = errno; eno = errno;
errno = 0; errno = 0;
skygw_log_write_flush( print_log_n_stderr(true, true, fprerr, logerr, eno);
LOGFILE_ERROR,
"Error : Failed to set the signal set for %s "
"due to %d, %s. Exiting.",
program_invocation_short_name,
eno,
strerror(eno));
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
gw_daemonize(); gw_daemonize();
} }
/**
l = signal_set(SIGHUP, sighup_handler); * Set signal handlers for SIGHUP, SIGTERM, and SIGINT.
/*l=1;/**/ */
if (l != 0)
{ {
eno = errno; char* fprerr = "Failed to initialize signal handlers. Exiting.";
errno = 0; char* logerr = NULL;
print_signal_set_error(SIGHUP, eno); l = signal_set(SIGHUP, sighup_handler);
rc = 1; /*l=1;/**/
goto return_main; if (l != 0)
{
logerr = strdup("Failed to set signal handler for "
"SIGHUP. Exiting.");
goto sigset_err;
}
l = signal_set(SIGTERM, sigterm_handler);
/*l=1;/**/
if (l != 0)
{
logerr = strdup("Failed to set signal handler for "
"SIGTERM. Exiting.");
goto sigset_err;
}
l = signal_set(SIGINT, sigint_handler);
/*l=1; /**/
if (l != 0)
{
logerr = strdup("Failed to set signal handler for "
"SIGINT. Exiting.");
goto sigset_err;
}
sigset_err:
if (l != 0)
{
eno = errno;
errno = 0;
print_log_n_stderr(true, !daemon_mode, logerr, fprerr, eno);
free(logerr);
rc = 1;
goto return_main;
}
} }
l = signal_set(SIGTERM, sigterm_handler);
/*l=1;/**/
if (l != 0)
{
eno = errno;
errno = 0;
print_signal_set_error(SIGTERM, eno);
rc = 1;
goto return_main;
}
l = signal_set(SIGINT, sigint_handler);
/*l=1; /**/
if (l != 0)
{
eno = errno;
errno = 0;
print_signal_set_error(SIGINT, eno);
rc = 1;
goto return_main;
}
eno = pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask); eno = pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask);
/*eno=EINTR; /**/ /*eno=EINTR; /**/
if (eno != 0) if (eno != 0)
{ {
fprintf(stderr, char* logerr = "Failed to initialise signal mask for MaxScale. "
"*\n* Error : Failed to set signal mask for %s due to " "Exiting.";
"%d, %s.\n* Exiting.\n*\n", print_log_n_stderr(true, true, logerr, logerr, eno);
program_invocation_short_name,
eno,
strerror(eno));
skygw_log_write_flush(
LOGFILE_ERROR,
"Error : Failed to set signal mask for %s. due to "
"%d, %s. Exiting.",
program_invocation_short_name,
eno,
strerror(eno));
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
l = atexit(libmysqld_done); l = atexit(libmysqld_done);
/*l=1;/**/
if (l != 0) { if (l != 0) {
fprintf(stderr, char* fprerr = "Failed to register exit function for\n* "
"*\n* Error : Failed to register exit function libmysql_done " "embedded MySQL library.\n* Exiting.";
"for %s.\n* Exiting.\n*\n", char* logerr = "Failed to register exit function libmysql_done for MaxScale. "
program_invocation_short_name); "Exiting.";
skygw_log_write_flush( print_log_n_stderr(true, true, logerr, fprerr, 0);
LOGFILE_ERROR,
"Error : Failed to register exit function "
"libmysql_done for %s. Exiting.",
program_invocation_short_name);
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
@ -637,14 +645,14 @@ int main(int argc, char **argv)
{ {
eno = errno; eno = errno;
errno = 0; errno = 0;
fprintf(stderr, if (!daemon_mode)
"*\n* Error : Failed to read the " {
"value of\n* MAXSCALE_HOME, %s, due " fprintf(stderr,
"to %d, %s.\n* " "*\n* Error : Failed to read the "
"Exiting.\n*\n", "value of\n* MAXSCALE_HOME, %s.\n* "
home, "Exiting.\n*\n",
eno, home);
strerror(eno)); }
skygw_log_write_flush( skygw_log_write_flush(
LOGFILE_ERROR, LOGFILE_ERROR,
"Error : Failed to read the " "Error : Failed to read the "
@ -673,14 +681,14 @@ int main(int argc, char **argv)
{ {
eno = errno; eno = errno;
errno = 0; errno = 0;
fprintf(stderr, if (!daemon_mode)
"*\n* Error : Failed to read the " {
"configuration \n* file %s \n* due " fprintf(stderr,
"to %d, %s.\n* " "*\n* Error : Failed to read the "
"Exiting.\n*\n", "configuration \n* file %s.\n* "
buf, "Exiting.\n*\n",
eno, buf);
strerror(eno)); }
skygw_log_write_flush( skygw_log_write_flush(
LOGFILE_ERROR, LOGFILE_ERROR,
"Error : Failed to read the " "Error : Failed to read the "
@ -747,17 +755,11 @@ int main(int argc, char **argv)
} }
if (cnf_file == NULL) { if (cnf_file == NULL) {
fprintf(stderr, char* logerr = "Failed to find or read the configuration "
"*\n* Error : Failed to find or read the "
"configuration file MaxScale.cnf.\n* Either install one in /etc/, "
"$MAXSCALE_HOME/etc/ or specify it by using the -c option.\n* "
"Exiting.\n*\n");
skygw_log_write_flush(
LOGFILE_ERROR,
"Error : Failed to find or read the configuration "
"file MaxScale.cnf.\n Either install one in /etc/, " "file MaxScale.cnf.\n Either install one in /etc/, "
"$MAXSCALE_HOME/etc/ " "$MAXSCALE_HOME/etc/ , or specify it by using "
"or use the -c option. Exiting."); "the -c option. Exiting.";
print_log_n_stderr(true, !daemon_mode, logerr, logerr, 0);
rc = 1; rc = 1;
goto return_main; goto return_main;
} }
@ -774,62 +776,59 @@ int main(int argc, char **argv)
if (mysql_library_init(num_elements, server_options, server_groups)) if (mysql_library_init(num_elements, server_options, server_groups))
{ {
if (!daemon_mode)
{
char* fprerr = "Failed to initialise the MySQL library. "
"Exiting.";
print_log_n_stderr(false, true, fprerr, fprerr, 0);
}
skygw_log_write_flush( skygw_log_write_flush(
LOGFILE_ERROR, LOGFILE_ERROR,
"Fatal : mysql_library_init failed. It is a " "Error : mysql_library_init failed. It is a "
"mandatory component, required by router services and " "mandatory component, required by router services and "
"the MaxScale core. Error %s, %s : %d. Exiting.", "the MaxScale core. Error %s, %s : %d. Exiting.",
mysql_error(NULL), mysql_error(NULL),
__FILE__, __FILE__,
__LINE__); __LINE__);
fprintf(stderr, rc = 1;
"Failed to initialise the MySQL library. Exiting.\n"); goto return_main;
exit(1);
} }
libmysqld_started = TRUE; libmysqld_started = TRUE;
if (!config_load(cnf_file)) if (!config_load(cnf_file))
{ {
char* fprerr = "Failed to load MaxScale configuration "
"file. Exiting.";
print_log_n_stderr(false, !daemon_mode, fprerr, fprerr, 0);
skygw_log_write_flush( skygw_log_write_flush(
LOGFILE_ERROR, LOGFILE_ERROR,
"Error : Failed to load MaxScale configuration file %s. " "Error : Failed to load MaxScale configuration file %s. "
"Exiting.", "Exiting.",
cnf_file); cnf_file);
fprintf(stderr, rc = 1;
"* Failed to load MaxScale configuration file. " goto return_main;
"Exiting.\n");
exit(1);
} }
skygw_log_write( skygw_log_write(
LOGFILE_MESSAGE, LOGFILE_MESSAGE,
"SkySQL MaxScale (C) SkySQL Ab 2013"); "SkySQL MaxScale (C) SkySQL Ab 2013");
skygw_log_write( skygw_log_write(
LOGFILE_MESSAGE, LOGFILE_MESSAGE,
"MaxScale is starting, PID %i", "MaxScale is running in process %i",
getpid()); getpid());
poll_init(); poll_init();
/* /**
* Start the services that were created above * Start the services that were created above
*/ */
n_services = serviceStartAll(); n_services = serviceStartAll();
if (n_services == 0) if (n_services == 0)
{ {
skygw_log_write_flush( char* logerr = "Failed to start any MaxScale services. Exiting.";
LOGFILE_ERROR, print_log_n_stderr(true, !daemon_mode, logerr, logerr, 0);
"Fatal : Failed to start any MaxScale services. " rc = 1;
"Exiting."); goto return_main;
fprintf(stderr,
"* Failed to start any MaxScale services. Exiting.\n");
exit(1);
} }
skygw_log_write(
LOGFILE_MESSAGE,
"Started %d services succesfully.",
n_services);
/** /**
* Start periodic log flusher thread. * Start periodic log flusher thread.
*/ */

View File

@ -1601,8 +1601,8 @@ static bool file_write_header(
header_buf2 = (char *)calloc(1, strlen(file->sf_fname)+2); header_buf2 = (char *)calloc(1, strlen(file->sf_fname)+2);
snprintf(header_buf2, strlen(file->sf_fname)+2, "%s ", file->sf_fname); snprintf(header_buf2, strlen(file->sf_fname)+2, "%s ", file->sf_fname);
header_buf3 = strdup(asctime(tm)); header_buf3 = strdup(asctime(tm));
header_buf4 = "---------------------------------------------------------" header_buf4 = "------------------------------------------------------"
"---------------------------\n"; "-----------------\n";
if (header_buf2 == NULL) { if (header_buf2 == NULL) {
goto return_succp; goto return_succp;