diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-02-24 22:21:02 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-02-24 22:21:02 +0300 |
commit | 490a93a1d7a347ab586637bf9d8163e114285a02 (patch) | |
tree | cae315ef7502f6e9b4b55bd68bac66d5fb8e33ff /src | |
parent | a5926417dad1f91966ccacfc5572369d309c59be (diff) | |
download | manaverse-490a93a1d7a347ab586637bf9d8163e114285a02.tar.gz manaverse-490a93a1d7a347ab586637bf9d8163e114285a02.tar.bz2 manaverse-490a93a1d7a347ab586637bf9d8163e114285a02.tar.xz manaverse-490a93a1d7a347ab586637bf9d8163e114285a02.zip |
Add safe error string reporting function.
Diffstat (limited to 'src')
-rw-r--r-- | src/client.cpp | 4 | ||||
-rw-r--r-- | src/gui/setup_video.cpp | 2 | ||||
-rw-r--r-- | src/logger.cpp | 30 | ||||
-rw-r--r-- | src/logger.h | 7 | ||||
-rw-r--r-- | src/net/manaserv/network.cpp | 8 | ||||
-rw-r--r-- | src/net/tmwa/network.cpp | 2 |
6 files changed, 44 insertions, 9 deletions
diff --git a/src/client.cpp b/src/client.cpp index 242bb2f21..978c58eef 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -363,7 +363,7 @@ void Client::gameInit() logger->log1("Initializing SDL..."); if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { - logger->error(strprintf("Could not initialize SDL: %s", + logger->safeError(strprintf("Could not initialize SDL: %s", SDL_GetError())); } atexit(SDL_Quit); @@ -555,7 +555,7 @@ void Client::gameInit() if (!mainGraphics->setVideoMode(oldWidth, oldHeight, bpp, oldFullscreen, hwaccel, enableResize, noFrame)) { - logger->error(strprintf("Couldn't restore %dx%dx%d " + logger->safeError(strprintf("Couldn't restore %dx%dx%d " "video mode: %s", oldWidth, oldHeight, bpp, SDL_GetError())); } diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index ae7e7a9cc..eff04614d 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -383,7 +383,7 @@ void Setup_Video::apply() " and restoration of old mode also " "failed!") << std::endl; } - logger->error(errorMsg.str()); + logger->safeError(errorMsg.str()); } } #if defined(WIN32) || defined(__APPLE__) diff --git a/src/logger.cpp b/src/logger.cpp index 8eba8fdb9..3c439136b 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -189,6 +189,34 @@ void Logger::log(const char *log_text, ...) delete [] buf; } +// here string must be safe for any usage +void Logger::safeError(const std::string &error_text) +{ + log("Error: %s", error_text.c_str()); +#ifdef WIN32 + MessageBox(nullptr, error_text.c_str(), "Error", MB_ICONERROR | MB_OK); +#elif defined __APPLE__ +// Str255 msg; +// CFStringRef error; +// error = CFStringCreateWithCString(nullptr, +// error_text.c_str(), +// kCFStringEncodingMacRoman); +// CFStringGetPascalString(error, msg, 255, kCFStringEncodingMacRoman); +// StandardAlert(kAlertStopAlert, +// (const unsigned char*)"\pError", +// (ConstStr255Param) msg, nullptr, nullptr); +#elif defined __linux__ || __linux + std::cerr << "Error: " << error_text << std::endl; + std::string msg = "xmessage \"" + error_text + "\""; + if (system(msg.c_str()) == -1) + std::cerr << "Error: " << error_text << std::endl; +#else + std::cerr << "Error: " << error_text << std::endl; +#endif + exit(1); +} + +// here string can be unsafe strings void Logger::error(const std::string &error_text) { log("Error: %s", error_text.c_str()); @@ -206,7 +234,7 @@ void Logger::error(const std::string &error_text) // (ConstStr255Param) msg, nullptr, nullptr); #elif defined __linux__ || __linux std::cerr << "Error: " << error_text << std::endl; - std::string msg = "xmessage \"Error happand. Please see log file for more information.\""; + std::string msg = "xmessage \"Error happend. Please see log file for more information.\""; if (system(msg.c_str()) == -1) std::cerr << "Error: " << error_text << std::endl; #else diff --git a/src/logger.h b/src/logger.h index db618b275..3dc24420d 100644 --- a/src/logger.h +++ b/src/logger.h @@ -98,6 +98,13 @@ class Logger */ void error(const std::string &error_text) __attribute__ ((noreturn)); + /** + * Log an error and quit. The error will pop-up on Windows and Mac, and + * will be printed to standard error everywhere else. + */ + void safeError(const std::string &error_text) + __attribute__ ((noreturn)); + private: std::ofstream mLogFile; bool mLogToStandardOut; diff --git a/src/net/manaserv/network.cpp b/src/net/manaserv/network.cpp index 84e71eaf3..a75da3643 100644 --- a/src/net/manaserv/network.cpp +++ b/src/net/manaserv/network.cpp @@ -54,7 +54,7 @@ void initialize() { if (enet_initialize()) { - logger->error("Failed to initialize ENet."); + logger->safeError("Failed to initialize ENet."); } #if defined(ENET_VERSION) && ENET_VERSION >= ENET_CUTOFF @@ -65,7 +65,7 @@ void initialize() if (!client) { - logger->error("Failed to create the local host."); + logger->safeError("Failed to create the local host."); } } @@ -76,7 +76,7 @@ void finalize() if (connections) { - logger->error("Tried to shutdown the network subsystem while there " + logger->safeError("Tried to shutdown the network subsystem while there " "are network connections left!"); } @@ -88,7 +88,7 @@ Connection *getConnection() { if (!client) { - logger->error("Tried to instantiate a network object before " + logger->safeError("Tried to instantiate a network object before " "initializing the network subsystem!"); } diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp index 6e9e367c0..b8ba7554e 100644 --- a/src/net/tmwa/network.cpp +++ b/src/net/tmwa/network.cpp @@ -236,7 +236,7 @@ void Network::dispatchMessages() MessageHandlerIterator iter = mMessageHandlers.find(msg.getId()); if (msg.getLength() == 0) - logger->error("Zero length packet received. Exiting."); + logger->safeError("Zero length packet received. Exiting."); if (iter != mMessageHandlers.end()) { |