<span class="com">#include</span><span class="pun"><</span><span class="typ">QtDebug</span><span class="pun">></span>
<span class="com">#include</span><span class="pun"><</span><span class="typ">QFile</span><span class="pun">></span>
<span class="com">#include</span><span class="pun"><</span><span class="typ">QTextStream</span><span class="pun">></span>
<span class="kwd">void</span><span class="pln"> customMessageHandler</span><span class="pun">(</span><span class="typ">QtMsgType</span><span class="pln"> type</span><span class="pun">,</span><span class="kwd">const</span><span class="kwd">char</span><span class="pun">*</span><span class="pln">msg</span><span class="pun">)</span>
<span class="pun">{</span>
<span class="typ">QString</span><span class="pln"> txt</span><span class="pun">;</span>
<span class="kwd">switch</span><span class="pun">(</span><span class="pln">type</span><span class="pun">)</span><span class="pun">{</span>
<span class="kwd">case</span><span class="typ">QtDebugMsg</span><span class="pun">:</span>
<span class="pln"> txt </span><span class="pun">=</span><span class="typ">QString</span><span class="pun">(</span><span class="str">"Debug: %1"</span><span class="pun">).</span><span class="pln">arg</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span>
<span class="kwd">break</span><span class="pun">;</span>
<span class="kwd">case</span><span class="typ">QtWarningMsg</span><span class="pun">:</span>
<span class="pln"> txt </span><span class="pun">=</span><span class="typ">QString</span><span class="pun">(</span><span class="str">"Warning: %1"</span><span class="pun">).</span><span class="pln">arg</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span>
<span class="kwd">break</span><span class="pun">;</span>
<span class="kwd">case</span><span class="typ">QtCriticalMsg</span><span class="pun">:</span>
<span class="pln"> txt </span><span class="pun">=</span><span class="typ">QString</span><span class="pun">(</span><span class="str">"Critical: %1"</span><span class="pun">).</span><span class="pln">arg</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span>
<span class="kwd">break</span><span class="pun">;</span>
<span class="kwd">case</span><span class="typ">QtFatalMsg</span><span class="pun">:</span>
<span class="pln"> txt </span><span class="pun">=</span><span class="typ">QString</span><span class="pun">(</span><span class="str">"Fatal: %1"</span><span class="pun">).</span><span class="pln">arg</span><span class="pun">(</span><span class="pln">msg</span><span class="pun">);</span>
<span class="pln"> abort</span><span class="pun">();</span>
<span class="pun">}</span>
<span class="typ">QFile</span><span class="pln"> outFile</span><span class="pun">(</span><span class="str">"debuglog.txt"</span><span class="pun">);</span>
<span class="pln"> outFile</span><span class="pun">.</span><span class="pln">open</span><span class="pun">(</span><span class="typ">QIODevice</span><span class="pun">::</span><span class="typ">WriteOnly</span><span class="pun">|</span><span class="typ">QIODevice</span><span class="pun">::</span><span class="typ">Append</span><span class="pun">);</span>
<span class="typ">QTextStream</span><span class="pln"> ts</span><span class="pun">(&</span><span class="pln">outFile</span><span class="pun">);</span>
<span class="pln"> ts </span><span class="pun"><<</span><span class="pln"> txt </span><span class="pun"><<</span><span class="pln"> endl</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="typ">int</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">int</span><span class="pln"> argc</span><span class="pun">,</span><span class="kwd">char</span><span class="pun">*</span><span class="pln"> argv</span><span class="pun">[]</span><span class="pun">)</span>
<span class="pun">{</span>
<span class="typ">QApplication</span><span class="pln"> app</span><span class="pun">(</span><span class="pln"> argc</span><span class="pun">,</span><span class="pln"> argv </span><span class="pun">);</span>
<span class="com">//Lets register our custom handler, before we start </span>
<span class="pln"> qInstallMsgHandler</span><span class="pun">(</span><span class="pln">customMessageHandler</span><span class="pun">);</span>
<span class="pun">...</span>
<span class="kwd">return</span><span class="pln"> app</span><span class="pun">.</span><span class="pln">exec</span><span class="pun">();</span>
<span class="pun">}</span>