From 79d067bcf500355e65e26a11c345a9427ed0128c Mon Sep 17 00:00:00 2001 From: congmu <congmu2024@163.com> Date: 星期三, 30 十月 2024 15:02:06 +0800 Subject: [PATCH] Merge branch 'master' of ssh://115.28.86.8:29418/~admin/昆仑_1025 --- /dev/null | 91 ------------------ Server/马丽萍/code/log/log.cpp | 92 +++++++++--------- Server/王琨元/document/数据库模块需求分析.docx | 0 Server/马丽萍/code/log/log.h | 56 ++++++++-- Server/马丽萍/code/log/.vscode/settings.json | 2 Server/马丽萍/code/log/README.md | 5 Server/王琨元/document/建表.docx | 0 7 files changed, 93 insertions(+), 153 deletions(-) diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt" "b/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt" deleted file mode 100644 index c07f1df..0000000 --- "a/Server/\347\216\213\347\220\250\345\205\203/document/\345\215\225\344\276\213\346\250\241\345\274\217\345\260\201\350\243\205.txt" +++ /dev/null @@ -1,51 +0,0 @@ -#include <iostream> -#include <memory> -#include <mutex> -#include <string> -#include <mysql_driver.h> -#include <mysql_connection.h> -#include <cppconn/statement.h> -#include <cppconn/resultset.h> - -class DatabaseOperator { -private: - // 绉佹湁鏋勯�犲嚱鏁� - DatabaseOperator() { - try { - driver = sql::mysql::get_mysql_driver_instance(); - connection = driver->connect("tcp://127.0.0.1:3306", "username", "password"); - connection->setSchema("your_database"); - } catch (sql::SQLException &e) { - std::cerr << "鏁版嵁搴撹繛鎺ラ敊璇�: " << e.what() << std::endl; - } - } - - static DatabaseOperator* instance; - static std::mutex mutex; - sql::Driver* driver; - std::unique_ptr<sql::Connection> connection; - -public: - // 鑾峰彇鍗曚緥瀹炰緥 - static DatabaseOperator* getInstance() { - std::lock_guard<std::mutex> lock(mutex); - if (instance == nullptr) { - instance = new DatabaseOperator(); - } - return instance; - } - - // 鎵ц鏌ヨ鎿嶄綔锛堢ず渚嬶級 - sql::ResultSet* query(const std::string& sql) { - try { - std::unique_ptr<sql::Statement> stmt(connection->createStatement()); - return stmt->executeQuery(sql); - } catch (sql::SQLException &e) { - std::cerr << "鏌ヨ閿欒: " << e.what() << std::endl; - } - return nullptr; - } -}; - -std::mutex DatabaseOperator::mutex; -DatabaseOperator* DatabaseOperator::instance = nullptr; \ No newline at end of file diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\345\273\272\350\241\250.docx" "b/Server/\347\216\213\347\220\250\345\205\203/document/\345\273\272\350\241\250.docx" new file mode 100644 index 0000000..b777db4 --- /dev/null +++ "b/Server/\347\216\213\347\220\250\345\205\203/document/\345\273\272\350\241\250.docx" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223.docx" "b/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223.docx" deleted file mode 100644 index c7dd41d..0000000 --- "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223.docx" +++ /dev/null Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\346\250\241\345\235\227\351\234\200\346\261\202\345\210\206\346\236\220.docx" "b/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\346\250\241\345\235\227\351\234\200\346\261\202\345\210\206\346\236\220.docx" index 0a608f6..22d785e 100644 --- "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\346\250\241\345\235\227\351\234\200\346\261\202\345\210\206\346\236\220.docx" +++ "b/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\346\250\241\345\235\227\351\234\200\346\261\202\345\210\206\346\236\220.docx" Binary files differ diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.txt" "b/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.txt" deleted file mode 100644 index dad6734..0000000 --- "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245.txt" +++ /dev/null @@ -1,16 +0,0 @@ -#include "MySQL.h" -#include <WinSock2.h> -#include <mysql.h> -#include <iostream> -#include <string> -using namespace std; -int main() { - MYSQL mysql; - mysql_init(&mysql);//鍒濆鍖� - //杩炴帴鏁版嵁搴撴湇鍔″櫒鏈嶅姟鍣� IP 鐢ㄦ埛鍚� 瀵嗙爜 鏁版嵁搴撶鍙e彿 - mysql_real_connect(&mysql, "192.168.136.128", "root", "123456", "wang", 3306, NULL, 0); - - string sql="set names gbk"; - mysql_real_query(&mysql, sql.c_str(), sql.size()); - mysql_close(&mysql); - return 0; \ No newline at end of file diff --git "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\346\261\240\347\261\273.txt" "b/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\346\261\240\347\261\273.txt" deleted file mode 100644 index 8c82c1a..0000000 --- "a/Server/\347\216\213\347\220\250\345\205\203/document/\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\346\261\240\347\261\273.txt" +++ /dev/null @@ -1,91 +0,0 @@ -#include <iostream> -#include <list> -#include <mutex> -#include <condition_variable> -#include <mysql_driver.h> -#include <mysql_connection.h> -#include <cppconn/statement.h> -#include <cppconn/resultset.h> - -class SQLConnectionPool { -private: - SQLConnectionPool(int minConns, int maxConns) : - minConnections(minConns), maxConnections(maxConns), currentConnections(0) {} - - static SQLConnectionPool* instance; - static std::mutex mutex; - std::condition_variable cv; - int minConnections; - int maxConnections; - int currentConnections; - std::list<std::unique_ptr<sql::Connection>> connections; - - // 鍒涘缓涓�涓柊鐨勬暟鎹簱杩炴帴 - std::unique_ptr<sql::Connection> createConnection() { - try { - sql::Driver* driver = sql::mysql::get_mysql_driver_instance(); - std::unique_ptr<sql::Connection> conn(driver->connect("tcp://127.0.0.1:3306", "username", "password")); - conn->setSchema("your_database"); - return conn; - } catch (sql::SQLException &e) { - std::cerr << "鍒涘缓杩炴帴閿欒: " << e.what() << std::endl; - return nullptr; - } - } - -public: - // 鑾峰彇鍗曚緥瀹炰緥 - static SQLConnectionPool* getInstance(int minConns, int maxConns) { - std::lock_guard<std::mutex> lock(mutex); - if (instance == nullptr) { - instance = new SQLConnectionPool(minConns, maxConns); - instance->initializePool(); - } - return instance; - } - - // 鍒濆鍖栬繛鎺ユ睜 - void initializePool() { - for (int i = 0; i < minConnections; ++i) { - std::unique_ptr<sql::Connection> conn = createConnection(); - if (conn) { - connections.push_back(std::move(conn)); - currentConnections++; - } - } - } - - // 鑾峰彇鏁版嵁搴撹繛鎺� - std::unique_ptr<sql::Connection> getConnection() { - std::unique_lock<std::mutex> lock(mutex); - while (connections.empty() && currentConnections >= maxConnections) { - cv.wait(lock); - } - - std::unique_ptr<sql::Connection> conn; - if (!connections.empty()) { - conn = std::move(connections.front()); - connections.pop_front(); - } else if (currentConnections < maxConnections) { - conn = createConnection(); - if (conn) { - currentConnections++; - } - } - return conn; - } - - // 褰掕繕鏁版嵁搴撹繛鎺� - void releaseConnection(std::unique_ptr<sql::Connection>& conn) { - std::lock_guard<std::mutex> lock(mutex); - if (currentConnections > minConnections) { - currentConnections--; - } else { - connections.push_back(std::move(conn)); - } - cv.notify_one(); - } -}; - -std::mutex SQLConnectionPool::mutex; -SQLConnectionPool* SQLConnectionPool::instance = nullptr; \ No newline at end of file diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" index bb879da..c9e66f1 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/.vscode/settings.json" @@ -4,7 +4,7 @@ "C_Cpp_Runner.debuggerPath": "gdb", "C_Cpp_Runner.cStandard": "", "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/Build/vcvarsall.bat", "C_Cpp_Runner.useMsvc": false, "C_Cpp_Runner.warnings": [ "-Wall", diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" index d6dec71..6a1cf51 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/README.md" @@ -1,9 +1,10 @@ -鍚屾/寮傛鏃ュ織绯荤粺 +寮傛鏃ュ織绯荤粺 =============== -鍚屾/寮傛鏃ュ織绯荤粺涓昏娑夊強浜嗕袱涓ā鍧楋紝涓�涓槸鏃ュ織妯″潡锛屼竴涓槸闃诲闃熷垪妯″潡,鍏朵腑鍔犲叆闃诲闃熷垪妯″潡涓昏鏄В鍐冲紓姝ュ啓鍏ユ棩蹇楀仛鍑嗗. +寮傛鏃ュ織绯荤粺涓昏娑夊強浜嗕袱涓ā鍧楋紝涓�涓槸鏃ュ織妯″潡锛屼竴涓槸闃诲闃熷垪妯″潡,鍏朵腑鍔犲叆闃诲闃熷垪妯″潡涓昏鏄В鍐冲紓姝ュ啓鍏ユ棩蹇楀仛鍑嗗. > * 鑷畾涔夐樆濉為槦鍒� > * 鍗曚緥妯″紡鍒涘缓鏃ュ織 > * 鍚屾鏃ュ織 > * 寮傛鏃ュ織 > * 瀹炵幇鎸夊ぉ銆佽秴琛屽垎绫� +> * 鏂囦欢闄愬埗澶у皬 闃叉鏃ュ織鏂囦欢杩囧ぇ \ No newline at end of file diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" index 411f8eb..7368c50 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.cpp" @@ -19,19 +19,17 @@ fclose(m_fp); } } -//寮傛闇�瑕佽缃樆濉為槦鍒楃殑闀垮害锛屽悓姝ヤ笉闇�瑕佽缃� + bool Log::init(const char *file_name, int close_log, int log_buf_size, int split_lines, int max_queue_size) { - //濡傛灉璁剧疆浜唌ax_queue_size,鍒欒缃负寮傛 if (max_queue_size >= 1) { m_is_async = true; m_log_queue = new block_queue<string>(max_queue_size); pthread_t tid; - //flush_log_thread涓哄洖璋冨嚱鏁�,杩欓噷琛ㄧず鍒涘缓绾跨▼寮傛鍐欐棩蹇� pthread_create(&tid, NULL, flush_log_thread, NULL); } - + m_close_log = close_log; m_log_buf_size = log_buf_size; m_buf = new char[m_log_buf_size]; @@ -40,25 +38,21 @@ time_t t = time(NULL); struct tm *sys_tm = localtime(&t); - struct tm my_tm = *sys_tm; - - const char *p = strrchr(file_name, '/'); char log_full_name[256] = {0}; if (p == NULL) { - snprintf(log_full_name, 255, "%d_%02d_%02d_%s", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, file_name); + snprintf(log_full_name, 255, "%d_%02d_%02d_%s", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, file_name); } else { strcpy(log_name, p + 1); strncpy(dir_name, file_name, p - file_name + 1); - snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s", dir_name, my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, log_name); + snprintf(log_full_name, 255, "%s%d_%02d_%02d_%s", dir_name, sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, log_name); } - m_today = my_tm.tm_mday; - + m_today = sys_tm->tm_mday; m_fp = fopen(log_full_name, "a"); if (m_fp == NULL) { @@ -70,48 +64,36 @@ void Log::write_log(int level, const char *format, ...) { - struct timeval now = {0, 0}; + struct timeval now; gettimeofday(&now, NULL); time_t t = now.tv_sec; struct tm *sys_tm = localtime(&t); - struct tm my_tm = *sys_tm; char s[16] = {0}; + switch (level) { - case 0: - strcpy(s, "[debug]:"); - break; - case 1: - strcpy(s, "[info]:"); - break; - case 2: - strcpy(s, "[warn]:"); - break; - case 3: - strcpy(s, "[erro]:"); - break; - default: - strcpy(s, "[info]:"); - break; + case 0: strcpy(s, "[debug]:"); break; + case 1: strcpy(s, "[info]:"); break; + case 2: strcpy(s, "[warn]:"); break; + case 3: strcpy(s, "[erro]:"); break; + default: strcpy(s, "[info]:"); break; } - //鍐欏叆涓�涓猯og锛屽m_count++, m_split_lines鏈�澶ц鏁� + m_mutex.lock(); m_count++; - if (m_today != my_tm.tm_mday || m_count % m_split_lines == 0) //everyday log + if (m_today != sys_tm->tm_mday || m_count % m_split_lines == 0) { - char new_log[256] = {0}; fflush(m_fp); fclose(m_fp); char tail[16] = {0}; - - snprintf(tail, 16, "%d_%02d_%02d_", my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday); - - if (m_today != my_tm.tm_mday) + snprintf(tail, 16, "%d_%02d_%02d_", sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday); + + if (m_today != sys_tm->tm_mday) { snprintf(new_log, 255, "%s%s%s", dir_name, tail, log_name); - m_today = my_tm.tm_mday; + m_today = sys_tm->tm_mday; m_count = 0; } else @@ -120,20 +102,20 @@ } m_fp = fopen(new_log, "a"); } - - m_mutex.unlock(); + + // 鍦ㄥ啓鍏ヤ箣鍓嶆鏌ユ棩蹇楁枃浠跺ぇ灏� + if (check_log_size()) + { + rotate_logs(); // 濡傛灉瓒呰繃鏈�澶ф枃浠跺ぇ灏忥紝杩涜鏃ュ織杞崲 + } va_list valst; va_start(valst, format); string log_str; - m_mutex.lock(); - - //鍐欏叆鐨勫叿浣撴椂闂村唴瀹规牸寮� int n = snprintf(m_buf, 48, "%d-%02d-%02d %02d:%02d:%02d.%06ld %s ", - my_tm.tm_year + 1900, my_tm.tm_mon + 1, my_tm.tm_mday, - my_tm.tm_hour, my_tm.tm_min, my_tm.tm_sec, now.tv_usec, s); - + sys_tm->tm_year + 1900, sys_tm->tm_mon + 1, sys_tm->tm_mday, + sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec, now.tv_usec, s); int m = vsnprintf(m_buf + n, m_log_buf_size - n - 1, format, valst); m_buf[n + m] = '\n'; m_buf[n + m + 1] = '\0'; @@ -158,7 +140,27 @@ void Log::flush(void) { m_mutex.lock(); - //寮哄埗鍒锋柊鍐欏叆娴佺紦鍐插尯 fflush(m_fp); m_mutex.unlock(); } + +// 娣诲姞鐨勬枃浠跺ぇ灏忔鏌ュ嚱鏁� +bool Log::check_log_size() +{ + if (m_fp) + { + fseek(m_fp, 0, SEEK_END); + long file_size = ftell(m_fp); + return file_size >= max_size; // 杩斿洖鏂囦欢鏄惁瓒呭嚭闄愬埗 + } + return false; +} + +void Log::rotate_logs() +{ + fclose(m_fp); // 鍏抽棴褰撳墠鏃ュ織鏂囦欢 + char new_log[256] = {0}; + snprintf(new_log, sizeof(new_log), "%s/%s.%lld", dir_name, log_name, m_count / m_split_lines); + m_fp = fopen(new_log, "a"); // 鎵撳紑鏂扮殑鏃ュ織鏂囦欢 + m_count = 0; // 閲嶇疆鏃ュ織璁℃暟 +} diff --git "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" index 64972af..e0f2c25 100644 --- "a/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" +++ "b/Server/\351\251\254\344\270\275\350\220\215/code/log/log.h" @@ -13,7 +13,7 @@ class Log { public: - //C++11浠ュ悗,浣跨敤灞�閮ㄥ彉閲忔噿姹変笉鐢ㄥ姞閿� + // C++11浠ュ悗,浣跨敤灞�閮ㄥ彉閲忔噿姹変笉鐢ㄥ姞閿� static Log *get_instance() { static Log instance; @@ -24,41 +24,69 @@ { Log::get_instance()->async_write_log(); } - //鍙�夋嫨鐨勫弬鏁版湁鏃ュ織鏂囦欢銆佹棩蹇楃紦鍐插尯澶у皬銆佹渶澶ц鏁颁互鍙婃渶闀挎棩蹇楁潯闃熷垪 + + // 鍙�夋嫨鐨勫弬鏁版湁鏃ュ織鏂囦欢銆佹棩蹇楃紦鍐插尯澶у皬銆佹渶澶ц鏁颁互鍙婃渶闀挎棩蹇楁潯闃熷垪 bool init(const char *file_name, int close_log, int log_buf_size = 8192, int split_lines = 5000000, int max_queue_size = 0); void write_log(int level, const char *format, ...); - void flush(void); private: Log(); virtual ~Log(); + void *async_write_log() { string single_log; - //浠庨樆濉為槦鍒椾腑鍙栧嚭涓�涓棩蹇梥tring锛屽啓鍏ユ枃浠� + // 浠庨樆濉為槦鍒椾腑鍙栧嚭涓�涓棩蹇梥tring锛屽啓鍏ユ枃浠� while (m_log_queue->pop(single_log)) { m_mutex.lock(); + if (check_log_size()) // 妫�鏌ユ枃浠跺ぇ灏� + { + rotate_logs(); // 杞崲鏃ュ織 + } fputs(single_log.c_str(), m_fp); m_mutex.unlock(); } } + // 妫�鏌ユ棩蹇楁枃浠跺ぇ灏� + bool check_log_size() + { + if (m_fp) + { + fseek(m_fp, 0, SEEK_END); + long file_size = ftell(m_fp); + return file_size >= max_size; // 杩斿洖鏂囦欢鏄惁瓒呭嚭闄愬埗 + } + return false; + } + + // 杞崲鏃ュ織鏂囦欢 + void rotate_logs() + { + fclose(m_fp); // 鍏抽棴褰撳墠鏃ュ織鏂囦欢 + char new_log[256] = {0}; + snprintf(new_log, sizeof(new_log), "%s/%s.%lld", dir_name, log_name, m_count / m_split_lines); + m_fp = fopen(new_log, "a"); // 鎵撳紑鏂扮殑鏃ュ織鏂囦欢 + m_count = 0; // 閲嶇疆鏃ュ織璁℃暟 + } + private: - char dir_name[128]; //璺緞鍚� - char log_name[128]; //log鏂囦欢鍚� - int m_split_lines; //鏃ュ織鏈�澶ц鏁� - int m_log_buf_size; //鏃ュ織缂撳啿鍖哄ぇ灏� - long long m_count; //鏃ュ織琛屾暟璁板綍 - int m_today; //鍥犱负鎸夊ぉ鍒嗙被,璁板綍褰撳墠鏃堕棿鏄偅涓�澶� - FILE *m_fp; //鎵撳紑log鐨勬枃浠舵寚閽� + char dir_name[128]; // 璺緞鍚� + char log_name[128]; // log鏂囦欢鍚� + int m_split_lines; // 鏃ュ織鏈�澶ц鏁� + int m_log_buf_size; // 鏃ュ織缂撳啿鍖哄ぇ灏� + long long m_count; // 鏃ュ織琛屾暟璁板綍 + int m_today; // 鍥犱负鎸夊ぉ鍒嗙被,璁板綍褰撳墠鏃堕棿鏄偅涓�澶� + FILE *m_fp; // 鎵撳紑log鐨勬枃浠舵寚閽� char *m_buf; - block_queue<string> *m_log_queue; //闃诲闃熷垪 - bool m_is_async; //鏄惁鍚屾鏍囧織浣� + block_queue<string> *m_log_queue; // 闃诲闃熷垪 + bool m_is_async; // 鏄惁鍚屾鏍囧織浣� locker m_mutex; - int m_close_log; //鍏抽棴鏃ュ織 + int m_close_log; // 鍏抽棴鏃ュ織 + long max_size = 10 * 1024 * 1024; // 10MB }; #define LOG_DEBUG(format, ...) if(0 == m_close_log) {Log::get_instance()->write_log(0, format, ##__VA_ARGS__); Log::get_instance()->flush();} -- Gitblit v1.8.0