0

I have a task to make some kind of logger application and integrate it in source code of already existing programm on linux.
There are lots of std::cout in it. So, I need to intercept them, add information like name of function from which it was called and send all of this to log function. There are several threads already and I want to avoid creating another one.

Main question: "how can I bind cout call to log function call whithout changing all source code? I need to get final string from cout and send it to logger."

So I'll be glad to get any information, suggestion, ideas and advices. Thank you.

Ivan
  • 445
  • 4
  • 6
  • i dont understand what threads and mutexes have to do with your question. If the code was thread safe before with printing to `std::cout` then this wont change by writing to a different stream. – 463035818_is_not_an_ai Jan 12 '18 at 11:23
  • One question per question please. As-is it is unclear what the problem is. Please take [the tour](https://stackoverflow.com/tour) and read [the help page](https://stackoverflow.com/help). Welcome to SO. – Ron Jan 12 '18 at 11:28
  • Could be a duplicate of [this](https://stackoverflow.com/questions/4753153/redirecting-stdcout) or [this](https://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files). Or simply google "redirect std::cout" – Jabberwocky Jan 12 '18 at 11:28
  • I mean that I really don't know how it's realized in full programm, safe or not. If I'll just redirect cout to stringstream it can cause problems if several threads write something to it in same time. As I think at least. – Ivan Jan 12 '18 at 11:31
  • You can the `freopen` in the first line of main to direct all stdout to a file. – mahdi_12167 Jan 12 '18 at 13:33
  • If I could I would :) There must be rotation of log files, so I have main log class, which writes & then checks if file is full. Thats why I need to send message to function, not to file directly. – Ivan Jan 12 '18 at 13:48

0 Answers0