增加SQLITE日志

This commit is contained in:
xx
2024-06-13 18:37:32 +08:00
parent 6b3be04eb2
commit 688cbb1d26
8 changed files with 130 additions and 38 deletions

View File

@@ -1,9 +1,9 @@
cmake_minimum_required(VERSION 3.10)
project(lua)
set(LUA_VERSION_MAJOR 5)
set(LUA_VERSION_MINOR 4)
set(LUA_VERSION_PATCH 4)
#set(LUA_VERSION_MAJOR 5)
#set(LUA_VERSION_MINOR 4)
#set(LUA_VERSION_PATCH 4)
set(PJN_LUAEXE "lua")
set(PJN_LUALIB "lualib")
@@ -44,7 +44,7 @@ set(LUA_SRC
)
include_directories(${PJN_LUALIB} include)
include_directories(${PJN_LUAEXE} include)
add_library(${PJN_LUALIB} STATIC ${LUA_SRC})
add_library(${PJN_LUALIB} SHARED ${LUA_SRC})
add_executable(${PJN_LUAEXE} src/lua.c)
target_link_libraries(${PJN_LUAEXE} ${PJN_LUALIB})

View File

@@ -4,7 +4,7 @@ project("fastweb")
# 设置全局属性
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# 设置自定义配置类型
if(MSVC)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build config types" FORCE)
@@ -54,7 +54,9 @@ if(MSVC)
include_directories(${PROJECT_SOURCE_DIR}/3rdparty/soci/include)
include_directories(${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Windows/Include)
include_directories(${YLIB}/include)
include_directories(D:/lib/ylib/3rdparty)
add_compile_options(/W3 /wd4819)
else()
include_directories(${PROJECT_SOURCE_DIR}/3rdparty)
include_directories(/usr/include/lua5.3/)
@@ -85,10 +87,12 @@ if(MSVC)
$<$<CONFIG:Debug>:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.lib>
$<$<CONFIG:Debug>:${YLIB}/lib/leveldb_d.lib>
$<$<CONFIG:Debug>:${YLIB}/lib/ylib_d.lib>
$<$<CONFIG:Debug>:${YLIB}/lib/sqlite3_d.lib>
$<$<CONFIG:Release>:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.lib>
$<$<CONFIG:Release>:${YLIB}/lib/leveldb.lib>
$<$<CONFIG:Release>:${YLIB}/lib/ylib.lib>
$<$<CONFIG:Debug>:${YLIB}/lib/sqlite3.lib>
)
else()
@@ -107,7 +111,10 @@ else()
endif()
# 编译测试调用示例
add_executable(${PROJECT_NAME} tests/main.cpp)
add_executable(${PROJECT_NAME}
tests/main.cpp
tests/slave.cpp
)
if(MSVC)
target_link_libraries(${PROJECT_NAME} PRIVATE
${FASTWEBCORE}
@@ -120,8 +127,10 @@ if(MSVC)
${YLIB}/lib/libcrypto_static_win64.lib
$<$<CONFIG:Debug>:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.lib>
$<$<CONFIG:Debug>:${YLIB}/lib/ylib_d.lib>
$<$<CONFIG:Release>:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.lib>
$<$<CONFIG:Release>:${YLIB}/lib/ylib.lib>
)
else()
target_link_libraries(${PROJECT_NAME}

View File

@@ -34,6 +34,8 @@ enable=1
dir=${base}/log
; 文件名(所有日志会保存到该文件中)
name=%Y%m%d.log
; SQLITE日志记录(开启后可用于管理器分析日志)
sqlite=1
; 域名配置信息,具体根据 website->domain 而定
[local.newobj.org]

View File

@@ -127,10 +127,11 @@ void fastweb::config::cache()
log.enable = m_ini.read("log", "enable") == "1";
log.dir = m_ini.read("log", "dir");
log.name = m_ini.read("log", "name");
log.succ = m_ini.read("log", "succ") == "1";
log.info = m_ini.read("log", "info") == "1";
log.warn = m_ini.read("log", "warn") == "1";
log.error = m_ini.read("log", "error") == "1";
log.sqlite = m_ini.read("log", "sqlite") == "1";
//log.succ = m_ini.read("log", "succ") == "1";
//log.info = m_ini.read("log", "info") == "1";
//log.warn = m_ini.read("log", "warn") == "1";
//log.error = m_ini.read("log", "error") == "1";
// website 域名参数

View File

@@ -38,10 +38,11 @@ namespace fastweb
bool enable = false;
std::string dir;
std::string name;
bool succ = false;
bool sqlite = false;
/*bool succ = false;
bool info = false;
bool warn = false;
bool error = false;
bool error = false;*/
};
public:
config(fastweb::app* ptr);

View File

