/*
 * Copyright (c) 1999-2019 Hartmut Seichter
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

#ifndef PW_CORE_LOG_HPP
#define PW_CORE_LOG_HPP

#include <pw/core/globals.hpp>

#include <vector>
#include <functional>

namespace pw {

/**
  * @brief multipurpose logger used internally
  */
class debug {
public:

    enum level {
        none,			//!< nothing will be logged, even no errors
        error,			//!< only errors will be logged
        warning,		//!< log warnings (non-critical errors)
        message,		//!< log messages (something to note but not an error)
        notification,	//!< log some more information
        info,			//!< log verbose information
        all = 0xFF		//!< log absolutely everything
    };

    /**
     * @brief the streaming interface for the logger
     */
    class stream {
    public:


        stream(debug* log = nullptr);
        ~stream();
        stream(const stream& other);


        stream& operator << (const bool &value);
        stream& operator << (const char *value);
        stream& operator << (const std::string& value);		///! log a string

        stream& operator << (const float &value);			///! log a float value
        stream& operator << (const double &value);			///! log a double value

        stream& operator << (const int &value);			///! log a int value
        stream& operator << (const unsigned int &value);	///! log a int value

        stream& operator << (const long &value);			///! log a long value
        stream& operator << (const unsigned long &value);	///! log a int value

        stream& operator << (const void *value);			///! pointer

    protected:

        debug* _log;
        std::string _line;
    };

    /** sets the logging level */
    void set_level(level level) {_level = level;}

    /** gets the logging level */
    level level() const { return _level; }

    /**
      * @brief get the stream interface of the logger
      * @return return a temporary object that will write and flush the logger
      */
    static stream s(enum level level = info);

    inline static stream d() { return s(debug::info); }
    inline static stream e() { return s(debug::error); }
    inline static stream w() { return s(debug::warning); }

    /**
     * @brief returns the instance of the logger
     * @return
     */
    static debug& get();

    /**
     * @brief write a message to the log
     * @param message string
     */
    void write(const std::string &message);

    typedef std::function<void(const char*)> Callback;
    typedef std::vector<Callback> CallbackList;

    ~debug();

protected:

    debug();

    CallbackList _callbacks;
    enum level _level;
};

///**
//  * @brief helper for changing the log level in a scope
//  */
//struct ScopeLogLevel
//{
//    debug::level levelOutside;
//    explicit ScopeLogLevel(debug::level level)
//    {
//        levelOutside = debug::get().level();
//        debug::get().setLevel(level);
//    }
//    ~ScopeLogLevel()
//    {
//        debug::get().setLevel(levelOutside);
//    }
//};

//template <debug::level level>
//struct tpScopeLog {
//    const char* info;
//    explicit tpScopeLog(const char* i) : info(i) {
//        debug::s(level) << info;
//    }

//    ~tpScopeLog() {
//        debug::s(level) << info;
//    }
//};

// some macros

//#define LOG_INFO() Log::s()
//#define LOG_FUNC() LOG_INFO() << __FUNCTION__ << " " << __LINE__ << " "
}


#endif
