ソースを参照

Merge pull request #263 from BeyondWUXF/feature/BeyondWUXF/log_prefix

日志增加前缀
ruanshudong 1 年間 前
コミット
a972eba8cb
1 ファイル変更18 行追加0 行削除
  1. 18 0
      util/include/util/tc_logger.h

+ 18 - 0
util/include/util/tc_logger.h

@@ -849,6 +849,13 @@ namespace tars
 		 */
 		void enableSqareWrapper(bool bEnable) { _bHasSquareBracket = bEnable; }
 
+        /**
+         * @brief 每行日志的前缀
+         * @brief Prefix of each log line
+         * @param str
+         */
+        void setPrefix(const string &str) { _sPrefix = str; }
+
 		/**
 		* @brief TARS记日志
 		* @brief TARS Log
@@ -899,6 +906,11 @@ namespace tars
 		{
 			size_t n = 0;
 
+            if (!_sPrefix.empty())
+            {
+                n += snprintf(c + n, len - n, "%s%s", _sPrefix.c_str(), _sSepar.c_str());
+            }
+
 			if (hasFlag(TC_Logger::HAS_MTIME))
 			{
 				// auto time_now = chrono::system_clock::now();
@@ -1037,6 +1049,12 @@ namespace tars
 		 * Is [] added to the date part
 		 */
 		bool _bHasSquareBracket;
+
+        /**
+         * 前缀
+         * prefix
+         */
+        string _sPrefix;
 	};
 
 	template <typename WriteT, template <class> class RollPolicy>