logs-output complete
This commit is contained in:
parent
265c7ecbb0
commit
8c21c6d07f
|
@ -93,6 +93,7 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dispatch.cpp" />
|
||||
<ClCompile Include="logs_port.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="validate_depict.cpp" />
|
||||
</ItemGroup>
|
||||
|
@ -101,6 +102,7 @@
|
|||
<ClInclude Include="cmds_basic.h" />
|
||||
<ClInclude Include="data_types.h" />
|
||||
<ClInclude Include="dispatch.h" />
|
||||
<ClInclude Include="logs_port.h" />
|
||||
<ClInclude Include="validate_depict.h" />
|
||||
<ClInclude Include="validate_impl.h" />
|
||||
<ClInclude Include="validate.h" />
|
||||
|
|
|
@ -32,6 +32,9 @@
|
|||
<ClCompile Include="validate_depict.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="logs_port.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="cmds_basic.h">
|
||||
|
@ -58,5 +61,8 @@
|
|||
<ClInclude Include="validate_depict.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="logs_port.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -9,6 +9,10 @@ Dispatch* dispatch::Dispatch::unique() {
|
|||
return _unique_inst;
|
||||
}
|
||||
|
||||
void dispatch::Dispatch::loadOutput(std::shared_ptr<IOutput> out_ins) {
|
||||
this->_output_list << out_ins;
|
||||
}
|
||||
|
||||
QList<quint64> Dispatch::allCmds() const {
|
||||
return _cmds_map.keys();
|
||||
}
|
||||
|
@ -37,12 +41,19 @@ QString Dispatch::getCmdDefault(quint64 addr) const {
|
|||
return "";
|
||||
}
|
||||
|
||||
#include "validate_depict.h"
|
||||
void Dispatch::runWith(IRunbase* unit) {
|
||||
depict::ValidateDocObject obj;
|
||||
|
||||
auto addrs = unit->address();
|
||||
if (_cmds_validators.contains(addrs)) {
|
||||
auto validate_templet = _cmds_validators[addrs];
|
||||
if (!validate_templet->doValidate(unit))
|
||||
return;
|
||||
if (_cmds_validators.contains(addrs) && !_cmds_validators[addrs]->doValidate(unit)) {
|
||||
unit->saveTo(obj);
|
||||
auto args_content = obj.toText();
|
||||
std::for_each(_output_list.begin(), _output_list.end(), [&](std::shared_ptr<IOutput> ins) {
|
||||
ins->write(u8"命令校验信息", u8"命令执行参数校验失败", args_content);
|
||||
ins->flush();
|
||||
});
|
||||
return;
|
||||
}
|
||||
unit->run();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <QString>
|
||||
#include <QHash>
|
||||
#include <memory>
|
||||
#include "cmds_basic.h"
|
||||
#include "validate_basic.h"
|
||||
|
||||
|
@ -10,7 +11,8 @@ namespace dispatch {
|
|||
public:
|
||||
virtual ~IOutput() = default;
|
||||
|
||||
virtual void write(const QString& title, const QString& desc) = 0;
|
||||
virtual void write(const QString &type, const QString& title, const QString& desc) = 0;
|
||||
virtual void flush() = 0;
|
||||
};
|
||||
|
||||
|
||||
|
@ -20,13 +22,15 @@ namespace dispatch {
|
|||
QHash<quint64, validate::IValidatorTemplet*> _cmds_validators;
|
||||
QHash<quint64, std::pair<QString, Inlet::IRunbase*>> _events_map;
|
||||
|
||||
QList<IOutput*> _output_list;
|
||||
QList<std::shared_ptr<IOutput>> _output_list;
|
||||
|
||||
static Dispatch* _unique_inst;
|
||||
|
||||
public:
|
||||
static Dispatch* unique();
|
||||
|
||||
void loadOutput(std::shared_ptr<IOutput> out_ins);
|
||||
|
||||
QList<quint64> allCmds() const;
|
||||
void registerCmd(quint64 addr, Inlet::IRunbase* unit);
|
||||
void setCmdAlias(quint64 addr, const QString& alias);
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
#include "logs_port.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QDateTime>
|
||||
|
||||
logs::LogsOutput::LogsOutput(const QDir& target, int size) : _cache_size(size) {
|
||||
auto dt = QDateTime::currentDateTime();
|
||||
auto base_name = QString(u8"logs_%1.u8txt").arg(dt.toString(u8"yyyyMMdd_hhmmss"));
|
||||
auto full_path = target.filePath(base_name);
|
||||
|
||||
_target_file = std::make_shared<QFile>(full_path);
|
||||
if (!_target_file->open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
throw - 1;
|
||||
}
|
||||
}
|
||||
|
||||
void logs::LogsOutput::write(const QString& type, const QString& title, const QString& msg) {
|
||||
auto dt = QDateTime::currentDateTime();
|
||||
|
||||
_mutex.lock();
|
||||
_message_buffer << QString(u8"%1[%2]£º%3£º%4").arg(type, dt.toString(u8"hh:mm:ss"), title, msg);
|
||||
_mutex.unlock();
|
||||
|
||||
flush();
|
||||
}
|
||||
|
||||
void logs::LogsOutput::information(const QString& title, const QString& msg) {
|
||||
write(u8"Information", title, msg);
|
||||
}
|
||||
|
||||
void logs::LogsOutput::warning(const QString& title, const QString& msg) {
|
||||
write(u8"Warning", title, msg);
|
||||
}
|
||||
|
||||
void logs::LogsOutput::error(const QString& title, const QString& msg) {
|
||||
write(u8"Error", title, msg);
|
||||
flush(true);
|
||||
}
|
||||
|
||||
#include <QTextStream>
|
||||
void logs::LogsOutput::flush(bool fouce) {
|
||||
QMutexLocker l(&_mutex);
|
||||
|
||||
if (!fouce && _message_buffer.size() < _cache_size)
|
||||
return;
|
||||
|
||||
QTextStream tout(_target_file.get());
|
||||
tout.setCodec(u8"UTF-8");
|
||||
for (auto& line : _message_buffer) {
|
||||
tout << line << u8"\n";
|
||||
}
|
||||
tout.flush();
|
||||
_message_buffer.clear();
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QMutex>
|
||||
#include <QDir>
|
||||
#include <memory>
|
||||
|
||||
namespace logs {
|
||||
class LogsPort {
|
||||
public:
|
||||
virtual ~LogsPort() = default;
|
||||
|
||||
virtual void write(const QString& type, const QString& title, const QString& msg) = 0;
|
||||
virtual void information(const QString& title, const QString& msg) = 0;
|
||||
virtual void warning(const QString& title, const QString& msg) = 0;
|
||||
virtual void error(const QString& title, const QString& msg) = 0;
|
||||
|
||||
virtual void flush(bool fouce = false) = 0;
|
||||
};
|
||||
|
||||
class LogsOutput : public LogsPort {
|
||||
public:
|
||||
LogsOutput(const QDir& target, int cache_size = 200);
|
||||
|
||||
// ͨ¹ý LogsPort ¼Ì³Ð
|
||||
void write(const QString& type, const QString& title, const QString& msg) override;
|
||||
void information(const QString& title, const QString& msg) override;
|
||||
void warning(const QString& title, const QString& msg) override;
|
||||
void error(const QString& title, const QString& msg) override;
|
||||
|
||||
virtual void flush(bool fouce = false);
|
||||
|
||||
private:
|
||||
QMutex _mutex;
|
||||
int _cache_size;
|
||||
std::shared_ptr<QFile> _target_file;
|
||||
|
||||
QList<QString> _message_buffer;
|
||||
};
|
||||
}
|
|
@ -8,35 +8,35 @@
|
|||
#include "validate_impl.h"
|
||||
#include "validate_depict.h"
|
||||
|
||||
int vp(int c) {
|
||||
qDebug() << c;
|
||||
return c;
|
||||
}
|
||||
verify::T<int(int)>::Validate<vp> vp_check;
|
||||
auto args0 = vp_check.pos<0>() & new impls::Int32Limit<true, true>(u8"Hello World", 0, 3);
|
||||
|
||||
cmds::T<int(int)>::Cmd<vp> entry("hello");
|
||||
|
||||
|
||||
namespace xproc {
|
||||
int xmp(int arg) {
|
||||
qDebug() << "xmp";
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
cmds::T<int(int)>::Cmd<xproc::xmp> xvv("sfaf");
|
||||
|
||||
template<> void datas::dataSet<int>(IDataObject& object, const QString& key, int value) { }
|
||||
template<> int datas::dataGet<int>(IDataObject& object, const QString& key) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//int vp(int c) {
|
||||
// qDebug() << c;
|
||||
// return c;
|
||||
//}
|
||||
//verify::T<int(int)>::Validate<vp> vp_check;
|
||||
//auto args0 = vp_check.pos<0>() & new impls::Int32Limit<true, true>(u8"Hello World", 0, 3);
|
||||
//
|
||||
//cmds::T<int(int)>::Cmd<vp> entry("hello");
|
||||
//
|
||||
//
|
||||
//namespace xproc {
|
||||
// int xmp(int arg) {
|
||||
// qDebug() << "xmp";
|
||||
// return 3;
|
||||
// }
|
||||
//}
|
||||
//cmds::T<int(int)>::Cmd<xproc::xmp> xvv("sfaf");
|
||||
//
|
||||
//template<> void datas::dataSet<int>(IDataObject& object, const QString& key, int value) { }
|
||||
//template<> int datas::dataGet<int>(IDataObject& object, const QString& key) {
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
#include "logs_port.h"
|
||||
using namespace Inlet;
|
||||
int main(int argc, char* argv[]) {
|
||||
QCoreApplication a(argc, argv);
|
||||
|
||||
SignatureImpl<int, int> v(3);
|
||||
/*SignatureImpl<int, int> v(3);
|
||||
v.execute(vp);
|
||||
|
||||
Callable<int, int> vptr = vp;
|
||||
|
@ -52,7 +52,7 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
depict::ValidateDocObject doc;
|
||||
dispatch::Dispatch::unique()->getValidateDepict(doc);
|
||||
qDebug().noquote() << doc.toText();
|
||||
qDebug().noquote() << doc.toText();*/
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue