<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Stringstream Sub Klasse mit eigenen Manipulatoren]]></title><description><![CDATA[<p>Hi,</p>
<p>Habe mir eine kleine Logger Klasse geschrieben mit der Ich über ein cout ähnliches verhalten also z.B.:</p>
<p>Logger.h</p>
<pre><code>LOGGER &lt;&lt; error &lt;&lt; e.what() &lt;&lt; print;
</code></pre>
<p>Einen Log eintrag erzeugen möchte.</p>
<p>Das ganze sieht dann ungefär so aus.</p>
<pre><code>#ifndef _LOGH_
#define _LOGH_
#include &lt;sstream&gt;
#include &lt;syslog.h&gt;
#include &lt;iostream&gt;

class LOG : public std::stringstream{
	public:
	enum class PRIORITIES : int;

	LOG();
	~LOG();
	void setLevel( PRIORITIES level );
	void setDestination( std::string identifier );
	void printMessage();

	private:
	PRIORITIES level;

};

extern LOG LOGGER;

LOG &amp;emergency(LOG &amp;out);
LOG &amp;alert(LOG &amp;out);
LOG &amp;critical(LOG &amp;out);
LOG &amp;error(LOG &amp;out);
LOG &amp;warning(LOG &amp;out);
LOG &amp;notice(LOG &amp;out);
LOG &amp;info(LOG &amp;out);
LOG &amp;debug(LOG &amp;out);
LOG &amp;print(LOG &amp;out);

#endif
</code></pre>
<p>Logger.cpp</p>
<pre><code>#ifndef _LOGCPP_
#define _LOGCPP_
#include &quot;LOG.h&quot;

LOG LOGGER;

enum class LOG::PRIORITIES{ EMERGENCY = LOG_EMERG, ALERT = LOG_ALERT, CRITICAL = LOG_CRIT, ERROR = LOG_ERR, WARNING = LOG_WARNING, NOTICE = LOG_NOTICE, INFO = LOG_INFO, DEBUG = LOG_DEBUG };

LOG::LOG(){
	closelog();
	openlog( &quot;d_console&quot;, LOG_PID, LOG_DAEMON );
}

LOG::~LOG(){
	closelog();
}

void LOG::setLevel( PRIORITIES level ){
	this-&gt;level = level;
}

void LOG::setDestination( std::string identifier ){
	closelog();
	openlog( identifier.c_str(), LOG_PID, LOG_DAEMON );
}

void LOG::printMessage(){
	std::cout &lt;&lt; &quot;should print: &quot; &lt;&lt; this-&gt;str() &lt;&lt; std::endl;
	syslog( (int)level, &quot;%s&quot;, this-&gt;str().c_str() );
	this-&gt;clear();
}

LOG &amp;emergency(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::EMERGENCY);
	return out;
}

LOG &amp;alert(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::ALERT);
	return out;
}

LOG &amp;critical(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::CRITICAL);
	return out;
}

LOG &amp;error(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::ERROR);
	return out;
}

LOG &amp;warning(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::WARNING);
	return out;
}

LOG &amp;notice(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::NOTICE);
	return out;
}

LOG &amp;info(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::INFO);
	return out;
}

LOG &amp;debug(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::DEBUG);
	return out;
}

LOG &amp;print(LOG &amp;out){
	out.printMessage();
	return out;
}

#endif
</code></pre>
<p>Das Problem das Ich derzeitig habe ist das beim Aufruf nach dem Beispiel nicht viel Passiert. Der Compiller gibt mir Warnungen mit:</p>
<pre><code>warning: the address of ‘LOG&amp; error(LOG&amp;)’ will always evaluate as ‘true’
</code></pre>
<p>Und Ich glaube das das zusammen hängt aber Ich sehe den Fehler nicht.</p>
<p>Könnte mir da jemand bitte weiterhelfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/315179/stringstream-sub-klasse-mit-eigenen-manipulatoren</link><generator>RSS for Node</generator><lastBuildDate>Fri, 31 Jul 2026 08:55:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/315179.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Mar 2013 03:00:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 03:00:41 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>Habe mir eine kleine Logger Klasse geschrieben mit der Ich über ein cout ähnliches verhalten also z.B.:</p>
<p>Logger.h</p>
<pre><code>LOGGER &lt;&lt; error &lt;&lt; e.what() &lt;&lt; print;
</code></pre>
<p>Einen Log eintrag erzeugen möchte.</p>
<p>Das ganze sieht dann ungefär so aus.</p>
<pre><code>#ifndef _LOGH_
#define _LOGH_
#include &lt;sstream&gt;
#include &lt;syslog.h&gt;
#include &lt;iostream&gt;

