#include "stdafx.h"
|
#include "LogSearch.h"
|
#include <iostream>
|
#include "MysqlConn.h"
|
using namespace std;
|
LogSearch::LogSearch()
|
{
|
}
|
|
|
LogSearch::~LogSearch()
|
{
|
}
|
|
bool LogSearch::searchLogBusiness(SOCKET client, LogQueryReq * req)
|
{
|
//// ³õʼ»¯ SQL ²éѯÓï¾ä
|
|
string sql = "SELECT * FROM parsed_logs WHERE";
|
|
if (*(req->startTime) && *(req->endTime)) {
|
sql += " time_stamp BETWEEN '" + string(req->startTime) + "' AND '" + string(req->endTime) + "'";
|
}
|
|
if (*(req->level)) {
|
sql += " level LIKE '%" + string(req->level) + "%'";
|
}
|
|
if (*(req->deviceId)) {
|
sql += " device_id LIKE '%" + string(req->deviceId) + "%'";
|
}
|
|
if ((*req->content)) {
|
sql += " content LIKE '%" + string(req->content) + "%'";
|
}
|
|
// ´òÓ¡¹¹½¨µÄ SQL Óï¾ä£¬·½±ãµ÷ÊÔ
|
cout << "SQL: " << sql << endl;
|
|
// ÉèÖà MySQL Á¬½Ó²¢Ö´Ðвéѯ
|
|
|
MysqlConn conn;
|
vector<vector<string>> result;
|
bool querySuccess = conn.query(sql, result);
|
if (!querySuccess) {
|
cout << "²éѯʧ°Ü" << endl;
|
return false;
|
}
|
|
int len = sizeof(LogQueryRes) + result.size() * sizeof(ParsedLog); // ¶¯Ì¬¼ÆËãÊý¾Ý³¤¶È
|
LogQueryRes* res = (LogQueryRes*)malloc(len); // Ϊ LogQueryRes ½á¹¹Ìå¼°ÆäÈáÐÔÊý×é·ÖÅäÄÚ´æ
|
|
// ³õʼ»¯½á¹¹Ìå
|
res->head.type = LOGSEARCH_RES;
|
res->head.len = len;
|
res->status = (result.empty()) ? 0 : 1; // Èç¹û½á¹ûΪ¿Õ£¬ÉèÖÃ״̬Ϊ 0
|
|
// ½«²éѯ½á¹ûÌî³äµ½ parsedLog Êý×éÖÐ
|
for (const auto& row : result) {
|
cout << "ÐУº" << row.size() << endl;
|
for (const auto& item : row) {
|
cout << item << " ";
|
|
}
|
}
|
cout << endl;
|
for (size_t i = 0; i < result.size(); ++i) {
|
const auto& row = result[i];
|
ParsedLog parlog;
|
if (row.size() >= 9) {
|
parlog.id = stoi(row[0]);
|
strncpy(parlog.timeStamp, row[1].c_str(), sizeof(parlog.timeStamp));
|
strncpy(parlog.deviceId, row[2].c_str(), sizeof(parlog.deviceId));
|
strncpy(parlog.level, row[3].c_str(), sizeof(parlog.level));
|
strncpy(parlog.content, row[4].c_str(), sizeof(parlog.content));
|
strncpy(parlog.userId, row[5].c_str(), sizeof(parlog.userId));
|
strncpy(parlog.fileName, row[6].c_str(), sizeof(parlog.fileName));
|
parlog.problemLine = stoi(row[7]);
|
strncpy(parlog.functionName, row[8].c_str(), sizeof(parlog.functionName));
|
res->parsedLog[i] = parlog; // Ìî³äÊý¾Ý
|
}
|
}
|
|
// ·¢ËÍÏìÓ¦
|
cout << __FUNCTION__ << send(client, (char*)res, res->head.len, 0) << endl;
|
|
// ÊÍ·ÅÄÚ´æ
|
free(res); // ÊÍ·Å·ÖÅäµÄÄÚ´æ
|
|
}
|