log4cxx 输出线程号

byxbai1989 2014-04-01 03:47:40
log4cxx 用%t 输出线程号。

为什么我的输出的是线程地址?0x7fa8e23b0700

是库的问题么?

谢谢!
...全文
331 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
byxbai1989 2014-04-02
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
[quote=引用 2 楼 byxbai1989 的回复:] [quote=引用 1 楼 zhao4zhong1 的回复:]
log4cxx 用%t 输出线程号。是在库内部做的么? [/quote] log4cxx是开源的吧, 单步跟踪到处理%t的地方一探究竟。[/quote]
引用 4 楼 zhcosin 的回复:
谁告诉你 "0x" 打头的就一定是地址,人家是用的十六进制表示好不
引用 5 楼 qq120848369 的回复:
这是log4cxx内部封装的format解释方法, 人家看见format串里有%t就会把线程号用16进制打印出来,没什么稀奇的,了解一下vsnprintf系列函数。
log4cxx 0.10.0版本,对线程的输出见:getCurrentThreadName()函数。
qq120848369 2014-04-01
  • 打赏
  • 举报
回复
这是log4cxx内部封装的format解释方法, 人家看见format串里有%t就会把线程号用16进制打印出来,没什么稀奇的,了解一下vsnprintf系列函数。
zhcosin 2014-04-01
  • 打赏
  • 举报
回复
谁告诉你 "0x" 打头的就一定是地址,人家是用的十六进制表示好不
赵4老师 2014-04-01
  • 打赏
  • 举报
回复
引用 2 楼 byxbai1989 的回复:
[quote=引用 1 楼 zhao4zhong1 的回复:]
log4cxx 用%t 输出线程号。是在库内部做的么? [/quote] log4cxx是开源的吧, 单步跟踪到处理%t的地方一探究竟。
byxbai1989 2014-04-01
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
log4cxx 用%t 输出线程号。是在库内部做的么?
赵4老师 2014-04-01
  • 打赏
  • 举报
回复
建议摒弃logcxx,使用我这个土造MyLog
#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 MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    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);
            }
        } else {
            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;
}
//1-78行添加到你带main的.c或.cpp的那个文件的最前面
//81-85行添加到你的main函数开头
//89-93行添加到你的main函数结束前
//在要写LOG的地方仿照第87行的写法写LOG到文件MyLog1.log中
GetCurrentThreadId The GetCurrentThreadId function returns the thread identifier of the calling thread. DWORD GetCurrentThreadId(VOID) Parameters This function has no parameters. Return Values The return value is the thread identifier of the calling thread. Remarks Until the thread terminates, the thread identifier uniquely identifies the thread throughout the system. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, GetCurrentThread

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