<?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[Log-System in einer multithreaded Anwendung]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich möchte mir gerne ein LogSystem bauen, dass in einer Umgebung mit mehreren Threads funktioniert. Das heißt: es soll aus mehreren Threads in unterschiedlichen Klassen in ein LogFile geschrieben werden. Ich habe mir vorgestellt das LogSystem als Singleton zu implementieren. Bin aber für andere Ideen offen...</p>
<p>Mein Ansatz(Header):</p>
<pre><code class="language-cpp">class LogSystem
{
    public:
        static LogSystem&amp; getInstance();
        void logEntry(const std::string &amp;strLogMessage);
    private:
        LogSystem();
        ~LogSystem();
        LogSystem(const LogSystem&amp; other){}

        std::ofstream m_LogFile;
        boost::mutex m_mutex;
};
</code></pre>
<p>Mein Ansatz(Source):</p>
<pre><code class="language-cpp">LogSystem::LogSystem()
:m_LogFile(&quot;app.log&quot;)
{
}

LogSystem::~LogSystem()
{
    m_LogFile.close();
}

LogSystem&amp; LogSystem::getInstance()
{
    static LogSystem instance;
    return instance;
}

void LogSystem::logEntry(const std::string &amp;strLogMessage)
{
    boost::mutex::scoped_lock lck(m_mutex);
    if(m_LogFile.good())
    {
        m_LogFile &lt;&lt; strLogMessage &lt;&lt; std::endl;
    }
}
</code></pre>
<p>Probleme:<br />
1. Ich habe keinen Konstruktor, dem ich einen LogFile Namen mitgeben kann.<br />
2. Ich weiß nicht, ob das Singleton so wie es hier implementiert ist in multithreaded Anwendungen zu Problemen führen kann.</p>
<p>Ein weiterer Ansatz ohne Singleton wäre das LogSystem aus dem Manager-Thread (Hauptthread) zu erzeugen und über &quot;extern&quot; den Klassen bzw Instanzen, die etwas loggen dürfen/sollen zugänglich zu machen...</p>
<p>Habt ihr vielleicht weitere Ideen bzw habt ihr Verbesserungsvorschläge für obigen Code?</p>
<p>Vielen Dank für eure Hilfe.</p>
<p>Gruß<br />
Robert</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/254499/log-system-in-einer-multithreaded-anwendung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 08:09:07 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/254499.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 17 Nov 2009 09:12:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 09:12:29 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich möchte mir gerne ein LogSystem bauen, dass in einer Umgebung mit mehreren Threads funktioniert. Das heißt: es soll aus mehreren Threads in unterschiedlichen Klassen in ein LogFile geschrieben werden. Ich habe mir vorgestellt das LogSystem als Singleton zu implementieren. Bin aber für andere Ideen offen...</p>
<p>Mein Ansatz(Header):</p>
<pre><code class="language-cpp">class LogSystem
{
    public:
        static LogSystem&amp; getInstance();
        void logEntry(const std::string &amp;strLogMessage);
    private:
        LogSystem();
        ~LogSystem();
        LogSystem(const LogSystem&amp; other){}

        std::ofstream m_LogFile;
        boost::mutex m_mutex;
};
</code></pre>
<p>Mein Ansatz(Source):</p>
<pre><code class="language-cpp">LogSystem::LogSystem()
:m_LogFile(&quot;app.log&quot;)
{
}

LogSystem::~LogSystem()
{
    m_LogFile.close();
}

LogSystem&amp; LogSystem::getInstance()
{
    static LogSystem instance;
    return instance;
}

void LogSystem::logEntry(const std::string &amp;strLogMessage)
{
    boost::mutex::scoped_lock lck(m_mutex);
    if(m_LogFile.good())
    {
        m_LogFile &lt;&lt; strLogMessage &lt;&lt; std::endl;
    }
}
</code></pre>
<p>Probleme:<br />
1. Ich habe keinen Konstruktor, dem ich einen LogFile Namen mitgeben kann.<br />
2. Ich weiß nicht, ob das Singleton so wie es hier implementiert ist in multithreaded Anwendungen zu Problemen führen kann.</p>
<p>Ein weiterer Ansatz ohne Singleton wäre das LogSystem aus dem Manager-Thread (Hauptthread) zu erzeugen und über &quot;extern&quot; den Klassen bzw Instanzen, die etwas loggen dürfen/sollen zugänglich zu machen...</p>
<p>Habt ihr vielleicht weitere Ideen bzw habt ihr Verbesserungsvorschläge für obigen Code?</p>
<p>Vielen Dank für eure Hilfe.</p>
<p>Gruß<br />
Robert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809210</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809210</guid><dc:creator><![CDATA[RobertH]]></dc:creator><pubDate>Tue, 17 Nov 2009 09:12:29 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 09:35:03 GMT]]></title><description><![CDATA[<blockquote>
<p>Ein weiterer Ansatz ohne Singleton wäre das LogSystem aus dem Manager-Thread (Hauptthread) zu erzeugen und über &quot;extern&quot; den Klassen bzw Instanzen, die etwas loggen dürfen/sollen zugänglich zu machen...</p>
</blockquote>
<p>Genauso hab ich das bei mir gemacht. Eine Logger-Klasse, die einen eigenen Thread startet und ne threadsafe Queue hat, in die die anderen Threads reinschreiben. Hatte keine Probleme damit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809218</guid><dc:creator><![CDATA[It0101]]></dc:creator><pubDate>Tue, 17 Nov 2009 09:35:03 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 10:16:06 GMT]]></title><description><![CDATA[<p>RobertH schrieb:</p>
<blockquote>
<p>2. Ich weiß nicht, ob das Singleton so wie es hier implementiert ist in multithreaded Anwendungen zu Problemen führen kann.</p>
</blockquote>
<p>Die Compilerbauer machen aus Eigenen Stücken den Meyers-Singleton (Um genau zu sein, das Initialisieren lokaler static-Variablen)Threadsicher. GCC hat gemacht. Ob MS auch so weit ist, weiß ich nicht, nehme es aber an.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809245</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809245</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 17 Nov 2009 10:16:06 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 10:23:21 GMT]]></title><description><![CDATA[<p>RobertH schrieb:</p>
<blockquote>
<p>Probleme:<br />
1. Ich habe keinen Konstruktor, dem ich einen LogFile Namen mitgeben kann.</p>
</blockquote>
<p>Normalerweise schreibt man nicht error.log und warnung.log, sonder nur das log mit &quot;error: gdsbsdghuipdsgisdgsdgbuz&quot; und &quot;warning: gusdisbgduisdgbzhsdp&quot;.<br />
Also brauchst Du wirklich Dateinamen?</p>
<p>Wenn ja, bau Dir halt eine</p>
<pre><code class="language-cpp">static LogSystem&amp; getInstance(std::string fileName);
</code></pre>
<p>eine map&lt;string,ofstream*&gt; dahinter und mutex oder critical section.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809251</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 17 Nov 2009 10:23:21 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 10:31:07 GMT]]></title><description><![CDATA[<p>Also ich mach solche Dateinamen immer über die .ini konfigurierbar... Genauso wie das Unterverzeichnis, wo die Logs möglicherweise reingeschrieben werden sollen.<br />
Gehört aus meiner Sicht zum guten Ton. D.h. ich muss mindestens das Config-Objekt übergeben, oder eben den Dateinamen direkt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809259</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809259</guid><dc:creator><![CDATA[It0101]]></dc:creator><pubDate>Tue, 17 Nov 2009 10:31:07 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 10:34:30 GMT]]></title><description><![CDATA[<p>It0101 schrieb:</p>
<blockquote>
<p>Gehört aus meiner Sicht zum guten Ton. D.h. ich muss mindestens das Config-Objekt übergeben, oder eben den Dateinamen direkt.</p>
</blockquote>
<p>Puh, da haben wir aber Glück gehabt, daß das Config-Objekt seinerseits ein Singleton ist und gar nicht übergeben werden muß.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809261</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 17 Nov 2009 10:34:30 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 11:28:50 GMT]]></title><description><![CDATA[<blockquote>
<p>Wenn ja, bau Dir halt eine</p>
<pre><code class="language-cpp">static LogSystem&amp; getInstance(std::string fileName);
</code></pre>
<p>eine map&lt;string,ofstream*&gt; dahinter und mutex oder critical section.</p>
</blockquote>
<p>Verstehe nicht, was Du meinst. Es geht mir nur um den File-Namen des Log Files, nicht um die warnings und errors, die im Log File stehen.<br />
Aber die Idee den Log Filenamen in einem ini-File festzulegen klingt gut.</p>
<blockquote>
<p>Genauso hab ich das bei mir gemacht. Eine Logger-Klasse, die einen eigenen Thread startet und ne threadsafe Queue hat, in die die anderen Threads reinschreiben. Hatte keine Probleme damit.</p>
</blockquote>
<p>Hmmm, ja das klingt ok. Ich habe allerdings nicht vor im LogSystem einen Thread laufen zu lassen. Wozu?</p>
<p>Viele Grüße<br />
Robert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809298</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809298</guid><dc:creator><![CDATA[RobertH]]></dc:creator><pubDate>Tue, 17 Nov 2009 11:28:50 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Tue, 17 Nov 2009 12:01:54 GMT]]></title><description><![CDATA[<p>Es macht Sinn, das Schreiben auf das LogFile vom Rest der Anwendung zu enkoppeln. Daher: Eine threadsafe Queue in die alle Ihre Logmessages rein schreiben und ein separater Thread, der da raus liest und das Schreiben ins File übernimmt. Dadurch lässt sich der Aufwand für das Schreiben ins File vom Rest der Anwendung entkoppeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809320</guid><dc:creator><![CDATA[gastgast]]></dc:creator><pubDate>Tue, 17 Nov 2009 12:01:54 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 11:46:57 GMT]]></title><description><![CDATA[<blockquote>
<p>Es macht Sinn, das Schreiben auf das LogFile vom Rest der Anwendung zu enkoppeln. Daher: Eine threadsafe Queue in die alle Ihre Logmessages rein schreiben und ein separater Thread, der da raus liest und das Schreiben ins File übernimmt. Dadurch lässt sich der Aufwand für das Schreiben ins File vom Rest der Anwendung entkoppeln.</p>
</blockquote>
<p>Wie kann man das verstehen. Wird die Queue von dem Thread dann schlicht gepollt in gewissen Zeitabständen oder wird der Thread über ein Event aufgeweckt, wenn etwas in die Queue geschrieben wird. Zweiteren Ansätz würde ich verstehen, wenn Du im Hinblick auf den Aufwand argumentierst..</p>
<p>Viele Grüße<br />
Robert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809818</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809818</guid><dc:creator><![CDATA[RobertH]]></dc:creator><pubDate>Wed, 18 Nov 2009 11:46:57 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 12:24:18 GMT]]></title><description><![CDATA[<p>RobertH schrieb:</p>
<blockquote>
<p>Event</p>
</blockquote>
<p>samephore <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809842</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Wed, 18 Nov 2009 12:24:18 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 12:31:16 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>RobertH schrieb:</p>
<blockquote>
<p>Event</p>
</blockquote>
<p>samephore <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f60b.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_savoring_food"
      title=":yum:"
      alt="😋"
    /></p>
</blockquote>
<p>fast <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/1809844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809844</guid><dc:creator><![CDATA[furianer]]></dc:creator><pubDate>Wed, 18 Nov 2009 12:31:16 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 12:35:54 GMT]]></title><description><![CDATA[<p>wie schreibt ihr eigtl in eure log-systeme?<br />
op&lt;&lt; (falls ja, wie kennzeichnet ihr den anfang/das ende eines logeintrages) oder fkt.<br />
falls fkt, was nehmen die? nen string? nen stream? oder nen template und dann spezialisieren bzw. über stringstream?</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809847</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 18 Nov 2009 12:35:54 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 12:47:09 GMT]]></title><description><![CDATA[<blockquote>
<p>Wie kann man das verstehen.</p>
</blockquote>
<p>Producer Consumer Problem ...</p>
<blockquote>
<p>Probleme:<br />
1. Ich habe keinen Konstruktor, dem ich einen LogFile Namen mitgeben kann.</p>
</blockquote>
<p>Meine Singletons haben einen Konstruktor. Aber du kannst genauso gut eine int-Methode benutzen. Normalerweise benutze ich aber keine Klasse fuer etwas &quot;Singleton&quot;-artiges.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1809848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809848</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 18 Nov 2009 12:47:09 GMT</pubDate></item><item><title><![CDATA[Reply to Log-System in einer multithreaded Anwendung on Wed, 18 Nov 2009 12:57:10 GMT]]></title><description><![CDATA[<p>Die Queue wird über einen Mutex geschützt.</p>
<p>Der Thread schaut, wie Du vermutest, nach, ob die Queue nicht leer ist. Steht etwas drin, wird geschrieben, solange etwas in der Queue steht. Ist sie leer, legt sich der Thread für eine gewisse Zeit schlafen</p>
<p>Hier mal ein Beispiel</p>
<pre><code class="language-cpp">void Logger::run()
{
    isTerminated = false;

    // Thread runs until terminateLogging() is called
    while( !shallTerminate )
    {
        bufferLock.lock();
        while ( !buffer.empty() )
        {
            string s = buffer.front();
            buffer.pop();
            bufferLock.unlock(); // while writing to log file, give others a chance to write to the queue
//            decorateMessage(s);  // add date and timestamp
            logFile &lt;&lt; s.c_str() &lt;&lt; &quot;\n&quot;; // flush later, hence no std::endl but &quot;\n&quot;
            bufferLock.lock();
        }
        bufferLock.unlock();
        usleep(1000);  // if buffer is empty, go to sleep for 1ms
    }

    // Now we shall terminate. Clean up:
    // - Empty the queue. 
    // - Keep the lock, so no other thread can write to the queue
    bufferLock.lock();
    while ( !buffer.empty() )
    {
        string s = buffer.front();
        buffer.pop();
        logFile &lt;&lt; s.c_str() &lt;&lt; &quot;\n&quot;;
    }    

    logFile.flush(); 

    isTerminated = true;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1809857</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1809857</guid><dc:creator><![CDATA[gastgast]]></dc:creator><pubDate>Wed, 18 Nov 2009 12:57:10 GMT</pubDate></item></channel></rss>