473,243 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,243 software developers and data experts.

How to create an "endless" std::stringstream?

I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

Mar 22 '07 #1
7 3571
Ziyan wrote:
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
Try a queue. Have the threads add strings to the queue,
and the reader remove strings from the front of the queue.
Mar 23 '07 #2
Larry Smith wrote:
Ziyan wrote:
>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

Try a queue. Have the threads add strings to the queue,
and the reader remove strings from the front of the queue.
Be sure to protect all access to the queue (read & write)
with a mutex.
Mar 23 '07 #3
Hello,

Ziyan wrote:
I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere.
>
If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
The architecture of the iostreams library suggests to implement another
kind of stream buffer class to support streaming to destinations
different to files and strings, e.g. network sockets. AFAIR such
implementations have been mentioned here and in
comp.lang.c++.moderated. I think the network stream is a typical
example in books about iostreams. I think the book by Langer and Krefft
on C++ iostreams gives a lot of information, although it might be
dated. A lot of useful information on C++ iostreams has been given by
Dietmar Kühl.

If you look at the implementation of the string streams, which are using
a string buffer class, this should give ideas what to do to implement a
streambuf for network sockets. I think under Linux which probably means
g++ and libstdc++ there is already an implementation of a buffer class
available as an extension using a standard C file handle, which might
be immediately usable for network sockets.

Bernd Strieder

Mar 23 '07 #4
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.
I once had a similar situation where I needed to pass information to a
thread. I wound up wrapping a std::queue<std::stringin a class with a
named locked on pushes and pops. Unfortunately, I have since lost that code
or I would post it here. It's not that difficult though.
Mar 23 '07 #5
"Jim Langston" <ta*******@rocketmail.comwrote in message
news:bp*************@newsfe06.lga...
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>>I am writing a C/C++ program that runs in background (Linux).
Therefore, normally no output would be written into standard output.
However, sometimes I want to have debug message collected and sent tho
network to a client so that errors and debug messages can be displayed
simultaneously anywhere. I tried to use a std::stringstream to do the
job. I created the stringstream in main() and pass it as pointer into
a few threads. Those threads have the protential to output debug
message at any time (debug << "debug message" << std::endl;). And
there is a special thread to keep reading from the stringstream
object.

Although, I can read the debug message out once I put it in somewhere
else in my program, but when I reach the end of the stream (no debug
message written between interval), the stream sets its failbit, thus,
no more debug message can be written into the stream.

If anyone could give me some ideas about creating such debug streams,
or creating an endless stringstream, I will be very grateful. Thank
you.

I once had a similar situation where I needed to pass information to a
thread. I wound up wrapping a std::queue<std::stringin a class with a
named locked on pushes and pops. Unfortunately, I have since lost that
code or I would post it here. It's not that difficult though.
Oh you're luckly. I looked a little deeper and found the code I was working
on to test this, never put into production, not fully tested. The following
code is not guaranteed to work correctly or be exactly what you want, and it
may be windows specific which you may need to fix.

#include <string>
#include <queue>
#include <iostream>
#include <vector>
#include <sstream>

#include <conio.h>

#include <windows.h>
#include <process.h>

template<typename T, typename F T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >to;
return to;
}

class ThreadQueue
{
public:

unsigned int ThreadID;

ThreadQueue(unsigned int limit): Shutdown( false ), ThreadID( 0 )
{
Limit = limit; // Only saved for copy and assignment constructors
which aren't used cause can't figure out good way

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);
}

~ThreadQueue()
{
::EnterCriticalSection(&lock);
Shutdown = true;
while ( ! MsgQueue.empty() )
{
delete MsgQueue.front();
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);

::CloseHandle(handles[SemaphoreIndex]);
::DeleteCriticalSection(&lock);
}

bool AddTail(std::string* pmessage)
{
if ( Shutdown )
return false;
bool result;
::EnterCriticalSection(&lock);
MsgQueue.push(pmessage);
result = ::ReleaseSemaphore(handles[SemaphoreIndex], 1, NULL) != 0;
if (!result)
{
// caller can use ::GetLastError to determine what went wrong
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);
return result;
}

std::string* RemoveHead( const unsigned long Milliseconds = 0 ) //
Milliseconds = INFINITE for blocking
{
if ( Shutdown )
return NULL;

std::string* result;

switch (::WaitForMultipleObjects(1, handles, FALSE, Milliseconds))
{
case SemaphoreIndex: // semaphore
::EnterCriticalSection(&lock);
result = MsgQueue.front();
MsgQueue.pop();
::LeaveCriticalSection(&lock);
return result;

case WAIT_TIMEOUT:
return NULL;

default:
throw "Unknown WaitForMultipleObjects value";
}
}

// Terminate sets shutdown and flushes queue
void Terminate()
{
::EnterCriticalSection(&lock);
Shutdown = true;
while ( ! MsgQueue.empty() )
{
delete MsgQueue.front();
MsgQueue.pop();
}
::LeaveCriticalSection(&lock);
}

void ShutItDown()
{
::EnterCriticalSection(&lock);
Shutdown = true;
::LeaveCriticalSection(&lock);
}

bool isShutDown()
{
::EnterCriticalSection(&lock);
bool ReturnStatus = Shutdown;
::LeaveCriticalSection(&lock);

return ReturnStatus;
}

protected:

enum {SemaphoreIndex};
HANDLE handles[1];
CRITICAL_SECTION lock;

std::queue< std::string* MsgQueue;
unsigned int Limit; // Need for copy and assignment constructors

volatile bool Shutdown;

private:

// Copy Constructor - Doesn't work correctly
ThreadQueue(const ThreadQueue& Queue): Shutdown( false ), ThreadID( 0 )
{
Limit = Queue.Limit; // Only saved for copy and assignment
constructors

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
Limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);
}

// Assignment Constructor - Doesn't work correctly
ThreadQueue& operator=(const ThreadQueue& Queue)
{
if (this == &Queue)
return *this;

ThreadID = 0;

Shutdown = false;

Limit = Queue.Limit; // Only saved for copy and assignment
constructors

handles[SemaphoreIndex] = ::CreateSemaphore(NULL, // no security
attributes
0, // initial count
Limit, // max count
NULL); // anonymous

::InitializeCriticalSection(&lock);

return *this;
}

};

unsigned __stdcall Connection(void* a)
{
ThreadQueue* Interface = reinterpret_cast< ThreadQueue* >( a );

std::string* Message;
for ( int i = 0; i < 100; ++i )
{
if ( Interface->isShutDown() )
return 0;

Message = new std::string;
*Message = *Message + StrmConvert<std::string>( i );
if ( ! Interface->AddTail( Message ) )
{
// Should do some error here, but what?
}
}

return 0;
}

struct ThreadInfo
{
unsigned int ThreadID;
HANDLE Handle;
};

int ThreadQueuemain()
{
const static int NumOfQueues = 5000;

std::vector< ThreadInfo Threads;
std::vector< ThreadQueue* Interfaces;

std::cout << "Initializing Threads..." << std::endl;
for ( int i = 0; i < NumOfQueues; ++i )
{
std::vector< ThreadQueue* >::iterator TQit = Interfaces.insert(
Interfaces.end(), new ThreadQueue( 32767 ) );
std::vector< ThreadInfo >::iterator TIit = Threads.insert(
Threads.end(), ThreadInfo() );
(*TIit).Handle = reinterpret_cast<HANDLE>(_beginthreadex(0, 0,
Connection, (void*) *TQit, 0, &(*TIit).ThreadID));
}

std::string* Message = NULL;
while ( ! _kbhit() )
{

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin();
it != Interfaces.end(); ++it )
{
Message = (*it)->RemoveHead();
if ( Message != NULL )
{
std::cout << *Message << " ";
delete Message;
}
}

}

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin(); it !=
Interfaces.end(); ++it )
(*it)->Terminate();

// Wait for all threads to terminate before ending
for ( std::vector< ThreadInfo >::iterator it = Threads.begin(); it !=
Threads.end(); ++it )
{
WaitForSingleObject( (*it).Handle, INFINITE );
CloseHandle( (*it).Handle );
}

for ( std::vector<ThreadQueue*>::iterator it = Interfaces.begin(); it !=
Interfaces.end(); ++it )
delete (*it);

std::cout << "\nPress Return..." << std::flush;

std::string wait;
std::getline( std::cin, wait );

return 0;
}
Mar 23 '07 #6
Thanks guys! I really appreciated it! Thank you!

Mar 24 '07 #7
"Ziyan" <Zi*******@gmail.comwrote in message
news:11**********************@n59g2000hsh.googlegr oups.com...
Thanks guys! I really appreciated it! Thank you!
Incidently, in the code I provided I was using a std::queue<std::string*>.
My feeling was so I wouldnt' have to copy the string. I think if I did it
today, I would probably go with a std::queue<std::stringwhich would
simplify the use quite a bit. It wouldnt' be hard at all to change the code
to use a std::queue<std::stringinstead of current. It's up to you.
Mar 24 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages....
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.