WIP: somethings wrong.. #19

Closed
muellerr wants to merge 808 commits from source/master into master
4 changed files with 69 additions and 37 deletions
Showing only changes of commit 925a54dec9 - Show all commits

View File

@ -8,10 +8,11 @@ extern "C" void printChar(const char*, bool errStream);
#ifndef UT699 #ifndef UT699
ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage, ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
bool errStream, bool addCrToPreamble, uint16_t port): bool addCrToPreamble, bool buffered , bool errStream, uint16_t port):
isActive(true), logMessage(setMessage), isActive(true), logMessage(setMessage),
addCrToPreamble(addCrToPreamble), errStream(errStream) { addCrToPreamble(addCrToPreamble), buffered(buffered),
if(not errStream) { errStream(errStream) {
if(buffered) {
// Set pointers if the stream is buffered. // Set pointers if the stream is buffered.
setp( buf, buf + BUF_SIZE ); setp( buf, buf + BUF_SIZE );
} }
@ -26,16 +27,27 @@ void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
memcpy(array, begin, length); memcpy(array, begin, length);
for(; begin != end; begin++){ for(; begin != end; begin++){
printChar(begin, false); if(errStream) {
printChar(begin, true);
}
else {
printChar(begin, false);
}
} }
} }
#endif #endif
int ServiceInterfaceBuffer::overflow(int c) { int ServiceInterfaceBuffer::overflow(int c) {
if(errStream and this->isActive) { if(not buffered and this->isActive) {
if (c != Traits::eof()) { if (c != Traits::eof()) {
printChar(reinterpret_cast<const char*>(&c), true); if(errStream) {
printChar(reinterpret_cast<const char*>(&c), true);
}
else {
printChar(reinterpret_cast<const char*>(&c), false);
}
} }
return 0; return 0;
} }
@ -53,8 +65,8 @@ int ServiceInterfaceBuffer::overflow(int c) {
} }
int ServiceInterfaceBuffer::sync(void) { int ServiceInterfaceBuffer::sync(void) {
if(not this->isActive or errStream) { if(not this->isActive) {
if(not errStream) { if(not buffered) {
setp(buf, buf + BUF_SIZE - 1); setp(buf, buf + BUF_SIZE - 1);
} }
return 0; return 0;

View File

@ -19,8 +19,8 @@ class ServiceInterfaceBuffer:
public std::streambuf { public std::streambuf {
friend class ServiceInterfaceStream; friend class ServiceInterfaceStream;
public: public:
ServiceInterfaceBuffer(std::string setMessage, bool errStream, ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
bool addCrToPreamble, uint16_t port); bool buffered, bool errStream, uint16_t port);
protected: protected:
bool isActive; bool isActive;
@ -41,6 +41,7 @@ private:
//! This is useful for some terminal programs which do not have //! This is useful for some terminal programs which do not have
//! implicit carriage return with newline characters. //! implicit carriage return with newline characters.
bool addCrToPreamble; bool addCrToPreamble;
bool buffered;
//! This specifies to print to stderr and work in unbuffered mode. //! This specifies to print to stderr and work in unbuffered mode.
bool errStream; bool errStream;

View File

@ -1,9 +1,9 @@
#include <framework/serviceinterface/ServiceInterfaceStream.h> #include <framework/serviceinterface/ServiceInterfaceStream.h>
ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream::ServiceInterfaceStream(std::string setMessage,
bool errStream, bool addCrToPreamble, uint16_t port) : bool addCrToPreamble, bool buffered, bool errStream, uint16_t port) :
std::ostream(&buf), std::ostream(&buf),
buf(setMessage, errStream, addCrToPreamble, port) { buf(setMessage, addCrToPreamble, buffered, errStream, port) {
} }
void ServiceInterfaceStream::setActive( bool myActive) { void ServiceInterfaceStream::setActive( bool myActive) {
@ -13,3 +13,21 @@ void ServiceInterfaceStream::setActive( bool myActive) {
std::string ServiceInterfaceStream::getPreamble() { std::string ServiceInterfaceStream::getPreamble() {
return buf.getPreamble(); return buf.getPreamble();
} }
void ServiceInterfaceStream::print(std::string error,
bool withPreamble, bool withNewline, bool flush) {
if(not buffered and withPreamble) {
*this << getPreamble() << error;
}
else {
*this << error;
}
if(withNewline) {
*this << "\n";
}
// if mode is non-buffered, no need to flush.
if(flush and buffered) {
this->flush();
}
}

View File

@ -5,19 +5,11 @@
#include <iostream> #include <iostream>
#include <cstdio> #include <cstdio>
/* Unfortunately, there must be a forward declaration of the log front end /**
* (MUST be defined in main), to let the system know where to write to. * Generic service interface stream which can be used like std::cout or
* The ServiceInterfaceStream instances, which are declared below and * std::cerr but has additional capability. Add preamble and timestamp
* can instantaited somewhere else can be passed to these front ends by passing * to output. Can be run in buffered or unbuffered mode.
* their underlying buffers with .rdbuf() */ */
namespace sif {
extern std::ostream debug;
extern std::ostream info;
extern std::ostream warning;
extern std::ostream error;
}
class ServiceInterfaceStream : class ServiceInterfaceStream :
public std::ostream { public std::ostream {
protected: protected:
@ -27,14 +19,14 @@ public:
* This constructor is used by specifying the preamble message. * This constructor is used by specifying the preamble message.
* Optionally, the output can be directed to stderr and a CR character * Optionally, the output can be directed to stderr and a CR character
* can be prepended to the preamble. * can be prepended to the preamble.
* @param set_message * @param setMessage message of preamble.
* @param errStream * @param addCrToPreamble Useful for applications like Puttty.
* @param addCrToPreamble * @param buffered specify whether to use buffered mode.
* @param port * @param errStream specify which output stream to use (stderr or stdout).
*/ */
ServiceInterfaceStream(std::string setMessage, ServiceInterfaceStream(std::string setMessage,
bool errStream = false, bool addCrToPreamble = false, bool addCrToPreamble = false, bool buffered = true,
uint16_t port = 1234); bool errStream = false, uint16_t port = 1234);
//! An inactive stream will not print anything. //! An inactive stream will not print anything.
void setActive( bool ); void setActive( bool );
@ -45,15 +37,24 @@ public:
* @return Preamle consisting of log message and timestamp. * @return Preamle consisting of log message and timestamp.
*/ */
std::string getPreamble(); std::string getPreamble();
/**
* This prints an error with a preamble. Useful if using the unbuffered
* mode. Flushes in default mode (prints immediately).
*/
void print(std::string error, bool withPreamble = true,
bool withNewline = true, bool flush = true);
bool buffered = false;
}; };
// Forward declaration of interface streams. These are needed so the public // Forward declaration of interface streams. These should be instantiated in
// functions can be used by including this header // main. They can then be used like std::cout or std::cerr.
namespace sif { namespace sif {
extern ServiceInterfaceStream debugStream; extern ServiceInterfaceStream debug;
extern ServiceInterfaceStream infoStream; extern ServiceInterfaceStream info;
extern ServiceInterfaceStream warningStream; extern ServiceInterfaceStream warning;
extern ServiceInterfaceStream errorStream; extern ServiceInterfaceStream error;
} }
#endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */ #endif /* FRAMEWORK_SERVICEINTERFACE_SERVICEINTERFACESTREAM_H_ */