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:
~CoutLikeStreamBuffer();
void setPrefix( const std::string &_prefix );
void setFilename( const std::string &_filename);
private:
//virtual std::streamsize xsputn(const base_type::char_type* s, std::streamsize n)
......@@ -74,6 +76,7 @@ private:
// for each endl:
virtual int sync();
void mirrorToFile( const std::string &_token );
private:
char *mBuffer;
std::string mPrefix;
......@@ -81,6 +84,8 @@ private:
size_t mBufferMaxSize; // size of the buffer
bool mNewLineIsAboutToStart;
bool mMirrorToFile;
std::string mFilename;
#ifdef __ANDROID__
android_LogPriority mAndroidPriority;
......@@ -138,6 +143,14 @@ public:
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 unmute() { rdbuf( mStreamBuffer ); }
private:
......
......@@ -8,6 +8,7 @@
#include <cstdio>
#include <iostream>
#include <fstream>
using namespace ACGL::Utils;
CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() {
......@@ -15,6 +16,7 @@ CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() {
mBufferMaxSize = 256;
mBuffer = new char[ mBufferMaxSize+1 ];
mNewLineIsAboutToStart = true;
mMirrorToFile = false;
}
CoutLikeStreamBuffer::~CoutLikeStreamBuffer() {
......@@ -29,6 +31,29 @@ void CoutLikeStreamBuffer::setPrefix( const std::string &_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) {
// print buffer
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
if (mNewLineIsAboutToStart) {
#ifndef __ANDROID__
std::cout << mPrefix;
mirrorToFile( mPrefix );
#endif
mNewLineIsAboutToStart = false;
}
......@@ -45,10 +71,13 @@ CoutLikeStreamBuffer::base_type::int_type CoutLikeStreamBuffer::overflow(base_ty
__android_log_print( mAndroidPriority, mPrefix.c_str(), "%s", mBuffer );
#else
std::cout << mBuffer;
mirrorToFile( mBuffer );
#endif
mBufferSize = 0;
}
} else {
}
if (!( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) {
mBuffer[ mBufferSize++ ] = (char) 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