Commit 119bc08c authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Create imstkLogUtility and use g3log

parent 74606a3b
......@@ -23,14 +23,16 @@
#include <iostream>
#include "g3log/g3log.hpp"
namespace imstk {
void
Module::start()
{
if (m_status != ModuleStatus::INACTIVE)
{
std::cerr << "Can not start " << m_name << std::endl
<< "Module already/still active." << std::endl;
LOG(WARNING) << "Can not start '" << m_name << "'.\n"
<< "Module already/still active.";
return;
}
......@@ -63,8 +65,8 @@ Module::run()
{
if (m_status != ModuleStatus::PAUSED)
{
std::cerr << "Can not run " << m_name << std::endl
<< "Module not paused." << std::endl;
LOG(WARNING) << "Can not run '" << m_name << "'.\n"
<< "Module not paused.";
return;
}
......@@ -76,8 +78,8 @@ Module::pause()
{
if (m_status != ModuleStatus::RUNNING)
{
std::cerr << "Can not pause " << m_name << std::endl
<< "Module not running." << std::endl;
LOG(WARNING) << "Can not pause '" << m_name << "'.\n"
<< "Module not running.";
return;
}
......@@ -92,8 +94,8 @@ Module::end()
if ((m_status == ModuleStatus::INACTIVE) ||
(m_status == ModuleStatus::TERMINATING))
{
std::cerr << "Can not end " << m_name << std::endl
<< "Module alreading inactive or terminating." << std::endl;
LOG(WARNING) << "Can not end '" << m_name << "'.\n"
<< "Module alreading inactive or terminating.";
return;
}
......
......@@ -23,19 +23,21 @@
#include <thread>
#include "g3log/g3log.hpp"
namespace imstk {
void Scene::initModule()
{
std::cout << m_name << " : init" << std::endl;
LOG(DEBUG) << m_name << " : init";
}
void Scene::cleanUpModule()
{
std::cout << m_name << " : cleanUp" << std::endl;
LOG(DEBUG) << m_name << " : cleanUp";
}
void Scene::runModule()
{
std::cout << m_name << " : running" << std::endl;
LOG(DEBUG) << m_name << " : running";
}
}
......@@ -9,6 +9,7 @@ imstk_add_library( SimulationManager
imstkSimulationManager.cpp
LIBRARIES
Scene
Utilities
)
#-----------------------------------------------------------------------------
......
......@@ -23,6 +23,8 @@
#include <string>
#include "g3log/g3log.hpp"
namespace imstk {
const
SimulationStatus& SimulationManager::getStatus() const
......@@ -41,8 +43,8 @@ SimulationManager::getScene(std::string sceneName)
{
if (!this->isSceneRegistered(sceneName))
{
std::cerr << "No scene named '" << sceneName
<< "' was registered in this simulation." << std::endl;
LOG(WARNING) << "No scene named '" << sceneName
<< "' was registered in this simulation.";
return nullptr;
}
......@@ -54,14 +56,14 @@ SimulationManager::createNewScene(std::string newSceneName)
{
if (this->isSceneRegistered(newSceneName))
{
std::cerr << "Can not create new scene: '" << newSceneName
<< "' is already registered in this simulation." << std::endl
<< "You can create a new scene using an unique name." << std::endl;
LOG(WARNING) << "Can not create new scene: '" << newSceneName
<< "' is already registered in this simulation.\n"
<< "You can create a new scene using an unique name.";
return nullptr;
}
m_sceneMap[newSceneName] = std::make_shared<Scene>(newSceneName);
std::cout << "New scene added: " << newSceneName << std::endl;
LOG(INFO) << "New scene added: " << newSceneName;
return m_sceneMap.at(newSceneName);
}
......@@ -81,14 +83,14 @@ SimulationManager::addScene(std::shared_ptr<Scene>newScene)
if (this->isSceneRegistered(newSceneName))
{
std::cerr << "Can not add scene: '" << newSceneName
<< "' is already registered in this simulation." << std::endl
<< "Set this scene name to a unique name first." << std::endl;
LOG(WARNING) << "Can not add scene: '" << newSceneName
<< "' is already registered in this simulation.\n"
<< "Set this scene name to a unique name first.";
return;
}
m_sceneMap[newSceneName] = newScene;
std::cout << "Scene added: " << newSceneName << std::endl;
LOG(INFO) << "Scene added: " << newSceneName;
}
void
......@@ -96,23 +98,23 @@ SimulationManager::removeScene(std::string sceneName)
{
if (!this->isSceneRegistered(sceneName))
{
std::cerr << "No scene named '" << sceneName
<< "' was registered in this simulation." << std::endl;
LOG(WARNING) << "No scene named '" << sceneName
<< "' was registered in this simulation.";
return;
}
m_sceneMap.erase(sceneName);
std::cout << "Scene removed: " << sceneName << std::endl;
LOG(INFO) << "Scene removed: " << sceneName;
}
void
SimulationManager::startSimulation(std::string sceneName)
{
std::cout << "Starting simulation." << std::endl;
LOG(INFO) << "Starting simulation.";
if (m_status != SimulationStatus::INACTIVE)
{
std::cerr << "Simulation already active." << std::endl;
LOG(WARNING) << "Simulation already active.";
return;
}
......@@ -120,14 +122,14 @@ SimulationManager::startSimulation(std::string sceneName)
if (!startingScene)
{
std::cerr << "Simulation canceled." << std::endl;
LOG(WARNING) << "Simulation canceled.";
return;
}
if (startingScene->getStatus() != ModuleStatus::INACTIVE)
{
std::cerr << "Scene '" << sceneName << "' is already active." << std::endl
<< "Simulation canceled." << std::endl;
LOG(WARNING) << "Scene '" << sceneName << "' is already active.\n"
<< "Simulation canceled.";
return;
}
......@@ -142,18 +144,18 @@ SimulationManager::startSimulation(std::string sceneName)
void
SimulationManager::switchScene(std::string newSceneName, bool unloadCurrentScene)
{
std::cout << "Switching scene." << std::endl;
LOG(INFO) << "Switching scene.";
if ((m_status != SimulationStatus::RUNNING) &&
(m_status != SimulationStatus::PAUSED))
{
std::cerr << "Simulation not active, can not switch scenes." << std::endl;
LOG(WARNING) << "Simulation not active, can not switch scenes.";
return;
}
if (newSceneName == m_currentSceneName)
{
std::cerr << "Scene '" << newSceneName << "' is already running." << std::endl;
LOG(WARNING) << "Scene '" << newSceneName << "' is already running.";
return;
}
......@@ -161,14 +163,14 @@ SimulationManager::switchScene(std::string newSceneName, bool unloadCurrentScene
if (!newScene)
{
std::cerr << "Can not switch scenes." << std::endl;
LOG(WARNING) << "Can not switch scenes.";
return;
}
if (unloadCurrentScene)
{
// Stop current scene
std::cout << "Unloading '" << m_currentSceneName << "'." << std::endl;
LOG(INFO) << "Unloading '" << m_currentSceneName << "'.";
m_sceneMap.at(m_currentSceneName)->end();
m_threadMap.at(m_currentSceneName).join();
}
......@@ -194,11 +196,11 @@ SimulationManager::switchScene(std::string newSceneName, bool unloadCurrentScene
void
SimulationManager::runSimulation()
{
std::cout << "Running simulation." << std::endl;
LOG(INFO) << "Running simulation.";
if (m_status != SimulationStatus::PAUSED)
{
std::cerr << "Simulation not paused, can not run." << std::endl;
LOG(WARNING) << "Simulation not paused, can not run.";
return;
}
......@@ -210,11 +212,11 @@ SimulationManager::runSimulation()
void
SimulationManager::pauseSimulation()
{
std::cout << "Pausing simulation." << std::endl;
LOG(INFO) << "Pausing simulation.";
if (m_status != SimulationStatus::RUNNING)
{
std::cerr << "Simulation not running, can not pause." << std::endl;
LOG(WARNING) << "Simulation not running, can not pause.";
return;
}
......@@ -228,12 +230,12 @@ SimulationManager::pauseSimulation()
void
SimulationManager::endSimulation()
{
std::cout << "Ending simulation." << std::endl;
LOG(INFO) << "Ending simulation.";
if ((m_status != SimulationStatus::RUNNING) &&
(m_status != SimulationStatus::PAUSED))
{
std::cerr << "Simulation already terminated." << std::endl;
LOG(WARNING) << "Simulation already terminated.";
return;
}
......
......@@ -25,8 +25,10 @@
#include <map>
#include <vector>
#include <thread>
#include <memory>
#include <imstkScene.h>
#include "imstkScene.h"
#include "imstkLogUtility.h"
namespace imstk {
using SimulationStatus = ModuleStatus;
......@@ -35,7 +37,12 @@ class SimulationManager
{
public:
SimulationManager() = default;
SimulationManager()
{
// Init g3logger
m_logUtil->createLogger("simulation", "./");
}
~SimulationManager() = default;
const SimulationStatus& getStatus() const;
......@@ -60,10 +67,13 @@ private:
void startModuleInNewThread(std::shared_ptr<Module>module);
SimulationStatus m_status = SimulationStatus::INACTIVE;
std::string m_currentSceneName;
std::map<std::string, std::shared_ptr<Scene> > m_sceneMap;
std::map<std::string, std::thread> m_threadMap;
SimulationStatus m_status = SimulationStatus::INACTIVE;
std::shared_ptr<LogUtility> m_logUtil = std::make_shared<LogUtility>();
};
}
......
#-----------------------------------------------------------------------------
# Create target
#-----------------------------------------------------------------------------
include(imstkAddLibrary)
imstk_add_library( Utilities
H_FILES
imstkLogUtility.h
LIBRARIES
g3log
)
#-----------------------------------------------------------------------------
# Testing
#-----------------------------------------------------------------------------
if( iMSTK_BUILD_TESTING )
add_subdirectory( Testing )
endif()
/*=========================================================================
Library: iMSTK
Copyright (c) Kitware, Inc. & Center for Modeling, Simulation,
& Imaging in Medicine, Rensselaer Polytechnic Institute.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
#ifndef imstkLogUtility_h
#define imstkLogUtility_h
#include <string>
#include <iostream>
#include "g3log/logmessage.hpp"
#include "g3log/logworker.hpp"
namespace imstk {
struct stdSink {
// Linux xterm color
// http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
enum FG_Color { YELLOW = 33, RED = 31, GREEN = 32, WHITE = 97 };
FG_Color GetColor(const LEVELS level) const
{
if (level.value == WARNING.value) return YELLOW;
if (level.value == DEBUG.value) return GREEN;
if (level.value == FATAL.value) return RED;
return WHITE;
}
void ReceiveLogMessage(g3::LogMessageMover logEntry)
{
auto level = logEntry.get()._level;
auto color = GetColor(level);
std::cout << "\033[" << color << "m"
<< logEntry.get().message()
<< "\033[m" << std::endl;
}
};
struct LogUtility {
void createLogger(std::string name, std::string path)
{
m_g3logWorker = g3::LogWorker::createLogWorker();
m_fileSinkHandle = m_g3logWorker->addDefaultLogger(name, path);
m_stdSinkHandle = m_g3logWorker->addSink(
std2::make_unique<stdSink>(), &stdSink::ReceiveLogMessage);
g3::initializeLogging(m_g3logWorker.get());
}
std::unique_ptr<g3::LogWorker> m_g3logWorker;
std::unique_ptr<g3::SinkHandle<g3::FileSink> >m_fileSinkHandle;
std::unique_ptr<g3::SinkHandle<stdSink> > m_stdSinkHandle;
};
}
#endif // ifndef imstkLogUtility_h
......@@ -24,6 +24,7 @@ function(imstk_add_library target)
${target_H_FILES}
${target_CPP_FILES}
)
set_target_properties(${target} PROPERTIES LINKER_LANGUAGE CXX)
#-----------------------------------------------------------------------------
# Link libraries to current target
......
......@@ -100,6 +100,7 @@ include(SetupUncrustifyConfig)
#--------------------------------------------------------------------------
# Add Source code subdirectories
#--------------------------------------------------------------------------
add_subdirectory(Base/Utilities)
add_subdirectory(Base/Core)
add_subdirectory(Base/Scene)
add_subdirectory(Base/SimulationManager)
......
......@@ -15,7 +15,7 @@ int main()
std::shared_ptr<imstk::SimulationManager> sdk =
std::make_shared<imstk::SimulationManager>();
std::cout << "-- Test add scenes" << std::endl;
LOG(INFO) << "-- Test add scenes";
// Create scene and add it (scene1)
std::shared_ptr<imstk::Scene> scene1 =
......@@ -36,7 +36,7 @@ int main()
sdk->removeScene("Scene_3");
// Test switch
std::cout << "-- Test scene switch" << std::endl;
LOG(INFO) << "-- Test scene switch";
sdk->startSimulation("scene1");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->switchScene("scene2", false);
......@@ -46,7 +46,7 @@ int main()
sdk->endSimulation();
// Test pause/run
std::cout << "-- Test simulation pause/run" << std::endl;
LOG(INFO) << "-- Test simulation pause/run";
sdk->startSimulation("scene2");
std::this_thread::sleep_for(std::chrono::seconds(2));
sdk->pauseSimulation();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment