Jump to content

  • Log In with Google      Sign In   
  • Create Account

Outputting data to a log file


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
7 replies to this topic

#1   Members   

153
Like
0Likes
Like

Posted 13 March 2012 - 12:43 PM

I am currently outputting infomation to a log file by using the freopen_s function with stdout to get all of the output from cout in a text file.

So at various places in the codebase I can use cout to output some text to the file.

I was just wondering if this is an ok method to create a log file or if there is something wrong with doing it like this?

#2   Members   

3773
Like
1Likes
Like

Posted 13 March 2012 - 12:58 PM

It seems awkward. Why not just write to a file, or redirect to file via > when running the app, or using cerr/clog?

#3   Members   

153
Like
0Likes
Like

Posted 13 March 2012 - 01:14 PM

Could you explain how I could do that?

#4   Moderators   

9752
Like
1Likes
Like

Posted 13 March 2012 - 01:28 PM

See this link.
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#5   Members   

252
Like
0Likes
Like

Posted 13 March 2012 - 01:45 PM


void CLog( const char* szMsg, ... ) //Own, custom function

{

    if( g_CLog_bInitialized )

    {

        char szBuffer[512] = { 0 };

        va_list vaArgs;

        FILE* pFile;



        va_start( vaArgs, szMsg );

        _vsnprintf_s( szBuffer, sizeof(szBuffer), szMsg, vaArgs );

        va_end( vaArgs );



        fopen_s( &pFile, "MyLog.txt", "a+" );



        if( pFile )

        {

            fprintf( pFile, "%s", szBuffer );

            fclose( pFile );

        }

    }

}



#6   Members   

153
Like
0Likes
Like

Posted 15 March 2012 - 07:03 AM

Sorry I'm a little confused by all of this. The reason I was redirecting the stdout to a text file was so that I can call it from any class without needing to keep track of a file or buffer.

is freopen a good way of doing this or is there a better way?

#7   Members   

962
Like
1Likes
Like

Posted 15 March 2012 - 08:09 AM

As above, if you just use stdout and friends then the person running your program can decide where they want the output to go by redirecting output as described in the link above. Alternatively, if you make sure to always use e.g. std::clog where appropriate, then you can do something like:

class ScopedStreamRedirect
{
  std::ostream& os;
  std::streambuf* old_rdbuf;

  public:
  ScopedStreamRedirect(std::ostream& os, std::streambuf* rdbuf)
	: os(os), old_rdbuf(os.rdbuf())
  { os.rdbuf(rdbuf); }

  ~ScopedStreamRedirect()
  { os.rdbuf(old_rdbuf); }
};

// ...

int main()
{
  // ...
  std::ofstream log_file("debug.log");
  ScopedStreamRedirect(std::clog, log_file.rdbuf());

  // writes to std::clog now end up in file 'debug.log'
}

These redirections also nest, albeit not concurrently, if that was useful for any reason.
[TheUnbeliever]

#8   Members   

153
Like
0Likes
Like

Posted 15 March 2012 - 01:56 PM

Ah I understand now, thank you very much!




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.