Просмотр исходного кода

Windows版本的clog。

VC6是不支持可变参数宏的,VC2005支持。

使用如下:
#define TRACE(pszFormat, ...) \
CTL_CommonLogger::Output(LOGGER, LEVEL_TRACE, pszFormat, __VA_ARGS__);

其中,__VA_ARGS__就是不定参数。

注意:直接这样,是没法编译通过的,因为这个宏要求编译器是VC2002(VC7.0)以上的版本,也就是说,_MSC_VER宏应该是1400以上的值。所以,使用前,应该加上如下的定义:
#ifndef _MSC_VER
#define _MSC_VER 1600
#endif
1600也就是VC8.0。

补充:改一下。CTL_CommonLogger::Output是一个类的静态成员函数。
类似,可以使用下面的写法:
#define PRINT(x, ...)    \
print(x, __VA_ARGS__);

print函数的原型:int Print(char * x, ...);

不定参数函数的实现,可以参考Windows的TRACE宏或ATLTRACE宏。
sat23 3 лет назад
Родитель
Сommit
eabbac7bb3

+ 6 - 0
TCLTools/TCLTools/TCLTools.cpp

@@ -63,6 +63,12 @@ BOOL CTCLToolsApp::InitInstance()
 	// 例如修改为公司或组织名
 	SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
 
+    log_init(LL_DEBUG, "mysql", ".");
+    LOG_TRACE("%s %d %d", "较好的Log打印", 1, 150);
+    LOG_NOTICE("%s %d %d", "较好的Log打印", 1, 1520);
+    LOG_WARN("%s %d %d", "较好的Log打印111", 1, 410);
+    LOG_ERROR("%s %d %d", "较好的Log打印222", 1, 310);
+
 	CTCLToolsDlg dlg;
 	m_pMainWnd = &dlg;
 	INT_PTR nResponse = dlg.DoModal();

+ 20 - 0
TCLTools/TCLTools/TCLTools.vcproj

@@ -290,6 +290,26 @@
 				>
 			</File>
 		</Filter>
+		<Filter
+			Name="log"
+			>
+			<File
+				RelativePath=".\CritSection.h"
+				>
+			</File>
+			<File
+				RelativePath=".\log.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\log.h"
+				>
+			</File>
+			<File
+				RelativePath=".\log_macro.h"
+				>
+			</File>
+		</Filter>
 		<File
 			RelativePath=".\command.data"
 			>

+ 1 - 1
TCLTools/TCLTools/stdafx.cpp

@@ -6,4 +6,4 @@
 #include "stdafx.h"
 
 
-TCLCommand g_tclCommand;
+TCLCommand g_tclCommand; 

+ 2 - 0
TCLTools/TCLTools/stdafx.h

@@ -47,6 +47,8 @@
 
 extern TCLCommand g_tclCommand;
 
+#include "log.h"
+#include "log_macro.h"
 
 #ifdef _UNICODE
 #if defined _M_IX86