﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-wish</title><link>http://www.cppblog.com/wish/</link><description /><language>zh-cn</language><lastBuildDate>Mon, 13 Apr 2026 09:38:06 GMT</lastBuildDate><pubDate>Mon, 13 Apr 2026 09:38:06 GMT</pubDate><ttl>60</ttl><item><title>简单的异步日志模块</title><link>http://www.cppblog.com/wish/archive/2008/08/30/60310.html</link><dc:creator>淼</dc:creator><author>淼</author><pubDate>Fri, 29 Aug 2008 19:26:00 GMT</pubDate><guid>http://www.cppblog.com/wish/archive/2008/08/30/60310.html</guid><wfw:comment>http://www.cppblog.com/wish/comments/60310.html</wfw:comment><comments>http://www.cppblog.com/wish/archive/2008/08/30/60310.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/wish/comments/commentRss/60310.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wish/services/trackbacks/60310.html</trackback:ping><description><![CDATA[<p>一个简单的异步日志模块，基本达到了灵活、高效、易用的目标。<br><br>此日志模块为文本日志，异步记录方式，即有单独的线程负责写日志文件。<br>具备以下特征：<br><br>1、可同时操作N个日志文件，N可配置；<br>2、可动态添加、删除和替换日志文件；<br>3、良好的多线程支持；<br>4、C++流风格的日志消息生成方式；<br><br>发布形式为ZLog.dll，使用时只需包含ZLog.h并连接ZLog.lib即可。<br><br>日志文件操作函数：<br><br>// 启动日志模块，logfile为日志文件名，level为输出到文件的最低日志等级，nQueueLength为内部缓冲队列的最大长度，一般使用默认值即可<br>// 日志等级由高至低分别为：LL_NONE、LL_FATAL、LL_ERROR、LL_WARNING、LL_INFO<br>// 例如，若level为LL_WARNING，则仅记录LL_WARNING、LL_ERROR和LL_FATAL等级的日志消息，而不记录LL_INFO等级的日志消息<br>// 成功启动后logfile的序号为0<br>// 返回0表明成功，其它值表明失败<br>int&nbsp; __cdecl startup(LPCTSTR logfile, LONG level, unsigned int nQueueLength = 256);<br></p>
<p>// 关闭日志模块<br>void __cdecl cleanup();<br><br>// 删除一个日志文件，fileindex指定该文件的序号（&gt;= 0），仅有一个日志文件时，不允许删除<br>BOOL __cdecl removefile(unsigned int fileindex);<br><br>// 得到日志文件的大小，fileindex为该文件的序号（&gt;= 0）<br>BOOL __cdecl getfilesize(unsigned int fileindex, ULARGE_INTEGER *lpuliFileSize);<br></p>
<p>// 将文件序号为fileindex的日志文件替换为newlogfile指定的文件，fileindex必需有效。<br>// 若当前有三个日志文件log1.txt，log2.txt和log3.txt，则序号分别为0，1，2。<br>// 若删除1代表的日志文件（即log2.txt），则log1.txt的序号不变，依然为0，log3.txt的序号变为1，同时序号2失效。<br>BOOL __cdecl replacefile(unsigned int fileindex, LPCTSTR newlogfile);<br><br>// 添加新的日志文件，newlogfile为文件名，若成功则返回新文件的序号（&gt;= 0），若失败，返回-1。日志文件最大数量为16<br>int&nbsp; __cdecl addfile(LPCTSTR newlogfile);<br><br>以上所有函数都是线程安全的。<br><br>使用方法：<br><br>&nbsp;&nbsp;&nbsp; loginfo，logwarn，logerr，logfatal分别生成information，warning，error，fatal类型的日志消息；<br>&nbsp;&nbsp;&nbsp; 输入参数格式与cout（ostream）基本一致，不同之处在于：<br>&nbsp;&nbsp;&nbsp; 1、可接受Unicode字符串<br>&nbsp;&nbsp;&nbsp; 2、第一个输入参数可为choosefile(n)，其中n为目标日志文件的序号<br><br>例子：<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">&nbsp;1</span>&nbsp;<span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ZLog.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;2</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;3</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;ZLog;<br></span><span style="COLOR: #008080">&nbsp;4</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;5</span>&nbsp;<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_tmain(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;argc,&nbsp;_TCHAR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;argv[])<br></span><span style="COLOR: #008080">&nbsp;6</span>&nbsp;<span style="COLOR: #000000">{<br></span><span style="COLOR: #008080">&nbsp;7</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;startup(TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log1.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">&nbsp;8</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">&nbsp;9</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;addfile(TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">10</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;addfile(TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log3.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">11</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">12</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;loginfo</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;log1.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;log1.txt,&nbsp;impliedly&nbsp;"choosefile(0)"</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">13</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logwarn</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;log2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;log2.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">14</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logerr</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">L</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;log3.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;log3.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">15</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">16</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;removefile(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">);&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;remove&nbsp;log3.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">17</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">18</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logerr</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;log3.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;log3.txt&nbsp;has&nbsp;been&nbsp;removed,&nbsp;so&nbsp;log&nbsp;entry&nbsp;will&nbsp;be&nbsp;redirected&nbsp;to&nbsp;log1.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">19</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">20</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;addfile(TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">newlog3.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;add&nbsp;"newlog3.txt"</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">21</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logerr</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;newlog3.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">22</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">23</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;replacefile(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">newlog2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;replace&nbsp;log2.txt&nbsp;with&nbsp;newlog2.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">24</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008080">25</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logfatal</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;newlog2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">26</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">27</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;replacefile(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">,&nbsp;TEXT(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">newnewlog2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">));&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">&nbsp;replace&nbsp;newlog2.txt&nbsp;with&nbsp;newnewlog2.txt</span><span style="COLOR: #008000"><br></span><span style="COLOR: #008080">28</span>&nbsp;<span style="COLOR: #008000"></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;logfatal</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">choosefile(</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;&lt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">log&nbsp;entry&nbsp;to&nbsp;newnewlog2.txt</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">29</span>&nbsp;<span style="COLOR: #000000"><br></span><span style="COLOR: #008080">30</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;cleanup();<br></span><span style="COLOR: #008080">31</span>&nbsp;<span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">32</span>&nbsp;<span style="COLOR: #000000">}<br></span><span style="COLOR: #008080">33</span>&nbsp;<span style="COLOR: #000000"></span></div>
<br>上例为单线程演示，在多线程环境下同样适用。<br>同一个程序中，若有多个模块同时引用了ZLog.dll，只需要调用一次startup和cleanup即可，但多次调用亦无副作用。<br><br>有兴趣的朋友可以测试一下，发现任何问题请告诉我。<br><br><a title=DLL下载 href="http://www.cppblog.com/Files/wish/ZLog.rar">DLL下载</a>&nbsp;&nbsp;&nbsp; <a title=源代码下载 href="http://www.cppblog.com/Files/wish/ZLogSource.rar">源代码下载</a><br>
<p>&nbsp;</p>
<img src ="http://www.cppblog.com/wish/aggbug/60310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wish/" target="_blank">淼</a> 2008-08-30 03:26 <a href="http://www.cppblog.com/wish/archive/2008/08/30/60310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>简单的Win32服务宿主和管理程序</title><link>http://www.cppblog.com/wish/archive/2008/02/02/42397.html</link><dc:creator>淼</dc:creator><author>淼</author><pubDate>Sat, 02 Feb 2008 06:22:00 GMT</pubDate><guid>http://www.cppblog.com/wish/archive/2008/02/02/42397.html</guid><wfw:comment>http://www.cppblog.com/wish/comments/42397.html</wfw:comment><comments>http://www.cppblog.com/wish/archive/2008/02/02/42397.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/wish/comments/commentRss/42397.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wish/services/trackbacks/42397.html</trackback:ping><description><![CDATA[一个简单的服务管理程序，用于创建/修改/删除Win32服务（未涉及内核服务，如tcpip，afd等）。<br><br>除此之外可以作为服务的宿主，命令行为：<br>"ServiceHost.exe" service "somedll.dll" "arg1" "arg2" ......<br><br>somedll.dll需要导出Start,Stop,Continue,Pause,Shutdown（可选，用于处理系统关闭事件），RequestStop（可选，用于服务主动要求停止）<br><br>函数原型：<br>DWORD WINAPI Start(int argc, TCHAR * const *argv); // 参数argv[n]即为arg1,arg2,...，返回0表明成功，其它值表明失败<br><br>DWORD WINAPI Stop();<br>DWORD WINAPI Shutdown();<br>DWORD WINAPI Pause();&nbsp;// 返回0表明成功，其它值表明失败<br>DWORD WINAPI Continue(); // 返回0表明成功，其它值表明失败<br>DWORD WINAPI RequestStop(DWORD (CALLBACK*)(DWORD dwErrorCode)); // 参数是一个函数指针。服务在启动时，DLL中的RequestStop（如果存在的话）将被调用，DLL可保存此函数指针。服务成功启动后，在任何需要的时候，DLL都可以通过调用此函数指针来主动要求停止服务（参数dwErrorCode为错误代码，dwErrorCode为0表明无错误）<br><br><a title=可执行文件 href="http://www.cppblog.com/Files/wish/ServiceHost.rar">可执行文件</a>&nbsp;&nbsp;&nbsp; <a title=源代码 href="http://www.cppblog.com/Files/wish/ServiceHostSource.rar">源代码</a><br>
<img src ="http://www.cppblog.com/wish/aggbug/42397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wish/" target="_blank">淼</a> 2008-02-02 14:22 <a href="http://www.cppblog.com/wish/archive/2008/02/02/42397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模仿MSN Messenger绘制的DirectUIHWND窗口</title><link>http://www.cppblog.com/wish/archive/2007/05/10/23827.html</link><dc:creator>淼</dc:creator><author>淼</author><pubDate>Thu, 10 May 2007 11:17:00 GMT</pubDate><guid>http://www.cppblog.com/wish/archive/2007/05/10/23827.html</guid><wfw:comment>http://www.cppblog.com/wish/comments/23827.html</wfw:comment><comments>http://www.cppblog.com/wish/archive/2007/05/10/23827.html#Feedback</comments><slash:comments>18</slash:comments><wfw:commentRss>http://www.cppblog.com/wish/comments/commentRss/23827.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wish/services/trackbacks/23827.html</trackback:ping><description><![CDATA[<p>模仿MSN画了一个DirectUI窗口，已经实现了静态控件，按钮，文本框，不过重绘效率还有点低，改变大小的时候响应比较慢。接下来打算把单选，复选，ComboBox等也做进去。<br><a title=可执行文件下载 href="http://www.cppblog.com/Files/wish/DirectUI.rar">可执行文件下载</a><br><br>截图如下：</p>
<br><img height=512 alt="" src="http://www.cppblog.com/images/cppblog_com/wish/sample.JPG" width=452 border=0> 
<img src ="http://www.cppblog.com/wish/aggbug/23827.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wish/" target="_blank">淼</a> 2007-05-10 19:17 <a href="http://www.cppblog.com/wish/archive/2007/05/10/23827.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>GUI程序也能使用控制台窗口</title><link>http://www.cppblog.com/wish/archive/2007/05/08/23643.html</link><dc:creator>淼</dc:creator><author>淼</author><pubDate>Tue, 08 May 2007 11:57:00 GMT</pubDate><guid>http://www.cppblog.com/wish/archive/2007/05/08/23643.html</guid><wfw:comment>http://www.cppblog.com/wish/comments/23643.html</wfw:comment><comments>http://www.cppblog.com/wish/archive/2007/05/08/23643.html#Feedback</comments><slash:comments>12</slash:comments><wfw:commentRss>http://www.cppblog.com/wish/comments/commentRss/23643.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wish/services/trackbacks/23643.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在GUI程序中使用cout/cin/printf&nbsp;&nbsp;<a href='http://www.cppblog.com/wish/archive/2007/05/08/23643.html'>阅读全文</a><img src ="http://www.cppblog.com/wish/aggbug/23643.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wish/" target="_blank">淼</a> 2007-05-08 19:57 <a href="http://www.cppblog.com/wish/archive/2007/05/08/23643.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>