로그인

검색

C/C++
2013.07.28 02:44

Simplified Logger Class

MoA
조회 수 12793 추천 수 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;
}



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
공지 Tool/etc Programming 게시판 관련 2 MoA 2014.11.01 108942
100 Tool/etc 쿠버네티스 클러스터 OBG 2022.11.11 12551
99 Web defer, async 스크립트 OBG 2023.01.10 18456
98 서버 Caching in Node.js to optimize app performance OBG 2023.01.16 18821
97 Tool/etc How To Set Up Multi-Factor Authentication for SSH on Ubuntu 20.04 OBG 2023.01.17 20760
96 Database 수신 기한이 지난 데이터를 MySQL에서 삭제할 때의 이야기 OBG 2023.01.25 9079
95 Python FastAPI 톺아보기 - 부제: python 백엔드 봄은 온다 OBG 2023.01.25 12119
94 Site 개발에 도움되는 사이트 (초보 개발자 꿀팁) OBG 2023.01.28 11634
93 서버 Caching In Node.js Applications OBG 2023.03.03 18146
92 Deeplearning LLaMA: INT8 edition OBG 2023.03.09 14444
91 서버 PM2를 활용한 Node.js 무중단 서비스하기 OBG 2023.03.09 9250
90 Tool/etc 잡담) AWS에 서버 띄워 놓으니 벼라별 리퀘스트가 다 날아 오네요 OBG 2023.03.11 12722
89 Deeplearning 추천 시스템 OBG 2023.03.30 13382
88 Deeplearning Reinforcement Learning for Dynamic Pricing Suggestion OBG 2023.04.01 19744
87 서버 SSH-Tunneling을 통한 MySQL 서버 연결 OBG 2023.04.21 18528
86 서버 Design a Basic Search Engine (Google or Bing) | System Design Interview Prep OBG 2023.05.27 17854
85 Python Numpy의 axis 변경 OBG 2023.06.09 21608
84 Deeplearning [한빛미디어] 머신러닝·딥러닝 도서 선택 가이드 OBG 2023.06.11 11961
83 LLM LLM 출력 속도 24배 높여주는 라이브러리 등장했다 OBG 2023.06.30 11763
82 서버 Debugging Node.js Memory Leaks: How to Detect, Solve or Avoid Them in Applications OBG 2023.07.04 18900
81 Deeplearning PyTorch 딥러닝 챗봇 OBG 2023.07.04 19559
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17