Log4CPP.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "Log4CPP.h"
  2. #include <sys/stat.h>
  3. #include <dirent.h>
  4. #include <sys/types.h>
  5. #include <tinyxml2.h>
  6. #include <ctime>
  7. #include <stdexcept>
  8. #include <iostream>
  9. #include <cstdio>
  10. #include <vector>
  11. #include <algorithm>
  12. // 全局模块映射和互斥锁
  13. static std::mutex g_moduleMutex;
  14. static std::map<std::string, LogInstance*> g_moduleLoggers;
  15. bool LogInstance::parseConfigFile(const std::string& configFile, LogConfig& config) {
  16. tinyxml2::XMLDocument doc;
  17. if (doc.LoadFile(configFile.c_str()) != tinyxml2::XML_SUCCESS) {
  18. std::cerr << "[" << moduleName << "] Load config failed: " << configFile << std::endl;
  19. return false;
  20. }
  21. tinyxml2::XMLElement* root = doc.RootElement();
  22. if (!root) return false;
  23. // 先解析全局配置
  24. for (tinyxml2::XMLElement* elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
  25. const char* name = elem->Name();
  26. const char* value = elem->Attribute("value");
  27. if (!name || !value) continue;
  28. // 跳过module元素,稍后处理
  29. if (strcmp(name, "module") == 0) continue;
  30. // 全局配置
  31. if (strcmp(name, "root_level") == 0) config.root_level = value;
  32. else if (strcmp(name, "log_dir") == 0) config.log_dir = value;
  33. else if (strcmp(name, "history_dir") == 0) config.history_dir = value;
  34. else if (strcmp(name, "MAX_FILE_SIZE") == 0) {
  35. std::string sizeStr = value;
  36. if (!sizeStr.empty() && (sizeStr.back() == 'M' || sizeStr.back() == 'm')) {
  37. sizeStr.pop_back();
  38. }
  39. try {
  40. config.max_file_size = std::stoul(sizeStr);
  41. }
  42. catch (const std::exception& e) {
  43. std::cerr << "[" << moduleName << "] Parse MAX_FILE_SIZE failed: " << e.what() << std::endl;
  44. config.max_file_size = 10; // 默认值
  45. }
  46. }
  47. else if (strcmp(name, "MAX_HISTORY") == 0) {
  48. try {
  49. config.max_backups = std::stoi(value);
  50. }
  51. catch (const std::exception& e) {
  52. std::cerr << "[" << moduleName << "] Parse MAX_HISTORY failed: " << e.what() << std::endl;
  53. config.max_backups = 5; // 默认值
  54. }
  55. }
  56. else if (strcmp(name, "file_pattern") == 0) config.file_pattern = value;
  57. else if (strcmp(name, "console_pattern") == 0) config.console_pattern = value;
  58. else if (strcmp(name, "log_file_name") == 0) config.log_file_name = value;
  59. }
  60. // 再查找模块特定配置
  61. for (tinyxml2::XMLElement* elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) {
  62. const char* name = elem->Name();
  63. if (!name || strcmp(name, "module") != 0) continue;
  64. const char* moduleNameAttr = elem->Attribute("name");
  65. if (!moduleNameAttr || moduleName != moduleNameAttr) continue;
  66. // 找到匹配的模块配置
  67. for (tinyxml2::XMLElement* subElem = elem->FirstChildElement(); subElem; subElem = subElem->NextSiblingElement()) {
  68. const char* subName = subElem->Name();
  69. const char* subValue = subElem->Attribute("value");
  70. if (!subName || !subValue) continue;
  71. if (strcmp(subName, "root_level") == 0) config.root_level = subValue;
  72. else if (strcmp(subName, "log_dir") == 0) config.log_dir = subValue;
  73. else if (strcmp(subName, "history_dir") == 0) config.history_dir = subValue;
  74. else if (strcmp(subName, "MAX_FILE_SIZE") == 0) {
  75. std::string sizeStr = subValue;
  76. if (!sizeStr.empty() && (sizeStr.back() == 'M' || sizeStr.back() == 'm')) {
  77. sizeStr.pop_back();
  78. }
  79. try {
  80. config.max_file_size = std::stoul(sizeStr);
  81. }
  82. catch (const std::exception& e) {
  83. std::cerr << "[" << moduleName << "] Parse module MAX_FILE_SIZE failed: " << e.what() << std::endl;
  84. }
  85. }
  86. else if (strcmp(subName, "MAX_HISTORY") == 0) {
  87. try {
  88. config.max_backups = std::stoi(subValue);
  89. }
  90. catch (const std::exception& e) {
  91. std::cerr << "[" << moduleName << "] Parse module MAX_HISTORY failed: " << e.what() << std::endl;
  92. }
  93. }
  94. else if (strcmp(subName, "file_pattern") == 0) config.file_pattern = subValue;
  95. else if (strcmp(subName, "console_pattern") == 0) config.console_pattern = subValue;
  96. else if (strcmp(subName, "log_file_name") == 0) config.log_file_name = subValue;
  97. }
  98. break; // 找到匹配模块后跳出循环
  99. }
  100. return true;
  101. }
  102. bool LogInstance::createDirectory(const std::string& dir) {
  103. if (dir.empty()) return false;
  104. std::string path;
  105. for (size_t i = 0; i < dir.length(); ++i) {
  106. path += dir[i];
  107. if (dir[i] == '/' || dir[i] == '\\' || i == dir.length() - 1) {
  108. // 跳过根目录和空目录名
  109. if (path.length() <= 1) continue;
  110. #ifdef _WIN32
  111. if (_mkdir(path.c_str()) != 0 && errno != EEXIST) {
  112. std::cerr << "[" << moduleName << "] Create directory failed: " << path << std::endl;
  113. return false;
  114. }
  115. #else
  116. if (mkdir(path.c_str(), 0755) != 0 && errno != EEXIST) {
  117. std::cerr << "[" << moduleName << "] Create directory failed: " << path << std::endl;
  118. return false;
  119. }
  120. #endif
  121. }
  122. }
  123. return true;
  124. }
  125. void LogInstance::getCurrentTimeWithMs(std::string& timeStr) {
  126. auto now = std::chrono::system_clock::now();
  127. auto now_time_t = std::chrono::system_clock::to_time_t(now);
  128. auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
  129. now.time_since_epoch()) % 1000;
  130. tm* now_tm = localtime(&now_time_t);
  131. char buffer[80];
  132. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", now_tm);
  133. std::ostringstream oss;
  134. oss << buffer << "." << std::setfill('0') << std::setw(3) << now_ms.count();
  135. timeStr = oss.str();
  136. }
  137. log4cpp::Priority::PriorityLevel LogInstance::parseLogLevel(const std::string& levelStr) {
  138. if (levelStr == "DEBUG") return log4cpp::Priority::DEBUG;
  139. if (levelStr == "INFO") return log4cpp::Priority::INFO;
  140. if (levelStr == "WARN") return log4cpp::Priority::WARN;
  141. if (levelStr == "ERROR") return log4cpp::Priority::ERROR;
  142. if (levelStr == "FATAL") return log4cpp::Priority::FATAL;
  143. return log4cpp::Priority::DEBUG; // 默认DEBUG
  144. }
  145. void LogInstance::moveOldLogsToHistory() {
  146. // 1. 获取当前日期(保留连字符,与日志文件名格式一致)
  147. std::string currentTimeWithMs;
  148. getCurrentTimeWithMs(currentTimeWithMs); // 格式:"2025-09-16 10:15:30.123"
  149. std::string currentDate;
  150. // 提取日期部分(前10个字符,直接保留连字符:"2025-09-16")
  151. if (currentTimeWithMs.length() >= 10) {
  152. currentDate = currentTimeWithMs.substr(0, 10); // 结果:"2025-09-16"
  153. }
  154. else {
  155. std::cerr << "[" << moduleName << "] 提取当前日期失败" << std::endl;
  156. return;
  157. }
  158. // 2. 构建目录路径
  159. std::string baseLogDir = config_.log_dir + "/" + logHost;
  160. std::string historyDir = config_.history_dir + "/" + logHost;
  161. if (!createDirectory(historyDir)) {
  162. std::cerr << "[" << moduleName << "] 创建历史目录失败: " << historyDir << std::endl;
  163. return;
  164. }
  165. // 3. 遍历日志目录
  166. DIR* dir = opendir(baseLogDir.c_str());
  167. if (!dir) {
  168. std::cerr << "[" << moduleName << "] 打开日志目录失败: " << baseLogDir << std::endl;
  169. return;
  170. }
  171. std::string logPrefix = actualLogFileName + "."; // 如 "DevPSGHD."
  172. struct dirent* entry;
  173. while ((entry = readdir(dir)) != nullptr) {
  174. std::string filename = entry->d_name;
  175. if (filename == "." || filename == "..") continue;
  176. if (filename.find(logPrefix) != 0) continue; // 只处理当前模块的日志
  177. // 4. 提取文件名中的日期(格式:"YYYY-MM-DD")
  178. // 文件名结构:logPrefix + [中间部分.] + "YYYY-MM-DD" + ".log"
  179. // 修正逻辑:从末尾查找,确保提取最后10位的日期部分
  180. const std::string logSuffix = ".log";
  181. size_t suffixPos = filename.find(logSuffix);
  182. if (suffixPos == std::string::npos) continue; // 不是.log文件,跳过
  183. // 日期应该在.log前面,且长度为10(YYYY-MM-DD)
  184. size_t dateStart = suffixPos - 10;
  185. if (suffixPos < 10) continue;
  186. std::string fileDate = filename.substr(dateStart, 10);
  187. // 验证日期格式(简单检查:包含两个连字符,且位置正确)
  188. if (fileDate.size() != 10 || fileDate[4] != '-' || fileDate[7] != '-') {
  189. continue; // 日期格式不正确,跳过
  190. }
  191. // 5. 调试输出:打印文件名日期和当前日期(方便确认是否匹配)
  192. std::cout << "[" << moduleName << "] 检查日志: " << filename
  193. << " | 文件名日期: " << fileDate
  194. << " | 当前日期: " << currentDate << std::endl;
  195. // 6. 只移动非当天的日志(日期不匹配时)
  196. if (fileDate != currentDate) {
  197. std::string srcPath = baseLogDir + "/" + filename;
  198. std::string destPath = historyDir + "/" + filename;
  199. if (std::rename(srcPath.c_str(), destPath.c_str()) != 0) {
  200. std::cerr << "[" << moduleName << "] 移动日志失败: " << srcPath << " -> " << destPath << std::endl;
  201. }
  202. else {
  203. std::cout << "[" << moduleName << "] 移动旧日志成功: " << srcPath << " -> " << destPath << std::endl;
  204. }
  205. }
  206. }
  207. closedir(dir);
  208. }
  209. bool LogInstance::init(const std::string& logHostName, const std::string& module,
  210. const std::string& configFile, bool toScreen) {
  211. if (isInitialized_) {
  212. std::cout << "[" << module << "] Log already initialized" << std::endl;
  213. return true;
  214. }
  215. // 初始化基础信息
  216. logHost = logHostName;
  217. moduleName = module;
  218. outToScreen = toScreen;
  219. // 加载配置(优先模块配置,其次全局配置,最后默认)
  220. LogConfig tempConfig;
  221. if (!configFile.empty()) {
  222. if (!parseConfigFile(configFile, tempConfig)) {
  223. std::cerr << "[" << module << "] Parse config file failed, using default config" << std::endl;
  224. }
  225. }
  226. config_ = tempConfig;
  227. // 确定实际使用的日志文件名
  228. if (config_.log_file_name.empty()) {
  229. actualLogFileName = moduleName; // 使用模块名作为日志文件名
  230. }
  231. else {
  232. actualLogFileName = config_.log_file_name; // 使用配置的日志文件名
  233. // 支持在日志文件名中使用 {host} 占位符
  234. size_t pos = actualLogFileName.find("{host}");
  235. if (pos != std::string::npos) {
  236. actualLogFileName.replace(pos, 6, logHost);
  237. }
  238. }
  239. // 创建日志目录
  240. std::string baseLogDir = config_.log_dir + "/" + logHost;
  241. if (!createDirectory(baseLogDir)) {
  242. std::cerr << "[" << module << "] Create log dir failed: " << baseLogDir << std::endl;
  243. return false;
  244. }
  245. // 生成日志文件路径
  246. std::string timeStr;
  247. getCurrentTimeWithMs(timeStr);
  248. // 提取年月日部分(前10个字符,格式为"YYYY-MM-DD")
  249. std::string dateStr = timeStr.substr(0, 10);
  250. // 构造日志路径,仅使用年月日作为时间标识
  251. std::string logPath = baseLogDir + "/" + actualLogFileName + "." + dateStr + ".log";
  252. // 初始化log4cpp组件
  253. try {
  254. // 布局
  255. logLayout = new log4cpp::PatternLayout();
  256. logLayout->setConversionPattern(config_.file_pattern);
  257. screenLayout = new log4cpp::PatternLayout();
  258. screenLayout->setConversionPattern(config_.console_pattern);
  259. // 文件Appender
  260. rollLogFile = new log4cpp::RollingFileAppender(
  261. "roll_" + moduleName, logPath,
  262. config_.max_file_size * 1024 * 1024, // 转换为字节
  263. config_.max_backups, true
  264. );
  265. rollLogFile->setLayout(logLayout);
  266. // 控制台Appender
  267. if (outToScreen) {
  268. logScreen = new log4cpp::OstreamAppender("console_" + moduleName, &std::cout);
  269. logScreen->setLayout(screenLayout);
  270. }
  271. // 日志类别(用模块名区分)
  272. log4cpp::Category& root = log4cpp::Category::getRoot();
  273. logCat = &root.getInstance("file_" + moduleName);
  274. logCat->addAppender(rollLogFile);
  275. logCat->setPriority(parseLogLevel(config_.root_level));
  276. if (outToScreen) {
  277. coutCat = &root.getInstance("console_" + moduleName);
  278. coutCat->addAppender(logScreen);
  279. coutCat->setPriority(parseLogLevel(config_.root_level));
  280. }
  281. // 移动旧日志
  282. moveOldLogsToHistory();
  283. // 记录初始化日志
  284. std::string initTime;
  285. getCurrentTimeWithMs(initTime);
  286. logCat->info("Log initialized (file: %s)", logPath.c_str());
  287. if (outToScreen) {
  288. coutCat->info("Log initialized (module: %s)", moduleName.c_str());
  289. }
  290. isInitialized_ = true;
  291. return true;
  292. }
  293. catch (const std::exception& e) {
  294. std::cerr << "[" << module << "] Init failed: " << e.what() << std::endl;
  295. destroyLog(); // 清理部分初始化的资源
  296. return false;
  297. }
  298. }
  299. void LogInstance::destroyLog() {
  300. if (!isInitialized_) return;
  301. std::lock_guard<std::mutex> lock(logMtx_);
  302. // 记录销毁日志
  303. if (logCat) logCat->info("Log destroying");
  304. // 清理log4cpp资源
  305. if (logCat) logCat->removeAllAppenders();
  306. if (coutCat) coutCat->removeAllAppenders();
  307. delete rollLogFile; rollLogFile = nullptr;
  308. delete logScreen; logScreen = nullptr;
  309. delete logLayout; logLayout = nullptr;
  310. delete screenLayout; screenLayout = nullptr;
  311. logCat = nullptr;
  312. coutCat = nullptr;
  313. isInitialized_ = false;
  314. }
  315. void LogInstance::setPriority(log4cpp::Priority::PriorityLevel consoleLevel,
  316. log4cpp::Priority::PriorityLevel fileLevel) {
  317. if (isInitialized_) {
  318. if (coutCat) coutCat->setPriority(consoleLevel);
  319. if (logCat) logCat->setPriority(fileLevel);
  320. }
  321. }
  322. // 日志模块初始化函数实现
  323. bool initLogModule(const std::string& logHost,
  324. const std::string& module,
  325. const std::string& configFile,
  326. bool toScreen) {
  327. std::lock_guard<std::mutex> lock(g_moduleMutex);
  328. // 检查是否已经初始化
  329. if (g_moduleLoggers.find(module) != g_moduleLoggers.end()) {
  330. return true; // 已经初始化
  331. }
  332. // 创建新的日志实例
  333. LogInstance* logger = new LogInstance();
  334. bool ret = logger->init(logHost, module, configFile, toScreen);
  335. if (ret) {
  336. LogManager::getInstance().registerInstance(module, logger);
  337. g_moduleLoggers[module] = logger;
  338. return true;
  339. }
  340. else {
  341. delete logger;
  342. return false;
  343. }
  344. }
  345. // 日志模块销毁函数实现
  346. void destroyLogModule(const std::string& module) {
  347. std::lock_guard<std::mutex> lock(g_moduleMutex);
  348. auto it = g_moduleLoggers.find(module);
  349. if (it != g_moduleLoggers.end()) {
  350. it->second->destroyLog();
  351. LogManager::getInstance().unregisterInstance(module);
  352. delete it->second;
  353. g_moduleLoggers.erase(it);
  354. }
  355. }
  356. // 获取所有已初始化的日志模块
  357. std::vector<std::string> getInitializedLogModules() {
  358. std::lock_guard<std::mutex> lock(g_moduleMutex);
  359. std::vector<std::string> modules;
  360. for (const auto& pair : g_moduleLoggers) {
  361. modules.push_back(pair.first);
  362. }
  363. return modules;
  364. }
  365. // 销毁所有日志模块
  366. void destroyAllLogModules() {
  367. std::lock_guard<std::mutex> lock(g_moduleMutex);
  368. for (auto& pair : g_moduleLoggers) {
  369. pair.second->destroyLog();
  370. LogManager::getInstance().unregisterInstance(pair.first);
  371. delete pair.second;
  372. }
  373. g_moduleLoggers.clear();
  374. }