#define
지시문과 매크로를 사용하여 코드에서 log4cxx를 사용하여 캡슐화하려고했습니다. 코드가 컴파일되지만 실행시 스트림 개체가 올바르게 초기화되지 않았으므로 액세스 위반이 발생합니다.<< 연산자와 매크로를 사용하여 log4cxx 액세스 예외
이 같은 log4cxx 플러그 모양 만들기 위해 시도 헤더 파일 :
#ifdef USE_LOG4CXX
#include "log4cxx/logger.h"
#define LOG_DEBUG(msg) LOG4CXX_DEBUG(logger, msg)
#define LOG_INFO(msg) LOG4CXX_INFO(logger, msg)
#define LOG_WARN(msg) LOG4CXX_WARN(logger, msg)
#define LOG_ERROR(msg) LOG4CXX_ERROR(logger, msg)
#define LOGGER_DECL static const log4cxx::LoggerPtr logger
#define LOGGER_INIT(source,name) const log4cxx::LoggerPtr source::logger = log4cxx::Logger::getLogger(#name);
#else // use some other log method, e.g. stdout which worked fine
.cpp 파일 로깅과 같이 수행합니다
LOG_INFO("state(): " << old_state << " ==> " << new_state);
전처리가 .cpp 파일을 확장을 :
{ if (logger->isInfoEnabled()) { ::log4cxx::helpers::MessageBuffer oss_; logger->forcedLog(::log4cxx::Level::getInfo(), oss_.str(oss_ << "state(): " << state_ << " ==> " << new_state), ::log4cxx::spi::LocationInfo("c:\\dev\\ezx-capi\\iserver-api\\iserver_client.cpp", __FUNCSIG__ , 190)); }};
old_state 및 new_state의 데이터 유형은입니다.. 그것은, 디버거에서
std::basic_ostream<char>& operator<<(CharMessageBuffer& os, const V& val) {
return ((std::basic_ostream<char>&) os) << val;
}
CharMessageBuffer
개체가 초기화되지 않은 std::basic_streambuf
멤버가 같은 문제가보고,이 값을 추가 간다 그래서 때
실행
, 응용 프로그램에 실패 죽는다. (그래도 난 그 설명에 대해 확실하지 않다.) 모든 방법 드릴 다운, 그것은 std::basic_ostream
에서 사망 :
_Myt& __CLR_OR_THIS_CALL operator<<(int _Val)
{ // insert an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this); // dies right here, in the constructor
을 어쨌든, 나는 이것이 어떻게 매크로를 사용하고 함께 할 수있는 뭔가가 실현 LOG4CXX를 호출합니다. (필자는 USE_LOG4FXX가 정의되지 있었을 때, 모든 로그 문은 std::cout
가고 있었다 그것은 괜찮 았는데.)
UPDATE
정보또 다른 조각 - 내가 호출 할 때이는 실패한 것 같다 정적 라이브러리 내에서 로깅. EXE 프로젝트에서 같은 (또는 비슷한) 매크로를 사용하면 전혀 실패하지 않습니다. 그래서 어떤 종류의 별도의 테스트 응용 프로그램에서이 문제를 재현 할 수 없습니다.