LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Output in Log file instead of Standard Input Output (https://www.linuxquestions.org/questions/programming-9/output-in-log-file-instead-of-standard-input-output-4175556331/)

srinietrx 10-16-2015 08:21 AM

Output in Log file instead of Standard Input Output
 
I am developing C++ application.As size of the code increases, I feel my printf output needs to go to some file instead of stdio.

Is there is any standard class or I need to write code from scratch?

NevemTeve 10-16-2015 09:06 AM

If it is about error/debug-messages, they could be sent to stderr (cf: fprintf), to separate them from the normal output. stderr can be redirected from shell:
Code:

./myexe 2>messages.log

HMW 10-16-2015 09:33 AM

Or... if you simply want to write to a FILE instead of to stdout,
Code:

#include <fstream>
Sort of like this:
Code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ofstream log;
    log.open("cpp.log", ios::app);
    log << "Log entry here.\n";
    log.close();

    return 0;
}

So after executing the code above (named file_write):
Code:

./file_write
...three times, I get the following content in the file cpp.log:
Code:

cat cpp.log
Log entry here.
Log entry here.
Log entry here.

But maybe that was too simple?

Best regards,
HMW

PS. Haven't used c++ in AGES. Please be gentle! DS.

srinietrx 10-18-2015 09:34 AM

Thanks.I want similar to what HMW has written.


All times are GMT -5. The time now is 10:01 AM.