Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
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 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <streambuf> #include <iostream> #include <fstream> namespace log { class two_streambuf : public std::streambuf { std::streambuf* m_first; std::streambuf* m_second; std::char_traits<char>::int_type overflow(std::char_traits<char>::int_type c) { if (std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof() == true)) return std::char_traits<char>::not_eof(c); c = m_first->sputc(c); if (std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof() == false)) c = m_second->overflow(); return c; } int sync() { return (m_first->sync() != -1 ? m_second->sync() : -1); } public: two_streambuf (std::streambuf* ptr_first, std::streambuf* ptr_second) : m_first(ptr_first), m_second(ptr_second) {} }; } int main() { std::ofstream file_stream("log.txt"); log::two_streambuf buffer(std::cout.rdbuf(), file_stream.rdbuf()); std::clog.rdbuf(&buffer); std::clog << "Das ist mein erster String!" << std::endl; } |
C-/C++-Quelltext |
|
1 2 3 4 |
Warnung 1 warning C4805: '==': unsichere Kombination von Typ 'std::char_traits<char>::int_type' mit Typ 'bool' in einer Operation f:\projekte\consoletestapp\main.cpp 14 Fehler 3 error C2248: "std::basic_streambuf<_Elem,_Traits>::sync": Kein Zugriff auf protected Member, dessen Deklaration in der std::basic_streambuf<_Elem,_Traits>-Klasse erfolgte. f:\projekte\consoletestapp\main.cpp 25 Fehler 4 error C2248: "std::basic_streambuf<_Elem,_Traits>::sync": Kein Zugriff auf protected Member, dessen Deklaration in der std::basic_streambuf<_Elem,_Traits>-Klasse erfolgte. f:\projekte\consoletestapp\main.cpp 25 Fehler 2 error C2248: "std::basic_streambuf<_Elem,_Traits>::overflow": Kein Zugriff auf protected Member, dessen Deklaration in der std::basic_streambuf<_Elem,_Traits>-Klasse erfolgte. f:\projekte\consoletestapp\main.cpp 19 |
Zitat von »"Das Gurke"«
C-/C++-Quelltext
1 2 3 4 5 if (std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof() == true)) /* man beachte den Unterschied in der Klammerung am Ende :) */ if (std::char_traits<char>::eq_int_type(c, std::char_traits<char>::eof()) == true)
C-/C++-Quelltext |
|
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 |
#include <streambuf> namespace log { template<typename _elem, typename _traits = std::char_traits<_elem> > class basic_dualstreambuf : public std::basic_streambuf<_elem, _traits > { std::basic_streambuf<_elem, _traits>* m_first; std::basic_streambuf<_elem, _traits>* m_second; protected: virtual int_type overflow(int_type c = traits_type::eof()) { return ((m_first && m_second && traits_type::eq_int_type(m_first->sputc(c), traits_type::eof()) == false && traits_type::eq_int_type(m_second->sputc(c), traits_type::eof() == false)) ? traits_type::not_eof(c) : traits_type::eof()); } public: explicit basic_dualstreambuf (std::basic_streambuf<_elem, _traits>* ptr_first, std::basic_streambuf<_elem, _traits>* ptr_second) : m_first(ptr_first), m_second(ptr_second) {} }; typedef basic_dualstreambuf<char> dualstreambuf; typedef basic_dualstreambuf<wchar_t> wdualstreambuf; }; // log |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <fstream> #include <iostream> #include "log.hpp" std::ostream& foo(std::ostream& out) { return out << "foo() - Function"; } int main() { std::ofstream file_stream("log.txt"); log::dualstreambuf buffer(std::clog.rdbuf(), file_stream.rdbuf()); std::clog.rdbuf(&buffer); std::clog << "Test 001\n" << foo(std::clog) << std::endl; } |
C-/C++-Quelltext |
|
1 2 3 4 |
// Setting up logging facilities std::ofstream logstream((const char*)"Server.log"); WoRD::output output(std::cout.rdbuf(), logstream.rdbuf()); // typedef basic_dualstreambuf<char> output; std::clog.rdbuf(&output); // std::clog now logs to screen and given file |
C-/C++-Quelltext |
|
1 2 3 4 |
// How did we compile? std::clog << "Compiler Options" << std::endl; std::clog << "\tCompiler (Version) : " << COMP_NAME << " (" << COMP_VER << ")" << std::endl; std::clog << "\tBuild configuration : " << CONF_NAME << std::endl; |
C-/C++-Quelltext |
|
1 2 |
error: `int_type' does not name a type error: (perhaps `typename std::basic_streambuf<_CharT, _Traits>::int_type' was intended) |
C-/C++-Quelltext |
|
1 |
virtual int_type overflow(int_type c = traits_type::eof()) |
C-/C++-Quelltext |
|
1 |
virtual typename int_type overflow(int_type c = traits_type::eof()) |
Werbeanzeige