#pragma once
#include <iostream>
#include <string>
#include <stdio.h>
#include <assert.h>
#include <ratio>
#include <chrono>
#include <sstream>
#include <string.h>
#include <stdarg.h>

#define YEAR ((((__DATE__[7] - '0') * 10 + (__DATE__[8] - '0')) * 10 \
    + (__DATE__ [9] - '0')) * 10 + (__DATE__ [10] - '0'))

#define MONTH (__DATE__ [2] == 'n' ? (__DATE__ [1] == 'a' ? 1 : 6) \
    : __DATE__ [2] == 'b' ? 2 \
    : __DATE__ [2] == 'r' ? (__DATE__ [0] == 'M' ? 3 : 4) \
    : __DATE__ [2] == 'y' ? 5 \
    : __DATE__ [2] == 'l' ? 7 \
    : __DATE__ [2] == 'g' ? 8 \
    : __DATE__ [2] == 'p' ? 9 \
    : __DATE__ [2] == 't' ? 10 \
    : __DATE__ [2] == 'v' ? 11 : 12)

#define DAY ((__DATE__ [4] == ' ' ? 0 : __DATE__ [4] - '0') * 10 \
    + (__DATE__[5] - '0'))


#define HOUR   ((__TIME__[0] - '0') * 10 + (__TIME__[1] - '0'))

#define MINUTE ((__TIME__[3] - '0') * 10 + (__TIME__[4] - '0'))

#define SECOND ((__TIME__[6] - '0') * 10 + (__TIME__[7] - '0'))

#define BUILD_TIME (std::to_string(YEAR) + "-" + \
           (MONTH < 10 ? "0" : "") + std::to_string(MONTH) + "-" +  \
           (DAY < 10 ? "0" : "") + std::to_string(DAY) + " " +  \
           (HOUR < 10 ? "0" : "") + std::to_string(HOUR) + ":" +    \
           (MINUTE < 10 ? "0" : "") + std::to_string(MINUTE) + ":" +    \
           (SECOND < 10 ? "0" : "") + std::to_string(SECOND))

#ifdef _WIN32
// windows:
#define logfilename(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#else
// linux:
#define logfilename(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif // _WIN32

#include <iostream>
#include <ctime>
#include <cstdarg>
#include <chrono>

#define _CRT_SECURE_NO_WARNINGS

///******************************************************************************************************************/
#ifdef __ANDROID__
#include <android/log.h>
#define  LOG_VERBOSE(...)  		__android_log_print(ANDROID_LOG_VERBOSE,	"VrAPPV", ##__VA_ARGS__)
#define  LOG_DEBUG(...) 		__android_log_print(ANDROID_LOG_DEBUG,		"VrAPPD", ##__VA_ARGS__)
#define  LOG_INFO(...)  		__android_log_print(ANDROID_LOG_INFO,		"VrAPPI", ##__VA_ARGS__)
#define  LOG_WARNING(...)  		__android_log_print(ANDROID_LOG_WARN,		"VrAPPW", ##__VA_ARGS__)
#define  LOG_ERRO(...)  		__android_log_print(ANDROID_LOG_ERROR,		"VrAPPE", ##__VA_ARGS__)
#else


#if 0
#include <qdebug.h>
#define _LOG_OUTPUT qDebug
#else
#define _LOG_OUTPUT printf
#endif

#define  LOG_TIME                           do { \
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();   \
    std::time_t timestamp = std::chrono::system_clock::to_time_t(now);              \
    std::tm local_time;    \
    localtime_s(&local_time, &timestamp);   \
    long long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count(); \
    _LOG_OUTPUT("%d-%02d-%02d %02d:%02d:%02d.%03lld", local_time.tm_year + 1900 , local_time.tm_mon + 1, local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, milliseconds % 1000);   \
} while(0)


#define  LOG_VERBOSE(fmt, ...)              do { { LOG_TIME; _LOG_OUTPUT(" V[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_DEBUG(fmt, ...)                do { { LOG_TIME; _LOG_OUTPUT(" D[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_DEBUG_NOLINE(fmt, ...) 	    do { { printf(fmt, ##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_INFO(fmt, ...)  		        do { { LOG_TIME; _LOG_OUTPUT(" I[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout);} while(0)
#define  LOG_WARNING(fmt, ...)              do { { LOG_TIME; _LOG_OUTPUT(" W[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_ERROR(fmt, ...)  		        do { { LOG_TIME; _LOG_OUTPUT(" E[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_ERRO(fmt, ...)  		        do { { LOG_TIME; _LOG_OUTPUT(" E[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)
#define  LOG_ERR(fmt, ...)  		        do { { LOG_TIME; _LOG_OUTPUT(" E[%20s : %4u] " fmt"", logfilename(__FILE__), __LINE__ ,##__VA_ARGS__); } fflush(stdout); } while(0)

#endif
///******************************************************************************************************************/
