首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

log4c的一些有关问题

2012-09-10 
log4c的一些问题#define log4p_category_define(aaa, bbb) static const char *log4p_category_define_##a

log4c的一些问题
#define log4p_category_define(aaa, bbb) static const char *log4p_category_define_##aaa = bbb; \
  static log4c_category_t *aaa __attribute__ ((unused)) = NULL;
问题一:static const char *log4p_category_define_##aaa = bbb;这句中##是什么语法,用来干嘛的?
问题二:static log4c_category_t *aaa __attribute__ ((unused)) = NULL;这句话什么意思?
问题三:我在调用log4c时不知道为什么中间被插进了时间[19700101 09:08:32.000],为什么会这样,如何修改?
求高人指点迷津!!!

[解决办法]
#与##在宏定义中的--宏展开
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
printf("%s\n", h(f(1,2))); // 12
printf("%s\n", g(f(1,2))); // f(1,2)
return 0;
}
宏展开时:
如果宏定义以#开头,不展开参数,直接替换。
故g(f(1,2))--->#f(1,2)--->"f(1,2)";
如果宏定义不以#开头,展开参数,直接替换,由外层向里层,如果碰到的是#开头的宏,不继续往里层展开,往外层展开。
由外层向里层,如果碰到的是以非#开头的宏,继续往里层走,直至最里层,开始一层层往外层展开。
故h(f(1,2))--->h(12)--->g(12)---->#12----->"12"。
PS:
##在宏中定义,是字符连接符
如a##b##c 等同于 "abc"
#在宏开头出现,是表示宏展开的方式不同
#a 等同于"a"
#abc 等同于 "abc"
复杂的:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main()
{
char a = 'a';
cout<<g(a)<<endl; // a
cout<<g(g(a))<<endl; // a
printf("%s\n", h(f(1,2))); // 12
printf("%s\n", g(f(1,2))); // f(1,2)
printf("%s\n", g(h(f(1,2)))); // h(f(1,2))
printf("%s\n", h(g(f(1,2)))); // "f(1,2)"
printf("%s\n", h(h(f(1,2)))); // "12"
system("pause");
return 0;
}
预处理后的:(在编译选项中添加/EP /P后编译生成的.i文件)
int main()
{
char a = 'a';
cout<<"a"<<endl;
cout<<"g(a)"<<endl;
printf("%s\n", "12");
printf("%s\n", "f(1,2)");
printf("%s\n", "h(f(1,2))");
printf("%s\n", "\"f(1,2)\"");
printf("%s\n", "\"12\"");
system("pause");
return 0;
}
---------------------------------------------------
宏解析
1. ##操作符
##操作符它的作用是在替代表中将其前后的参数连接成为一个预处理符号,它不能出现于宏替代表的开端和末尾。
例:
#define concat(s,t) s##t
#define AAA ABC
concat(A, AA)
将被替换成
ABC
2. 重新扫描和替换
在替换列表中的所有参数替换过之后,预处理器将对结果token序列重新扫描以便对其中的宏再次替换。
当正在替换的宏在其替换列表中发现自身时,就不再对其进行替换。今儿,在任何正在嵌套替换的宏的替换过程中遇到正被替换的宏就对其不再进行替换(防止递归)。
例:
#define ROOT AAA CCC
#define AAA ROOT
ROOT
将被替换成
ROOT CCC

[解决办法]
仅供参考

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef WIN32    #include <windows.h>    #include <io.h>#else    #include <unistd.h>    #include <sys/time.h>    #include <pthread.h>    #define  CRITICAL_SECTION   pthread_mutex_t    #define  _vsnprintf         vsnprintf#endif//Log{#define MAXLOGSIZE 20000000#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))#include <time.h>#include <sys/timeb.h>#include <stdarg.h>char logfilename1[]="MyLog1.log";char logfilename2[]="MyLog2.log";char logstr[16000];char datestr[16];char timestr[16];char mss[4];CRITICAL_SECTION cs_log;FILE *flog;#ifdef WIN32void Lock(CRITICAL_SECTION *l) {    EnterCriticalSection(l);}void Unlock(CRITICAL_SECTION *l) {    LeaveCriticalSection(l);}#elsevoid Lock(CRITICAL_SECTION *l) {    pthread_mutex_lock(l);}void Unlock(CRITICAL_SECTION *l) {    pthread_mutex_unlock(l);}#endifvoid LogV(const char *pszFmt,va_list argp) {    struct tm *now;    struct timeb tb;    if (NULL==pszFmt||0==pszFmt[0]) return;    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;    ftime(&tb);    now=localtime(&tb.time);    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );    sprintf(mss,"%03d",tb.millitm);    printf("%s %s.%s %s",datestr,timestr,mss,logstr);    flog=fopen(logfilename1,"a");    if (NULL!=flog) {        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);        if (ftell(flog)>MAXLOGSIZE) {            fclose(flog);            if (rename(logfilename1,logfilename2)) {                remove(logfilename2);                rename(logfilename1,logfilename2);            }            flog=fopen(logfilename1,"a");            if (NULL==flog) return;        }        fclose(flog);    }}void Log(const char *pszFmt,...) {    va_list argp;    Lock(&cs_log);    va_start(argp,pszFmt);    LogV(pszFmt,argp);    va_end(argp);    Unlock(&cs_log);}//Log}int main(int argc,char * argv[]) {    int i;#ifdef WIN32    InitializeCriticalSection(&cs_log);#else    pthread_mutex_init(&cs_log,NULL);#endif    for (i=0;i<10000;i++) {        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);    }#ifdef WIN32    DeleteCriticalSection(&cs_log);#else    pthread_mutex_destroy(&cs_log);#endif    return 0;} 

热点排行