base: Improve logging

This commit is contained in:
tt2468 2021-04-28 11:43:39 -07:00
parent e2d837958f
commit b53b40d9c8
3 changed files with 21 additions and 20 deletions

View File

@ -45,29 +45,29 @@ WebSocketServer::~WebSocketServer()
void WebSocketServer::ServerRunner() void WebSocketServer::ServerRunner()
{ {
blog(LOG_INFO, "IO thread started."); blog(LOG_INFO, "[ServerRunner] IO thread started.");
try { try {
_server.run(); _server.run();
} catch (websocketpp::exception const & e) { } catch (websocketpp::exception const & e) {
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what()); blog(LOG_ERROR, "[ServerRunner] websocketpp instance returned an error: %s", e.what());
} catch (const std::exception & e) { } catch (const std::exception & e) {
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what()); blog(LOG_ERROR, "[ServerRunner] websocketpp instance returned an error: %s", e.what());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "websocketpp instance returned an error"); blog(LOG_ERROR, "[ServerRunner] websocketpp instance returned an error");
} }
blog(LOG_INFO, "IO thread exited."); blog(LOG_INFO, "[ServerRunner] IO thread exited.");
} }
void WebSocketServer::Start() void WebSocketServer::Start()
{ {
if (_server.is_listening()) { if (_server.is_listening()) {
blog(LOG_WARNING, "Call to Start() but the server is already listening."); blog(LOG_WARNING, "[Start] Call to Start() but the server is already listening.");
return; return;
} }
auto conf = GetConfig(); auto conf = GetConfig();
if (!conf) { if (!conf) {
blog(LOG_ERROR, "Unable to retreive config!"); blog(LOG_ERROR, "[Start] Unable to retreive config!");
return; return;
} }
@ -82,7 +82,7 @@ void WebSocketServer::Start()
if (errorCode) { if (errorCode) {
std::string errorCodeMessage = errorCode.message(); std::string errorCodeMessage = errorCode.message();
blog(LOG_INFO, "Listen failed: %s", errorCodeMessage.c_str()); blog(LOG_INFO, "[Start] Listen failed: %s", errorCodeMessage.c_str());
return; return;
} }
@ -90,12 +90,13 @@ void WebSocketServer::Start()
_serverThread = std::thread(&WebSocketServer::ServerRunner, this); _serverThread = std::thread(&WebSocketServer::ServerRunner, this);
blog(LOG_INFO, "Server started successfully on port %d", _serverPort); blog(LOG_INFO, "[Start] Server started successfully on port %d", _serverPort);
} }
void WebSocketServer::Stop() void WebSocketServer::Stop()
{ {
if (!_server.is_listening()) { if (!_server.is_listening()) {
blog(LOG_WARNING, "[Stop] Call to Stop() but the server is not listening.");
return; return;
} }
@ -118,12 +119,12 @@ void WebSocketServer::Stop()
_serverThread.join(); _serverThread.join();
blog(LOG_INFO, "Server stopped successfully"); blog(LOG_INFO, "[Stop] Server stopped successfully");
} }
void WebSocketServer::InvalidateSession(websocketpp::connection_hdl hdl) void WebSocketServer::InvalidateSession(websocketpp::connection_hdl hdl)
{ {
blog(LOG_INFO, "Invalidating a session."); blog(LOG_INFO, "[InvalidateSession] Invalidating a session.");
_server.close(hdl, WebSocketCloseCode::SessionInvalidated, "Your session has been invalidated."); _server.close(hdl, WebSocketCloseCode::SessionInvalidated, "Your session has been invalidated.");
} }

View File

@ -36,7 +36,7 @@ void SettingsDialog::showEvent(QShowEvent* event)
{ {
auto conf = GetConfig(); auto conf = GetConfig();
if (!conf) { if (!conf) {
blog(LOG_ERROR, "Unable to retreive config!"); blog(LOG_ERROR, "[showEvent] Unable to retreive config!");
return; return;
} }
@ -62,7 +62,7 @@ void SettingsDialog::FillSessionTable()
{ {
auto webSocketServer = GetWebSocketServer(); auto webSocketServer = GetWebSocketServer();
if (!webSocketServer) { if (!webSocketServer) {
blog(LOG_ERROR, "Unable to fetch websocket server instance!"); blog(LOG_ERROR, "[FillSessionTable] Unable to fetch websocket server instance!");
return; return;
} }
@ -105,7 +105,7 @@ void SettingsDialog::FormAccepted()
{ {
auto conf = GetConfig(); auto conf = GetConfig();
if (!conf) { if (!conf) {
blog(LOG_ERROR, "Unable to retreive config!"); blog(LOG_ERROR, "[FormAccepted] Unable to retreive config!");
return; return;
} }

View File

@ -36,8 +36,8 @@ SettingsDialog *_settingsDialog = nullptr;
bool obs_module_load(void) bool obs_module_load(void)
{ {
blog(LOG_INFO, "you can haz websockets (Version: %s | RPC Version: %d)", OBS_WEBSOCKET_VERSION, OBS_WEBSOCKET_RPC_VERSION); blog(LOG_INFO, "[obs_module_load] you can haz websockets (Version: %s | RPC Version: %d)", OBS_WEBSOCKET_VERSION, OBS_WEBSOCKET_RPC_VERSION);
blog(LOG_INFO, "Qt version (compile-time): %s | Qt version (run-time): %s", blog(LOG_INFO, "[obs_module_load] Qt version (compile-time): %s | Qt version (run-time): %s",
QT_VERSION_STR, qVersion()); QT_VERSION_STR, qVersion());
_config = ConfigPtr(new Config()); _config = ConfigPtr(new Config());
@ -55,7 +55,7 @@ bool obs_module_load(void)
QObject::connect(menuAction, &QAction::triggered, [] { _settingsDialog->ToggleShowHide(); }); QObject::connect(menuAction, &QAction::triggered, [] { _settingsDialog->ToggleShowHide(); });
// Loading finished // Loading finished
blog(LOG_INFO, "Module loaded."); blog(LOG_INFO, "[obs_module_load] Module loaded.");
if (_config->ServerEnabled) if (_config->ServerEnabled)
_webSocketServer->Start(); _webSocketServer->Start();
@ -65,16 +65,16 @@ bool obs_module_load(void)
void obs_module_unload() void obs_module_unload()
{ {
blog(LOG_INFO, "Shutting down..."); blog(LOG_INFO, "[obs_module_unload] Shutting down...");
if (_webSocketServer->IsListening()) { if (_webSocketServer->IsListening()) {
blog(LOG_INFO, "WebSocket server is running. Stopping..."); blog(LOG_INFO, "[obs_module_unload] WebSocket server is running. Stopping...");
_webSocketServer->Stop(); _webSocketServer->Stop();
} }
_config.reset(); _config.reset();
_webSocketServer.reset(); _webSocketServer.reset();
blog(LOG_INFO, "Finished shutting down."); blog(LOG_INFO, "[obs_module_unload] Finished shutting down.");
} }
ConfigPtr GetConfig() ConfigPtr GetConfig()