class LOG : public std::stringstream{
	public:
	enum class PRIORITIES : int;

	LOG();
	~LOG();
	void setLevel( PRIORITIES level );
	void setDestination( std::string identifier );
	void printMessage();

	private:
	PRIORITIES level;

};

extern LOG LOGGER;

LOG &amp;emergency(LOG &amp;out);
LOG &amp;alert(LOG &amp;out);
LOG &amp;critical(LOG &amp;out);
LOG &amp;error(LOG &amp;out);
LOG &amp;warning(LOG &amp;out);
LOG &amp;notice(LOG &amp;out);
LOG &amp;info(LOG &amp;out);
LOG &amp;debug(LOG &amp;out);
LOG &amp;print(LOG &amp;out);

#endif
</code></pre>
<p>Logger.cpp</p>
<pre><code>#ifndef _LOGCPP_
#define _LOGCPP_
#include &quot;LOG.h&quot;

LOG LOGGER;

enum class LOG::PRIORITIES{ EMERGENCY = LOG_EMERG, ALERT = LOG_ALERT, CRITICAL = LOG_CRIT, ERROR = LOG_ERR, WARNING = LOG_WARNING, NOTICE = LOG_NOTICE, INFO = LOG_INFO, DEBUG = LOG_DEBUG };

LOG::LOG(){
	closelog();
	openlog( &quot;d_console&quot;, LOG_PID, LOG_DAEMON );
}

LOG::~LOG(){
	closelog();
}

void LOG::setLevel( PRIORITIES level ){
	this-&gt;level = level;
}

void LOG::setDestination( std::string identifier ){
	closelog();
	openlog( identifier.c_str(), LOG_PID, LOG_DAEMON );
}

void LOG::printMessage(){
	std::cout &lt;&lt; &quot;should print: &quot; &lt;&lt; this-&gt;str() &lt;&lt; std::endl;
	syslog( (int)level, &quot;%s&quot;, this-&gt;str().c_str() );
	this-&gt;clear();
}

LOG &amp;emergency(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::EMERGENCY);
	return out;
}

LOG &amp;alert(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::ALERT);
	return out;
}

LOG &amp;critical(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::CRITICAL);
	return out;
}

LOG &amp;error(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::ERROR);
	return out;
}

LOG &amp;warning(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::WARNING);
	return out;
}

LOG &amp;notice(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::NOTICE);
	return out;
}

LOG &amp;info(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::INFO);
	return out;
}

LOG &amp;debug(LOG &amp;out){
	out.setLevel(LOG::PRIORITIES::DEBUG);
	return out;
}

LOG &amp;print(LOG &amp;out){
	out.printMessage();
	return out;
}

