메뉴 건너뛰기

OBG

Programming

C/C++
2013.07.28 02:44

Simplified Logger Class

MoA
조회 수 438 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
#include <iostream>
#include <fstream>

using namespace std;

class Logger
{
 public:
   Logger(const char* filename = "program.log")
     {
       logfile = new fstream(filename, fstream::out);
     }
   ~Logger()
     {
       logfile->close();
       delete logfile;
     }

   Logger& operator<<(const char* msg)
     {
       *logfile << msg;
       return *this;
     }
   Logger& operator<<(const int val)
     {
       *logfile << val;
       return *this;
     }
 private:
   fstream* logfile;
};

class MyClass
{
 public:
   MyClass(const int n)
     {
       data = n;
     }

   ~MyClass()
     {
     }

   friend ostream& operator<<(ostream& o, const MyClass& v)
     {
       return o << "MyClass data = " << v.data << "n";
     }

 private:
   int data;
};

int main()
{
   Logger mainlog;
   MyClass a(10);

   // These work fine
   cout << a;
   mainlog << "Bla " << 200 << "n";

   // This doesn't work
   //mainlog << a;

   return 0;
}



?

  1. Programming 게시판 관련

  2. InvalidateRect 함수, flag성 메세지

  3. CreateThread, ExitThread, GetExitCodeThread ...

  4. High-speed Charting Control

  5. CSpreadSheet

  6. Which Font is the default for MFC Dialog Controls

  7. 구글 검색 알고리즘의 원리

  8. CSS, 자바스크립트 강좌

  9. 안드로이드 어플 개발 사이트

  10. CString class

  11. RAND_MAX

  12. Embedding Python in C/C++

  13. Quake 3 source code and review

  14. [첫게임 만들기] 5. 적을 생성하자

  15. Visual Studio Debug Tips

  16. Buffer Overrun

  17. Great summary cheat sheet (OpenCV)

  18. Redmine 설치

  19. CFormView

  20. C Runtime 환경의 메모리 릭 잡는 방법 (Memory leak)

  21. 스레드 강좌 + CreateThread() 와 _beginthreadex() 함수의 차이

Board Pagination Prev 1 ... 2 3 4 5 6 7 8 9 10 11 ... 15 Next
/ 15
위로