2012-02-05 2 views
1

로그 파일을 만들어야합니다. 나는 어떤 오류가 로그 파일에 넣어야하는지 모른다. 나는 다음과 같은 코드가 있습니다 (그러나 파일의 끝을 writting되지 않는 이유 나도 몰라) 내가 pLOg-을 가지고 Test.cpp에에서C++에서 로그 파일을 만드는 방법

log.cpp

#include "log.h" 
#include <ctime> 
#include <iostream> 
using namespace std; 
Log::Log(char* filename) { 
//ofstream m_stream(filename); 
m_stream.open(filename); 

} 

> 쓰기 (기음). 파일을 다시 쓰는 이유와 왜 파일을 쓰는 지 이해할 수 없습니다.

void Log::Write(char* logline) 
{ 
time_t rawtime; 
    struct tm * timeinfo; 

    time (&rawtime); 
    timeinfo = localtime (&rawtime); 
    m_stream.seekp (0, ios::end); 
    while ((m_stream.eof())){} 
    { 
    m_stream <<"current time: "<< asctime (timeinfo) <<" "<< logline << endl; 
    } 

} 

Log::~Log(){ 

    m_stream.close(); 
} 

log.h

#include <fstream> 

using namespace std; 

class Log { 
    public: 
    Log(char* filename); 
    ~Log(); 
    void Write(char* logline); 
private: 
    ofstream m_stream; 
}; 
+0

관련 : HTTP : //log4cpp.sourceforge. net/ –

+0

'std :: ios :: out | std :: ios :: end', 그래서 당신은 파일의 끝에서 시작합니다. – Xeo

+0

어디에 추가해야합니까? .open (file, std :: ios :: out | std :: ios :: end) – user1165435

답변

3
m_stream.open(filename, ios_base::app | ios_base::out); 
+0

아, 맞아,'app' ... +1 – Xeo

+0

여기에 어떤 유형의 오류를 포함시켜야합니까? 광고 어떻게이 오류를 찾을 수 있습니까? – user1165435

+0

실수로 기술적으로 논리 오류가 발생하지 않으며 오류 코드가이를 막지 않습니다. 그렇지 않으면 파일이 "if (m_stream) ..."또는 "if (m_stream.open() ..."과 같이 제대로 열렸는지 확인하십시오. – Duck

관련 문제