diff --git a/src/ign.cc b/src/ign.cc index 12ef07713..faba5dcc3 100644 --- a/src/ign.cc +++ b/src/ign.cc @@ -33,6 +33,18 @@ char* g_argv[] = reinterpret_cast(const_cast("./ignition")), }; +////////////////////////////////////////////////// +void startConsoleLog() +{ + std::string home; + ignition::common::env(IGN_HOMEDIR, home); + + std::string logPathMod = ignition::common::joinPaths(home, + ".ignition", "gui", "log", + ignition::common::timeToIso(IGN_SYSTEM_TIME())); + ignLogInit(logPathMod, "console.log"); +} + ////////////////////////////////////////////////// extern "C" IGNITION_GUI_VISIBLE char *ignitionVersion() { @@ -42,6 +54,8 @@ extern "C" IGNITION_GUI_VISIBLE char *ignitionVersion() ////////////////////////////////////////////////// extern "C" IGNITION_GUI_VISIBLE void cmdPluginList() { + startConsoleLog(); + ignition::gui::Application app(g_argc, g_argv); auto pluginsList = app.PluginList(); @@ -65,6 +79,8 @@ extern "C" IGNITION_GUI_VISIBLE void cmdPluginList() ////////////////////////////////////////////////// extern "C" IGNITION_GUI_VISIBLE void cmdStandalone(const char *_filename) { + startConsoleLog(); + ignition::gui::Application app(g_argc, g_argv, ignition::gui::WindowType::kDialog); @@ -79,6 +95,8 @@ extern "C" IGNITION_GUI_VISIBLE void cmdStandalone(const char *_filename) ////////////////////////////////////////////////// extern "C" IGNITION_GUI_VISIBLE void cmdConfig(const char *_config) { + startConsoleLog(); + ignition::gui::Application app(g_argc, g_argv); if (!app.findChild()) @@ -103,6 +121,8 @@ extern "C" IGNITION_GUI_VISIBLE void cmdVerbose(const char *_verbosity) ////////////////////////////////////////////////// extern "C" IGNITION_GUI_VISIBLE void cmdEmptyWindow() { + startConsoleLog(); + ignition::gui::Application app(g_argc, g_argv); if (!app.findChild()) diff --git a/src/ign_TEST.cc b/src/ign_TEST.cc index 80a240ac8..cf3ba5ea7 100644 --- a/src/ign_TEST.cc +++ b/src/ign_TEST.cc @@ -21,6 +21,8 @@ #include +#include +#include #include #include "test_config.h" // NOLINT(build/include) @@ -52,10 +54,45 @@ std::string custom_exec_str(std::string _cmd) return result; } +using namespace ignition; + +class CmdLine : public ::testing::Test +{ + // Documentation inherited + protected: void SetUp() override + { + // Change environment variable so that test files aren't written to $HOME + common::env(IGN_HOMEDIR, this->realHome); + EXPECT_TRUE(common::setenv(IGN_HOMEDIR, this->kFakeHome.c_str())); + } + + // Documentation inherited + protected: void TearDown() override + { + // Restore $HOME + EXPECT_TRUE(common::setenv(IGN_HOMEDIR, this->realHome.c_str())); + } + + /// \brief Directory to act as $HOME for tests + public: const std::string kFakeHome = common::joinPaths(PROJECT_BINARY_PATH, + "test", "fake_home"); + + /// \brief Store user's real $HOME to set it back at the end of tests. + public: std::string realHome; +}; + // See https://github.com/ignitionrobotics/ign-gui/issues/75 -TEST(CmdLine, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(list)) +TEST_F(CmdLine, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(list)) { + // Clear home if it exists + common::removeAll(this->kFakeHome); + + EXPECT_FALSE(common::exists(this->kFakeHome)); + std::string output = custom_exec_str("ign gui -l"); EXPECT_NE(output.find("TopicEcho"), std::string::npos) << output; EXPECT_NE(output.find("Publisher"), std::string::npos) << output; + + EXPECT_TRUE(common::exists(common::joinPaths(this->kFakeHome, ".ignition", + "gui"))); } diff --git a/tutorials/02_commandline.md b/tutorials/02_commandline.md index daa19ab4e..978021331 100644 --- a/tutorials/02_commandline.md +++ b/tutorials/02_commandline.md @@ -32,3 +32,6 @@ If you have Ignition Tools installed, you can use the `ign gui` command line too --force-version Use a specific library version. --versions Show the available versions. + +When using the command line tool, all console messages are logged to +`$HOME/.ignition/gui/log/`.