瀏覽代碼

增加LOG日志

xx 2 年之前
父節點
當前提交
5232afadce
共有 8 個文件被更改,包括 103 次插入22 次删除
  1. 11 1
      config.ini
  2. 8 0
      src/core/config.cpp
  3. 11 0
      src/core/config.h
  4. 5 5
      src/core/define.h
  5. 1 1
      src/core/subscribemanager.cpp
  6. 2 2
      src/module/mssql.cpp
  7. 51 11
      src/utils/logutils.cpp
  8. 14 2
      src/utils/logutils.h

+ 11 - 1
config.ini

@@ -3,6 +3,7 @@
 base=${current_dir}
 ;网站程序目录
 www=${current_dir}/www
+
 [scripts]
 ; LUA库目录
 lib_dir=["${www}/api/lib"]
@@ -12,6 +13,7 @@ module_dir=${base}/module
 lua_cache_size=3000
 ; 自动检测文件修改时间(秒)
 auto_update_sec=3
+
 [website]
 ; 网站静态文件目录
 dir=${www}
@@ -27,11 +29,19 @@ session_timeout_sec=86400
 Initialization_script=${www}/api/lib/init.lua
 ; 拦截器
 interceptor_scripts= [["${www}/api/lib/interceptor.lua","/api/*.*"]]
-; 调试模式,开启后控制台输出异常错误信息 (0=关闭 1=开启)
+; 调试模式,开启后启用LUA错误信息 (0=关闭 1=开启)
 debug=1
 ; 绑定域名
 domain=["local.newobj.org","127.0.0.1"]
 
+[log]
+; 开启或关闭
+enable=1
+; 保存目录
+dir=${base}/log
+; 文件名(所有日志会保存到该文件中)
+name=%Y%m%d.log
+
 ; 域名配置信息,具体根据 website->domain 而定
 [local.newobj.org]
 ; 是否为HTTPS

+ 8 - 0
src/core/config.cpp

@@ -100,6 +100,14 @@ void config::cache()
 	website.debug = m_ini.read("website", "debug") == "1";
 	website.domain = ylib::json::from(m_ini.read("website", "domain")).to<std::vector<std::string>>();
 
+	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";
+
 	{
 		ylib::json interceptors = ylib::json::from(m_ini.read("website", "interceptor_scripts"));
 		for (size_t i = 0; i < interceptors.size(); i++)

+ 11 - 0
src/core/config.h

@@ -32,6 +32,16 @@ public:
 		bool debug = false;
 		std::vector<std::string> domain;
 	};
+
+	struct log {
+		bool enable = false;
+		std::string dir;
+		std::string name;
+		bool succ = false;
+		bool info = false;
+		bool warn = false;
+		bool error = false;
+	};
 public:
 	config();
 	bool open(const std::string& ini_filepath);
@@ -54,5 +64,6 @@ private:
 public:
 	scripts scripts;
 	website website;
+	log log;
 	std::map<std::string, domain> domain;
 };

+ 5 - 5
src/core/define.h

@@ -14,11 +14,11 @@
 #include "utils/logutils.h"
 #include <variant>
 
-#define LOG_ERROR(MSG) LogUtils::error(MSG,__FILE__ ,__func__,__LINE__ )
-#define LOG_WARN(MSG) LogUtils::warn(MSG,__FILE__ ,__func__,__LINE__ )
-#define LOG_INFO(MSG) LogUtils::info(MSG,__FILE__ ,__func__,__LINE__ )
-#define LOG_SUCC(MSG) LogUtils::success(MSG,__FILE__ ,__func__,__LINE__ )
-#define LOG_DEBUG(MSG) LogUtils::debug(MSG,__FILE__ ,__func__,__LINE__ )
+#define LOG_ERROR(MSG) LogUtils::getInstance()->error(MSG,__FILE__ ,__func__,__LINE__ )
+#define LOG_WARN(MSG) LogUtils::getInstance()->warn(MSG,__FILE__ ,__func__,__LINE__ )
+#define LOG_INFO(MSG) LogUtils::getInstance()->info(MSG,__FILE__ ,__func__,__LINE__ )
+#define LOG_SUCC(MSG) LogUtils::getInstance()->success(MSG,__FILE__ ,__func__,__LINE__ )
+#define LOG_DEBUG(MSG) LogUtils::getInstance()->debug(MSG,__FILE__ ,__func__,__LINE__ )
 
 
 #define sConfig 	::config::getInstance()

+ 1 - 1
src/core/subscribemanager.cpp

@@ -59,7 +59,7 @@ void subscribe_manager::other(network::http::request* request, network::http::re
 	};
 
 	std::string filepath;
