/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
#ifndef SKGTRACES_H
#define SKGTRACES_H
/** @file
 * This file defines classes SKGTraces an macros.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skgdefine.h"

#include <QTextStream>
#include <QStringList>
#include <QMap>
#include <QStack>

/**
 * Standard output stream for traces
 */
static QTextStream cout(stdout, QIODevice::WriteOnly);

/**
* Macro for traces
*/
#define SKGTRACE                SKGTRACESUITE << SKGTraces::SKGIndentTrace
/**
* Macro for traces
*/
#define SKGTRACESUITE           cout
/**
* Macro for traces
*/
#define SKGTRACESEPARATOR SKGTRACE << "##############################################\n" << flush;

#ifdef SKGNOTRACES
/**
* Macro for traces
*/
#define IFSKGTRACEL(Level) if(Level!=Level)
#else
/**
* Macro for traces
*/
#define IFSKGTRACEL(Level) if(Level<=SKGTraces::SKGLevelTrace)
#endif

/**
 * Macro for traces
 */
#define SKGTRACEL(Level) SKGTRACESUITEL(Level) << SKGTraces::SKGIndentTrace
/**
 * Macro for traces
 */
#define SKGTRACESUITEL(Level)   IFSKGTRACEL(Level) SKGTRACESUITE

/**
 * This structure represents one performance measure
 */
typedef struct {
    /** The number of call for the methode */
    int NbCall;
    /** The global time passed in the methode */
    double Time;
    /** The time consumed by the methode */
    double TimePropre;
    /** The minimum time passed in the methode */
    double TimeMin;
    /** The maximum time passed in the methode */
    double TimeMax;
} SKGPerfoInfo;

/**
 * A map of strings ==> SKGPerfoInfo
 * @see SKGPerfoMapIterator
 */
typedef QMap<QString, SKGPerfoInfo> SKGPerfoMap;

/**
 * A iterator for SKGPerfoMap ==> SKGPerfoInfo
 * @see SKGPerfoMap
 */
typedef QMap<QString, SKGPerfoInfo>::Iterator SKGPerfoMapIterator;

/**
 * A stack of strings
 */
typedef QStack<QString> SKGQStringStack;

class SKGError;

/**
* This class manages traces
*/
class SKGBASEMODELER_EXPORT SKGTraces
{
public:
    /**
    * Constructor
    * @param iName The message to display
    * @param iLevel The level to display this error.
    * The error will be display if the level of traces asked is greater or equal than
    * the level of this trace (iLevel)
    * @param iRC A pointer of the error object of the calling methode
    *   The SKGError
    */
    SKGTraces(int iLevel, const QString& iName, SKGError* iRC = NULL);

    /**
    * Destructor
    */
    virtual ~SKGTraces();

    /**
     * Clean profiling statistics
     */
    static void cleanProfilingStatistics();

    /**
    * Get profiling statistics
    */
    static QStringList getProfilingStatistics();

    /**
     * Dump profiling statistics
     */
    static void dumpProfilingStatistics();

public:
    /**
     * The current level of indentation
     */
    static QString                      SKGIndentTrace;

    /**
     * The current level of taces
     */
    static int                         SKGLevelTrace;

    /**
     * To enable, disable profiling
     */
    static bool                        SKGPerfo;

private:
    QString                            m_mame;
    bool                               m_output;
    bool                               m_profiling;
    SKGError*                          m_rc;
    double                             m_elapse;
    SKGPerfoMapIterator                m_it;

    static SKGPerfoMap          m_SKGPerfoMethode;
    static SKGQStringStack      m_SKGPerfoPathMethode;
};

#ifdef SKGNOTRACES
/**
* Macro for traces
*/
#define SKGTRACEINRC(Level, Name, RC)

/**
* Macro for traces
*/
#define SKGTRACEIN(Level, Name)
#else
/**
* Macro for traces
*/
#define SKGTRACEINRC(Level, Name, RC) \
    SKGTraces __t1__(Level, Name, &RC);

/**
* Macro for traces
*/
#define SKGTRACEIN(Level, Name) \
    SKGTraces __t2__(Level, Name);
#endif

#ifdef SKGFULLTRACES
/**
* Macro for traces
*/
#define _SKGTRACEINRC(Level, Name, RC) SKGTRACEINRC(Level, Name, RC)
/**
* Macro for traces
*/
#define _SKGTRACEIN(Level, Name)    SKGTRACEIN(Level, Name)
#else
/**
* Macro for traces
*/
#define _SKGTRACEINRC(Level, Name, RC)
/**
* Macro for traces
*/
#define _SKGTRACEIN(Level, Name)
#endif

#endif

