I've painted myself into a corner!

J

JustSomeGuy

I had written a debugging/trace loggin class that l have been using to
test and develope my applications...
At first the application was tested and ran as a single thread, with all
output going to a single file, but now the application has been expanded
to run in multiple threads...

the debug class was instantiated as a global variable outside of main
and all classes used this external variable to dump there output to...
something like 'cerr'

Well this was fine while I was only dealing with a single thread...
However now I need that each thread's output go to a different file...

Assuming for simplicity sake that all the classes had 'cerr' hard coded
into them as the location to dump log
messages to...
What would you do to the 'cerr' class in order for each thread to have
it's own output?

TIA
\
 
P

Phlip

JustSomeGuy said:
I had written a debugging/trace loggin class that l have been using to
test and develope my applications...

Sounds like a great place to start writing unit tests on your code.
At first the application was tested and ran as a single thread, with all
output going to a single file, but now the application has been expanded
to run in multiple threads...

Why multiple threads?
What would you do to the 'cerr' class in order for each thread to have
it's own output?

Get on a newsgroup covering threads for your platform, and ask about "thread
local storage". You can create one file handle per thread.

Alternately, add another thread. All the worker threads push loggable items
into a queue, and the other thread (maybe lower priority) pops items out of
the queue and logs them.

But try to take the threads out first. An event driven architecture is
always better.
 
J

Julie

JustSomeGuy said:
I had written a debugging/trace loggin class that l have been using to
test and develope my applications...
At first the application was tested and ran as a single thread, with all
output going to a single file, but now the application has been expanded
to run in multiple threads...

the debug class was instantiated as a global variable outside of main
and all classes used this external variable to dump there output to...
something like 'cerr'

Well this was fine while I was only dealing with a single thread...
However now I need that each thread's output go to a different file...

Assuming for simplicity sake that all the classes had 'cerr' hard coded
into them as the location to dump log
messages to...
What would you do to the 'cerr' class in order for each thread to have
it's own output?

TIA
\

There is no C++ answer to your question, as there isn't any notion of threads
in the language.

You will need to refer to your operating system documentation to see what
impact it has on your C++ std library and what mechanism are available for
thread-local storage.

<off-topic> If you are running under Windows, look at __declspec(thread).
</off-topic>
 
B

Brandon

What would you do to the 'cerr' class in order for each thread to have
it's own output?

Without knowing just how your class works, it's a little hard to say.
But, however it is that you are getting messages to your logging
class, maybe you could simply add a parameter to specify which file to
write to. For example, if you had one global instance that you tell
to write to a log using a function like this:

gLoggerInstance.writeThisMessage("Error X happened");

and it automatically writes to a particular file, try something like
this instead:

gLoggerInstance.writeThisMessage("Error X happened", "file.txt");

and provide a different file name for messages originating from each
thread. Internally, keep a list where the nodes contain both an
ofstream (or whatever you want to use) and a string containing the
file name. When writeThisMessage() is called, search the list for the
file name. If it is found, write the message to the file. If it is
not, create the file, add it to your list... and write the message of
course.

I don't know if this is really what you were looking for but I hope it
helps.

-Brandon
 
D

Dietmar Kuehl

JustSomeGuy said:
What would you do to the 'cerr' class in order for each thread to have
it's own output?

First of all, "cerr" is not a class: it is an object of type 'std::eek:stream'.
If you hardcoded 'std::cerr' in loads of places and now have to split it
into various files, you can create a new stream buffer (ie. a class derived
from 'std::streambuf') which writes stuff to a location depending no the
thread (assuming you can figure out the current thread and map that to the
location). You would than install this stream buffer into 'std::cerr' using
the 'rdbuf()' method. Of course, you still need to handle synchronization
of mutating accesses to the 'std::cerr' object between the various threads.

I have posted various examples of how to create stream buffers to this
newsgroups and to comp.lang.c++.moderated. Details on accessing thread
attributes aer off-topic in this forum such that you have to go to
platform specific forum for this stuff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top