Writing a log file in c / C ++

I want to write a log file in C ++. I process some things, and so I need to keep a log of the properties of the things that I process so that I can return to this log file to see the properties of everything that interests me. Can someone help me achieve this?

+6
source share
6 answers

Well thanks for all the answers ... I think the answer I was looking for is that even the format for the log file is in UTF8, like the txt file, so c will not have a problem writing this type of file with a simple file record which he provides.

-4
source

The standard logging method (in my experience) is to use stdout or stderr streams. In C ++, to use them you will need to enable iostream and use as shown 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, allows you to print only those outputs that usually end on the terminal. If you want to use these standard stream methods (which are quite readable) for output to a file, you need to redirect your output somehow. One way to do this is to use the freopen function provided by cstdio. This means that the file is open and moves the given stream to this file. See here for documentation. An example is:

 #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 changed to using namespace std; for brevity only.)

You move the standard output stream stdout (which is used by cout ) to output.txt (in write mode), and you move stderr (which is used by cerr ) to error.txt also in write mode.

Hope this does the trick.

+24
source

What you are trying to do is too large to provide a compromise solution here on this site. What you can do is check the documentation for the journal library of your choice. In my case, this is Boost.Log , the looging library for the Boost C ++ library , the documentation for which can be found here .

He indicated at the bottom of the page that I just contacted with this

This library is not an official part of the Boost library collection, although it has been reviewed and temporarily accepted. The result of the review is available here .

do as you want.

+5
source

Why not use one of the many logging frameworks available, like Apache log4cxx ? I would suggest this instead of trying to roll on your own - why reinvent the wheel?

+2
source

It is very convenient, just plug it in, for example. some common header file that should be called from anywhere in the program (the best 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 a log message"); Writes a file (or adds an existing file)

 /somedir/log_2017-10-20.txt 

with content:

 2017-10-20 09:50:59 This is log message 
+2
source

You can also consider http://www.logog.org . It is a performance-oriented C ++ registration system. However, if this is a little too intense for your project, the good old cerr and cout will do this fine.

0
source

Source: https://habr.com/ru/post/897219/


All Articles