本人施用log4cxx0.10.0,读取资源配置文件中存在中文乱码现象
本人使用log4cxx0.10.0,读取资源配置文件中存在中文乱码现象
大家好,如题,因为本人最近刚刚使用log4cxx,对其理解甚少,在使用log4cxx0.10.0时读取的资源配置文件中存在中文,路径中也存在中文字符,使得找不到中文路径,和提取的中文资源信息为一串"?",后来在网上查找资料发现在读取配置文件前的代码添加setlocale(LC_ALL,“zh_CN.UTF-8”)后,读取的中文信息变为了乱码,有谁知道怎么改写吗?
------解决方案--------------------
字符编码的问题,使用:
MultiByteToWideChar
WideCharToMultiByte
去转换编码
------解决方案--------------------
把配置文件存成ANSI格式的,一般直接读取不会出现乱码。如果出现乱码则参考2楼
------解决方案--------------------
摒弃log4cxx
使用这个:
大家好,如题,因为本人最近刚刚使用log4cxx,对其理解甚少,在使用log4cxx0.10.0时读取的资源配置文件中存在中文,路径中也存在中文字符,使得找不到中文路径,和提取的中文资源信息为一串"?",后来在网上查找资料发现在读取配置文件前的代码添加setlocale(LC_ALL,“zh_CN.UTF-8”)后,读取的中文信息变为了乱码,有谁知道怎么改写吗?
------解决方案--------------------
字符编码的问题,使用:
MultiByteToWideChar
WideCharToMultiByte
去转换编码
------解决方案--------------------
把配置文件存成ANSI格式的,一般直接读取不会出现乱码。如果出现乱码则参考2楼
------解决方案--------------------
摒弃log4cxx
使用这个:
#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 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;
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);