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

Can it use in threads? #14

Open
JaosonMa opened this issue Mar 22, 2021 · 2 comments
Open

Can it use in threads? #14

JaosonMa opened this issue Mar 22, 2021 · 2 comments

Comments

@JaosonMa
Copy link

JaosonMa commented Mar 22, 2021

`
int i[2] = { 0,1 };

string t[2] = { R"(E:\product\qqm\cv_pro\log\t1.log)",R"(E:\product\qqm\cv_pro\log\t2.log)" };

thread t1(&xxxx, i[0], t[0].c_str());

thread t2(&xxxx, i[1], t[1].c_str());

t1.join();

t2.join();

system("pause");

`
i want to use it in thread, as this code
i want log every thread with a single log file ,
can aixlog do it?
this 1.4 version do not work well , create two log files, but only ont have the log info

@miloder
Copy link

miloder commented Apr 23, 2024

I'm also interested in the answer to this, I'm using aixlog to log from different threads. In the console sometimes I get strange behaviour where the two messages from different threads and with different tags get mixed together: message A ends up being displayed with the wrong tag B, and message B never actually makes it to the console.

@LWEfarmdroid
Copy link

Hi,
I struggled with the same problem and was seeing wrong tags when logging from multiple threads. The output would become something like:

2025-02-28 08-45-19.803 [Info] (T) TTT
2025-02-28 08-45-19.803 [Info] (M) MMM
2025-02-28 08-45-19.803 [Info] (M) DDD   <-----
2025-02-28 08-45-19.803 [Info] (L) LLL
2025-02-28 08-45-19.803 [Info] (I) III

The problem can be reproduced by:

  AixLog::Log::init<AixLog::SinkCout>(AixLog::Severity::debug);

  std::vector<std::shared_future<void>> futures;

  for (int n = 0; n < 20; ++n)
  {
    const char x = 'A' + n;
    auto task = std::async(std::launch::async, [this, x]
    {
      int n = 1000;
      while (--n)
      {
        LOG(INFO, std::string{x}) << std::string{x} << std::string{x} << std::string{x} << std::endl;
        std::this_thread::sleep_for(1ms);
      }
    });

    futures.push_back(std::move(task));
  }

  for (auto& future : futures)
  {
    future.get();
  }

It seems to be caused by the fact that metadata_ is not stored per thread id (like the std::stringstream is in buffer_ and therefore the different threads operate on one single metadata_ causing the problem.

One possible solution is to put the std::stringstream into the Metadata struct and then change the std::map to hold Metadata per thread id:

std::map<std::thread::id, Metadata> buffer_;

and then rewrite the get_stream() to something like:

Metadata& get_meta()
{
  const auto id = std::this_thread::get_id();
  return buffer_[id];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants