views:

75

answers:

4

Hi All, I developed a logger for testing our modules in c++, Win32 console, visual studio(Windows) Logger is running in one thread. While it displays output in console window, thread is getting preempted. Some other module thread is running. So output of other modules is getting mixed with output of Logger module in Console window.

Is there any way to avoid preemption of logger thread, so that entire logger output can be at one place in console output window ?

Writing to file instead of output window is one solution. But as the drive names may be different on different machines, its difficult to hardcode the logger output file path. Even then, we can still write the code for finding the drives available on any machine and write to first drive etc. But tester may not understand where to search for the logger output file.

+3  A: 

Add the thread Id to the logger output, and then use a log viewer that can filter.

DebugView (under windows) allows you to add highlight filters to dynamic logging.

Mitch Wheat
Yes, i can use Log viewer.But while debugging, output will become even more clumsy.So that time, i can't filter out output for each character or word.
bjskishore123
A: 

Preventing Thread preemption is generally dangerous. You can try to temporarily increase the thread priority, but i dont advise it (Dangerous; will not work on multiprocessor, ...).

Other Ways:

  • rewrite all modules to only use your logger for output
  • if other modules only write to cout/stdout: logger should write to cerr/stderr. This will not prevent the intermingled output in the console. But when redirecting the program output to different files, it will.
Markus Kull
+1  A: 

The standard solution is to use a mutex . After formatting, but before starting the output to the console, you lock the mutex. When all output is sent, you unlock the mutex again. If a second thread comes in, its attempt to lock the mutex will cause that thread to be preempted until the first thread is done.

CriticalSections in Windows behave mutex-like and are also usable. They use a slightly different terminology. You don't "lock" them, you "enter" and "leave" a critical section with EnterCriticalSection and LeaveCriticalSection.

MSalters
A: 

I think the best solution is to simply separate the logger output from the rest of your program's output. You mentioned the possibility of writing the logging to a file. If your only hang-up with this solution is coding an appropriate path, then you can choose the output path dynamically:

TCHAR buffer[ MAX_PATH ];

SHGetSpecialFolderPath( NULL, buffer, CSIDL_LOCAL_APPDATA, TRUE );

This will give you the local app data folder for the current user. You can then just append your log file name.

Peter Ruderman
Hi, which header file(s) i have to include ?
bjskishore123
Include "Shlobj.h." You can find the MSDN documentation for this function here: http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx
Peter Ruderman