Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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 lukesmith123   Members   -  Reputation: 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?

Sponsor:

#2 Telastyn   Crossbones+   -  Reputation: 3754

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 lukesmith123   Members   -  Reputation: 153

Like
0Likes
Like

Posted 13 March 2012 - 01:14 PM

Could you explain how I could do that?

#4 fastcall22   Crossbones+   -  Reputation: 5755

Like
1Likes
Like

Posted 13 March 2012 - 01:28 PM

See this link.
QWxsIHRvYXN0LXRvYXN0aW5nIHRvYXN0ZXJzIGNhbiB0b2FzdCB0b2FzdGVkIHRvYXN0LCBhbHRob3Vn aCByZS10b2FzdGluZyB0b2FzdGVkIHRvYXN0IGlzIGdlbmVyYWxseSBub3QgcmVjb21tZW5kZWQgYnkg dGhlIG1hbnVmYWN0dXJlcnMgb2YgdG9hc3QtdG9hc3RpbmcgdG9hc3RlcnMuLi4=

#5 MarlboroKing   Members   -  Reputation: 233

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 lukesmith123   Members   -  Reputation: 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 TheUnbeliever   Members   -  Reputation: 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 lukesmith123   Members   -  Reputation: 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.



PARTNERS