Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support multiple activation and deactivation #13

Open
wants to merge 2 commits into
base: indigo-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/rosconsole_bridge/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class OutputHandlerROS : public console_bridge::OutputHandler
virtual void log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line);
};

void activate();
void deactivate();

struct RegisterOutputHandlerProxy
{
RegisterOutputHandlerProxy(void);
Expand Down
56 changes: 49 additions & 7 deletions src/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ OutputHandlerROS::OutputHandlerROS(void) : OutputHandler()
{
}

OutputHandlerROS::~OutputHandlerROS(void) {
console_bridge::restorePreviousOutputHandler();
OutputHandlerROS::~OutputHandlerROS(void){
}

void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel level, const char *filename, int line)
Expand Down Expand Up @@ -121,13 +120,56 @@ void OutputHandlerROS::log(const std::string &text, console_bridge::LogLevel lev
}
}

class OutputHandlerRosManager {
public:
void activate() {
if(!is_activated_){
is_activated_ = true;
ROS_DEBUG("Activating the console_bridge ROS output handler.");
console_bridge::useOutputHandler(&oh_ros_);

// we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
previous_log_level_ = console_bridge::getLogLevel();
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
}
}

void deactivate(){
if(is_activated_){
ROS_DEBUG("Deactivating the console_bridge ROS output handler.");
is_activated_ = false;
console_bridge::restorePreviousOutputHandler();
console_bridge::setLogLevel(previous_log_level_);
}
}

static OutputHandlerRosManager & getInstance() {
static OutputHandlerRosManager ohRosManager;
return ohRosManager;
}
private:
OutputHandlerRosManager() : is_activated_(false), previous_log_level_(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG) {
}
~OutputHandlerRosManager() {
deactivate();
}

bool is_activated_;
console_bridge::LogLevel previous_log_level_;
OutputHandlerROS oh_ros_;
};

void activate() {
OutputHandlerRosManager::getInstance().activate();
}

void deactivate() {
OutputHandlerRosManager::getInstance().deactivate();
}

RegisterOutputHandlerProxy::RegisterOutputHandlerProxy(void)
{
static OutputHandlerROS oh_ros;
console_bridge::useOutputHandler(&oh_ros);

// we want the output level to be decided by rosconsole, so we bring all messages to rosconsole
console_bridge::setLogLevel(console_bridge::CONSOLE_BRIDGE_LOG_DEBUG);
activate();
}

}
17 changes: 3 additions & 14 deletions test/cleanup.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
#include <console_bridge/console.h>
#include <rosconsole_bridge/bridge.h>

struct A {
A(const char* hint) {
logWarn("initializing class: %s", hint);
}
~A() {
logWarn("destroying class");
}
};

// destructor of static instance should use the original output handler
static A a("static");

REGISTER_ROSCONSOLE_BRIDGE;

int main(int argc, char **argv)
{
A a("local");
logWarn("This warning should be delivered through rosconsole");
rosconsole_bridge::deactivate();
logWarn("This warning should be delivered through the original console_bridge output handler");
return 0;
}