I want to write a log file in C++. I am processing certain things and thus I need to maintain a log of the properties of the things that I process so that I could revert back to this log file to see the properties of anything that interests me in particular... Could someone help me in achieving this?
To create a build log file for a managed-code projectOn the menu bar, choose Build > Build Solution. In the Output window, click somewhere in the text. Press Ctrl+S. Visual Studio prompts you for a location to save the build output.
Log messages are written to a file with basic log rotation: when max number of lines or bytes is defined to be other than Inf , then the log file is renamed with a . 1 suffix and a new log file is created. The renaming happens recursively (eg logfile.
The standard method of logging (in my experience) is to use either the stdout or stderr streams. In C++ to use these you would need to include iostream, and use as below:
#include <iostream> int main(int argc, char* argv[]) { using std::cout; using std::cerr; using std::endl; cout << "Output message" << endl; cerr << "Error message" << endl; }
This, however, only achieves printing to those outputs, which usually end up at a terminal. If you want to use these standard stream methods (which are quite readable) to output to a file, then you have to redirect your output somehow. One way of doing this is by using the freopen
function, provided by cstdio. What this does is open a file, and moves a given stream to that file. See here for documentation. An example would be:
#include <iostream> #include <cstdio> int main(int argc, char* argv[]) { using namespace std; freopen( "output.txt", "w", stdout ); freopen( "error.txt", "w", stderr ); cout << "Output message" << endl; cerr << "Error message" << endl; }
(I've changed to using namespace std;
there just for conciseness.)
You're moving the standard output stream stdout
(which is used by cout
) to output.txt (in write mode), and you're moving stderr
(which is used by cerr
) to error.txt also in write mode.
Hopefully this does the trick.
This is quite handy, just plug into e.g. some common header file to be called from anywhere in the program (better approach would be to form a class with these functions)
inline string getCurrentDateTime( string s ){ time_t now = time(0); struct tm tstruct; char buf[80]; tstruct = *localtime(&now); if(s=="now") strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); else if(s=="date") strftime(buf, sizeof(buf), "%Y-%m-%d", &tstruct); return string(buf); }; inline void Logger( string logMsg ){ string filePath = "/somedir/log_"+getCurrentDateTime("date")+".txt"; string now = getCurrentDateTime("now"); ofstream ofs(filePath.c_str(), std::ios_base::out | std::ios_base::app ); ofs << now << '\t' << logMsg << '\n'; ofs.close(); }
Usage: Logger("This is log message"); Writes a file (or appends existing file)
/somedir/log_2017-10-20.txt
with content:
2017-10-20 09:50:59 This is log message
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With