Writing a Log file in c/c++

2019-03-16 02:29发布

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?

标签: c++ file logging
6条回答
萌系小妹纸
2楼-- · 2019-03-16 03:03

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.

查看更多
We Are One
3楼-- · 2019-03-16 03:17

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
查看更多
迷人小祖宗
4楼-- · 2019-03-16 03:18

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?

查看更多
Bombasti
5楼-- · 2019-03-16 03:22

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.

查看更多
一夜七次
6楼-- · 2019-03-16 03:27

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.

查看更多
Juvenile、少年°
7楼-- · 2019-03-16 03:28

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

This library is not an official part of Boost libraries collection although it has passed the review and is provisionally accepted. The review result is available here.

so make of that what you will.

查看更多
登录 后发表回答