#endif
</code></pre>
<p>Das Problem das Ich derzeitig habe ist das beim Aufruf nach dem Beispiel nicht viel Passiert. Der Compiller gibt mir Warnungen mit:</p>
<pre><code>warning: the address of ‘LOG&amp; error(LOG&amp;)’ will always evaluate as ‘true’
</code></pre>
<p>Und Ich glaube das das zusammen hängt aber Ich sehe den Fehler nicht.</p>
<p>Könnte mir da jemand bitte weiterhelfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309833</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309833</guid><dc:creator><![CDATA[TheOneWhoShallBeNamed]]></dc:creator><pubDate>Tue, 26 Mar 2013 03:00:41 GMT</pubDate></item><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 03:49:08 GMT]]></title><description><![CDATA[<p>Nachdem Ich die Augen wieder nen stück weit geöffnet hab hab Ich den Fehler auch schon gefunden. Trotzdem danke. Ich weiß das wenn ich zu ner freundlicheren Zeit gepostet hätte hier wohl mittlerweile auch schon eine Antwort hätte.</p>
<p>Wen es interessiert, es hat</p>
<pre><code>LOG&amp; operator&lt;&lt;(LOG&amp; (*m)(LOG&amp;));
</code></pre>
<p>mit</p>
<pre><code>LOG&amp; LOG::operator&lt;&lt;(LOG&amp; (*m)(LOG&amp;)){  
	return (*m)(*this);
}
</code></pre>
<p>im code gefehlt</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309835</guid><dc:creator><![CDATA[TheOneWhoShallBeNamed]]></dc:creator><pubDate>Tue, 26 Mar 2013 03:49:08 GMT</pubDate></item><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 04:04:14 GMT]]></title><description><![CDATA[<p>Dachte ich,</p>
<p>Hahaha.. ich geb auf für heute.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309836</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309836</guid><dc:creator><![CDATA[TheOneWhoFailed]]></dc:creator><pubDate>Tue, 26 Mar 2013 04:04:14 GMT</pubDate></item><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 08:05:35 GMT]]></title><description><![CDATA[<p>TheOneWhoShallBeNamed schrieb:</p>
<blockquote>
<p>Logger.h</p>
<pre><code>#ifndef _LOGH_
#define _LOGH_
#include &lt;sstream&gt;
#include &lt;syslog.h&gt;
#include &lt;iostream&gt;

class LOG : public std::stringstream{
	public:
        // ...
</code></pre>
</blockquote>
<p>es ist wohl ein Fehler, den jeder C++-Programmierer einmal gemacht haben haben muss.<br />
Grundsätzlich ist es nicht zu empfehlen, seine LOG-Klasse von std::stringstream (oder std::fstream oder egal-stream) abzuleiten. Weil dann genau passiert, was hier auch passiert, nämlich der Zwang alle möglichen <a href="http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/" rel="nofollow">inserters</a> (operator&lt;&lt;) zu überladen.</p>
<p>Abgesehen von der (überflüssigen) Arbeit zieht das auch noch Arbeit bei jeder Klasse nach sich, die in das LOG ausgegeben werden soll.</p>
<p>Die einfachere Variante ist, die LOG-Klasse von einem std::streambuf abzuleiten und anschließend einen std::ostream seiner Wahl (std::cout, std::clog oder ein eigenes Objekt) auf eben diesen Streambuf umzuleiten (mit <a href="http://www.cplusplus.com/reference/ios/ios/rdbuf/" rel="nofollow">rdbuf</a>). Siehe auch <a href="http://www.c-plusplus.net/forum/p942394#942394" rel="nofollow">IoSwitch</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309850</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 26 Mar 2013 08:05:35 GMT</pubDate></item><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 08:10:36 GMT]]></title><description><![CDATA[<p>Was ist denn das Problem?<br />
Wenn er eine neue Klasse einführt, überlädt er einfach operator &lt;&lt; für ostream und kann sie auch im Logger verwenden.<br />
Oder verstehe ich da etwas falsch?</p>
<p>Edit: <strong>Include</strong>guard in einem <strong>Source</strong>file macht irgendwie wenig Sinn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309852</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309852</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Tue, 26 Mar 2013 08:10:36 GMT</pubDate></item><item><title><![CDATA[Reply to Stringstream Sub Klasse mit eigenen Manipulatoren on Tue, 26 Mar 2013 08:14:58 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Was ist denn das Problem?<br />
Wenn er eine neue Klasse einführt, überlädt er einfach operator &lt;&lt; für ostream und kann sie auch im Logger verwenden.<br />
Oder verstehe ich da etwas falsch?</p>
</blockquote>
<p>ja - Du hast Recht. Zumindest solange der Anwender die Reihenfolge bei der Ausgabe einhält und den eigenen Manipulator immer am Anfang der Ausgabe schreibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2309854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2309854</guid><dc:creator><![CDATA[Werner Salomon]]></dc:creator><pubDate>Tue, 26 Mar 2013 08:14:58 GMT</pubDate></item></channel></rss>