@@ -23,7 +23,7 @@
#define ITHREAD_WAIT_MSEC(MSEC) \
for(int i=0;i<(MSEC/10);i++) \
for(int i=0;i<(MSEC/100);i++) \
{ \
if (::ithread::m_state == 1) \
break; \

View File

@@ -36,28 +36,61 @@
#include <Windows.h>
#endif
#define DEBUG_INFO 0
void fastweb::log::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
std::string __now_time = time::now_time("%m-%d %H:%M:%S");
inline std::string typestring(fastweb::log::log_type type)
{
std::string type_str;
switch (type)
{
case fastweb::log::LT_SUCCESS:
type_str = "[SUCC ] ";
break;
case fastweb::log::LT_INFO:
type_str = "[INFO ] ";
break;
case fastweb::log::LT_ERROR:
type_str = "[ERROR] ";
break;
case fastweb::log::LT_WARN:
type_str = "[WARN ] ";
break;
case fastweb::log::LT_DEBUG:
type_str = "[DEBUG] ";
break;
case fastweb::log::LT_LUA:
type_str = "[LUA ] ";
break;
default:
type_str = "[NONE ] ";
break;
}
return type_str;
}
void fastweb::log::print(log_type type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
std::string typestr = typestring(type);
std::string __now_time = time::now_time("%Y-%m-%d %H:%M:%S");
printf("[%s] ", __now_time.c_str());
#if DEBUG_INFO == 1
printf("%s", ylib::file::filename(filepath).c_str());
printf("%s ", std::string(":" + func + ":" + std::to_string(line) + "\t").c_str());
#endif
ylib::print(" "+type +" ", (ylib::ConsoleTextColor)color);
ylib::print(" "+ typestr +" ", (ylib::ConsoleTextColor)color);
#ifdef _WIN32
ylib::println(codec::to_gbk(msg), (ylib::ConsoleTextColor)color);
#else
ylib::println(msg, (ylib::ConsoleTextColor)color);
#endif
std::string logcontent = __now_time + " " + type + " " + msg
#ifdef _WIN32
+ "\r\n";
#else
+ "\n";
#endif
m_queue.push(logcontent);
log_info li;
li.create_at = __now_time;
li.filepath = filepath;
li.function = func;
li.line = line;
li.msg = msg;
li.type = type;
m_queue.push(li);
}
bool fastweb::log::run()
{
@@ -88,39 +121,37 @@ fastweb::log::~log()
{
::ithread::stop();
::ithread::wait();
}
void fastweb::log::start()
{
}
void fastweb::log::success(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
//log_error();
this->print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN
this->print(LT_SUCCESS ,msg, filepath, func, line, ylib::ConsoleTextColor::GREEN
#ifdef _WIN32
|FOREGROUND_INTENSITY
#endif
,false);
}
void fastweb::log::info(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
this->print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
this->print(LT_INFO,msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
}
void fastweb::log::error(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
//#ifdef _DEBUG
this->print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED
this->print(LT_ERROR, msg, filepath, func, line, ylib::ConsoleTextColor::RED
#ifdef _WIN32
| FOREGROUND_INTENSITY
#endif
, true);
//#else
//#endif
}
void fastweb::log::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
this->print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW
this->print(LT_WARN, msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW
#ifdef _WIN32
| FOREGROUND_INTENSITY
#endif
@@ -129,7 +160,7 @@ void fastweb::log::warn(const std::string& msg, const std::string& filepath, con
void fastweb::log::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line)
{
this->print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
this->print(LT_DEBUG, msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
#ifdef _WIN32
| FOREGROUND_INTENSITY
#endif
@@ -138,7 +169,7 @@ void fastweb::log::debug(const std::string& msg, const std::string& filepath, co
void fastweb::log::lua(const std::string& msg)
{
this->print("[LUA ] ", msg,"","",0, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
this->print(LT_LUA, msg,"","",0, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
}
bool fastweb::log::write()
@@ -161,10 +192,31 @@ bool fastweb::log::write()
std::cout << "error: open log file failed, filepath: " << newfilepath << std::endl;
return false;
}
if (app()->config->log.sqlite)
{
m_sqlite.open(app()->config->log.dir + "/" + new_time + ".db");
m_sqlite.exec(R"(CREATE TABLE "log" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"type" INTEGER,
"create_at" TEXT,
"content" TEXT,
"flag" integer
); )");
}
}
while (m_queue.pop(logcontent))
log_info li;
while (m_queue.pop(li))
{
std::string typestr = typestring(li.type);
std::string logcontent = li.create_at + " " + typestr + " " + li.msg
#ifdef _WIN32
+ "\r\n";
#else
+ "\n";
#endif
m_file.appead(logcontent);
if(app()->config->log.sqlite)
m_sqlite.exec("INSERT INTO log(type,create_at,content,flag)VALUES(" + std::to_string(li.type) + ",'"+li.create_at+"','"+li.msg+"',0)");
}
return true;
}

View File

@@ -4,13 +4,35 @@
#include "core/define.h"
#include "util/thread.h"
#include "util/queue.hpp"
#include "util/sqlite3.h"
namespace fastweb
{
class log :public Interface,private ylib::ithread {
public:
enum log_type {
LT_SUCCESS = 1,
LT_INFO = 2,
LT_ERROR = 3,
LT_WARN = 4,
LT_DEBUG = 5,
LT_LUA = 6
};
private:
struct log_info {
log_type type = LT_SUCCESS;
std::string msg;
std::string filepath;
std::string function;
std::string create_at;
int line;
};
public:
log(fastweb::app* ptr);
~log();
void start();
void success(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void info(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void error(const std::string& msg, const std::string& filepath, const std::string& func, int line);
@@ -20,14 +42,19 @@ namespace fastweb
bool write();
private:
void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error);
void print(log_type type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error);
bool run() override;
private:
// 当前文件名
std::string m_current_name;
// 文件
ylib::file_io m_file;
// SQLITE
ylib::sqlite3 m_sqlite;
// 日志队列
ylib::queue<std::string> m_queue;
ylib::queue<log_info> m_queue;
};
}