From: James S. <jam...@op...> - 2006-11-01 02:31:12
|
On Tue, 2006-10-31 at 16:22 -0800, Brian Dessent wrote: > James Steward wrote: > > > I guess this may not be quite on topic, but some may have run into this > > before... > > I'd say it's certainly on topic. > > > Recently I wanted to capture debug info from 2 threads of a program > > written for Windows (compiled with MinGW). As the data was transmit > > and receive messages (from the 2 threads respectively) I thought I'd > > just open the same file in each thread and fflush after each fprintf. > > > > The result of this was garbled mess. Often one threads data would all > > be at the head of the file and the other at the tail. The desired > > result of course was messages interleaved as in tx, rx, tx, rx, etc. > > > > So I thought well maybe it's the stream buffering that's affecting it. > > So I turned buffering off and the result was as bad. Then I tried > > opening a file with _open (instead of fopen) to do raw writes to it. > > Also with a '_commit' call after the writes the result was the same as > > before! > > > > What would you expect the results to be if the program was run on a *nix > > like OS? Should what I was trying to achieve have worked? > > > > I have never needed to write threaded programs before, so maybe this is > > my thread naivety showing through? > > Did you compile every object with -mthreads? This is a mingw-specific > gcc flag. I'm not sure it matters in your particular case, but it would > certainly matter if you are using C++ exceptions. I should have been more specific. The app was compiled with VisualWx. After an inspection of the build messages -mthreads is there. > Also, can I suggest a very effective way of handling debug output? > Sprinkle your code with calls to OutputDebugString() (you can even wrap > it in e.g. vsprintf() for formatting ease) and then just run DbgView. I > find this technique extremely powerful. You don't have to bother > opening/closing a file and then reviewing it later, you can just watch > the messages occur in real-time while you run your app. You don't have > to bother with time-stamps, as DbgView adds them for you. It's > thread-safe and very simple. > > http://windowssdk.msdn.microsoft.com/en-us/library/ms680544.aspx > http://www.sysinternals.com/utilities/debugview.html That looks like just what the doctor ordered! Thank you very much. I added it to my code wrapped in a macro, something like; #define DEBUG(format, ...) \ do { \ char str[255]; \ snprintf(str, 254, format, ## __VA_ARGS__); \ OutputDebugString(str); \ } while (0) The result is much like calling syslog(...); and tail -f /var/log/syslog for example. Very handy indeed. Regards, James. |