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?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- how do I log requests and responses for debugging
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Well thanks for all the responses...i think the answer that i was looking for was that even the format for a log file in UTF8 just like a txt file so c would have no problem in writing those kind of a file with simple file writing that it provides.
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)
Usage: Logger("This is log message"); Writes a file (or appends existing file)
with content:
Why not use one of the many logging frameworks available, like Apache log4cxx? I would suggest this rather than attempting to roll your own - why re-invent the wheel?
You also might want to consider http://www.logog.org . It's a performance-oriented C++ logging system. However, if that's a little too intense for your project, good old cerr and cout work fine for this.
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:
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:(I've changed to
using namespace std;
there just for conciseness.)You're moving the standard output stream
stdout
(which is used bycout
) to output.txt (in write mode), and you're movingstderr
(which is used bycerr
) to error.txt also in write mode.Hopefully this does the trick.
The sort of thing you're trying to do is too indepth to provide a compelte solution here on this site. What you can do is check out the documentation for the logging library of your choice. In my case, that's
Boost.Log
, a looging library for the Boost C++ libraries the documentation for which can be found here.It's pointed out at the bottom of the page I've just linked to that
so make of that what you will.