diff --git a/src/interface/server.h b/src/interface/server.h index e4f531e..43b9894 100644 --- a/src/interface/server.h +++ b/src/interface/server.h @@ -40,7 +40,7 @@ namespace interface { virtual ~Server(){} - virtual void shutdown(int exit_status = 0) = 0; + virtual void shutdown(int exit_status = 0, const ss_ &reason="") = 0; virtual bool load_module(const ss_ &module_name, const ss_ &path) = 0; virtual void unload_module(const ss_ &module_name) = 0; diff --git a/src/server/main.cpp b/src/server/main.cpp index 7b2eb27..3bcbadc 100644 --- a/src/server/main.cpp +++ b/src/server/main.cpp @@ -11,6 +11,7 @@ #include #include #include // strerror() +#define MODULE "main" server::Config g_server_config; @@ -117,6 +118,7 @@ int main(int argc, char *argv[]) // Main loop int exit_status = 0; + ss_ shutdown_reason; uint64_t next_tick_us = get_timeofday_us(); uint64_t t_per_tick = 1000 * 100; set_ attempt_bad_fds; @@ -214,10 +216,20 @@ int main(int argc, char *argv[]) state->handle_events(); - if(state->is_shutdown_requested(&exit_status)) + if(state->is_shutdown_requested(&exit_status, &shutdown_reason)) break; } + // Destruct server state here + state.reset(nullptr); + + if(shutdown_reason != ""){ + if(exit_status != 0) + log_w(MODULE, "Shutdown: %s", cs(shutdown_reason)); + else + log_v(MODULE, "Shutdown: %s", cs(shutdown_reason)); + } + return exit_status; } diff --git a/src/server/state.cpp b/src/server/state.cpp index d6bcf81..9f0f27b 100644 --- a/src/server/state.cpp +++ b/src/server/state.cpp @@ -107,6 +107,7 @@ struct CState: public State, public interface::Server bool m_shutdown_requested = false; int m_shutdown_exit_status = 0; + ss_ m_shutdown_reason; up_ m_compiler; ss_ m_modules_path; @@ -150,21 +151,27 @@ struct CState: public State, public interface::Server } } - void shutdown(int exit_status) + void shutdown(int exit_status, const ss_ &reason) { if(m_shutdown_requested && exit_status == 0){ // Only reset these values for exit values indicating failure return; } - log_i(MODULE, "Server shutdown requested; exit_status=%i", exit_status); + log_i(MODULE, "Server shutdown requested; exit_status=%i, reason=\"%s\"", + exit_status, cs(reason)); m_shutdown_requested = true; m_shutdown_exit_status = exit_status; + m_shutdown_reason = reason; } - bool is_shutdown_requested(int *exit_status = nullptr) + bool is_shutdown_requested(int *exit_status = nullptr, ss_ *reason = nullptr) { - if(m_shutdown_requested && exit_status) - *exit_status = m_shutdown_exit_status; + if(m_shutdown_requested){ + if(exit_status) + *exit_status = m_shutdown_exit_status; + if(reason) + *reason = m_shutdown_reason; + } return m_shutdown_requested; } diff --git a/src/server/state.h b/src/server/state.h index a078402..32f1dd0 100644 --- a/src/server/state.h +++ b/src/server/state.h @@ -24,8 +24,9 @@ namespace server struct State { virtual ~State(){} - virtual void shutdown(int exit_status = 0) = 0; - virtual bool is_shutdown_requested(int *exit_status = nullptr) = 0; + virtual void shutdown(int exit_status = 0, const ss_ &reason="") = 0; + virtual bool is_shutdown_requested(int *exit_status = nullptr, + ss_ *reason = nullptr) = 0; virtual bool load_module(const ss_ &module_name, const ss_ &path) = 0; virtual void load_modules(const ss_ &path) = 0; virtual interface::Module* get_module(const ss_ &module_name) = 0;