   Creating Log File
  I want to start creating a logfile for my games.  I've done a lot of searching and google and I'm just not finding anything that fits my needs.  I was hoping I could get some suggestions.Here is what I need:1.  Simple -  I need it to be pretty straightforward, easy to implement, nothing too complex.2.  crash Proof - Most importantly it needs to be accurate even if the program crashes, most of the Fopen and stdout logs I've tried didn't write to the text file unless the program closed properly.3. Scalable - I'd like to use the logging system to hopefully one day do more complex things like write system specs and hresults.  If it's scalable that's a huge plus.I'm using C++ in VS.net and I've tried using fopen() with an stdout to a text file (with a lot of variations), and writing an open() and then using write().  Anyone know any good code for this? The va structs I've seen but it's a little intimidating for me.  I'm hoping there's an easier method that accomplishes the same thing.   
  I would suggest writing to std::cerr.  It's not buffered, so everything is written out immediately, it's scalable since you can just replace its streambuf with something else at a later time without changing any other code, it's standard, and yu can write custom manipulator to get real fancy-like.It just doesn't get better than that.;Quote:Original post by BregmaI would suggest writing to std::cerr.  It's not buffered, so everything is written out immediately, it's scalable since you can just replace its streambuf with something else at a later time without changing any other code, it's standard, and yu can write custom manipulator to get real fancy-like.It just doesn't get better than that.Entirely agreed. Wrap a class around std::cerr and you'll be fine, it's how most programmers do it, and it's probably the single most reusable piece of code you'll ever write. If you're smart about it, you'll set a few const variables at the top of the file for reusable strings (this allows your project to be rapidly translated, including its log-files), include a good way to delimit special errors (I personally go with the trifecta "WARNING", "ERROR" and "FATAL"), a way to add linebreaks, horizontal rules, etc., and that's about all of the magic that goes into it.Easiest class you'll ever have to write, the most useful code you'll ever write, and the most reusable code you'll ever write, in a nice small package.   ;Quote:Original post by ciroknightEntirely agreed. Wrap a class around std::cerr and you'll be fine...No no no, just use std::cerr directly.  Wrap a class around std::basic_streambuf, and use std::cerr.rdbuf() to replace the existing streambuf.  You don't want to wrap the IOStream itself, since all it does is format your output using the stream insertion operators.  std::cerr just works, right out of the box, and replacing its streambuf makes it work your way, when you're ready, without changing any existing code.  Because it's standard, it improves the reusability of your code.;  Thanks for the help guys.  cerr worked perfectly.  If anyone google's this here's what I used.ofstream LogFile( "Log.txt" );cerr.rdbuf( LogFile.rdbuf( ) );cerr<<LogMessage;where LogMessage is a character buffer you can set at initialization or via sprintf.ie char LogMessage[]="Logging File\n";orchar LogMessage[];sprintf(LogMessage,"Logging File\n");or cerr<<"Logging File\n";Then I just cerr anytime I want to append to my logfile.if(!Success){cerr<<"Function Failed to Load\n";}orif(!Success){sprintf(LogMessage,"Function Failed to Load\n");cerr<<LogMessage;}   ;Quote:Original post by ChurchSkiz2.  crash Proof - Most importantly it needs to be accurate even if the program crashes, most of the Fopen and stdout logs I've tried didn't write to the text file unless the program closed properly.cerr fixes that, but just for your knowledge, using fflush(file) will save the buffer to the file (so you can crash later and not loose the log).As for a simple log library, Ive been using Paul Nettle's excellent log.Its only one file so very easy to plug in, simple (to use) yet feature rich, and its bugless (I believe). Only trouble with it that I havent found documentation for it, so I just explored the macros at the top.Features include hierarchy + indenting and probably more (well I only use the hierarchy and indenting so havent explored more [smile]).you can for example:INDENT("init") .... LOG("graphics") .... LOG("sounds")...  UNDENT("init done!")As a bonus, you can download MMGR from his site too, the excellent memory leakage detection (also just one file to plug in). Paul Nettle's site (freeware section) Iftah.   ;  Wow, I just opened a ofstream, and flushed all the messages...If you want the code just ask, I didn't have any real system to it...   ;  There are just two problems with your usage of std::cerr. First, DO NOT USE SPRINTF. Second, DO NOT USE SPRINTF. Now, I know that that is technically only one thing, but I felt it was such an important one that it was worth mentioning twice.sprintf(some_char_buffer_that_might_be_too_small,"%d%d%d",1,2,3);--OR--std::cerr << 1 << 2 << 3;Which one do you think will never overflow a buffer and is also typesafe?   ;Quote:Original post by BregmaQuote:Original post by ciroknightEntirely agreed. Wrap a class around std::cerr and you'll be fine...No no no, just use std::cerr directly.  Wrap a class around std::basic_streambuf, and use std::cerr.rdbuf() to replace the existing streambuf.  You don't want to wrap the IOStream itself, since all it does is format your output using the stream insertion operators.  std::cerr just works, right out of the box, and replacing its streambuf makes it work your way, when you're ready, without changing any existing code.  Because it's standard, it improves the reusability of your code.But then you have all kinds of strings deep-linked in your code, and that's inheirently harder to translate. With my log class all I have to do is tell it an error number, a notice level and have the program set the language at runtime, and poof, instant language agonstism. The whole system is easier than using a library for string-translation, and since all of the strings are simply in one file, to change out languages, you simply have to change the one file. That means I don't have to distribute source to change out strings, and I can change error messages on the fly.  Of course, if you don't care for language support, you can use the object raw in the way you're explaining, but I rather care for things to take care of themselves than me handhold them. Not to mention that it's not a rocket-science class (even though it does have some neat features like reverse lookup; I can just type the first few words of the phrase in English instead of having to remember an error code, and the class will automatically output the first matching string in the correct language). These are just some of the things I take for granted and really like as a programmer; no library dependency, simplicity, standards compliant and fast.   ;  The simplest way to create a log file is using cplusplus' tutorial on file input output, just use it in a loop.http://www.cplusplus.com/doc/tutorial/files.html   