How to write tango device log in user thread?

Hi, tangoer

If i create a thread in init_device(), but in thread function, how to send log information to device’s log target?

void MyDevice::init_device()
{
  // ...
  thr = new std::thread (my_thread_func); // create a user thread
  // ...
}

void my_thread_func()
{
  while (true) {
    // background process ...

    // In thread function, how to send log message to the device's log?
  }
}

Hi,

You can use the LogAdapter class to achieve what you want.

For instance, you can look at how it is done in TangoTest with the DataGenerator class, which is inheriting from omni_thread and Tango::LogAdapter.

See TangoTest.cpp · main · tango-controls / TangoTest · GitLab

Here is the documentation section mentioning the LogAdapter class: Developing a Tango device server — Tango Controls 10.0.0 documentation

I don’t have an example in mind using std::thread.

Hoping this helps a bit.
Reynald

I think you could do something like :

void my_thread_func(MyDevice_ns::MyDevice * dev)
{
	Tango::LogAdapter my_log_adapter = Tango::LogAdapter(dev);
	my_log_adapter.get_logger()->debug_stream() << "In my_thread_func()" << std::endl;
}
void MyDevice::init_device()
{
  // ...
  thr = new std::thread (my_thread_func,this); // create a user thread
  // ...
}

Thank you Reynald. is there thread-safety problem for tango log?

is there thread-safety problem for tango log?

That should be thread-safe.