夜雨聆风学习资料网

ARTICLE · 1417268

附完整源码 | C语言设计跨平台日志库|企业级开发

附完整源码 | C语言设计跨平台日志库|企业级开发

<section class="mp_profile_iframe_wrp" nodeleaf=""><mp-common-profile class="custom_select_card mp_profile_iframe mp_common_widget" data-pluginname="mpprofile" data-nickname="C语言中文社区" data-alias="cyyzwsq" data-from="0" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/hMWGJFibgaTtb77gBS2QiaX1HQvSFb14kSlxUibMKx6YY1cwxENASI8ib6aglE4OXCWFGYNPCZD9GMiayahXwpIMD1w/0?wx_fmt=png" data-signature="学习路线 | AI编程 | 代码实例 | 实战源码 | 开发工具 | 视频教程 | 面试题 | 电子书 | 专注于C语言编程技术分享" data-id="MzIyNjQ3NDk2Mg==" data-service_type="1" data-verify_status="1"></mp-common-profile></section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="" data-pm-slice="0 0 []"><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 20px;color: rgb(0, 150, 136);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 3px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">推荐阅读</span></span><span style="display: none;"></span></h2><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><a class="normal_text_link" localeditorid="ujv5uz8uf9s0000000" href="https://mp.weixin.qq.com/s?__biz=MzIyNjQ3NDk2Mg==&amp;mid=2247503375&amp;idx=2&amp;sn=e72005fc531eae9e6accfaa3fb5d6a28&amp;scene=21#wechat_redirect" textvalue="一文搞懂C语言中的“栈溢出”和“堆溢出”,全网最通俗的解释" target="_blank" data-linktype="2">一文搞懂C语言中的“栈溢出”和“堆溢出”,全网最通俗的解释</a><br /></p><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><a class="normal_text_link" localeditorid="2popz6p77ws00000000" href="https://mp.weixin.qq.com/s?__biz=MzIyNjQ3NDk2Mg==&amp;mid=2247503375&amp;idx=1&amp;sn=fc4a5bace78f6a10740cdeee9656bcfb&amp;scene=21#wechat_redirect" textvalue="GitHub&nbsp;标星 88.9 K,&nbsp;9&nbsp;个开源项目绝了 !" target="_blank" data-linktype="2">GitHub&nbsp;标星 88.9 K,&nbsp;9&nbsp;个开源项目绝了 !</a><br /></p><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><a class="normal_text_link" localeditorid="gnsa59cg6lc0000000" href="https://mp.weixin.qq.com/s?__biz=MzIyNjQ3NDk2Mg==&amp;mid=2247503375&amp;idx=6&amp;sn=a30099a8dd11aba73c4d532522a0da91&amp;scene=21#wechat_redirect" textvalue="C语言最新面试题汇总PDF详解版" target="_blank" data-linktype="2">C语言最新面试题汇总PDF详解版</a><br /></p><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><a class="normal_text_link" localeditorid="asd5yqbouao0000000" href="https://mp.weixin.qq.com/s?__biz=MzIyNjQ3NDk2Mg==&amp;mid=2247503352&amp;idx=1&amp;sn=c96c44c0915eb56e3f666cefeb4db003&amp;scene=21#wechat_redirect" textvalue="原码, 反码, 补码 你搞懂了吗?" target="_blank" data-linktype="2">原码, 反码, 补码 你搞懂了吗?</a><br /></p><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><a class="normal_text_link" localeditorid="t2keuz8dsz40000000" href="https://mp.weixin.qq.com/s?__biz=MzIyNjQ3NDk2Mg==&amp;mid=2247503346&amp;idx=1&amp;sn=29bd82d11e610d186375a6ec5470b948&amp;scene=21#wechat_redirect" textvalue="从本质到实现,谈谈什么是 C 和 C ++ 标准库?" target="_blank" data-linktype="2">从本质到实现,谈谈什么是 C 和 C ++ 标准库?</a><br /></p><hr style="margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;border-top-style: solid;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 3px;border-left-width: 3px;border-right-width: 3px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgba(0, 0, 0, 0.4);border-left-color: rgba(0, 0, 0, 0.4);border-right-color: rgba(0, 0, 0, 0.4);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-right-radius: 0px;border-bottom-left-radius: 0px;background-attachment: scroll;background-clip: border-box;background-color: rgba(0, 0, 0, 0);background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;width: auto;height: 1px;" /><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><strong style="color: rgb(0, 150, 136);font-weight: bold;background-attachment: scroll;background-clip: border-box;background-color: rgba(0, 0, 0, 0);background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;width: auto;height: auto;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 3px;border-bottom-width: 3px;border-left-width: 3px;border-right-width: 3px;border-top-color: rgba(0, 0, 0, 0.4);border-bottom-color: rgba(0, 0, 0, 0.4);border-left-color: rgba(0, 0, 0, 0.4);border-right-color: rgba(0, 0, 0, 0.4);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-right-radius: 0px;border-bottom-left-radius: 0px;"><span leaf="">C语言中文社区源码</span></strong><span leaf="">git仓库:</span><strong style="color: rgb(0, 150, 136);font-weight: bold;background-attachment: scroll;background-clip: border-box;background-color: rgba(0, 0, 0, 0);background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;width: auto;height: auto;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 3px;border-bottom-width: 3px;border-left-width: 3px;border-right-width: 3px;border-top-color: rgba(0, 0, 0, 0.4);border-bottom-color: rgba(0, 0, 0, 0.4);border-left-color: rgba(0, 0, 0, 0.4);border-right-color: rgba(0, 0, 0, 0.4);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-right-radius: 0px;border-bottom-left-radius: 0px;"><span leaf="">https://gitee.com/cyyzwsq/C-Coding.git</span></strong></p><hr style="margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;border-top-style: solid;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 3px;border-left-width: 3px;border-right-width: 3px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgba(0, 0, 0, 0.4);border-left-color: rgba(0, 0, 0, 0.4);border-right-color: rgba(0, 0, 0, 0.4);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-right-radius: 0px;border-bottom-left-radius: 0px;background-attachment: scroll;background-clip: border-box;background-color: rgba(0, 0, 0, 0);background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;width: auto;height: 1px;" /><h1 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: flex;flex-direction: unset;float: unset;height: auto;justify-content: center;line-height: 1.5em;overflow-x: unset;overflow-y: unset;text-align: left;text-shadow: none;transform: none;width: 100%;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 22px;border-bottom-color: rgb(0, 150, 136);color: rgb(0, 150, 136);line-height: 1.5em;letter-spacing: 0em;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;width: 100%;height: auto;align-items: unset;border-top-style: none;border-bottom-style: solid;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 10px;padding-left: 0px;padding-right: 0px;text-align: center;text-indent: 0em;text-shadow: none;transform: none;-webkit-box-reflect: unset;"><span leaf="">正文</span></span><span style="display: none;"></span></h1><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><span leaf="">大家好,我是冲哥。今天我们来实现一个实用又经典的项目——跨平台C语言日志库</span></p><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 20px;color: rgb(0, 150, 136);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 3px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">设计详解</span></span><span style="display: none;"></span></h2><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">1. 日志级别</span></span><span style="display: none;"></span></h3><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><span leaf="">定义了5种标准日志级别:DEBUG、INFO、WARNING、ERROR和FATAL,允许用户根据需要过滤日志。</span></p><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">2. 多线程安全</span></span><span style="display: none;"></span></h3><ul style="list-style-type: disc;margin-top: 8px;margin-bottom: 8px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 25px;padding-right: 0px;color: rgb(0, 0, 0);" class="list-paddingleft-1"><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">使用互斥锁保护共享资源(日志队列)</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">使用条件变量实现高效的生产者-消费者模型</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">跨平台线程ID获取</span></section></li></ul><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">3. 异步写入</span></span><span style="display: none;"></span></h3><ul style="list-style-type: disc;margin-top: 8px;margin-bottom: 8px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 25px;padding-right: 0px;color: rgb(0, 0, 0);" class="list-paddingleft-1"><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">主线程将日志消息放入队列后立即返回</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">专用工作线程负责从队列中取出消息并写入文件</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">减少I/O操作对主程序性能的影响</span></section></li></ul><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">4. 跨平台支持</span></span><span style="display: none;"></span></h3><ul style="list-style-type: disc;margin-top: 8px;margin-bottom: 8px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 25px;padding-right: 0px;color: rgb(0, 0, 0);" class="list-paddingleft-1"><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">使用条件编译处理Windows和POSIX系统的差异</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">统一的API接口,隐藏平台实现细节</span></section></li></ul><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">5. 灵活的配置</span></span><span style="display: none;"></span></h3><ul style="list-style-type: disc;margin-top: 8px;margin-bottom: 8px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 25px;padding-right: 0px;color: rgb(0, 0, 0);" class="list-paddingleft-1"><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">支持控制台和文件输出</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">可配置日志级别、文件路径和缓冲区大小</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">同步/异步模式可选</span></section></li></ul><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">6. 易用性</span></span><span style="display: none;"></span></h3><ul style="list-style-type: disc;margin-top: 8px;margin-bottom: 8px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 25px;padding-right: 0px;color: rgb(0, 0, 0);" class="list-paddingleft-1"><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">提供宏简化日志调用</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">自动包含文件名和行号信息</span></section></li><li><section style="margin-top: 5px;margin-bottom: 5px;color: rgb(1, 1, 1);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: left;font-weight: normal;"><span leaf="">格式化时间戳和线程ID</span></section></li></ul><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><span leaf="">这个日志库提供了生产环境所需的基本功能,可以根据需要进一步扩展,比如添加日志滚动、网络输出、更精细的配置选项等功能。</span></p><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 20px;color: rgb(0, 150, 136);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 3px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">源码</span></span><span style="display: none;"></span></h2><p data-tool="mdnice编辑器" style="color: rgb(0, 0, 0);font-size: 16px;line-height: 1.8em;letter-spacing: 0em;text-align: justify;text-indent: 0em;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;padding-top: 8px;padding-bottom: 8px;padding-left: 0px;padding-right: 0px;"><span leaf="">完整的跨平台C语言日志库实现,支持分级日志、多线程安全和异步写入功能。</span></p><figure data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;display: flex;flex-direction: column;justify-content: center;align-items: center;"><span leaf=""><img class="rich_pages wxw-img" data-imgfileid="100019742" data-type="png" style="display: block;margin: 0px auto;max-width: 100%;border-style: none;border-width: 3px;border-color: rgba(0, 0, 0, 0.4);border-radius: 0px;object-fit: fill;box-shadow: rgba(0, 0, 0, 0) 0px 0px 0px 0px;height: auto !important;" data-ratio="0.6314814814814815" data-w="1080" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/hMWGJFibgaTtDDs39KQj2UFTNHefzvQf2huDmQGKG6tZGBBMZMwGbsiabnkgAAs0mkyTqTg586vkDbMibqmcIOoyA/640?wx_fmt=png&amp;from=appmsg" /></span></figure><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">头文件 (logging.h)</span></span><span style="display: none;"></span></h3><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;text-align: left;margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"><span data-cacheurl="" data-remoteid="" style="display: block;background: none;height: 30px;width: 100%;background-size: 40px;background-repeat: no-repeat;background-color: #282c34;margin-bottom: -7px;border-radius: 5px;background-position: 10px 10px;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/vPjficwtgTJBB6Aric7UOo5QNfgHfrYniaLLibAEOsickh6Ust0STwSwr6zUEibgMFGj2eTS8e6wZ2jhUOdwKuSoUzz8kRpCwRL6hY/640?wx_fmt=svg&amp;from=appmsg&quot;);"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;padding-top: 15px;background: #282c34;border-radius: 5px;display: -webkit-box;font-family: Consolas, Monaco, Menlo, monospace;font-size: 12px;"><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifndef</span></span><span leaf="">&nbsp;LOGGING_H</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOGGING_H</span></span><br /><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;stdio.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;stdlib.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;string.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;time.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;stdarg.h&gt;</span></span></span><br /><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;windows.h&gt;</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;process.h&gt;</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;THREAD_ID GetCurrentThreadId()</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;pthread.h&gt;</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;unistd.h&gt;</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;THREAD_ID (unsigned long)pthread_self()</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志级别定义</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">typedef</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">enum</span></span><span leaf="">&nbsp;{</span><br /><span leaf="">&nbsp; &nbsp; LOG_LEVEL_DEBUG,</span><br /><span leaf="">&nbsp; &nbsp; LOG_LEVEL_INFO,</span><br /><span leaf="">&nbsp; &nbsp; LOG_LEVEL_WARNING,</span><br /><span leaf="">&nbsp; &nbsp; LOG_LEVEL_ERROR,</span><br /><span leaf="">&nbsp; &nbsp; LOG_LEVEL_FATAL</span><br /><span leaf="">}&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志配置结构</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">typedef</span></span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;{</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">&nbsp;level;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* file_path;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;use_console;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;async_mode;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">size_t</span></span><span leaf="">&nbsp;buffer_size;</span><br /><span leaf="">}&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 初始化日志系统</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_init</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">* config)</span></span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 关闭日志系统</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_shutdown</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">)</span></span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志记录函数</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_write</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">&nbsp;level,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* file,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;line,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* fmt, ...)</span></span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 宏简化日志调用</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOG_DEBUG(...) log_write(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOG_INFO(...) log_write(LOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOG_WARNING(...) log_write(LOG_LEVEL_WARNING, __FILE__, __LINE__, __VA_ARGS__)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOG_ERROR(...) log_write(LOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;LOG_FATAL(...) log_write(LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)</span></span><br /><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span><span leaf="">&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// LOGGING_H</span></span></span><br /></code></pre><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">实现文件 (logging.c)</span></span><span style="display: none;"></span></h3><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;text-align: left;margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"><span data-cacheurl="" data-remoteid="" style="display: block;background: none;height: 30px;width: 100%;background-size: 40px;background-repeat: no-repeat;background-color: #282c34;margin-bottom: -7px;border-radius: 5px;background-position: 10px 10px;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/vPjficwtgTJBB6Aric7UOo5QNfgHfrYniaLLibAEOsickh6Ust0STwSwr6zUEibgMFGj2eTS8e6wZ2jhUOdwKuSoUzz8kRpCwRL6hY/640?wx_fmt=svg&amp;from=appmsg&quot;);"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;padding-top: 15px;background: #282c34;border-radius: 5px;display: -webkit-box;font-family: Consolas, Monaco, Menlo, monospace;font-size: 12px;"><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;logging.h&quot;</span></span></span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志消息结构</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">typedef</span></span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;{</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* message;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">&nbsp;level;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">time_t</span></span><span leaf="">&nbsp;timestamp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">unsigned</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">long</span></span><span leaf="">&nbsp;thread_id;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* file;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;line;</span><br /><span leaf="">}&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志队列节点</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">typedef</span></span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">log_node</span></span><span leaf="">&nbsp;{</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">log_node</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">next</span></span><span leaf="">;</span></span><br /><span leaf="">}&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志队列</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">typedef</span></span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;{</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* head;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* tail;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">size_t</span></span><span leaf="">&nbsp;size;</span><br /><span leaf="">}&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 全局变量</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">&nbsp;g_config;</span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;FILE* g_log_file =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;g_initialized =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 多线程同步</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;CRITICAL_SECTION g_queue_mutex;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;CONDITION_VARIABLE g_queue_cond;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;HANDLE g_worker_thread;</span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">pthread_mutex_t</span></span><span leaf="">&nbsp;g_queue_mutex = PTHREAD_MUTEX_INITIALIZER;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">pthread_cond_t</span></span><span leaf="">&nbsp;g_queue_cond = PTHREAD_COND_INITIALIZER;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">pthread_t</span></span><span leaf="">&nbsp;g_worker_thread;</span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 异步模式相关</span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">&nbsp;g_log_queue;</span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">volatile</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;g_shutdown =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 队列操作函数</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">queue_init</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;size =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">}</span><br /><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">queue_push</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* node = (</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">*)</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">malloc</span></span><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">sizeof</span></span><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">));</span><br /><span leaf="">&nbsp; &nbsp; node-&gt;msg = msg;</span><br /><span leaf="">&nbsp; &nbsp; node-&gt;next =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail-&gt;next = node;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail = node;</span><br /><span leaf="">&nbsp; &nbsp; }&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">else</span></span><span leaf="">&nbsp;{</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail = node;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;size++;</span><br /><span leaf="">}</span><br /><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">*&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">queue_pop</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head)&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* node =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg = node-&gt;msg;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head = node-&gt;next;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head)&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(node);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;size--;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span leaf="">&nbsp;msg;</span><br /><span leaf="">}</span><br /><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">queue_clear</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_queue_t</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* current =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">while</span></span><span leaf="">&nbsp;(current) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_node_t</span></span><span leaf="">* next = current-&gt;next;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(current-&gt;msg-&gt;message);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(current-&gt;msg);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(current);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; current = next;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;head =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;tail =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">queue</span></span><span leaf="">-&gt;size =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 获取日志级别名称</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">*&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">get_level_string</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">&nbsp;level)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">switch</span></span><span leaf="">&nbsp;(level) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">case</span></span><span leaf="">&nbsp;LOG_LEVEL_DEBUG:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;DEBUG&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">case</span></span><span leaf="">&nbsp;LOG_LEVEL_INFO:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;INFO&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">case</span></span><span leaf="">&nbsp;LOG_LEVEL_WARNING:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;WARNING&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">case</span></span><span leaf="">&nbsp;LOG_LEVEL_ERROR:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;ERROR&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">case</span></span><span leaf="">&nbsp;LOG_LEVEL_FATAL:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;FATAL&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">default</span></span><span leaf="">:&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;UNKNOWN&quot;</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 格式化时间</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">format_time</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* buffer,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">size_t</span></span><span leaf="">&nbsp;size)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">time_t</span></span><span leaf="">&nbsp;now = time(</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">struct</span></span><span leaf="">&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">tm</span></span><span leaf="">*&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">tm_info</span></span><span leaf="">&nbsp;=&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">localtime</span></span><span leaf="">(&amp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">now</span></span><span leaf="">);</span></span><br /><span leaf="">&nbsp; &nbsp; strftime(buffer, size,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;%Y-%m-%d %H:%M:%S&quot;</span></span><span leaf="">, tm_info);</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 写入日志到文件和控制台</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">write_log_direct</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">&nbsp;time_buffer[</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">20</span></span><span leaf="">];</span><br /><span leaf="">&nbsp; &nbsp; format_time(time_buffer,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">sizeof</span></span><span leaf="">(time_buffer));</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 格式化日志消息</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* level_str = get_level_string(msg-&gt;level);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* file_name =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">strrchr</span></span><span leaf="">(msg-&gt;file,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">'/'</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!file_name) file_name =&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">strrchr</span></span><span leaf="">(msg-&gt;file,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">'\\'</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; file_name = file_name ? file_name +&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">&nbsp;: msg-&gt;file;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 输出到文件</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_log_file) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(g_log_file,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;[%s] [%lu] [%s] [%s:%d] %s\n&quot;</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_buffer, msg-&gt;thread_id, level_str,&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_name, msg-&gt;line, msg-&gt;message);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; fflush(g_log_file);</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 输出到控制台</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_config.use_console) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(msg-&gt;level &gt;= LOG_LEVEL_WARNING ?&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stderr</span></span><span leaf="">&nbsp;:&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stdout</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;[%s] [%lu] [%s] [%s:%d] %s\n&quot;</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time_buffer, msg-&gt;thread_id, level_str,&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_name, msg-&gt;line, msg-&gt;message);</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 工作线程函数(异步模式)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">unsigned</span></span><span leaf="">&nbsp;__stdcall&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">worker_thread</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">* arg)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">*&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">worker_thread</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">* arg)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">while</span></span><span leaf="">&nbsp;(!g_shutdown || g_log_queue.size &gt;&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 锁定队列</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnterCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_log_queue.size ==&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">&nbsp;&amp;&amp; !g_shutdown) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SleepConditionVariableCS(&amp;g_queue_cond, &amp;g_queue_mutex, INFINITE);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_lock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_log_queue.size ==&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">&nbsp;&amp;&amp; !g_shutdown) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_cond_wait(&amp;g_queue_cond, &amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 处理所有队列中的消息</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">while</span></span><span leaf="">&nbsp;(g_log_queue.size &gt;&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg = queue_pop(&amp;g_log_queue);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 解锁以便其他线程可以继续添加日志</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LeaveCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_unlock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 写入日志</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(msg) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write_log_direct(msg);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(msg-&gt;message);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(msg);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 重新锁定以检查队列</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EnterCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_lock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 解锁队列</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LeaveCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_unlock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 短暂休眠以减少CPU使用</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sleep(</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">10</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usleep(</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">10000</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 初始化日志系统</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_init</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">* config)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_initialized)&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 复制配置</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">memcpy</span></span><span leaf="">(&amp;g_config, config,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">sizeof</span></span><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">));</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 打开日志文件</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_config.file_path) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; g_log_file = fopen(g_config.file_path,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;a&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!g_log_file) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stderr</span></span><span leaf="">,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Failed to open log file: %s\n&quot;</span></span><span leaf="">, g_config.file_path);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">-1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 初始化队列</span></span><br /><span leaf="">&nbsp; &nbsp; queue_init(&amp;g_log_queue);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 初始化同步原语</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; InitializeCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; InitializeConditionVariable(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 启动工作线程(异步模式)</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_config.async_mode) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g_worker_thread = (HANDLE)_beginthreadex(</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">,&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">, worker_thread,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">,&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!g_worker_thread) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stderr</span></span><span leaf="">,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Failed to create worker thread\n&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">-1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(pthread_create(&amp;g_worker_thread,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">, worker_thread,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">) !=&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stderr</span></span><span leaf="">,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Failed to create worker thread\n&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">-1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; g_initialized =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 关闭日志系统</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_shutdown</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!g_initialized)&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; g_shutdown =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 通知工作线程</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; EnterCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; WakeConditionVariable(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; LeaveCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 等待工作线程结束</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(g_worker_thread, INFINITE);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(g_worker_thread);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; DeleteCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_lock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_cond_signal(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_unlock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 等待工作线程结束</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_join(g_worker_thread,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_destroy(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_cond_destroy(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 清理队列中剩余的消息</span></span><br /><span leaf="">&nbsp; &nbsp; queue_clear(&amp;g_log_queue);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 关闭日志文件</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(g_log_file) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; fclose(g_log_file);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; g_log_file =&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; g_initialized =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">}</span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 日志记录函数</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">log_write</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_level_t</span></span><span leaf="">&nbsp;level,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* file,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;line,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">const</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">* fmt, ...)</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!g_initialized || level &lt; g_config.level)&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 格式化消息</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">char</span></span><span leaf="">&nbsp;message[</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1024</span></span><span leaf="">];</span><br /><span leaf="">&nbsp; &nbsp; va_list args;</span><br /><span leaf="">&nbsp; &nbsp; va_start(args, fmt);</span><br /><span leaf="">&nbsp; &nbsp; vsnprintf(message,&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">sizeof</span></span><span leaf="">(message), fmt, args);</span><br /><span leaf="">&nbsp; &nbsp; va_end(args);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 创建日志消息</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">* msg = (</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">*)</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">malloc</span></span><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">sizeof</span></span><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_message_t</span></span><span leaf="">));</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;level = level;</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;timestamp = time(</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;thread_id = THREAD_ID;</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;file = file;</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;line = line;</span><br /><span leaf="">&nbsp; &nbsp; msg-&gt;message = strdup(message);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 同步模式直接写入</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(!g_config.async_mode) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; write_log_direct(msg);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(msg-&gt;message);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">free</span></span><span leaf="">(msg);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 异步模式:添加到队列</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; EnterCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_lock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp; queue_push(&amp;g_log_queue, msg);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; WakeConditionVariable(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; LeaveCriticalSection(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_cond_signal(&amp;g_queue_cond);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; pthread_mutex_unlock(&amp;g_queue_mutex);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">}</span><br /></code></pre><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">使用示例 (example.c)</span></span><span style="display: none;"></span></h3><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;text-align: left;margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"><span data-cacheurl="" data-remoteid="" style="display: block;background: none;height: 30px;width: 100%;background-size: 40px;background-repeat: no-repeat;background-color: #282c34;margin-bottom: -7px;border-radius: 5px;background-position: 10px 10px;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/vPjficwtgTJBB6Aric7UOo5QNfgHfrYniaLLibAEOsickh6Ust0STwSwr6zUEibgMFGj2eTS8e6wZ2jhUOdwKuSoUzz8kRpCwRL6hY/640?wx_fmt=svg&amp;from=appmsg&quot;);"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;padding-top: 15px;background: #282c34;border-radius: 5px;display: -webkit-box;font-family: Consolas, Monaco, Menlo, monospace;font-size: 12px;"><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;logging.h&quot;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;stdio.h&gt;</span></span></span><br /><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;windows.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">define</span></span><span leaf="">&nbsp;sleep(n) Sleep(n * 1000)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">include</span></span><span leaf="">&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&lt;unistd.h&gt;</span></span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><br /><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 模拟多线程环境</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">unsigned</span></span><span leaf="">&nbsp;__stdcall&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">thread_func</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">* arg)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span style="color: #c678dd;line-height: 26px;"><span leaf="">static</span></span><span leaf="">&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">*&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">thread_func</span></span><span style="line-height: 26px;"><span leaf="">(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">void</span></span><span leaf="">* arg)</span></span><br /><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;thread_id = *(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">*)arg;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">for</span></span><span leaf="">&nbsp;(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;i =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">; i &lt;&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">5</span></span><span leaf="">; i++) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; LOG_INFO(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Thread %d: message %d&quot;</span></span><span leaf="">, thread_id, i);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; sleep(</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">}</span><br /><br /><span style="line-height: 26px;"><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">main</span></span><span style="line-height: 26px;"><span leaf="">()</span></span><span leaf="">&nbsp;</span></span><span leaf="">{</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 配置日志系统</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">log_config_t</span></span><span leaf="">&nbsp;config = {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; .level = LOG_LEVEL_DEBUG,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; .file_path =&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;app.log&quot;</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; .use_console =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; .async_mode =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">,</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; .buffer_size =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1024</span></span><br /><span leaf="">&nbsp; &nbsp; };</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 初始化日志系统</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">if</span></span><span leaf="">&nbsp;(log_init(&amp;config) !=&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">fprintf</span></span><span leaf="">(</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">stderr</span></span><span leaf="">,&nbsp;</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Failed to initialize logging system\n&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">;</span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 记录一些日志</span></span><br /><span leaf="">&nbsp; &nbsp; LOG_DEBUG(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;This is a debug message&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; LOG_INFO(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;This is an info message&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; LOG_WARNING(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;This is a warning message&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; LOG_ERROR(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;This is an error message&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; LOG_FATAL(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;This is a fatal message&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 创建多个线程测试多线程安全性</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; HANDLE threads[</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">];</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">pthread_t</span></span><span leaf="">&nbsp;threads[</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">];</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;thread_ids[</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">] = {</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">1</span></span><span leaf="">,&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">2</span></span><span leaf="">,&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">};</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">for</span></span><span leaf="">&nbsp;(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;i =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">; i &lt;&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">; i++) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; threads[i] = (HANDLE)_beginthreadex(</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">,&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">, thread_func, &amp;thread_ids[i],&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">,&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_create(&amp;threads[i],&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">, thread_func, &amp;thread_ids[i]);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 等待所有线程完成</span></span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">for</span></span><span leaf="">&nbsp;(</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">int</span></span><span leaf="">&nbsp;i =&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">; i &lt;&nbsp;</span><span style="color: #d19a66;line-height: 26px;"><span leaf="">3</span></span><span leaf="">; i++) {</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">ifdef</span></span><span leaf="">&nbsp;_WIN32</span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject(threads[i], INFINITE);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CloseHandle(threads[i]);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">else</span></span></span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pthread_join(threads[i],&nbsp;</span><span style="color: #56b6c2;line-height: 26px;"><span leaf="">NULL</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="color: #61aeee;line-height: 26px;"><span leaf="">#</span><span style="line-height: 26px;"><span leaf="">endif</span></span></span><br /><span leaf="">&nbsp; &nbsp; }</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #5c6370;font-style: italic;line-height: 26px;"><span leaf="">// 关闭日志系统</span></span><br /><span leaf="">&nbsp; &nbsp; log_shutdown();</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #e6c07b;line-height: 26px;"><span leaf="">printf</span></span><span leaf="">(</span><span style="color: #98c379;line-height: 26px;"><span leaf="">&quot;Logging completed. Check app.log for output.\n&quot;</span></span><span leaf="">);</span><br /><span leaf="">&nbsp; &nbsp;&nbsp;</span><span style="color: #c678dd;line-height: 26px;"><span leaf="">return</span></span><span style="color: #d19a66;line-height: 26px;"><span leaf="">0</span></span><span leaf="">;</span><br /><span leaf="">}</span><br /></code></pre><h2 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 20px;color: rgb(0, 150, 136);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 3px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">编译说明</span></span><span style="display: none;"></span></h2><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">Linux/macOS 编译:</span></span><span style="display: none;"></span></h3><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;text-align: left;margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"><span data-cacheurl="" data-remoteid="" style="display: block;background: none;height: 30px;width: 100%;background-size: 40px;background-repeat: no-repeat;background-color: #282c34;margin-bottom: -7px;border-radius: 5px;background-position: 10px 10px;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/vPjficwtgTJBB6Aric7UOo5QNfgHfrYniaLLibAEOsickh6Ust0STwSwr6zUEibgMFGj2eTS8e6wZ2jhUOdwKuSoUzz8kRpCwRL6hY/640?wx_fmt=svg&amp;from=appmsg&quot;);"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;padding-top: 15px;background: #282c34;border-radius: 5px;display: -webkit-box;font-family: Consolas, Monaco, Menlo, monospace;font-size: 12px;"><span leaf="">gcc -o example example.c logging.c -lpthread -D_POSIX_C_SOURCE=199309L</span><br /></code></pre><h3 data-tool="mdnice编辑器" style="margin-top: 30px;margin-bottom: 15px;margin-left: 0px;margin-right: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;border-top-style: none;border-bottom-style: none;border-left-style: none;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 1px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 0, 0);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;box-shadow: none;display: block;flex-direction: unset;float: unset;height: auto;justify-content: unset;line-height: 1.5em;overflow-x: unset;overflow-y: unset;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;text-align: left;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span style="display: none;"></span><span style="font-size: 18px;color: rgb(34, 34, 34);line-height: 1.8em;letter-spacing: 0em;padding-top: 0px;padding-bottom: 0px;padding-left: 10px;padding-right: 0px;border-top-style: none;border-bottom-style: none;border-left-style: solid;border-right-style: none;border-top-width: 1px;border-bottom-width: 1px;border-left-width: 2px;border-right-width: 1px;border-top-color: rgb(0, 0, 0);border-bottom-color: rgb(0, 0, 0);border-left-color: rgb(0, 150, 136);border-right-color: rgb(0, 0, 0);border-top-left-radius: 0px;border-top-right-radius: 0px;border-bottom-left-radius: 0px;border-bottom-right-radius: 0px;align-items: unset;background-attachment: scroll;background-clip: border-box;background-color: unset;background-image: none;background-origin: padding-box;background-position-x: 0%;background-position-y: 0%;background-repeat: no-repeat;background-size: auto;box-shadow: none;display: block;font-weight: bold;flex-direction: unset;float: unset;height: auto;justify-content: unset;margin-top: 0px;margin-bottom: 0px;margin-left: 0px;margin-right: 0px;overflow-x: unset;overflow-y: unset;text-align: left;text-indent: 0em;text-shadow: none;transform: none;width: auto;-webkit-box-reflect: unset;"><span leaf="">Windows 编译 (使用 MinGW):</span></span><span style="display: none;"></span></h3><pre data-tool="mdnice编辑器" style="border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;text-align: left;margin-top: 10px;margin-bottom: 10px;margin-left: 0px;margin-right: 0px;padding-top: 0px;padding-bottom: 0px;padding-left: 0px;padding-right: 0px;"><span data-cacheurl="" data-remoteid="" style="display: block;background: none;height: 30px;width: 100%;background-size: 40px;background-repeat: no-repeat;background-color: #282c34;margin-bottom: -7px;border-radius: 5px;background-position: 10px 10px;background-image: url(&quot;https://mmbiz.qpic.cn/mmbiz_svg/vPjficwtgTJBB6Aric7UOo5QNfgHfrYniaLLibAEOsickh6Ust0STwSwr6zUEibgMFGj2eTS8e6wZ2jhUOdwKuSoUzz8kRpCwRL6hY/640?wx_fmt=svg&amp;from=appmsg&quot;);"></span><code style="overflow-x: auto;padding: 16px;color: #abb2bf;padding-top: 15px;background: #282c34;border-radius: 5px;display: -webkit-box;font-family: Consolas, Monaco, Menlo, monospace;font-size: 12px;"><span leaf="">gcc -o example example.c logging.c -lwinpthread</span><br /></code></pre></section><section style="text-align: center;" nodeleaf=""><img class="rich_pages wxw-img" data-imgfileid="100019745" data-s="300,640" data-type="png" type="block" style="height: auto !important;" data-ratio="0.6666666666666666" data-w="1080" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/hMWGJFibgaTtDDs39KQj2UFTNHefzvQf2cOCOGWlsmuCTc9CMZkeZicFscqKJaOE31wicMZF9JxXHzBwlLyob4hlg/640?wx_fmt=png&amp;from=appmsg" /></section><section style="text-align: center;"><span leaf="" style="color: rgb(250, 83, 89);">--完--</span></section><section><span leaf="">读到这里说明你喜欢本公众号的文章,欢迎&nbsp;<span style="color: rgb(250, 83, 89);">置顶(标星)</span>本公众号&nbsp;<span style="color: rgb(250, 83, 89);">C语言中文社区</span>,这样就可以<span style="color: rgb(250, 83, 89);">第一时间获取推送</span>了~</span></section><section><span leaf="">在本公众号,后台回复:<span style="color: rgb(250, 83, 89);">1024</span>&nbsp;,免费领取一份<span style="color: rgb(250, 83, 89);">C语言学习大礼包</span></span></section><section class="mp_profile_iframe_wrp" nodeleaf=""><mp-common-profile class="custom_select_card mp_profile_iframe mp_common_widget" data-pluginname="mpprofile" data-nickname="C语言中文社区" data-alias="cyyzwsq" data-from="0" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/hMWGJFibgaTtb77gBS2QiaX1HQvSFb14kSlxUibMKx6YY1cwxENASI8ib6aglE4OXCWFGYNPCZD9GMiayahXwpIMD1w/0?wx_fmt=png" data-signature="学习路线 | AI编程 | 代码实例 | 实战源码 | 开发工具 | 视频教程 | 面试题 | 电子书 | 专注于C语言编程技术分享" data-id="MzIyNjQ3NDk2Mg==" data-service_type="1" data-verify_status="1"></mp-common-profile><br /></section><section class="" nodeleaf=""><section style="text-align: right;"><span leaf="">觉得内容还不错的话,给我点个“<span style="color: rgb(250, 83, 89);">推荐</span>”呗</span></section></section><p style="display: none;"><mp-style-type data-value="3"></mp-style-type></p>

回首页,搜索你想要的文档

在首页搜索框输入关键词,快速定位资料

文库首页搜索指引 立即搜索

相关学习资料