Une nouvelle version du portail de gestion des comptes externes sera mise en production lundi 09 août. Elle permettra d'allonger la validité d'un compte externe jusqu'à 3 ans. Pour plus de détails sur cette version consulter : https://doc-si.inria.fr/x/FCeS

FDebug.hpp 4.08 KB
Newer Older
1 2
#ifndef FDEBUG_HPP
#define FDEBUG_HPP
3
// [--License--]
4 5

#include "FGlobal.hpp"
6
#include "FNoCopyable.hpp"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

#ifndef SCALFMM_USE_DEBUG

#define FDEBUG( X )

#else

#define FDEBUG( X ) X

#include <iostream>
#include <fstream>
#include <sstream>

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FDebug
* Please read the license
*
* This class is used to print debug data durint processing.
* You have to use the DEBUG macro as shown in the example.
*
* <code>
* FDEBUG( FDebug::Controller.writeFromLine("hello World", __LINE__, __FILE__); ) <br>
* FDEBUG( FDebug::Controller << "I want to debug " << toto << "\n"; )
* </code>
*/
33
class FDebug : public FNoCopyable {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
private:
	std::ostream* stream;	//< Standart c++ ostream

	/** Default constructor forbiden */
        FDebug() : stream(&std::cout) {
	}

	/** Default destructor forbiden */
	virtual ~FDebug(){
		close();
	}

	/**
	* Close the current debug stream
	* dealloc the stream if differents from cout.
	* after this call stream is useless
	*/
	void close(){
                flush();
                if(this->stream != &std::cout) delete(this->stream);
	}

public:
	static FDebug Controller; 	//< Singleton

	/**
	* To set the debug stream to write into a file
	* @param filename the file to write
	*/
	void writeToFile(const char* const filename){
		close();

		std::ofstream* const file = new std::ofstream();
		file->open(filename);

                this->stream = file;
	}

	/**
	* To set the debug stream to write to std::cout
	*/
	void writeToCout(){
		close();
                this->stream = &std::cout;
	}

	/**
	* stream operator to print debug data
	* @param inMessage a message - from any type - to print
	* @return current FDebug
	*/
	template <class T>
	FDebug& operator<<(const T& inMessage){
                return write(inMessage);
	}

	/**
	* to write debug data
	* @param inMessage a message - from any type - to print
	* @return current FDebug
	*/
	template <class T>
	FDebug& write(const T& inMessage){
                (*this->stream) << inMessage;
		return *this;
	}

        /** Flush data into stream */
        void flush(){
            this->stream->flush();
        }

        enum FlushType{
            Flush,
            FlushWithLine
        };

        /**
        * stream operator to flush debug data
        * @param inType flush type
        * @return current FDebug
        */
        FDebug& write(const FlushType inType){
            if(inType == FlushWithLine) (*this->stream) << '\n';
            flush();
            return *this;
        }

	/**
	* to write debug data with line & file
	* @param inMessage a message - from any type - to print
	* @param inLinePosition line number
	* @param inFilePosition file name
	* @return current FDebug
	*
        * <code> FDebug::Controller.writeFromLine("hello World", __LINE__, __FILE__); </code>
	*
	* To prevent use from multiple thread we use a ostringstream before printing
	*/
	template <class T, class Tline, class Tfile>
	FDebug& writeFromLine(const T& inMessage, const Tline& inLinePosition, const Tfile& inFilePosition){
		std::ostringstream oss;
		oss << "Message from " << inFilePosition << " (at line " << inLinePosition <<")\n";
		oss << ">> " << inMessage << "\n";

                (*this->stream) << oss.str();
		return *this;
	}

	/**
	* to write debug data with line & file
	* @param inVariable variable name
	* @param inValue variable value
	* @param inLinePosition line number
	* @param inFilePosition file name
	* @return current FDebug
	*
        * <code> FDebug::Controller.writeVariableFromLine( "toto", toto, __LINE__, __FILE__); </code>
	*
	* To prevent use from multiple thread we use a ostringstream before printing
	*/
	template <class T, class Tline, class Tfile>
	FDebug& writeVariableFromLine(const char* const inVariable, const T& inValue, const Tline& inLinePosition, const Tfile& inFilePosition){
		std::ostringstream oss;
		oss << "[Value] " << inVariable << " = " << inValue << " at line " << inLinePosition <<" (file " << inFilePosition << ")\n";

                (*this->stream) << oss.str();
		return *this;
	}

};

#endif //SCALFMM_USE_DEBUG

#endif //FDEBUG_HPP

170
// [--END--]