See "heimdall"; Android Qt/C++ client for the private doorbell project heimdall. Uses MQTT to communicate.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 

114 lines
2.2 KiB

#ifndef LOG_H
#define LOG_H
#include <iostream>
#include <chrono>
#include <ctime>
#include <sstream>
#include <thread>
#include <QDebug>
#define QSTR(x) QObject::tr(x)
namespace tai {
class Log {
public:
~Log() {}
enum class Level {
DEBUG = 0,
INFO,
WARNING,
ERROR,
FATAL
};
static Log& logger() {
static Log _inst;
return _inst;
}
static std::string threadIdToStr(std::thread::id const& tid)
{
std::stringstream ss;
ss << tid;
return ss.str();
}
std::string currentTime()
{
time_t now = time(nullptr);
tm* ltm = localtime(&now);
std::string currentTime =
std::to_string(ltm->tm_mday) + "." +
std::to_string(ltm->tm_mon + 1) + "." +
std::to_string(ltm->tm_year + 1900) + ", ";
if (ltm->tm_hour < 10)
currentTime += "0";
currentTime += std::to_string(ltm->tm_hour) + ":";
if (ltm->tm_min < 10)
currentTime += "0";
currentTime += std::to_string(ltm->tm_min) + ":";
if (ltm->tm_sec < 10)
currentTime += "0";
currentTime += std::to_string(ltm->tm_sec);
return currentTime;
}
void log(std::string const& msg, Level lv = Level::DEBUG) {
std::string log_level;
switch (lv) {
case Level::DEBUG:
log_level = "DEBUG\t";
break;
case Level::INFO:
log_level = "INFO\t\t";
break;
case Level::WARNING:
log_level = "WARNING\t";
break;
case Level::ERROR:
log_level = "ERROR\t";
break;
case Level::FATAL:
log_level = "FATAL\t";
break;
default:
log_level = "UNKNOWN\t";
break;
}
qDebug() << "(" << QString::fromStdString(log_level) << ") " << QString::fromStdString(msg);
std::cerr
<< "(" << currentTime() << ") "
<< log_level << " | "
<< msg << std::endl;
}
private:
Log() {}
Log(const Log&);
Log& operator=(const Log&) = delete;
};
} // namespace tai
#define LOG_DBG(x) tai::Log::logger().log(x, tai::Log::Level::DEBUG)
#define LOG_INF(x) tai::Log::logger().log(x, tai::Log::Level::INFO)
#define LOG_WRN(x) tai::Log::logger().log(x, tai::Log::Level::WARNING)
#define LOG_ERR(x) tai::Log::logger().log(x, tai::Log::Level::ERROR)
#define LOG_FTL(x) tai::Log::logger().log(x, tai::Log::Level::FATAL)
#define LOG_UKN(x) tai::Log::logger().log(x, tai::Log::Level::UNKNOWN)
#endif // LOG_H