/* vim: set tabstop=4:softtabstop=4:shiftwidth=4:noexpandtab */
/*
 * SSTT - Simplified Spatial Target Tracker
 *
 * (c) Copyrights 2007-2018 Hartmut Seichter
 */
#include "pw/core/log.hpp"

#include <cstdarg>
#include <cstdio>

#include <sstream>
#include <thread>


namespace pw {

#if defined(ANDROID)

#include <android/log.h>


struct tpConsoleLog : tpLogCallback
{
    void operator()(const char* cstr)
    {
        __android_log_print(ANDROID_LOG_INFO, "libsstt", "%s", cstr);
    }
};

#else
struct tpConsoleLog
{
    void operator()(const char* cstr) const
    {
        ::fputs(cstr,stdout);
        ::fflush(stdout);
    }
};
#endif


struct tpFileLog {
    FILE* m_file;

    tpFileLog() : m_file(fopen("libsstt.txt","a+"))
    {
    }

    ~tpFileLog()
    {
        if (m_file != nullptr) fclose(m_file);
    }

    void operator()(const char* stuff) {
        if (m_file) ::fputs(stuff,m_file);
    }
};



Log::Log() :
    _level(Log::kNotify)
{
#if defined(_WINCE)
    _callbacks.add(new tpFileLog());
#else
    _callbacks.push_back(tpConsoleLog());
#endif
    // drop some info
    LogStream stream(this);

//    stream << "SSTT " << versionString(kVersionFull)  << " " << std::thread::hardware_concurrency() << " threads";

}

Log::~Log()
{
}

void Log::write(const std::string& message)
{
    for (CallbackList::iterator i = _callbacks.begin();
         i != _callbacks.end();
         ++i)
    {
        if (message.length()) (*i)(message.c_str());
    }
}


/* static */
LogStream Log::s(Log::LogLevel level) {
    return LogStream(&Log::get());
}

Log &Log::get() {
    static Log the_log;
    return the_log;
}


//
// tpLogStream
//

LogStream::LogStream(Log *log)
    : _log(log)
{
//    _line.append(std::to_string(Timer::now(Timer::UnitSeconds)) + " ");
}

LogStream::~LogStream()
{
    _log->write(_line + "\n");
}

LogStream::LogStream(const LogStream &other)
    : _log(other._log)
    , _line(other._line)
{
}

LogStream &LogStream::operator <<(const bool &value)
{
    _line.append(value ? "true" : "false");
    return *this;
}

LogStream &LogStream::operator <<(const char* value)
{
    _line.append(value);
    return *this;
}

LogStream &LogStream::operator <<(const std::string &value)
{
    _line.append(value);
    return *this;
}

LogStream &LogStream::operator <<(const float &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const double &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const int &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const unsigned int &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const long &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const unsigned long &value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}

LogStream &LogStream::operator <<(const void *value)
{
    std::stringstream ss;
    ss << value;
    (*this) << ss.str();

    return *this;
}
}
