代码拉取完成,页面将自动刷新
#include "log.h"
#include "settings.h"
#include <iostream>
#include <fstream>
#include <QMutex>
QMutex mutex;
std::ofstream flog;
Log* Log::m_instance = NULL;
Log& Log::instance()
{
if (m_instance == NULL)
m_instance = new Log();
return *m_instance;
}
Log::Log()
{
show_log = Settings::instance().value("httpd/show_log", true).toBool();
QString filename = Settings::instance().value("httpd/logfile", DEFAULT_HTTPD_LOGFILE).toString();
if (filename != "")
{
flog.open(filename.toUtf8().data(), std::ios_base::app); //append mode
log_file = flog.is_open();
}
else
log_file = false;
}
Log::~Log()
{
if (buffer.hasLocalData())
{
mutex.lock();
if (show_log)
{
std::cout << buffer.localData()->str();
std::cout.flush();
}
if (log_file)
{
flog << buffer.localData()->str();
flog.close();
}
mutex.unlock();
delete buffer.localData();
}
}
Log & Log::operator << (QString str)
{
if (!buffer.hasLocalData())
buffer.setLocalData(new std::ostringstream);
*buffer.localData() << str.toUtf8().data();
return *this;
}
Log & Log::operator << (int num)
{
if (!buffer.hasLocalData())
buffer.setLocalData(new std::ostringstream);
* buffer.localData() << num;
return *this;
}
Log & Log::operator << (const char * str)
{
if (!buffer.hasLocalData())
buffer.setLocalData(new std::ostringstream);
* buffer.localData() << str;
return *this;
}
Log & Log::operator << (const char chr)
{
if (!buffer.hasLocalData())
buffer.setLocalData(new std::ostringstream);
* buffer.localData() << chr;
return *this;
}
Log & Log::operator << (ctrl_t code)
{
if (!buffer.hasLocalData())
buffer.setLocalData(new std::ostringstream);
mutex.lock();
if (code == FLUSH)
{
if (show_log)
{
std::cout << buffer.localData()->str();
std::cout.flush();
}
if (log_file)
{
flog << buffer.localData()->str();
flog.flush();
}
buffer.localData()->clear();
}
else if (code == NEWLINE)
{
* buffer.localData() << '\n';
}
mutex.unlock();
return *this;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。