Commit f96584a2 authored by Robert Menzel's avatar Robert Menzel

added simple message mirroring to a file and fixed a bug in long debug outputs

parent 0f4206e9
...@@ -62,6 +62,8 @@ public: ...@@ -62,6 +62,8 @@ public:
~CoutLikeStreamBuffer(); ~CoutLikeStreamBuffer();
void setPrefix( const std::string &_prefix ); void setPrefix( const std::string &_prefix );
void setFilename( const std::string &_filename);
private: private:
//virtual std::streamsize xsputn(const base_type::char_type* s, std::streamsize n) //virtual std::streamsize xsputn(const base_type::char_type* s, std::streamsize n)
...@@ -74,6 +76,7 @@ private: ...@@ -74,6 +76,7 @@ private:
// for each endl: // for each endl:
virtual int sync(); virtual int sync();
void mirrorToFile( const std::string &_token );
private: private:
char *mBuffer; char *mBuffer;
std::string mPrefix; std::string mPrefix;
...@@ -81,6 +84,8 @@ private: ...@@ -81,6 +84,8 @@ private:
size_t mBufferMaxSize; // size of the buffer size_t mBufferMaxSize; // size of the buffer
bool mNewLineIsAboutToStart; bool mNewLineIsAboutToStart;
bool mMirrorToFile;
std::string mFilename;
#ifdef __ANDROID__ #ifdef __ANDROID__
android_LogPriority mAndroidPriority; android_LogPriority mAndroidPriority;
...@@ -138,6 +143,14 @@ public: ...@@ -138,6 +143,14 @@ public:
mStreamBuffer->setPrefix(_prefix); mStreamBuffer->setPrefix(_prefix);
} }
} }
//! a filename of a text file to mirror all outputs into
//! set a filename of "" to disable this
void setFilename( const std::string &_filename ) {
if (mStreamBuffer) {
mStreamBuffer->setFilename(_filename);
}
}
void mute() { rdbuf( NULL ); } void mute() { rdbuf( NULL ); }
void unmute() { rdbuf( mStreamBuffer ); } void unmute() { rdbuf( mStreamBuffer ); }
private: private:
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <cstdio> #include <cstdio>
#include <iostream> #include <iostream>
#include <fstream>
using namespace ACGL::Utils; using namespace ACGL::Utils;
CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() { CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() {
...@@ -15,6 +16,7 @@ CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() { ...@@ -15,6 +16,7 @@ CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() {
mBufferMaxSize = 256; mBufferMaxSize = 256;
mBuffer = new char[ mBufferMaxSize+1 ]; mBuffer = new char[ mBufferMaxSize+1 ];
mNewLineIsAboutToStart = true; mNewLineIsAboutToStart = true;
mMirrorToFile = false;
} }
CoutLikeStreamBuffer::~CoutLikeStreamBuffer() { CoutLikeStreamBuffer::~CoutLikeStreamBuffer() {
...@@ -29,6 +31,29 @@ void CoutLikeStreamBuffer::setPrefix( const std::string &_prefix ) { ...@@ -29,6 +31,29 @@ void CoutLikeStreamBuffer::setPrefix( const std::string &_prefix ) {
mPrefix = _prefix; mPrefix = _prefix;
} }
void CoutLikeStreamBuffer::setFilename( const std::string &_filename ) {
mFilename = _filename;
if (_filename == "") {
mMirrorToFile = false;
} else {
mMirrorToFile = true;
}
}
void CoutLikeStreamBuffer::mirrorToFile( const std::string &_token ) {
if (mMirrorToFile == false) {
return;
}
// opening and closing the file each time is slow but we don't
// loose much in case the app crashes and debugging is the main
// usecase for this function!
std::ofstream file;
file.open( mFilename, std::ios::app );
file << _token;
file.close();
}
CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_type::int_type ch) { CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_type::int_type ch) {
// print buffer // print buffer
if ((mBufferSize >= mBufferMaxSize) || ( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) { if ((mBufferSize >= mBufferMaxSize) || ( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) {
...@@ -36,6 +61,7 @@ CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_ty ...@@ -36,6 +61,7 @@ CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_ty
if (mNewLineIsAboutToStart) { if (mNewLineIsAboutToStart) {
#ifndef __ANDROID__ #ifndef __ANDROID__
std::cout << mPrefix; std::cout << mPrefix;
mirrorToFile( mPrefix );
#endif #endif
mNewLineIsAboutToStart = false; mNewLineIsAboutToStart = false;
} }
...@@ -45,10 +71,13 @@ CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_ty ...@@ -45,10 +71,13 @@ CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_ty
__android_log_print( mAndroidPriority, mPrefix.c_str(), "%s", mBuffer ); __android_log_print( mAndroidPriority, mPrefix.c_str(), "%s", mBuffer );
#else #else
std::cout << mBuffer; std::cout << mBuffer;
mirrorToFile( mBuffer );
#endif #endif
mBufferSize = 0; mBufferSize = 0;
} }
} else { }
if (!( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) {
mBuffer[ mBufferSize++ ] = (char) ch; mBuffer[ mBufferSize++ ] = (char) ch;
} }
return base_type::traits_type::not_eof( ch ); return base_type::traits_type::not_eof( ch );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment