diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index f73d02714..356d1a7c0 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -1308,38 +1308,6 @@ static int log_write(int priority, return rv; } -int skygw_log_flush(logfile_id_t id) -{ - int err = -1; - - if (id == LOGFILE_ERROR) - { - if (logmanager_register(false)) - { - CHK_LOGMANAGER(lm); - - logfile_t *lf = logmanager_get_logfile(lm); - CHK_LOGFILE(lf); - - logfile_flush(lf); - err = 0; - - logmanager_unregister(); - } - else - { - ss_dfprintf(stderr, "Can't register to logmanager, flushing failed.\n"); - } - } - else - { - // We'll pretend everything went ok. - err = 0; - } - - return err; -} - /** * @node Register as a logging client to logmanager. * @@ -2592,26 +2560,6 @@ void flushall_logfiles(bool flush) simple_mutex_unlock(&lm->lm_mutex); } -/** - * Flush all log files synchronously - */ -void skygw_log_sync_all(void) -{ - if (!log_config.use_stdout) - { - skygw_log_write(LOGFILE_TRACE,"Starting log flushing to disk."); - } - - /** If initialization of the log manager has not been done, lm pointer can be - * NULL. */ - if (lm) - { - flushall_logfiles(true); - skygw_message_send(lm->lm_logmes); - skygw_message_wait(lm->lm_clientmes); - } -} - /** * Toggle high precision logging * @param val 0 for disabled, 1 for enabled @@ -2652,7 +2600,63 @@ void logmanager_enable_maxscalelog(int val) */ int mxs_log_flush() { - return skygw_log_flush(LOGFILE_ERROR); + int err = -1; + + if (logmanager_register(false)) + { + CHK_LOGMANAGER(lm); + + logfile_t *lf = logmanager_get_logfile(lm); + CHK_LOGFILE(lf); + + logfile_flush(lf); + err = 0; + + logmanager_unregister(); + } + else + { + ss_dfprintf(stderr, "Can't register to logmanager, flushing failed.\n"); + } + + return err; +} + +/** + * Explicitly ensure that all pending log messages are flushed. + * + * @return 0 if the flushing was successfully performed, otherwise -1. + * + * When the function returns 0, the flushing has been initiated and the + * flushing thread has indicated that the operation has been performed. + * However, 0 will be returned also in the case that the flushing thread + * for, whatever reason, failed to actually flush the log. + */ +int mxs_log_flush_sync(void) +{ + int err = 0; + + if (!log_config.use_stdout) + { + MXS_INFO("Starting log flushing to disk."); + } + + /** If initialization of the log manager has not been done, lm pointer can be + * NULL. */ + // TODO: Why is logmanager_register() not called here? + if (lm) + { + flushall_logfiles(true); + err = skygw_message_send(lm->lm_logmes); + + if (!err) + { + // TODO: Add error handling to skygw_message_wait. Now void. + skygw_message_wait(lm->lm_clientmes); + } + } + + return err; } /** diff --git a/log_manager/log_manager.h b/log_manager/log_manager.h index 539df718f..35cab496d 100644 --- a/log_manager/log_manager.h +++ b/log_manager/log_manager.h @@ -142,7 +142,9 @@ bool mxs_log_init(const char* ident, const char* logdir, log_target_t target); void mxs_log_finish(void); int mxs_log_flush(); +int mxs_log_flush_sync(); int mxs_log_rotate(); + int mxs_log_enable_priority(int priority); int mxs_log_disable_priority(int priority); @@ -150,14 +152,8 @@ int mxs_log_message(int priority, const char* file, int line, const char* function, const char* format, ...); -/** - * free private write buffer list - */ -int skygw_log_flush(logfile_id_t id); -void skygw_log_sync_all(void); int skygw_log_enable(logfile_id_t id); int skygw_log_disable(logfile_id_t id); -void skygw_log_sync_all(void); void skygw_set_highp(int); void logmanager_enable_syslog(int); void logmanager_enable_maxscalelog(int); diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index c6f6320f0..ddf1c410a 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -152,14 +152,14 @@ int main(int argc, char* argv[]) logstr = ("Fourth write, no flush. Next flush only."); err = skygw_log_write(LOGFILE_ERROR, logstr); - err = skygw_log_flush(LOGFILE_ERROR); + err = mxs_log_flush(); logstr = "My name is %s %d years and %d months."; #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif err = skygw_log_write(LOGFILE_TRACE, logstr, "TraceyTracey", 3, 7); - skygw_log_flush(LOGFILE_TRACE); + mxs_log_flush(); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif @@ -508,7 +508,7 @@ static void* thr_run(void* data) int err; mxs_log_init(NULL, "/tmp", LOG_TARGET_FS); - skygw_log_flush(LOGFILE_MESSAGE); + mxs_log_flush(); logstr = ("Hi, how are you?"); err = skygw_log_write(LOGFILE_MESSAGE, logstr); @@ -518,8 +518,7 @@ static void* thr_run(void* data) } ss_dassert(err == 0); mxs_log_finish(); - skygw_log_flush(LOGFILE_TRACE); - skygw_log_flush(LOGFILE_MESSAGE); + mxs_log_flush(); logstr = ("I was wondering, you know, it has been such a lovely weather whole morning and " "I thought that would you like to come to my place and have a little piece of " "cheese with us. Just me and my mom - and you, of course. Then, if you wish, " @@ -541,7 +540,7 @@ static void* thr_run(void* data) ss_dassert(err == 0); mxs_log_init(NULL, "/tmp", LOG_TARGET_FS); mxs_log_init(NULL, "/tmp", LOG_TARGET_FS); - skygw_log_flush(LOGFILE_ERROR); + mxs_log_flush(); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); #if !defined(SS_DEBUG) @@ -568,7 +567,7 @@ static void* thr_run(void* data) ss_dassert(err == 0); mxs_log_init(NULL, "/tmp", LOG_TARGET_FS); mxs_log_finish(); - skygw_log_flush(LOGFILE_ERROR); + mxs_log_flush(); mxs_log_finish(); mxs_log_init(NULL, "/tmp", LOG_TARGET_FS); logstr = ("..and you?"); @@ -615,7 +614,7 @@ static void* thr_run(void* data) #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); #endif - skygw_log_flush(LOGFILE_TRACE); + mxs_log_flush(); logstr = ("For automatic and register variables, it is done each time the function or block is entered."); #if !defined(SS_DEBUG) skygw_log_enable(LOGFILE_TRACE); diff --git a/log_manager/test/testorder.c b/log_manager/test/testorder.c index 7656e8b94..06494cfdd 100644 --- a/log_manager/test/testorder.c +++ b/log_manager/test/testorder.c @@ -107,7 +107,7 @@ int main(int argc, char** argv) nanosleep(&ts1, NULL); } - skygw_log_flush(LOGFILE_ERROR); + mxs_log_flush(); mxs_log_finish(); free(message); return 0; diff --git a/server/core/dcb.c b/server/core/dcb.c index f45f682f1..ac7646ade 100644 --- a/server/core/dcb.c +++ b/server/core/dcb.c @@ -980,7 +980,7 @@ int dcb_read_SSL(DCB *dcb, if (GWBUF_LENGTH(buffer) != n) { - skygw_log_sync_all(); + mxs_log_flush_sync(); } ss_info_dassert((buffer->start <= buffer->end), "Buffer start has passed end."); diff --git a/server/core/gateway.c b/server/core/gateway.c index 338c90b8c..71cbb321c 100644 --- a/server/core/gateway.c +++ b/server/core/gateway.c @@ -316,7 +316,7 @@ static void sigterm_handler (int i) { skygw_log_write_flush( LOGFILE_ERROR, "MaxScale received signal SIGTERM. Exiting."); - skygw_log_sync_all(); + mxs_log_flush_sync(); shutdown_server(); } @@ -328,7 +328,7 @@ sigint_handler (int i) skygw_log_write_flush( LOGFILE_ERROR, "MaxScale received signal SIGINT. Shutting down."); - skygw_log_sync_all(); + mxs_log_flush_sync(); shutdown_server(); fprintf(stderr, "\n\nShutting down MaxScale\n\n"); } @@ -409,7 +409,7 @@ sigfatal_handler(int i) } } - skygw_log_sync_all(); + mxs_log_flush_sync(); /* re-raise signal to enforce core dump */ fprintf(stderr, "\n\nWriting core dump\n"); @@ -822,7 +822,7 @@ static bool file_is_readable( absolute_pathname, eno, strerror_r(eno, errbuf, sizeof(errbuf))); - skygw_log_sync_all(); + mxs_log_flush_sync(); succp = false; } return succp; @@ -2054,10 +2054,7 @@ static void log_flush_cb( LOGIF(LM, (skygw_log_write(LOGFILE_MESSAGE, "Started MaxScale log flusher."))); while (!do_exit) { - skygw_log_flush(LOGFILE_ERROR); - skygw_log_flush(LOGFILE_MESSAGE); - skygw_log_flush(LOGFILE_TRACE); - skygw_log_flush(LOGFILE_DEBUG); + mxs_log_flush(); nanosleep(&ts1, NULL); } LOGIF(LM, (skygw_log_write(LOGFILE_MESSAGE, diff --git a/server/core/maxkeys.c b/server/core/maxkeys.c index 14e94f9bb..21546fb49 100644 --- a/server/core/maxkeys.c +++ b/server/core/maxkeys.c @@ -56,7 +56,7 @@ int main(int argc, char **argv) rval = 1; } - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); return rval; diff --git a/server/core/maxpasswd.c b/server/core/maxpasswd.c index 9afe83d98..89d37caaf 100644 --- a/server/core/maxpasswd.c +++ b/server/core/maxpasswd.c @@ -74,7 +74,7 @@ main(int argc, char **argv) } free(pw); - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); return rval; } diff --git a/server/core/test/testhint.c b/server/core/test/testhint.c index 77c1aa4e8..38bccc3e3 100644 --- a/server/core/test/testhint.c +++ b/server/core/test/testhint.c @@ -44,7 +44,7 @@ * test1 Allocate table of users and mess around with it * */ -void skygw_log_sync_all(void); +int mxs_log_flush_sync(void); static int test1() { @@ -56,13 +56,13 @@ HINT *hint; char* name = strdup("name"); hint = hint_create_parameter(NULL, name, "value"); free(name); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL != hint, "New hint list should not be null"); ss_info_dassert(0 == strcmp("value", hint->value), "Hint value should be correct"); ss_info_dassert(0 != hint_exists(&hint, HINT_PARAMETER), "Hint of parameter type should exist"); ss_dfprintf(stderr, "\t..done\nFree hints."); if (NULL != hint) hint_free(hint); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_dfprintf(stderr, "\t..done\n"); return 0; diff --git a/server/core/test/testserver.c b/server/core/test/testserver.c index b82c1f2c8..57f4d4e3a 100644 --- a/server/core/test/testserver.c +++ b/server/core/test/testserver.c @@ -55,7 +55,7 @@ char *status; ss_dfprintf(stderr, "testserver : creating server called MyServer"); server = server_alloc("MyServer", "HTTPD", 9876); - skygw_log_sync_all(); + mxs_log_flush_sync(); //ss_info_dassert(NULL != service, "New server with valid protocol and port must not be null"); //ss_info_dassert(0 != service_isvalid(service), "Service must be valid after creation"); @@ -63,32 +63,32 @@ char *status; ss_dfprintf(stderr, "\t..done\nTest Parameter for Server."); ss_info_dassert(NULL == serverGetParameter(server, "name"), "Parameter should be null when not set"); serverAddParameter(server, "name", "value"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 == strcmp("value", serverGetParameter(server, "name")), "Parameter should be returned correctly"); ss_dfprintf(stderr, "\t..done\nTesting Unique Name for Server."); ss_info_dassert(NULL == server_find_by_unique_name("uniquename"), "Should not find non-existent unique name."); server_set_unique_name(server, "uniquename"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(server == server_find_by_unique_name("uniquename"), "Should find by unique name."); ss_dfprintf(stderr, "\t..done\nTesting Status Setting for Server."); status = server_status(server); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running by default."); if (NULL != status) free(status); server_set_status(server, SERVER_MASTER); status = server_status(server); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 == strcmp("Master, Running", status), "Should find correct status."); server_clear_status(server, SERVER_MASTER); free(status); status = server_status(server); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 == strcmp("Running", status), "Status of Server should be Running after master status cleared."); if (NULL != status) free(status); ss_dfprintf(stderr, "\t..done\nRun Prints for Server and all Servers."); printServer(server); printAllServers(); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_dfprintf(stderr, "\t..done\nFreeing Server."); ss_info_dassert(0 != server_free(server), "Free should succeed"); ss_dfprintf(stderr, "\t..done\n"); diff --git a/server/core/test/testservice.c b/server/core/test/testservice.c index 5fa7e0436..4bb41fee5 100644 --- a/server/core/test/testservice.c +++ b/server/core/test/testservice.c @@ -60,12 +60,12 @@ init_test_env(NULL); ss_dfprintf(stderr, "testservice : creating service called MyService with router nonexistent"); service = service_alloc("MyService", "non-existent"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL == service, "New service with invalid router should be null"); ss_info_dassert(0 == service_isvalid(service), "Service must not be valid after incorrect creation"); ss_dfprintf(stderr, "\t..done\nValid service creation, router testroute."); service = service_alloc("MyService", "testroute"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL != service, "New service with valid router must not be null"); ss_info_dassert(0 != service_isvalid(service), "Service must be valid after creation"); ss_info_dassert(0 == strcmp("MyService", service_get_name(service)), "Service must have given name"); @@ -73,26 +73,26 @@ init_test_env(NULL); ss_info_dassert(0 != serviceAddProtocol(service, "testprotocol", "localhost", 9876), "Add Protocol should succeed"); ss_info_dassert(0 != serviceHasProtocol(service, "testprotocol", 9876), "Service should have new protocol as requested"); serviceStartProtocol(service, "testprotocol", 9876); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_dfprintf(stderr, "\t..done\nStarting Service."); result = serviceStart(service); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 != result, "Start should succeed"); serviceStop(service); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(service->state == SERVICE_STATE_STOPPED, "Stop should succeed"); result = serviceStartAll(); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 != result, "Start all should succeed"); ss_dfprintf(stderr, "\t..done\nTiming out a session."); service->conn_timeout = 1; result = serviceStart(service); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(0 != result, "Start should succeed"); serviceStop(service); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(service->state == SERVICE_STATE_STOPPED, "Stop should succeed"); if((dcb = dcb_alloc(DCB_ROLE_REQUEST_HANDLER)) == NULL) diff --git a/server/core/test/testusers.c b/server/core/test/testusers.c index 4385d74eb..fcc933b1a 100644 --- a/server/core/test/testusers.c +++ b/server/core/test/testusers.c @@ -58,39 +58,39 @@ int result, count; ss_dfprintf(stderr, "testusers : Initialise the user table."); users = users_alloc(); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL != users, "Allocating user table should not return NULL.") ss_dfprintf(stderr, "\t..done\nAdd a user"); count = users_add(users, "username", "authorisation"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(1 == count, "Should add one user"); authdata = users_fetch(users, "username"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL != authdata, "Fetch valid user must not return NULL"); ss_info_dassert(0 == strcmp("authorisation", authdata), "User authorisation should be correct"); ss_dfprintf(stderr, "\t..done\nPrint users"); usersPrint(users); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_dfprintf(stderr, "\t..done\nUpdate a user"); count = users_update(users, "username", "newauth"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(1 == count, "Should update just one user"); authdata = users_fetch(users, "username"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(NULL != authdata, "Fetch valid user must not return NULL"); ss_info_dassert(0 == strcmp("newauth", authdata), "User authorisation should be correctly updated"); ss_dfprintf(stderr, "\t..done\nAdd another user"); count = users_add(users, "username2", "authorisation2"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(1 == count, "Should add one user"); ss_dfprintf(stderr, "\t..done\nDelete a user."); count = users_delete(users, "username"); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_info_dassert(1 == count, "Should delete just one user"); ss_dfprintf(stderr, "\t..done\nFree user table."); users_free(users); - skygw_log_sync_all(); + mxs_log_flush_sync(); ss_dfprintf(stderr, "\t..done\n"); return 0; diff --git a/server/modules/filter/dbfwfilter.c b/server/modules/filter/dbfwfilter.c index 0d72f7ccb..37dc5419f 100644 --- a/server/modules/filter/dbfwfilter.c +++ b/server/modules/filter/dbfwfilter.c @@ -2231,7 +2231,7 @@ int main(int argc, char** argv) printf("Failed to parse rule. Read the error log for the reason of the failure.\n"); } - skygw_log_sync_all(); + mxs_log_flush_sync(); return 0; } diff --git a/server/modules/routing/binlog/maxbinlogcheck.c b/server/modules/routing/binlog/maxbinlogcheck.c index 0af03b0b8..086e63e71 100644 --- a/server/modules/routing/binlog/maxbinlogcheck.c +++ b/server/modules/routing/binlog/maxbinlogcheck.c @@ -137,7 +137,7 @@ int main(int argc, char **argv) { LOGIF(LE, (skygw_log_write_flush(LOGFILE_ERROR, "Error: Memory allocation failed for ROUTER_INSTANCE"))); - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); return 1; @@ -161,7 +161,7 @@ int main(int argc, char **argv) { "Failed to open binlog file %s: %s", path, strerror(errno)))); - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); free(inst); @@ -194,12 +194,12 @@ int main(int argc, char **argv) { close(inst->binlog_fd); - skygw_log_sync_all(); + mxs_log_flush_sync(); LOGIF(LM, (skygw_log_write_flush(LOGFILE_MESSAGE, "Check retcode: %i, Binlog Pos = %llu", ret, inst->binlog_position))); - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); free(inst); diff --git a/server/modules/routing/binlog/test/testbinlog.c b/server/modules/routing/binlog/test/testbinlog.c index b034307cf..ee4609517 100644 --- a/server/modules/routing/binlog/test/testbinlog.c +++ b/server/modules/routing/binlog/test/testbinlog.c @@ -127,7 +127,7 @@ int main(int argc, char **argv) { LOGIF(LE, (skygw_log_write_flush(LOGFILE_ERROR, "Error: Memory allocation FAILED for ROUTER_INSTANCE"))); - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); return 1; @@ -578,7 +578,7 @@ int main(int argc, char **argv) { return 1; } - skygw_log_sync_all(); + mxs_log_flush_sync(); mxs_log_finish(); free(inst); diff --git a/utils/skygw_debug.h b/utils/skygw_debug.h index 2fa094ffb..fe99fcd85 100644 --- a/utils/skygw_debug.h +++ b/utils/skygw_debug.h @@ -55,11 +55,11 @@ # define ss_dassert(exp) if(!(exp)){(skygw_log_write(LE,\ "debug assert %s:%d\n", \ (char*)__FILE__, \ - __LINE__));skygw_log_sync_all();assert(exp);} + __LINE__));mxs_log_flush_sync();assert(exp);} #define ss_info_dassert(exp,info) if(!(exp)){(skygw_log_write(LE,\ "debug assert %s:%d %s\n", \ (char*)__FILE__, \ - __LINE__,info));skygw_log_sync_all();assert(exp);} + __LINE__,info));mxs_log_flush_sync();assert(exp);} # define ss_debug(exp) exp # define ss_dfprintf fprintf # define ss_dfflush fflush