-	if (request->filepath() == "/")
+	if (strutils::right(request->filepath(),1) == "/")
 	{
 		for (size_t i = 0; i < sConfig->website.default_index.size(); i++)
 		{

+ 2 - 2
src/module/mssql.cpp

@@ -64,14 +64,14 @@ double module::mssql::get_dob(const std::string& name)
 }
 void module::mssql::query(const std::string& sql)
 {
-    std::cout << "[mssql][query]: " << sql << std::endl;
+    //std::cout << "[mssql][query]: " << sql << std::endl;
     m_read = false;
     m_rows = (m_session->prepare << sql);
     //m_iter = m_rows.begin();
 }
 int64 module::mssql::update(const std::string& sql)
 {
-    std::cout << "[mssql][update]: " << sql << std::endl;
+    //std::cout << "[mssql][update]: " << sql << std::endl;
     soci::statement st = m_session->prepare << sql;
     st.execute();
     return st.get_affected_rows();

+ 51 - 11
src/utils/logutils.cpp

@@ -3,13 +3,17 @@
 #include "util/time.h"
 #include "util/codec.h"
 #include "util/file.h"
+#include "core/define.h"
+#include "core/config.h"
+
 #ifdef _WIN32
 #include <Windows.h>
 #endif
 #define DEBUG_INFO 0
-void print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color) {
+void LogUtils::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
 
-    printf("[%s] ", time::now_time("%m-%d %H:%M:%S").c_str());
+    std::string __now_time = time::now_time("%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());
@@ -22,30 +26,66 @@ void print(const std::string& type,const std::string& msg,const std::string& fil
     ylib::println(msg, (ylib::ConsoleTextColor)color);
 #endif
 
+    if (sConfig->log.enable)
+    {
+        std::string logcontent = __now_time + " " + type + " " + msg
+#ifdef _WIN32
+            + "\r\n";
+#else
+            + "\n";
+#endif
+        std::string new_time = time::now_time(sConfig->log.name);
+        if (new_time == "")
+        {
+            std::cout << "error: The log file name is incorrect: " << sConfig->log.name << std::endl;
+            return;
+        }
+
+        if (m_current_name != new_time)
+        {
+            ylib::file::create_dir(sConfig->log.dir, true);
+            std::string newfilepath = sConfig->log.dir + "/" + new_time;
+            m_file.close();
+            if (m_file.open(newfilepath) == false)
+            {
+                std::cout << "error: open log file failed, filepath: " << newfilepath << std::endl;
+                return;
+            }
+        }
+        m_file.appead(logcontent);
+    }
+}
+LogUtils::LogUtils()
+{
+
+}
+LogUtils::~LogUtils()
+{
 }
 void LogUtils::success(const std::string& msg, const std::string& filepath, const std::string& func, int line)
 {
-    ::print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN 
+    //log_error();
+    this->print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN 
     #ifdef _WIN32
     |FOREGROUND_INTENSITY
     #endif
-    );
+    ,false);
 }
 
 void LogUtils::info(const std::string& msg, const std::string& filepath, const std::string& func, int line)
 {
-    ::print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE);
+    this->print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
     
 }
 
 void LogUtils::error(const std::string& msg, const std::string& filepath, const std::string& func, int line)
 {
 //#ifdef _DEBUG
-    ::print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED 
+    this->print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED
     #ifdef _WIN32
     | FOREGROUND_INTENSITY
     #endif
-    );
+        , true);
 //#else
     
 //#endif
@@ -53,18 +93,18 @@ void LogUtils::error(const std::string& msg, const std::string& filepath, const
 
 void LogUtils::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line)
 {
-    ::print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW 
+    this->print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW
     #ifdef _WIN32
     | FOREGROUND_INTENSITY
     #endif
-    );
+        , false);
 }
 
 void LogUtils::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line)
 {
-    ::print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
+    this->print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
     #ifdef _WIN32
     | FOREGROUND_INTENSITY
     #endif
-    );
+        , false);
 }

+ 14 - 2
src/utils/logutils.h

@@ -1,10 +1,22 @@
 #pragma once
 #include <string>
-namespace LogUtils {
+#include "base/singleton.hpp"
+#include "util/file.h"
+class LogUtils :public ylib::singleton<LogUtils> {
+public:
+	LogUtils();
+	~LogUtils();
 
 	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);
 	void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line);
 	void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line);
-}
+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);
+private:
+	// 当前文件名
+	std::string m_current_name;
+	// 文件
+	ylib::file_io m_file;
+};