#How to make std::cout print nothing

33 messages · Page 1 of 1 (latest)

proper spire
#
  #ifndef NDEBUG
      void LOG_INFO(std::ostream& output) {
          output << "\n";
      }
  #else
      void LOG_INFO(std::ostream& output) {
          output << "";
      }
  #endif

Trying to implement a basic logging function in debug mode only, is there any way I can make it so that the second defintion doesn't output anything to the standard output? I've tried setting output to various things but I get compile errors. Looking this up just gives me advice on how to flush the buffer which isn't what I want

severe karmaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

hazy pilot
#

Why not just remove that line?

proper spire
#

Wdym "remove that line"

#

How can you remove lines from std::cout?

hazy pilot
#

Output<<"";

proper spire
#

Ohh, that doesn't work it still outputs it

hazy pilot
#

Make the function body empty

hazy pilot
proper spire
#

because the input to my function is std::cout << "my string"....etc

#

so it will still output which is why i need a way to set that std::cout variable to nothing, or remove the data from the output buffer or something

hazy pilot
#

That is because it is evaluated before it is passed to the function

#

You could make the stream and the output string two separate arguments

proper spire
#

but if i pass it into the function and do what i have in the first definition it only outputs the string once

proper spire
hazy pilot
#

When you call LOG_INFO(cout<<"text") the expression cout<<"text" returns the stream cout which is what is passed into the function

azure thistle
#

Hello, @everyone

severe karmaBOT
proper spire
#

Yes, i need a way to change the state of that stream so that nothing is output to the standard output

azure thistle
#

what's the matter?

#

I can help you

proper spire
hazy pilot
#

You can use std::format or std::stringstream to construct a string to pass to the functiob

azure thistle
#

This technique is often used for logging purposes, where you might want to include or exclude logging statements based on whether the program is being compiled in debug mode (NDEBUG not defined) or release mode (NDEBUG defined). In release mode, you typically want to exclude debugging/logging statements to improve performance and reduce binary size.

proper spire
#

chatGPT lmao

proper spire
hazy pilot
#

If the function body is empty when not debugging the compiler can optimise out the function call

severe karmaBOT
#

@proper spire Has your question been resolved? If so, type !solved :)

#

success James Willson was muted
Remember to provide a reason

#

error User is already muted

proper spire
#

!solved

severe karmaBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity