<?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[[Erledigt] Loggen mit stream?]]></title><description><![CDATA[<p>Hallo,</p>
<p>hier mal ein neues Problemchen. Ich würde gern einen Logger schreiben, der mit einer Syntax ähnlich Stringstreams arbeitet, also idealerweise so:</p>
<pre><code>Logger l;
l &lt;&lt; &quot;Das ist ne Zahl: &lt;&lt; 100 &lt;&lt; &quot;und wieder text &quot;;
// Bzw.
l &lt;&lt; &quot;Das ist ne Zahl: &lt;&lt; 100 &lt;&lt; &quot;und wieder text &quot; &lt;&lt; Logger::endl;
</code></pre>
<p>Noch lieber wäre mir eigentlich sowas:</p>
<pre><code>l.log(&quot;Blah&quot; &lt;&lt; &quot;blubb&quot;);
</code></pre>
<p>Allerdings weiß ich nicht so recht, wie man das implentieren soll... habt ihr da Ideen?</p>
<p>Dank &amp; Gruß, Markus</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/263897/erledigt-loggen-mit-stream</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 04:00:47 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/263897.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 29 Mar 2010 12:58:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 14:14:00 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>hier mal ein neues Problemchen. Ich würde gern einen Logger schreiben, der mit einer Syntax ähnlich Stringstreams arbeitet, also idealerweise so:</p>
<pre><code>Logger l;
l &lt;&lt; &quot;Das ist ne Zahl: &lt;&lt; 100 &lt;&lt; &quot;und wieder text &quot;;
// Bzw.
l &lt;&lt; &quot;Das ist ne Zahl: &lt;&lt; 100 &lt;&lt; &quot;und wieder text &quot; &lt;&lt; Logger::endl;
</code></pre>
<p>Noch lieber wäre mir eigentlich sowas:</p>
<pre><code>l.log(&quot;Blah&quot; &lt;&lt; &quot;blubb&quot;);
</code></pre>
<p>Allerdings weiß ich nicht so recht, wie man das implentieren soll... habt ihr da Ideen?</p>
<p>Dank &amp; Gruß, Markus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875091</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875091</guid><dc:creator><![CDATA[TdZ]]></dc:creator><pubDate>Mon, 29 Mar 2010 14:14:00 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 12:59:44 GMT]]></title><description><![CDATA[<p>Operator&lt;&lt; überladen. Dein zweiter Wunsch geht so direkt nicht.. Höchstens l.log(l &lt;&lt; ...); was aber auch relativ Sinnbefreit ist. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875092</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Mon, 29 Mar 2010 12:59:44 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:02:42 GMT]]></title><description><![CDATA[<p>Schade, das hatte ich bereits befürchtet. Und mit was überlade ich den? Brauch ein da ein ostream Member oder wie würdest du das machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875095</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875095</guid><dc:creator><![CDATA[TdZ]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:02:42 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:06:10 GMT]]></title><description><![CDATA[<p>Kleines Beispielprogramm:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Fubar
{
public:
  // Operator&lt;&lt; innerhalb der Klasse überladen
  Fubar&amp; operator&lt;&lt;(int t)
  {
    cout &lt;&lt; t;
    return *this;
  }
};

// Operator&lt;&lt; außerhalb der Klasse überladen
Fubar&amp; operator&lt;&lt;(Fubar &amp;f, char c)
{
  cout &lt;&lt; c;
  return f;
}

int main()
{
  Fubar f;
  f &lt;&lt; 5 &lt;&lt; 9 &lt;&lt; 2;
  f &lt;&lt; 'a' &lt;&lt; 't' &lt;&lt; 'c';
}
</code></pre>
<p>Sollte allerdings auch in deinem Buch stehen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875098</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875098</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:06:10 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:08:16 GMT]]></title><description><![CDATA[<p>Janjan schrieb:</p>
<blockquote>
<p>geht so direkt nicht.. Höchstens l.log(l &lt;&lt; ...);</p>
</blockquote>
<p>Es müssten auch gehen:<br />
l.log() &lt;&lt; &quot;bla&quot; &lt;&lt; &quot;blubb&quot;;<br />
oder<br />
l.log &lt;&lt; &quot;bla&quot; &lt;&lt; &quot;blubb&quot;;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875101</guid><dc:creator><![CDATA[empgodot]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:08:16 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:09:50 GMT]]></title><description><![CDATA[<p>Mein Buch.. ähhhh... das hab ich ... verlegt... mein Hund hat's gefressen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Danke dir, so hab ich's auch schon gemacht. Allerdings noch eine Kleinigkeit: Ich brauche einen endline-Operator, wie std::endl, damit die Zeile intern ins mit Datum und ein paar Infos Logfile geschrieben wird, kannst du mir sagen, wie ich da ein eigenes Logger::endl implementieren kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875102</guid><dc:creator><![CDATA[TdZ]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:09:50 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:12:56 GMT]]></title><description><![CDATA[<p>TdZ schrieb:</p>
<blockquote>
<p>Noch lieber wäre mir eigentlich sowas:</p>
<pre><code>l.log(&quot;Blah&quot; &lt;&lt; &quot;blubb&quot;);
</code></pre>
</blockquote>
<p>Diese Syntax kann man leicht mit Makros hinbekommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875107</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:12:56 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 13:23:00 GMT]]></title><description><![CDATA[<p>TdZ schrieb:</p>
<blockquote>
<p>Mein Buch.. ähhhh... das hab ich ... verlegt... mein Hund hat's gefressen. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Ein Buch sollte Grundvoraussetzung sein. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>TdZ schrieb:</p>
<blockquote>
<p>Allerdings noch eine Kleinigkeit: Ich brauche einen endline-Operator, wie std::endl, damit die Zeile intern ins mit Datum und ein paar Infos Logfile geschrieben wird, kannst du mir sagen, wie ich da ein eigenes Logger::endl implementieren kann?</p>
</blockquote>
<p>Dies wird mittels Funktionszeiger gelöst. Du übergibst also dann die Funktion &quot;endl&quot; an den operator&lt;&lt;, welche als Parameter eine Referenz auf die Klasse annimmt und diese Referenz zurückgibt.</p>
<p>Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Fubar
{
public:
  // Operator&lt;&lt; überladen, dass dieser Funktionszeiger annimmt
  Fubar&amp; operator&lt;&lt;(Fubar&amp; (*fp)(Fubar&amp;))
  {
    (*fp)(*this);
    return *this;
  }
};

Fubar&amp; myEndl(Fubar &amp;f)
{
  cout &lt;&lt; &quot;Mein eigenes Endl!&quot;;
  return f;
}

int main()
{
  Fubar f;
  f &lt;&lt; myEndl;
}
</code></pre>
<p>SeppJ schrieb:</p>
<blockquote>
<p>TdZ schrieb:</p>
<blockquote>
<p>Noch lieber wäre mir eigentlich sowas:</p>
<pre><code>l.log(&quot;Blah&quot; &lt;&lt; &quot;blubb&quot;);
</code></pre>
</blockquote>
<p>Diese Syntax kann man leicht mit Makros hinbekommen.</p>
</blockquote>
<p>Und das wäre schrecklich. Makros sollten nicht für so einen Mist missbraucht werden. Dadurch wird der Code schwerer durchschaubar und es schleichen sich sehr leicht hinterhältige Fehler ein. Bitte bring ihm nicht so einen Müll bei.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875111</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Mon, 29 Mar 2010 13:23:00 GMT</pubDate></item><item><title><![CDATA[Reply to [Erledigt] Loggen mit stream? on Mon, 29 Mar 2010 14:13:35 GMT]]></title><description><![CDATA[<p>So, hab's jetzt -- dank janjans Hilfe folgendermaßen gelöst:</p>
<pre><code>class Log
{
public:
    static Log&amp; Warning(Log&amp; l) {
        l.m_stream &lt;&lt; std::endl;
        l.m_log+=getPrefix(&quot;Warning&quot;);
        l.m_log+=l.m_stream.str();
        l.m_stream.clear();
        return l;
    }

    static Log&amp; Notice(Log&amp; l) {
        l.m_stream &lt;&lt; std::endl;
        l.m_log+=getPrefix(&quot;Notice&quot;);
        l.m_log+=l.m_stream.str();
        l.m_stream.clear();
        return l;
    }

    static Log&amp; Error(Log&amp; l) {
        l.m_stream &lt;&lt; std::endl;
        l.m_log+=getPrefix(&quot;Error&quot;);
        l.m_log+=l.m_stream.str();
        l.m_stream.clear();
        return l;
    }

    Log()
    {

    }

    virtual ~Log()
    {
        m_stream.clear();
        m_log.clear();
    }

    Log&amp; operator&lt;&lt;(char* p)
    {
        m_stream &lt;&lt; p;
        return *this;
    }
    Log&amp; operator&lt;&lt;(std::string s)
    {
        m_stream &lt;&lt; s;
        return *this;
    }
    Log&amp; operator&lt;&lt;(int n)
    {
        m_stream &lt;&lt; n;
        return *this;
    }
    Log&amp; operator&lt;&lt;(float n)
    {
        m_stream &lt;&lt; n;
        return *this;
    }

    Log&amp; operator&lt;&lt;(Log&amp; (*fp)(Log&amp;))
    {
        (*fp)(*this);
        return *this;
    }

    inline std::string getString()
    {
        return m_log;
    }

protected:
private:
    static std::string getPrefix(std::string desc=&quot;&quot;)
    {
        return std::string(&quot;00:00:00 [&quot;)+desc+std::string(&quot;]: &quot;);
    }

    std::string m_log;
    std::stringstream m_stream;
};
</code></pre>
<p>Und funktionieren tut's so:</p>
<pre><code>Log l;
l &lt;&lt; &quot;Blah der Text und ne NUmmer &lt;&lt; 100 &lt;&lt; 200.0f &lt;&lt; Log::Notice;
</code></pre>
<p>Vielen Dank nochmal für die Tips!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1875139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1875139</guid><dc:creator><![CDATA[TdZ]]></dc:creator><pubDate>Mon, 29 Mar 2010 14:13:35 GMT</pubDate></item></channel></rss>