- 论坛徽章:
- 0
|
vi中汉字的问题
模仿VC中的_DEBUG宏,写线程安全的打印宏TRACE。
很多时候调试起来还要靠这些东西,特别是多线程的时候。
将输出分级,请参考如下代码中的级别。
另外,配合配置文件,指定输出级别和模块。比如配置文件:
DebugLevel = 3
DebugAllModule = true
DebugLogFile = true
NoDebugModule =
DebugModule = Hello.cpp,\
Hello2.cpp,\
Hello3.cpp
WithFileName = true
WithLineNumber = false
表示输出级别为3,所有大于3的级别都不会有输出了。
另外,输出中会带有文件名、线程号,调试起多线程来很有力。
如果不是debug版,这个宏会被屏蔽。
以下的代码在windows和linux中通过
Trace.h
- // **********************************************************************
- //
- // Copyright (c) 2001-2004
- // StarMiddleware.net
- // www.StarMiddleware.net
- //
- // All Rights Reserved
- //
- // Author: Wang Kebo mep AT 263.net
- // Author: Huang Jie huangjie AT email.com
- // Author: Su Liang centuryfree AT yahoo.com.cn
- //
- // **********************************************************************
- // Version: 1.0.0
- #ifndef __Trace_h__
- #define __Trace_h__
- #ifdef _DEBUG
- //LEVEL0:关闭所有输出;
- //LEVEL1:发生致命错或异常(使得系统不可能继续运行下去);
- //LEVEL2:发生错误或异常,需要提示用户;
- //LEVEL3:输出系统运行的提示信息(比如:系统启动);
- //LEVEL4:指明系统正在进行的比较具体的动作(比如:容器初始化);
- //LEVEL5:输出trace的具体的位置,比如:输出方法名;
- //LEVEL6:输出具体的参数的值;
- #define LEVEL0 0
- #define LEVEL1 1
- #define LEVEL2 2
- #define LEVEL3 3
- #define LEVEL4 4
- #define LEVEL5 5
- #define LEVEL6 6
- #include <stdio.h>;
- #include <string.h>;
- #include <assert.h>;
- extern int debugLevel;
- extern char* debugModule;
- extern char* noDebugModule;
- extern bool debugAllModule;
- extern bool debugLogFile;
- extern bool withFileName;
- extern bool withLineNumber;
- extern FILE* log;
- char* noPathFileName(char* file);
- bool checkDebugLevel(int lvl);
- bool checkModule(const char* file);
- #ifdef WIN32
- #include <Windows.h>;
- #define _THREAD_ID GetCurrentThreadId()
- #define _SPIN_LOCK_STRING \
- __asm{ \
- __asm L1: \
- __asm lock dec lock_ \
- __asm js L2 \
- __asm jmp L3 \
- __asm L2: \
- __asm cmp lock_ ,0x0 \
- __asm rep nop \
- __asm jle L2 \
- __asm jmp L1 \
- __asm L3: \
- }
- #define _SPIN_UNLOCK_STRING __asm{ \
- __asm mov lock_, 0x1 \
- }
- #else /*#ifdef WIN32*/
- #include <pthread.h>;
- #define _THREAD_ID pthread_self()
- #ifdef LINUX
- #define _SPIN_LOCK_STRING \
- __asm__ __volatile__( \
- "\n1:\t" \
- "lock ; decb %0\n\t" \
- "js 2f\n" \
- ".subsection 1\n" \
- "2:\t" \
- "cmpb $0,%0\n\t" \
- "rep;nop\n\t" \
- "jle 2b\n\t" \
- "jmp 1b\n" \
- ".subsection 0\n" \
- :"=m" (lock_) : : "memory" \
- );
- #define _SPIN_UNLOCK_STRING \
- __asm__ __volatile__( \
- "movb $1,%0" \
- :"=m" (lock->;lock) : : "memory" \
- );
- #endif /*#ifdef LINUX*/
- #define _SPIN_LOCK_STRING
- #define _SPIN_UNLOCK_STRING
- #endif /*#ifdef WIN32*/
- extern volatile unsigned int lock_;
- class SpinLock
- {
- public:
- SpinLock(){ _SPIN_LOCK_STRING }
- ~SpinLock(){ _SPIN_UNLOCK_STRING }
- };
- #define _TRACE_BEGIN(lvl) do{ \
- if(checkDebugLevel(lvl) && \
- checkModule(noPathFileName(__FILE__))) \
- { \
- SpinLock lock;
- #define _TRACE_END } \
- } while(0)
- #define TRACE0(lvl,s) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s); \
- } \
- _TRACE_END
- #define TRACE1(lvl,s,v1) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1); \
- } \
- _TRACE_END
- #define TRACE2(lvl,s,v1,v2) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1,v2); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1,v2); \
- } \
- _TRACE_END
- #define TRACE3(lvl,s,v1,v2,v3) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1,v2,v3); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1,v2,v3); \
- } \
- _TRACE_END
- #define TRACE4(lvl,s,v1,v2,v3,v4) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1,v2,v3,v4); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1,v2,v3,v4); \
- } \
- _TRACE_END
- #define TRACE5(lvl,s,v1,v2,v3,v4,v5) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1,v2,v3,v4,v5); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1,v2,v3,v4,v5); \
- } \
- _TRACE_END
- #define TRACE6(lvl,s,v1,v2,v3,v4,v5,v6) _TRACE_BEGIN(lvl) \
- if(log != NULL) \
- { \
- if(withFileName) \
- { \
- fprintf(log,"%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- fprintf(log," %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- fprintf(log,"\n"); \
- fprintf(log,"T@%d ",_THREAD_ID); \
- fprintf(log,s,v1,v2,v3,v4,v5,v6); \
- fflush(log); \
- } \
- else \
- { \
- if(withFileName) \
- { \
- printf("%s",__FILE__); \
- } \
- if(withLineNumber) \
- { \
- printf(" %d",__LINE__); \
- } \
- if(withFileName||withLineNumber) \
- printf("\n"); \
- printf("T@%d ",_THREAD_ID); \
- printf(s,v1,v2,v3,v4,v5,v6); \
- } \
- _TRACE_END
- #define ASSERT(exp) assert(exp)
- class InitHelper
- {
- void LoadProfile();
- public:
- InitHelper();
- };
- #else /*#ifdef _DEBUG*/
- #define TRACE0(lvl,s)
- #define TRACE1(lvl,s,v1)
- #define TRACE2(lvl,s,v1,v2)
- #define TRACE3(lvl,s,v1,v2,v3)
- #define TRACE4(lvl,s,v1,v2,v3,v4)
- #define TRACE5(lvl,s,v1,v2,v3,v4,v5)
- #define TRACE6(lvl,s,v1,v2,v3,v4,v5,v6)
- #define ASSERT(exp)
- #endif /*#ifdef _DEBUG*/
- #endif /*#ifndef __Trace_h__*/
复制代码
Trace.cpp
- // **********************************************************************
- //
- // Copyright (c) 2001-2004
- // StarMiddleware.net
- // www.StarMiddleware.net
- //
- // All Rights Reserved
- //
- // Author: Wang Kebo mep AT 263.net
- // Author: Huang Jie huangjie AT email.com
- // Author: Su Liang centuryfree AT yahoo.com.cn
- //
- // **********************************************************************
- // Version: 1.0.0
- #ifdef _DEBUG
- #include <Trace.h>;
- #include <stdlib.h>;
- #ifdef WIN32
- #include <direct.h>;
- #include <windows.h>;
- #else
- #include <unistd.h>;
- #endif
- int debugLevel = LEVEL6;
- bool debugAllModule = false;
- bool withFileName = false;
- bool withLineNumber = false;
- bool debugLogFile = false;
- char* debugModule = NULL;
- char* noDebugModule = NULL;
- FILE* log = NULL;
- volatile unsigned int lock_ = 1;
- InitHelper dummy;
- char* noPathFileName(char* file)
- {
- char* tempName = file;
- char* noPathName = file;
- #ifdef WIN32
- while(tempName = strstr(tempName,"\\"))
- #else
- while(tempName = strstr(tempName,"/"))
- #endif
- {
- noPathName = ++tempName;
- }
- return noPathName;
- }
- bool checkDebugLevel(int lvl)
- {
- if( lvl <= debugLevel )
- return true;
- else
- return false;
- }
- bool checkModule(const char* file)
- {
- char* copy = _strlwr(_strdup((char*)file));
- if(noDebugModule != NULL)
- {
- if(strstr(_strlwr(noDebugModule),copy))
- {
- free(copy);
- return false;
- }
- }
- if(debugAllModule)
- {
- free(copy);
- return true;
- }
- if(debugModule == NULL)
- {
- free(copy);
- return false;
- }
- if(strstr(_strlwr(debugModule),copy))
- {
- free(copy);
- return true;
- }
- else
- {
- free(copy);
- return false;
- }
- }
- InitHelper::InitHelper()
- {
- const char* homeDir = getenv("CCM_HOME");
-
- if(homeDir != NULL && strcmp(homeDir, "") != 0)
- {
- chdir(homeDir);
- }
- else
- {
- chdir("c:\\ccm_home");
- }
- LoadProfile();
- if(debugLogFile)
- {
- char exeName[128];
- #ifdef WIN32
- GetModuleFileName(NULL, exeName, 128);
- char* index;
- char* old = exeName;
- while(index = strstr(old, "\\"))
- {
- old = index + 1;
- }
- strcpy(exeName, old);
- exeName[strlen(exeName)-4] = 0;
- DWORD id = GetCurrentProcessId();
- sprintf(exeName + strlen(exeName), "%d",id);
- strcat(exeName, ".log");
- #else
- strcpy(exeName, "trace.log");
- #endif
- log = fopen(exeName,"w+");
- }
- }
- void
- InitHelper::LoadProfile()
- {
- char rawBuf[1024];
- char buf[1024];
- char skip;
- char* value;
- char* oldBuf = rawBuf;
- char* newBuf = buf;
- FILE* iniFile = fopen("DebugConfig.ini","r");
- if(iniFile == NULL)
- {
- printf("Can't open `DebugConfig.ini\' file.\n");
- return;
- }
- while(!feof(iniFile))
- {
- oldBuf = rawBuf;
-
- fread(oldBuf++,sizeof(char),1,iniFile);
- while(*(oldBuf-1) != '\n' && !feof(iniFile))
- {
- if(*(oldBuf-1) == '\\')
- {
- oldBuf--;
- do
- {
- fread(&skip,sizeof(char),1,iniFile);
- } while(skip != '\n' && !feof(iniFile) );
- }
- fread(oldBuf++,sizeof(char),1,iniFile);
- }
-
- if(!feof(iniFile))
- {
- if(*(oldBuf-1) == '\n')
- *(oldBuf-1) = 0;
- else
- *oldBuf =0;
- }
- else
- *(oldBuf-1) = 0;
- oldBuf = rawBuf;
- newBuf = buf;
- while(*oldBuf)
- {
- if((*oldBuf) != ' ' && (*oldBuf) != '\t')
- {
- *newBuf = *oldBuf;
- newBuf ++;
- oldBuf ++;
- }
- else
- oldBuf ++;
- }
- *newBuf = 0;
- //printf("%s\n",buf);
- if(strncmp(buf,"DebugLevel",strlen("DebugLevel"))==0)
- {
- value = buf + strlen("DebugLevel") + 1;
- debugLevel = atoi(value);
- }
- else if(strncmp(buf,"DebugAllModule",strlen("DebugAllModule"))==0)
- {
- value = buf + strlen("DebugAllModule") + 1;
- if(strcmp(value,"true") == 0)
- debugAllModule = true;
- else if(strcmp(value,"false") == 0)
- debugAllModule = false;
- }
- else if(strncmp(buf,"WithFileName",strlen("WithFileName"))==0)
- {
- value = buf + strlen("WithFileName") + 1;
- if(strcmp(value,"true") == 0)
- withFileName = true;
- else if(strcmp(value,"false") == 0)
- withFileName = false;
- }
- else if(strncmp(buf,"WithLineNumber",strlen("WithLineNumber"))==0)
- {
- value = buf + strlen("WithLineNumber") + 1;
- if(strcmp(value,"true") == 0)
- withLineNumber = true;
- else if(strcmp(value,"false") == 0)
- withLineNumber = false;
- }
- else if(strncmp(buf,"DebugLogFile",strlen("DebugLogFile"))==0)
- {
- value = buf + strlen("DebugLogFile") + 1;
- if(strcmp(value,"true") == 0)
- debugLogFile = true;
- else if(strcmp(value,"false") == 0)
- debugLogFile = false;
- }
- else if(strncmp(buf,"DebugModule",strlen("DebugModule"))==0)
- {
- value = buf + strlen("DebugModule") + 1;
- debugModule = strdup(value);
- }
- else if(strncmp(buf,"NoDebugModule",strlen("NoDebugModule"))==0)
- {
- value = buf + strlen("NoDebugModule") + 1;
- noDebugModule = strdup(value);
- }
- }
- fclose(iniFile);
- }
- #endif
复制代码 |
|