/*
 * Copyright (C) 1999-2017 Hartmut Seichter
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef PW_CORE_LOG_HPP_
#define PW_CORE_LOG_HPP_

#include <pw/core/globals.hpp>

#include <vector>
#include <functional>

namespace pw {

class Log;

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


    LogStream(Log* log = nullptr);
    ~LogStream();
    LogStream(const LogStream& other);


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

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

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

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

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

protected:

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

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

    enum LogLevel {
        kNone,		//!< nothing will be logged, even no errors
        kError,		//!< only errors will be logged
        kWarning, 	//!< log warnings (non-critical errors)
        kMessage,	//!< log messages (something to note but not an error)
        kNotify,	//!< log some more information
        kInfo,		//!< log verbose information
        kAll = 0xFF	//!< log absolutely everything
    };


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

    /** gets the logging level */
    LogLevel 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 LogStream s(LogLevel level = kInfo);

    inline static LogStream d() { return s(Log::kInfo); }
    inline static LogStream e() { return s(Log::kError); }
    inline static LogStream w() { return s(Log::kWarning); }

    /**
     * @brief returns the instance of the logger
     * @return
     */
    static Log& 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;

protected:

    Log();
    ~Log();

    CallbackList _callbacks;
    LogLevel _level;
};

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

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

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

// some macros

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


#endif
