티스토리 뷰

Programming/Library

[Poco] 파일 로그 쓰기

HwansChoi 2016. 5. 4. 14:25


Poco라이브러리를 이용하여 파일에 로그를 써보자.


우선 로그 파일을 생성할 폴더를 만들어야겠다. 디렉토리 생성에 대해서는 이전 포스팅를 참고하면 된다.

Poco를 이용한 디렉토리 생성하기


자 그럼 파일에 로그를 써보자!!!


#include "Poco/FormattingChannel.h"

#include "Poco/PatternFormatter.h"

#include "Poco/DateTime.h"

#include "Poco/FileChannel.h"


// 로그 파일명을 날짜에 맞춰 만든다. (Poco 라이브러리의 DateTime을 이용)

std::string strFileName = Poco::format("%d%02d%02d", Poco::DateTime().year(), Poco::DateTime().month(), Poco::DateTime().day());

// Poco 라이브러리의 PatternFormatter를 이용하여 내부 포멧 설정

Poco::FormattingChannel* pFCTime = new Poco::FormattingChannel(new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S: %t"));

// 체널을 FileChannel로 세팅한다. ( 파일이 아닌 콘솔에 쓸거면 ConsoleChannel을 세팅한다.)

// 자신이 설정한 strFullDir 폴더 경로에 파일을 지정해 준다.

pFCTime->setChannel(new Poco::FileChannel(Poco::format("%s/%s.log", strFullDir, strFileName)));

// 로그 파일을 생성한다.

pFCTime->open();

// 세팅한 정보를 이용하여 Poco 라이브러리의 Logger를 생성한다.

// 첫번째 인자로 넣은 "MyLogger"을 이용하여 외부에서도 GET 해서 사용 할 수 있으니 네이밍을 잘 해놓자~

Poco::Logger& Logger = Poco::Logger::create("MyLogger", pFCTime, Poco::Message::PRIO_INFORMATION);


//생성한 Logger를 이용하여 로그에 기록해 보자.

Logger.information("File logger cteated.");



위와 같이 하면 자신이 원하는 위치에 로그파일이 생성되고 그 로그파일 안에 로그가 남겨져 있을 것이다.


이제 외부에서 가져다 사용할 경우 아래와 같이 우리가 지정한 "MyLogger" 를 이용하여 GET 해서 사용할 수 있다.

Poco::Logger::get("FileLogger").information("file Logger TEST");


// 이와같은 if문으로 해당 Logger가 있는지 찾아볼 수도 있다.

if (Poco::Logger::has("Application") == 0)




참고 : http://pocoproject.org/docs/

'Programming > Library' 카테고리의 다른 글

[VTK] 이미지를 클립보드 저장하기  (0) 2016.08.30
[VTK] 이미지 저장하기  (0) 2016.08.25
[Poco] 디렉토리 생성 하기  (0) 2016.05.04
[VTK] VTK Chart point size 변경  (0) 2016.04.15
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함