Log.cc 2.65 KB
Newer Older
1 2 3 4 5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

Robert Menzel's avatar
Robert Menzel committed
7 8 9 10
#include <ACGL/Utils/Log.hh>

#include <cstdio>
#include <iostream>
11
#include <fstream>
Robert Menzel's avatar
Robert Menzel committed
12 13 14 15 16 17 18
using namespace ACGL::Utils;

CoutLikeStreamBuffer::CoutLikeStreamBuffer() : base_type() {
    mBufferSize    = 0;
    mBufferMaxSize = 256;
    mBuffer        = new char[ mBufferMaxSize+1 ];
    mNewLineIsAboutToStart = true;
19
    mMirrorToFile  = false;
Robert Menzel's avatar
Robert Menzel committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33
}

CoutLikeStreamBuffer::~CoutLikeStreamBuffer() {
    if ( mBufferSize > 0 ) {
        overflow( base_type::traits_type::eof() );
        sync();
    }
    delete[] mBuffer;
}

void CoutLikeStreamBuffer::setPrefix( const std::string &_prefix ) {
    mPrefix = _prefix;
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
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();
}

Robert Menzel's avatar
Robert Menzel committed
57 58 59 60 61
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()))) {

        if (mNewLineIsAboutToStart) {
62
#ifndef __ANDROID__
Robert Menzel's avatar
Robert Menzel committed
63
            std::cout << mPrefix;
64
            mirrorToFile( mPrefix );
65
#endif
Robert Menzel's avatar
Robert Menzel committed
66 67 68 69
            mNewLineIsAboutToStart = false;
        }
        if (mBufferSize > 0) {
            mBuffer[mBufferSize] = (char) 0;
70 71 72
#ifdef __ANDROID__
	    __android_log_print( mAndroidPriority, mPrefix.c_str(), "%s", mBuffer );
#else
Robert Menzel's avatar
Robert Menzel committed
73
            std::cout << mBuffer;
74
            mirrorToFile( mBuffer );
75
#endif
Robert Menzel's avatar
Robert Menzel committed
76 77
            mBufferSize = 0;
        }
78 79 80
    }

    if (!( base_type::traits_type::eq_int_type(ch, base_type::traits_type::eof()))) {
Robert Menzel's avatar
Robert Menzel committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        mBuffer[ mBufferSize++ ] = (char) ch;
    }
    return base_type::traits_type::not_eof( ch );
}

// for each endl:
int CoutLikeStreamBuffer::sync() {
    overflow( base_type::traits_type::eof() );
    std::cout.flush();
    mNewLineIsAboutToStart = true;
    return base_type::sync();
}