diff --git a/dic/compdic.cpp b/dic/compdic.cpp index 1114fd2..d8233c2 100644 --- a/dic/compdic.cpp +++ b/dic/compdic.cpp @@ -23,7 +23,6 @@ #include #include -#include #include #include #include @@ -65,7 +64,7 @@ #define fmt(a) boost::format(a) -INIT_LOGGER(CompDic::logger, "Compdic"); +INIT_LOGGER(dic, CompDic); CompDic::CompDic() : m_currentRec(0), m_maxRec(0), m_loadTime(0), m_buildTime(0) @@ -186,7 +185,7 @@ void CompDic::writeNode(uint32_t *ioEdges, unsigned int num, ostream &outFile) ioEdges[i] = htonl(ioEdges[i]); } - LOG_TRACE(logger, fmt("writing %1% edges") % num); + LOG_TRACE(fmt("writing %1% edges") % num); outFile.write((char*)ioEdges, num * sizeof(DicEdge)); } diff --git a/dic/compdic.h b/dic/compdic.h index 7016576..6a3c08b 100644 --- a/dic/compdic.h +++ b/dic/compdic.h @@ -42,7 +42,7 @@ using namespace std; class CompDic { - DEFINE_LOGGER(logger); + DEFINE_LOGGER(); typedef boost::unordered_map, unsigned int> HashMap; public: diff --git a/dic/encoding.cpp b/dic/encoding.cpp index 564f96b..ebecfba 100644 --- a/dic/encoding.cpp +++ b/dic/encoding.cpp @@ -20,7 +20,6 @@ #include "config.h" -#include #include #include #include diff --git a/dic/logging.h b/dic/logging.h index d8fb24b..79cfa47 100644 --- a/dic/logging.h +++ b/dic/logging.h @@ -26,25 +26,29 @@ #ifdef USE_LOGGING # include -# define DEFINE_LOGGER(logger) static log4cxx::LoggerPtr logger; -# define INIT_LOGGER(logger, name) log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger(name)) +# define DEFINE_LOGGER() static log4cxx::LoggerPtr logger +# define INIT_LOGGER(prefix, className) log4cxx::LoggerPtr className::logger(log4cxx::Logger::getLogger(#prefix "." #className)) -# define LOG_TRACE(a, b) LOG4CXX_TRACE(a, b) -# define LOG_DEBUG(a, b) LOG4CXX_DEBUG(a, b) -# define LOG_INFO(a, b) LOG4CXX_INFO(a, b) -# define LOG_WARN(a, b) LOG4CXX_WARN(a, b) -# define LOG_ERROR(a, b) LOG4CXX_ERROR(a, b) -# define LOG_FATAL(a, b) LOG4CXX_FATAL(a, b) +# define LOG_TRACE(a) LOG4CXX_TRACE(logger, a) +# define LOG_DEBUG(a) LOG4CXX_DEBUG(logger, a) +# define LOG_INFO(a) LOG4CXX_INFO(logger, a) +# define LOG_WARN(a) LOG4CXX_WARN(logger, a) +# define LOG_ERROR(a) LOG4CXX_ERROR(logger, a) +# define LOG_FATAL(a) LOG4CXX_FATAL(logger, a) +# define LOG_ROOT_ERROR(a) LOG4CXX_ERROR(log4cxx::Logger::getRootLogger(), a) +# define LOG_ROOT_FATAL(a) LOG4CXX_FATAL(log4cxx::Logger::getRootLogger(), a) #else -# define DEFINE_LOGGER(logger) -# define INIT_LOGGER(logger, name) +# define DEFINE_LOGGER() +# define INIT_LOGGER(prefix, name) -# define LOG_TRACE(a, b) -# define LOG_DEBUG(a, b) -# define LOG_INFO(a, b) -# define LOG_WARN(a, b) -# define LOG_ERROR(a, b) -# define LOG_FATAL(a, b) +# define LOG_TRACE(a) +# define LOG_DEBUG(a) +# define LOG_INFO(a) +# define LOG_WARN(a) +# define LOG_ERROR(a) +# define LOG_FATAL(a) +# define LOG_ROOT_ERROR(a) +# define LOG_ROOT_FATAL(a) #endif // USE_LOGGING #endif diff --git a/game/board_search.cpp b/game/board_search.cpp index 7ed4a53..f61adce 100644 --- a/game/board_search.cpp +++ b/game/board_search.cpp @@ -19,9 +19,10 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ -#include "dic.h" +#include // For towupper #include "board_search.h" +#include "dic.h" #include "board.h" #include "tile.h" #include "rack.h" diff --git a/game/board_search.h b/game/board_search.h index 78ae590..62cae92 100644 --- a/game/board_search.h +++ b/game/board_search.h @@ -25,6 +25,7 @@ #include "matrix.h" class Dictionary; +class Tile; class Rack; class Results; class Round; diff --git a/game/duplicate.cpp b/game/duplicate.cpp index 9ffdc0b..7228c67 100644 --- a/game/duplicate.cpp +++ b/game/duplicate.cpp @@ -45,9 +45,13 @@ #include "mark_played_cmd.h" #include "ai_player.h" #include "settings.h" +#include "encoding.h" #include "debug.h" +INIT_LOGGER(game, Duplicate); + + Duplicate::Duplicate(const Dictionary &iDic) : Game(iDic) { @@ -177,6 +181,7 @@ void Duplicate::tryEndTurn() void Duplicate::recordPlayerMove(const Move &iMove, Player &ioPlayer, bool isForHuman) { + LOG_INFO("Player " << ioPlayer.getId() << " plays: " << convertToMb(iMove.toString())); Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove); pCmd->setAutoExecution(!isForHuman); accessNavigation().addAndExecute(pCmd); @@ -278,6 +283,7 @@ void Duplicate::endTurn() void Duplicate::endGame() { + LOG_INFO("End of the game"); m_finished = true; } diff --git a/game/duplicate.h b/game/duplicate.h index 5f8d396..fa3954d 100644 --- a/game/duplicate.h +++ b/game/duplicate.h @@ -23,6 +23,7 @@ #include "game.h" #include "command.h" +#include "logging.h" class Player; @@ -53,6 +54,7 @@ using std::wstring; */ class Duplicate: public Game { + DEFINE_LOGGER(); friend class GameFactory; friend class MarkPlayedCmd; public: diff --git a/game/freegame.cpp b/game/freegame.cpp index 450ecd6..3a51bf5 100644 --- a/game/freegame.cpp +++ b/game/freegame.cpp @@ -40,9 +40,12 @@ #include "ai_player.h" #include "settings.h" #include "turn.h" +#include "encoding.h" #include "debug.h" +INIT_LOGGER(game, FreeGame); + FreeGame::FreeGame(const Dictionary &iDic) : Game(iDic) @@ -113,6 +116,7 @@ void FreeGame::playAI(unsigned int p) void FreeGame::recordPlayerMove(const Move &iMove, Player &ioPlayer, bool isForHuman) { + LOG_INFO("Player " << ioPlayer.getId() << " plays: " << convertToMb(iMove.toString())); Command *pCmd = new PlayerMoveCmd(ioPlayer, iMove); pCmd->setAutoExecution(!isForHuman); accessNavigation().addAndExecute(pCmd); @@ -189,6 +193,8 @@ int FreeGame::endTurn() // Adjust the scores of the players with the points of the remaining tiles void FreeGame::endGame() { + LOG_INFO("End of the game"); + vector tiles; // TODO: According to the rules of the game in the ODS, a game can end in 3 diff --git a/game/freegame.h b/game/freegame.h index d0fa61b..0f4e636 100644 --- a/game/freegame.h +++ b/game/freegame.h @@ -22,6 +22,7 @@ #define FREEGAME_H_ #include "game.h" +#include "logging.h" class Player; @@ -42,6 +43,7 @@ using std::vector; */ class FreeGame: public Game { + DEFINE_LOGGER(); friend class GameFactory; public: virtual GameMode getMode() const { return kFREEGAME; } diff --git a/game/game.cpp b/game/game.cpp index 6617738..c5b4dc1 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -46,6 +46,8 @@ #include "debug.h" +INIT_LOGGER(game, Game); + const unsigned int Game::RACK_SIZE = 7; const int Game::BONUS_POINTS = 50; @@ -77,6 +79,7 @@ const Player& Game::getPlayer(unsigned int iNum) const void Game::shuffleRack() { + LOG_DEBUG("Shuffling rack for player " << currPlayer()); PlayedRack pld = getCurrentPlayer().getCurrentRack(); pld.shuffle(); m_players[currPlayer()]->setCurrentRack(pld); @@ -340,11 +343,10 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, // Get the best word const Round & bestRound = res.get(0); -#ifdef DEBUG - cout << "helperSetRackRandom(): initial rack: " - << convertToMb(pld.toString()) << " (best word: " - << convertToMb(bestRound.getWord()) << ")" << endl; -#endif + LOG_DEBUG("helperSetRackRandom(): initial rack: " + << convertToMb(pld.toString()) << " (best word: " + << convertToMb(bestRound.getWord()) << ")"); + // Identify the joker for (unsigned int i = 0; i < bestRound.getWordLen(); ++i) { @@ -352,10 +354,9 @@ PlayedRack Game::helperSetRackRandom(const PlayedRack &iPld, { const Tile &jokerTile = bestRound.getTile(i); Tile replacingTile(towupper(jokerTile.toChar())); -#ifdef DEBUG - cout << "helperSetRackRandom(): replacing Joker with " - << convertToMb(replacingTile.toChar()) << endl; -#endif + LOG_DEBUG("helperSetRackRandom(): replacing Joker with " + << convertToMb(replacingTile.toChar())); + // If the bag does not contain this letter anymore, // simply keep the joker in the rack. if (bag.in(replacingTile)) @@ -457,6 +458,10 @@ void Game::addPlayer(Player *iPlayer) // The ID of the player is its position in the m_players vector iPlayer->setId(getNPlayers()); m_players.push_back(iPlayer); + + LOG_INFO("Adding player '" << convertToMb(iPlayer->getName()) + << "' (" << (iPlayer->isHuman() ? "human" : "AI") << ")" + << " with ID " << iPlayer->getId()); } diff --git a/game/game.h b/game/game.h index 5fe1683..b0af6c3 100644 --- a/game/game.h +++ b/game/game.h @@ -24,7 +24,7 @@ #include #include -#include +#include "logging.h" #include "bag.h" #include "board.h" #include "history.h" @@ -48,6 +48,7 @@ using namespace std; */ class Game { + DEFINE_LOGGER(); public: /// Game specs. static const unsigned int RACK_SIZE; diff --git a/game/game_factory.cpp b/game/game_factory.cpp index 86854cc..2c9fcb2 100644 --- a/game/game_factory.cpp +++ b/game/game_factory.cpp @@ -45,6 +45,8 @@ #include "xml_reader.h" +INIT_LOGGER(game, GameFactory); + GameFactory *GameFactory::m_factory = NULL; @@ -76,6 +78,7 @@ void GameFactory::Destroy() Training *GameFactory::createTraining(const Dictionary &iDic) { + LOG_INFO("Creating a training game"); Training *game = new Training(iDic); return game; } @@ -83,6 +86,7 @@ Training *GameFactory::createTraining(const Dictionary &iDic) FreeGame *GameFactory::createFreeGame(const Dictionary &iDic) { + LOG_INFO("Creating a free game"); FreeGame *game = new FreeGame(iDic); return game; } @@ -90,6 +94,7 @@ FreeGame *GameFactory::createFreeGame(const Dictionary &iDic) Duplicate *GameFactory::createDuplicate(const Dictionary &iDic) { + LOG_INFO("Creating a duplicate game"); Duplicate *game = new Duplicate(iDic); return game; } diff --git a/game/game_factory.h b/game/game_factory.h index 7946d08..7fea1cb 100644 --- a/game/game_factory.h +++ b/game/game_factory.h @@ -23,6 +23,7 @@ #include #include +#include "logging.h" using std::string; using std::wstring; @@ -44,6 +45,7 @@ class Duplicate; */ class GameFactory { + DEFINE_LOGGER(); public: static GameFactory *Instance(); static void Destroy(); diff --git a/game/game_io.cpp b/game/game_io.cpp index ca3dd3f..58ae0ce 100644 --- a/game/game_io.cpp +++ b/game/game_io.cpp @@ -21,6 +21,7 @@ #include #include // For atoi +#include // For iswlower #include #include "dic.h" diff --git a/game/navigation.cpp b/game/navigation.cpp index b80ab67..c304950 100644 --- a/game/navigation.cpp +++ b/game/navigation.cpp @@ -28,6 +28,8 @@ #include "encoding.h" +INIT_LOGGER(game, Navigation); + Navigation::Navigation() : m_currTurn(0) { @@ -46,6 +48,7 @@ Navigation::~Navigation() void Navigation::newTurn() { + LOG_INFO("New turn"); lastTurn(); m_turnCommands.push_back(new TurnCmd); ++m_currTurn; @@ -98,6 +101,7 @@ void Navigation::prevTurn() { if (m_currTurn > 1) { + LOG_DEBUG("Navigating to the previous turn"); --m_currTurn; m_turnCommands[m_currTurn]->undo(); // Special case: when the last turn is empty, automatically @@ -115,6 +119,7 @@ void Navigation::nextTurn() { if (m_currTurn < m_turnCommands.size()) { + LOG_DEBUG("Navigating to the next turn"); m_turnCommands[m_currTurn]->execute(); ++m_currTurn; // Special case: when the last turn is empty, automatically @@ -130,6 +135,7 @@ void Navigation::nextTurn() void Navigation::firstTurn() { + LOG_DEBUG("Navigating to the first turn"); while (m_currTurn > 1) { prevTurn(); @@ -139,6 +145,7 @@ void Navigation::firstTurn() void Navigation::lastTurn() { + LOG_DEBUG("Navigating to the last turn"); while (m_currTurn < m_turnCommands.size()) { nextTurn(); @@ -148,6 +155,8 @@ void Navigation::lastTurn() void Navigation::clearFuture() { + LOG_DEBUG("Erasing all the future turns"); + // Replay the auto-execution turns // (i.e. turns where only the AI was involved) while (!isLastTurn() && m_turnCommands[m_currTurn]->isAutoExecution()) @@ -180,12 +189,12 @@ const vector & Navigation::getCommands() const void Navigation::print() const { - cout << "=== Commands history ===" << endl; - cout << "Current position right after turn " << m_currTurn - 1 << endl; + LOG_DEBUG("=== Commands history ==="); + LOG_DEBUG("Current position right after turn " << m_currTurn - 1); int index = 0; BOOST_FOREACH(const Command *c, m_turnCommands) { - cout << index << " " << convertToMb(c->toString()) << endl; + LOG_DEBUG(index << " " << convertToMb(c->toString())); ++index; } } diff --git a/game/navigation.h b/game/navigation.h index d4e1f4b..ae026bf 100644 --- a/game/navigation.h +++ b/game/navigation.h @@ -22,6 +22,7 @@ #define NAVIGATION_H_ #include +#include "logging.h" class TurnCmd; class Command; @@ -31,6 +32,7 @@ using namespace std; class Navigation { + DEFINE_LOGGER(); public: Navigation(); ~Navigation(); diff --git a/game/training.cpp b/game/training.cpp index ecb3128..343b69a 100644 --- a/game/training.cpp +++ b/game/training.cpp @@ -20,6 +20,7 @@ *****************************************************************************/ #include +#include // For towupper #if ENABLE_NLS # include @@ -45,6 +46,9 @@ #include "debug.h" +INIT_LOGGER(game, Training); + + Training::Training(const Dictionary &iDic) : Game(iDic), m_results(1000) { @@ -105,6 +109,7 @@ int Training::play(const wstring &iCoord, const wstring &iWord) void Training::recordPlayerMove(const Move &iMove, Player &ioPlayer) { + LOG_INFO("Player " << ioPlayer.getId() << " plays: " << convertToMb(iMove.toString())); // Update the rack and the score of the current player // PlayerMoveCmd::execute() must be called before Game::helperPlayMove() // (called in this class in endTurn()). diff --git a/game/training.h b/game/training.h index 6866d4f..25ddd94 100644 --- a/game/training.h +++ b/game/training.h @@ -27,6 +27,7 @@ #include "game.h" #include "round.h" #include "results.h" +#include "logging.h" class Player; @@ -44,6 +45,7 @@ using std::wstring; */ class Training: public Game { + DEFINE_LOGGER(); friend class GameFactory; public: virtual GameMode getMode() const { return kTRAINING; } diff --git a/game/turn_cmd.cpp b/game/turn_cmd.cpp index 28c56a4..fb05788 100644 --- a/game/turn_cmd.cpp +++ b/game/turn_cmd.cpp @@ -83,10 +83,10 @@ bool TurnCmd::isAutoExecution() const wstring TurnCmd::toString() const { wostringstream oss; - oss << L"TurnCmd:" << endl; + oss << L"TurnCmd:"; BOOST_FOREACH(Command *cmd, m_commands) { - oss << L" " << cmd->toString() << endl; + oss << endl << L" " << cmd->toString(); } return oss.str(); } diff --git a/game/xml_reader.cpp b/game/xml_reader.cpp index 22d2d40..5fdc66b 100644 --- a/game/xml_reader.cpp +++ b/game/xml_reader.cpp @@ -40,12 +40,17 @@ using namespace std; +INIT_LOGGER(game, XmlReader); + Game * XmlReader::read(const string &iFileName, const Dictionary &iDic) { // Try to load the old format first + LOG_INFO("Parsing savegame '" << iFileName << "'"); + try { + LOG_DEBUG("Trying old format"); FILE *fin = fopen(iFileName.c_str(), "r"); if (fin != NULL) { @@ -53,14 +58,20 @@ Game * XmlReader::read(const string &iFileName, const Dictionary &iDic) fclose(fin); if (game != NULL) + { + LOG_INFO("Savegame parsed successfully"); return game; + } } } catch (const GameException &e) { // Ignore the exception + LOG_DEBUG("This doesn't look like the old format"); } + LOG_DEBUG("Trying XML format"); + ifstream is(iFileName.c_str()); if (!is.is_open()) throw LoadGameException("Cannot open file '" + iFileName + "'"); @@ -79,6 +90,8 @@ Game * XmlReader::read(const string &iFileName, const Dictionary &iDic) Game *game = handler.getGame(); if (game == NULL) throw LoadGameException(handler.errorMessage); + + LOG_INFO("Savegame parsed successfully"); return game; } @@ -155,12 +168,8 @@ void XmlReader::startElement(const string& namespaceURI, { (void) namespaceURI; (void) qName; -#if 0 - if (!localName.empty()) - std::cout << "Start Element: " << namespaceURI << ":" << localName << std::endl; - else - std::cout << "Start Element: " << qName << std::endl; -#endif + LOG_DEBUG("Start Element: " << (localName.empty() ? qName : namespaceURI + ":" + localName)); + m_data.clear(); const string &tag = localName; if (tag == "Player") @@ -196,9 +205,8 @@ void XmlReader::endElement(const string& namespaceURI, const string&) { (void) namespaceURI; -#if 0 - std::cout << "endElement: " << namespaceURI << ":" << localName << "(" << m_data << ")" << std::endl; -#endif + LOG_DEBUG("endElement: " << namespaceURI << ":" << localName << "(" << m_data << ")"); + const string &tag = localName; if (tag == "Mode") { @@ -276,16 +284,12 @@ void XmlReader::endElement(const string& namespaceURI, throw LoadGameException("Rack invalid for the current dictionary: " + m_data); } pldrack.setManual(rackStr); -#if 0 - cerr << "loaded rack: " << convertToMb(pldrack.toString()) << endl; -#endif + LOG_DEBUG("loaded rack: " << convertToMb(pldrack.toString())); Player &p = getPlayer(m_players, m_attributes["playerid"]); PlayerRackCmd *cmd = new PlayerRackCmd(p, pldrack); m_game->accessNavigation().addAndExecute(cmd); -#if 0 - cerr << "rack: " << convertToMb(pldrack.toString()) << endl; -#endif + LOG_DEBUG("rack: " << convertToMb(pldrack.toString())); } else if (tag == "PlayerMove") @@ -310,15 +314,14 @@ void XmlReader::endElement(const string& namespaceURI, void XmlReader::characters(const string& ch) { m_data += ch; -#if 0 - std::cout << "Characters: " << ch << std::endl; -#endif + LOG_DEBUG("Characters: " << ch); } void XmlReader::warning(const Arabica::SAX::SAXParseException& exception) { errorMessage = string("warning: ") + exception.what(); + LOG_WARN(errorMessage); //throw LoadGameException(string("warning: ") + exception.what()); } @@ -326,6 +329,7 @@ void XmlReader::warning(const Arabica::SAX::SAXParseException& exception void XmlReader::error(const Arabica::SAX::SAXParseException& exception) { errorMessage = string("error: ") + exception.what(); + LOG_ERROR(errorMessage); //throw LoadGameException(string("error: ") + exception.what()); } @@ -333,6 +337,7 @@ void XmlReader::error(const Arabica::SAX::SAXParseException& exception) void XmlReader::fatalError(const Arabica::SAX::SAXParseException& exception) { errorMessage = string("fatal error: ") + exception.what(); + LOG_FATAL(errorMessage); //throw LoadGameException(string("fatal error: ") + exception.what()); } diff --git a/game/xml_reader.h b/game/xml_reader.h index f0cad1b..90dc66b 100644 --- a/game/xml_reader.h +++ b/game/xml_reader.h @@ -25,6 +25,7 @@ #include #include #include +#include "logging.h" class Dictionary; class Game; @@ -36,6 +37,7 @@ using std::map; class XmlReader : public Arabica::SAX::DefaultHandler { + DEFINE_LOGGER(); public: virtual ~XmlReader() {} diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index 3bbfa3a..b8c9d1c 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -39,6 +39,7 @@ using namespace std; +INIT_LOGGER(game, XmlWriter); static void addIndent(string &s) { @@ -81,6 +82,7 @@ static void writeMove(ostream &out, const Move &iMove, void XmlWriter::write(const Game &iGame, const string &iFileName) { + LOG_INFO("Saving game into '" << iFileName << "'"); ofstream out(iFileName.c_str()); if (!out.is_open()) throw SaveGameException("Cannot open file for writing: '" + iFileName + "'"); diff --git a/game/xml_writer.h b/game/xml_writer.h index b3961cc..5358208 100644 --- a/game/xml_writer.h +++ b/game/xml_writer.h @@ -21,6 +21,7 @@ #define XML_WRITER_H_ #include +#include "logging.h" class Game; @@ -29,6 +30,7 @@ using std::string; class XmlWriter { + DEFINE_LOGGER(); public: static void write(const Game &iGame, const string &iFileName); }; diff --git a/qt/dic_wizard.cpp b/qt/dic_wizard.cpp index 87d8931..c86e28e 100644 --- a/qt/dic_wizard.cpp +++ b/qt/dic_wizard.cpp @@ -18,8 +18,6 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *****************************************************************************/ -#include - #include #include #include diff --git a/qt/main.cpp b/qt/main.cpp index 163f94d..f94744c 100644 --- a/qt/main.cpp +++ b/qt/main.cpp @@ -22,10 +22,10 @@ #include #include -#include #include #include #include +#include "logging.h" #include "base_exception.h" #include "stacktrace.h" #include "main_window.h" @@ -63,8 +63,7 @@ public: } catch (const BaseException &e) { - cerr << "Exception caught: " << e.what() << endl; - cerr << e.getStackTrace() << endl; + LOG_ROOT_ERROR("Exception caught: " << e.what() << "\n" << e.getStackTrace()); return false; } } @@ -86,7 +85,7 @@ int main(int argc, char **argv) /* Check if $LANG is set. */ if (NULL == getenv("LANG")) { - // Retrieve the preferred language as chosen in System Preferences.app + // Retrieve the preferred language as chosen in System Preferences.app // (note that CFLocaleCopyCurrent() is not used because it returns the // preferred locale not language) CFArrayRef all_locales = CFLocaleCopyAvailableLocaleIdentifiers(); @@ -166,9 +165,8 @@ int main(int argc, char **argv) #ifdef HAVE_EXECINFO_H static void bt_sighandler(int signum) { - cerr << "Segmentation fault!" << endl; - cerr << "Backtrace:" << endl; - cerr << StackTrace::GetStack() << endl; + LOG_ROOT_FATAL("Segmentation fault!"); + LOG_ROOT_FATAL("Backtrace:\n" << StackTrace::GetStack()); // Restore the default handler to generate a nice core dump signal(signum, SIG_DFL); diff --git a/qt/main_window.cpp b/qt/main_window.cpp index 18bdc60..1bcf0bd 100644 --- a/qt/main_window.cpp +++ b/qt/main_window.cpp @@ -60,6 +60,8 @@ #include "coord.h" +INIT_LOGGER(qt, MainWindow); + const char *MainWindow::m_windowName = "MainWindow"; MainWindow::MainWindow(QWidget *iParent) @@ -67,6 +69,7 @@ MainWindow::MainWindow(QWidget *iParent) m_prefsDialog(NULL), m_bagWindow(NULL), m_boardWindow(NULL), m_historyWindow(NULL), m_dicToolsWindow(NULL), m_dicNameLabel(NULL) { + LOG_DEBUG("Creating main window"); m_ui.setupUi(this); createMenu(); readSettings(); @@ -79,10 +82,9 @@ MainWindow::MainWindow(QWidget *iParent) // but we would lose the desktop integration... unsigned int val = time(NULL); srand(val); -#ifdef DEBUG + // Make it easier to reproduce bugs - cout << "Rand seed: " << val << endl; -#endif + LOG_DEBUG("Rand seed: " << val); QObject::connect(this, SIGNAL(gameChanged(const PublicGame*)), this, SLOT(updateForGame(const PublicGame*))); @@ -162,6 +164,7 @@ MainWindow::MainWindow(QWidget *iParent) QString dicPath = qs.value(PrefsDialog::kINTF_DIC_PATH, "").toString(); if (dicPath != "") { + LOG_INFO("Using dictionary " << qtl(dicPath)); try { m_dic = new Dictionary(qtl(dicPath)); @@ -193,6 +196,8 @@ void MainWindow::destroyCurrentGame() if (m_game == NULL) return; + LOG_DEBUG("Destroying current game"); + // Some controls, like the board, can live when there is no game. // We only have to give them a NULL handler instead of the current one. emit gameChangedNonConst(NULL); @@ -202,6 +207,8 @@ void MainWindow::destroyCurrentGame() delete m_game; m_game = NULL; + + LOG_DEBUG("Game destroyed"); } @@ -217,8 +224,7 @@ void MainWindow::refresh() m_actionHistoryLastTurn->setEnabled(!isLastTurn); m_actionHistoryReplayTurn->setEnabled(!isLastTurn); #ifdef DEBUG - cout << endl << endl; - m_game->printTurns(); + //m_game->printTurns(); #endif } } @@ -226,6 +232,7 @@ void MainWindow::refresh() void MainWindow::prefsUpdated() { + LOG_DEBUG("Preferences updated"); // Disconnect the training rack updates from the "Plus 1" tab of the // dictionary tools m_playersWidget->disconnect(SIGNAL(trainingRackUpdated(const QString&))); @@ -295,6 +302,7 @@ void MainWindow::updateStatusBar(const Dictionary *iDic) void MainWindow::displayErrorMsg(QString iMsg, QString iContext) { + LOG_ERROR("Displayed error: " << qtl(iMsg)); if (iContext == "") iContext = _q("Eliot - Error"); @@ -304,6 +312,7 @@ void MainWindow::displayErrorMsg(QString iMsg, QString iContext) void MainWindow::displayInfoMsg(QString iMsg) { + LOG_INFO("Displayed message: " << qtl(iMsg)); statusBar()->showMessage(iMsg); } @@ -364,6 +373,8 @@ void MainWindow::changeDictionary(QString iFileName) return; } + LOG_INFO("Loading new dictionary file: " << qtl(iFileName)); + destroyCurrentGame(); try @@ -437,7 +448,7 @@ void MainWindow::createMenu() false, QIcon(":/images/printer.png")); menuFile->addSeparator(); addMenuAction(menuFile, _q("&Quit"), _q("Ctrl+Q"), - _q("Quit Eliot"), SLOT(close()), + _q("Quit Eliot"), SLOT(onGameQuit()), false, QIcon(":/images/quit_16px.png")); QMenu *menuHistory = new QMenu(m_ui.menubar); @@ -505,6 +516,8 @@ void MainWindow::createMenu() void MainWindow::onGameNew() { + LOG_DEBUG("Starting a new game (unconfirmed)"); + if (m_dic == NULL) { displayErrorMsg(_q("You have to select a dictionary (.dawg file) " @@ -538,11 +551,11 @@ void MainWindow::onGameNew() m_ui.groupBoxPlayers->show(); + displayInfoMsg(_q("Game started")); m_game->start(); emit gameChangedNonConst(m_game); emit gameChanged(m_game); emit gameUpdated(); - displayInfoMsg(_q("Game started")); } @@ -600,6 +613,7 @@ void MainWindow::onGameSaveAs() void MainWindow::onGamePrint() { + LOG_DEBUG("Printing game (unconfirmed)"); if (m_game == NULL) return; @@ -607,6 +621,8 @@ void MainWindow::onGamePrint() QPrintDialog printDialog(&printer, this); if (printDialog.exec() == QDialog::Accepted) { + LOG_INFO("Printing game"); + QPainter painter(&printer); const History &history = m_game->getHistory(); @@ -740,10 +756,19 @@ void MainWindow::onGamePrint() // Total score nextHeight += LINE_HEIGHT; painter.drawText(curWidth, nextHeight, QString("%1").arg(score)); + + LOG_INFO("Game printed"); } } +void MainWindow::onGameQuit() +{ + LOG_INFO("Exiting"); + close(); +} + + void MainWindow::onSettingsPreferences() { if (m_prefsDialog == NULL) diff --git a/qt/main_window.h b/qt/main_window.h index 1600a3b..6ffba1a 100644 --- a/qt/main_window.h +++ b/qt/main_window.h @@ -24,6 +24,7 @@ #include #include +#include "logging.h" #include "coord_model.h" @@ -41,6 +42,7 @@ class QAction; class MainWindow: public QMainWindow { + DEFINE_LOGGER(); Q_OBJECT; public: @@ -67,6 +69,7 @@ private slots: void onGameLoad(); void onGameSaveAs(); void onGamePrint(); + void onGameQuit(); void onSettingsChooseDic(); void onSettingsCreateDic(); void onSettingsPreferences(); diff --git a/qt/tile_layout.cpp b/qt/tile_layout.cpp index 7291fea..454d73a 100644 --- a/qt/tile_layout.cpp +++ b/qt/tile_layout.cpp @@ -19,8 +19,6 @@ *****************************************************************************/ #include -// XXX: tmp -#include #include "tile_layout.h" #include "tile_widget.h" diff --git a/utils/ncurses.cpp b/utils/ncurses.cpp index 8b3c1f9..26199fd 100644 --- a/utils/ncurses.cpp +++ b/utils/ncurses.cpp @@ -33,6 +33,7 @@ #include #include // For strlen +#include // For iswalnum #include #include "ncurses.h"