4 Star 3 Fork 4

Gitee 极速下载/Tiny-httpd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/Huangtuzhi/Tinyhttpd
克隆/下载
log.cpp 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
Huangtuzhi 提交于 10年前 . a
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/mirrors/Tiny-httpd.git
git@gitee.com:mirrors/Tiny-httpd.git
mirrors
Tiny-httpd
Tiny-httpd
master

搜索帮助