Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)Obtain a std::ostream either from std::cout or std::ofstream(file)

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)

added 279 characters in body
Source Link
MasterGberry
  • 2.9k
  • 6
  • 40
  • 60

EDIT2: An error I am now receiving with the new code below error : 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected

// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE };     // Needed by default  Log(Mode,const char **file = NULL); ~Log();   Log(Log// &l);Writing methods void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream *out;out; }; #endif   // log.cpp #include "log.h" #include <iostream> #include <stdlib.h> #include <time.h> Log::Log(Mode mode = STDOUT,const char *file) { if (modefile ==!= FILENULL) { of.open(file); buf = of.rdbuf(); mode = FILE; } else   if (mode == STDOUT){ buf = std::cout.rdbuf();  mode = STDOUT; std::ostream}  tmp // Attach to out out.rdbuf(buf); }  Log::~Log() {  this->outif =(mode &tmp;== FILE)  of.close(); } void Log::~Logwrite(std::string s) {  out << s << std::endl; } void Log::Logwrite(Logchar &l*s) {  out << s << std::endl; } 
// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE }; Log(Mode, char *); ~Log(); Log(Log &l); void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream *out; }; #endif // log.cpp #include "log.h" #include <iostream> Log::Log(Mode mode = STDOUT, char *file) { if (mode == FILE) { of.open(file); buf = of.rdbuf(); } else if (mode == STDOUT) buf = std::cout.rdbuf(); std::ostream tmp(buf); this->out = &tmp; } Log::~Log() { } Log::Log(Log &l) { } 

EDIT2: An error I am now receiving with the new code below error : 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected

// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE };     // Needed by default  Log(const char *file = NULL); ~Log();   // Writing methods void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream out; }; #endif   // log.cpp #include "log.h" #include <iostream> #include <stdlib.h> #include <time.h> Log::Log(const char *file) { if (file != NULL) { of.open(file); buf = of.rdbuf(); mode = FILE; } else   { buf = std::cout.rdbuf();  mode = STDOUT; }   // Attach to out out.rdbuf(buf); }  Log::~Log() {  if (mode == FILE)  of.close(); } void Log::write(std::string s) {  out << s << std::endl; } void Log::write(char *s) {  out << s << std::endl; } 
added 30 characters in body
Source Link
MasterGberry
  • 2.9k
  • 6
  • 40
  • 60

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)

The only difference between this thread and my own is that I want to do it inside of a class. Looking at the solution though they use std::ostream out(buf) and construct the ostream on the fly with buf. How can i declare this properly in my Log class to be able to construct the "out" object only once i enter my Log constructor?

I took a quick stab at it below but I am not sure if it is correct or if I am on the right track. Appreciate any help, thanks.

EDIT: I want to be able to do out << "Some string" << endl; after i get this Log class working properly.

// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE }; Log(Mode, char *); ~Log(); Log(Log &l); void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream *out; }; #endif   // log.cpp #include "log.h" #include <iostream> Log::Log(Mode mode = STDOUT, char *file) { if (mode == FILE) { of.open(file); buf = of.rdbuf(); } else if (mode == STDOUT) buf = std::cout.rdbuf(); std::ostream tmp(buf); this->out = &tmp; } Log::~Log() { } Log::Log(Log &l) { } 

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)

The only difference between this thread and my own is that I want to do it inside of a class. Looking at the solution though they use std::ostream out(buf) and construct the ostream on the fly with buf. How can i declare this properly in my Log class to be able to construct the "out" object only once i enter my Log constructor?

I took a quick stab at it below but I am not sure if it is correct or if I am on the right track. Appreciate any help, thanks.

EDIT: I want to be able to do out << "Some string" << endl; after i get this Log class working properly.

#include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE }; Log(Mode, char *); ~Log(); Log(Log &l); void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream *out; }; #endif #include "log.h" #include <iostream> Log::Log(Mode mode = STDOUT, char *file) { if (mode == FILE) { of.open(file); buf = of.rdbuf(); } else if (mode == STDOUT) buf = std::cout.rdbuf(); std::ostream tmp(buf); this->out = &tmp; } Log::~Log() { } Log::Log(Log &l) { } 

I am trying to create a Log class for my project at school. It needs to either be able to write information to the stdout or to a file depending on the parameters it is passed. I was looking into how to do this and I stumbled upon a thread with a similar question here: Obtain a std::ostream either from std::cout or std::ofstream(file)

The only difference between this thread and my own is that I want to do it inside of a class. Looking at the solution though they use std::ostream out(buf) and construct the ostream on the fly with buf. How can i declare this properly in my Log class to be able to construct the "out" object only once i enter my Log constructor?

I took a quick stab at it below but I am not sure if it is correct or if I am on the right track. Appreciate any help, thanks.

EDIT: I want to be able to do out << "Some string" << endl; after i get this Log class working properly.

// log.h #include <string> #include <fstream> #ifndef LOG_H_ #define LOG_H_ class Log { public: enum Mode { STDOUT, FILE }; Log(Mode, char *); ~Log(); Log(Log &l); void write(char *); void write(std::string); private: Mode mode; std::streambuf *buf; std::ofstream of; std::ostream *out; }; #endif   // log.cpp #include "log.h" #include <iostream> Log::Log(Mode mode = STDOUT, char *file) { if (mode == FILE) { of.open(file); buf = of.rdbuf(); } else if (mode == STDOUT) buf = std::cout.rdbuf(); std::ostream tmp(buf); this->out = &tmp; } Log::~Log() { } Log::Log(Log &l) { } 
Source Link
MasterGberry
  • 2.9k
  • 6
  • 40
  • 60
Loading