{"id":40,"date":"2024-03-17T16:40:43","date_gmt":"2024-03-17T08:40:43","guid":{"rendered":"http:\/\/175.178.247.106\/?p=40"},"modified":"2025-03-03T16:45:25","modified_gmt":"2025-03-03T08:45:25","slug":"%e7%bc%96%e5%86%99%e8%83%bd%e6%8f%90%e5%8d%87%e8%a6%86%e7%9b%96%e7%8e%87%e7%9a%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/175.178.247.106\/index.php\/2024\/03\/17\/%e7%bc%96%e5%86%99%e8%83%bd%e6%8f%90%e5%8d%87%e8%a6%86%e7%9b%96%e7%8e%87%e7%9a%84%e4%bb%a3%e7%a0%81\/","title":{"rendered":"\u7f16\u5199\u80fd\u63d0\u5347\u8986\u76d6\u7387\u7684\u4ee3\u7801"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"ue240c0a9\">\u4ee3\u7801\u7684\u8986\u76d6\u7387\u4e0d\u662f\u6d4b\u8bd5\u51fa\u6765\u7684\u5417\uff1f\u4e3a\u4ec0\u4e48\u8fd8\u80fd\u901a\u8fc7\u4ee3\u7801\u672c\u8eab\u63d0\u9ad8\u4ee3\u7801\u7684\u8986\u76d6\u7387\uff1f<\/p>\n\n\n\n<p id=\"u0ea306c1\">\u4e8b\u60c5\u662f\u8fd9\u6837\uff0c\u6700\u8fd1\u4ea7\u54c1\u8981\u53d1\u5e03\uff0c\u8981\u5bf9\u4ee3\u7801\u8fdb\u884c\u8986\u76d6\u7387\u6d4b\u8bd5\uff0c\u5c31\u662f\u8fd0\u884c\u5b9e\u9645\u7684\u4e1a\u52a1\uff0c\u770b\u4ee3\u7801\u6700\u7ec8\u7684\u8986\u76d6\u60c5\u51b5\u3002\u7528\u5230\u7684\u5de5\u5177\u662f gcov\u3002<\/p>\n\n\n\n<p id=\"udefccc8c\">\u8fd9\u6837\u7684\u8bdd\uff0c\u8981\u4ee3\u7801\u8986\u76d6\u7387\u9ad8\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u8981\u5c3d\u91cf\u8986\u76d6\u5230\u6240\u6709\u7684\u60c5\u51b5\uff0c\u6b63\u5e38\u4e1a\u52a1\uff0c\u4ee5\u53ca\u4ee3\u7801\u4e2d\u7684\u4e00\u573a\u5206\u652f\u3002\u6240\u4ee5\u4e3a\u4e86\u63d0\u5347\u8986\u76d6\u7387\uff0c\u90a3\u4e48\u81ea\u7136\u7684\u662f\uff0c\u4e00\u4e2a\u4e1a\u52a1\u64cd\u4f5c\u80fd\u5c3d\u91cf\u9ad8\u7684\u8986\u76d6\u7387\uff0c\u907f\u514d\u6784\u5efa\u5404\u79cd\u590d\u6742\u7684\u60c5\u51b5\u8fdb\u884c\u8986\u76d6\uff0c\u8fd9\u6837\u5de5\u4f5c\u6548\u7387\u5c31\u4e0a\u4e0d\u53bb\u4e86\u3002<\/p>\n\n\n\n<p id=\"u247a8561\">\u6211\u4eec\u6765\u770b\u6bb5\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre id=\"AiDLF\" class=\"wp-block-code\"><code>void HttpRequestError(enum evhttp_request_error error, void *arg)\n{\n    switch (error) {\n        case EVREQ_HTTP_TIMEOUT:\n            printf(\"HTTP requests timed out\\n\");\n            break;\n        case EVREQ_HTTP_EOF:\n            printf(\"Premature end of HTTP response\\n\");\n            break;\n        case EVREQ_HTTP_INVALID_HEADER:\n            printf(\"Invalid HTTP header encountered\\n\");\n            break;\n        case EVREQ_HTTP_BUFFER_ERROR:\n            printf(\"HTTP buffer related error\\n\");\n            break;\n        default:\n            printf(\"HTTP requests failed with error code %d \\n\", error);\n            break;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p id=\"u84b52ead\">\u8fd9\u662f\u4f7f\u7528 libevent \u5e93\u65f6\uff0c\u8bbe\u7f6e\u7684\u4e00\u4e2a\u9519\u8bef\u56de\u8c03\u51fd\u6570\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9519\u8bef\u7801\u6253\u5370\u4e0d\u540c\u7684\u6d88\u606f\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u8981\u8986\u76d6\u6240\u6709\u7684\u5206\u652f\u662f\u6bd4\u8f83\u96be\u7684\uff0c\u56e0\u4e3a\u8981\u6a21\u62df\u5404\u79cd\u5f02\u5e38\u7684\u60c5\u51b5\u3002\u4e00\u822c\u6a21\u62df\u4e2a\u8d85\u65f6\u662f\u6bd4\u8f83\u5bb9\u6613\u7684\u3002<\/p>\n\n\n\n<p id=\"u2547f920\">\u53bb\u6389 libevent \u5f15\u7528\u90e8\u5206\uff0c\u6211\u4eec\u6539\u4e0b\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre id=\"SKhii\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\n#define EVREQ_HTTP_TIMEOUT        0\n#define EVREQ_HTTP_EOF            1\n#define EVREQ_HTTP_INVALID_HEADER 2\n#define EVREQ_HTTP_BUFFER_ERROR   3\n\nvoid HttpRequestError(int error)\n{\n    switch (error) {\n        case EVREQ_HTTP_TIMEOUT:\n            printf(\"HTTP requests timed out\\n\");\n            break;\n        case EVREQ_HTTP_EOF:\n            printf(\"Premature end of HTTP response\\n\");\n            break;\n        case EVREQ_HTTP_INVALID_HEADER:\n            printf(\"Invalid HTTP header encountered\\n\");\n            break;\n        case EVREQ_HTTP_BUFFER_ERROR:\n            printf(\"HTTP buffer related error\\n\");\n            break;\n        default:\n            printf(\"HTTP requests failed with error code %d \\n\", error);\n            break;\n    }\n}\n\nint main()\n{\n        HttpRequestError(EVREQ_HTTP_TIMEOUT);\n        return 0;\n}\n<\/code><\/pre>\n\n\n\n<p id=\"u353ea233\">makefile \u6587\u4ef6\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre id=\"HPiDx\" class=\"wp-block-code\"><code>CFLAGS+= -fprofile-arcs -ftest-coverage -fbranch-probabilities\nCC=g++\n\nall: helloworld\n\nhelloworld: *.cpp\n        @${CC} ${CFLAGS} -o helloworld_gcov converage.cpp\n\n\nconverage:\n        echo \"result.\"\n        gcov -c -d . -o helloworld_gcov.info\n        lcov -c -d . -o helloworld_gcov.info --rc lcov_branch_coverage=1\n        genhtml -o result --branch-coverage  helloworld_gcov.info\n\nclean:\n        rm -rf *.gcno *.gcda\n<\/code><\/pre>\n\n\n\n<p id=\"uaa448faf\">\u5148 make \u7f16\u8bd1\u5f97\u5230 \u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7136\u540e\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u4f1a\u5f97\u5230 gcno \u4ee5\u53ca gcda \u6587\u4ef6\uff0c\u6700\u540e make converage\uff0c \u4f1a\u5f97\u5230 result \u76ee\u5f55\uff0c\u6253\u5f00\u91cc\u9762\u7684 index.html\uff0c\u53ef\u4ee5\u770b\u5230\u8986\u76d6\u7387\u7684\u8be6\u7ec6\u4fe1\u606f\uff1a<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"394\" src=\"http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-1024x394.png\" alt=\"\" class=\"wp-image-45\" srcset=\"http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-1024x394.png 1024w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-300x115.png 300w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-768x295.png 768w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-1536x591.png 1536w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3-604x232.png 604w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-3.png 1922w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p id=\"uaa448faf\"><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><\/td><td><\/td><td><\/td><td>Lines:<\/td><td>8<\/td><td>16<\/td><td>50.0 %<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td>Functions:<\/td><td>2<\/td><td>2<\/td><td>100.0 %<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td>Branches:<\/td><td>1<\/td><td>5<\/td><td>20.0 %<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p id=\"uaa448faf\"><\/p>\n\n\n\n<p id=\"uce5157b3\">\u548c\u9884\u671f\u7684\u4e00\u6837\uff0c\u53ea\u8986\u76d6\u5230\u4e86 EVREQ_HTTP_TIMEOUT \u5206\u652f\u3002<\/p>\n\n\n\n<p id=\"ua96cee41\">\u6211\u4eec\u7528 map \u91cd\u6784\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre id=\"VpAtX\" class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;map&gt;\n#include &lt;string&gt;\nusing namespace std;\n\n#define EVREQ_HTTP_TIMEOUT        0\n#define EVREQ_HTTP_EOF            1\n#define EVREQ_HTTP_INVALID_HEADER 2\n#define EVREQ_HTTP_BUFFER_ERROR   3\n#define EVREQ_DEFAULT             4\n\nstd::map&lt;int, string&gt; mm = {\n        {EVREQ_HTTP_TIMEOUT, \"HTTP requests timed out\\n\"},\n        {EVREQ_HTTP_EOF, \"Premature end of HTTP response\\n\"},\n        {EVREQ_HTTP_INVALID_HEADER, \"Invalid HTTP header encountered\\n\"},\n        {EVREQ_HTTP_BUFFER_ERROR, \"HTTP buffer related error\\n\"},\n        {EVREQ_DEFAULT, \"HTTP requests failed with error code %d \\n\"}\n};\n\n\nvoid HttpRequestError(int error)\n{\n   string out = mm&#91;EVREQ_DEFAULT];\n   if (mm.find(error)!= mm.end())\n   {\n        out = mm&#91;error];\n   }\n\n   printf(out.c_str(), error);\n}\n\n\nint main()\n{\n        HttpRequestError(EVREQ_HTTP_TIMEOUT);\n        return 0;\n}\n<\/code><\/pre>\n\n\n\n<p id=\"ub4bee424\">\u518d\u6b21\u770b\u4e0b\u8986\u76d6\u7387\uff1a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"433\" src=\"http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-1024x433.png\" alt=\"\" class=\"wp-image-43\" srcset=\"http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-1024x433.png 1024w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-300x127.png 300w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-768x325.png 768w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-1536x650.png 1536w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2-604x255.png 604w, http:\/\/175.178.247.106\/wp-content\/uploads\/2025\/03\/image-2.png 1906w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><\/td><td><\/td><td><\/td><td>Lines:<\/td><td>12<\/td><td>12<\/td><td>100.0 %<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td>Functions:<\/td><td>4<\/td><td>4<\/td><td>100.0 %<\/td><\/tr><tr><td><\/td><td><\/td><td><\/td><td>Branches:<\/td><td>17<\/td><td>34<\/td><td>50.0 %<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p id=\"u759ad5ed\">\u884c\u8986\u76d6\u7387\u548c\u5206\u652f\u8986\u76d6\u7387\u90fd\u6709\u6240\u63d0\u5347\u3002<\/p>\n\n\n\n<p id=\"uf27f9fe0\">\u56de\u987e\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u53ea\u8981\u8dd1\u4e00\u79cd\u5f02\u5e38\u60c5\u51b5\uff0c\u5c31\u8986\u76d6\u4e86\u6240\u6709\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p id=\"u59d5629e\">\u8fd9\u91cc\u7684\u505a\u6cd5\u5c31\u662f\u628a\u4e00\u6837\u7684\u903b\u8f91\uff0c\u901a\u8fc7 map \u8fdb\u884c\u5c01\u88c5\uff0c\u538b\u7f29\u6210\u4e86\u53ea\u6709\u4e00\u79cd\u60c5\u51b5\u4e86\u3002<\/p>\n\n\n\n<p id=\"ua23b5174\">\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u4ee3\u7801\u7684\u5199\u6cd5\u4e5f\u80fd\u7c7b\u4f3c\u7684\u63d0\u5347\u8986\u76d6\u7387\u5462\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u7684\u8986\u76d6\u7387\u4e0d\u662f\u6d4b\u8bd5\u51fa\u6765\u7684\u5417\uff1f\u4e3a\u4ec0\u4e48\u8fd8\u80fd\u901a\u8fc7\u4ee3\u7801\u672c\u8eab\u63d0\u9ad8\u4ee3\u7801\u7684\u8986\u76d6\u7387\uff1f \u4e8b\u60c5\u662f\u8fd9\u6837\uff0c\u6700\u8fd1\u4ea7\u54c1\u8981\u53d1\u5e03\uff0c\u8981\u5bf9\u4ee3\u7801\u8fdb\u884c\u8986\u76d6\u7387\u6d4b\u8bd5\uff0c\u5c31\u662f\u8fd0\u884c\u5b9e\u9645\u7684\u4e1a\u52a1\uff0c\u770b\u4ee3\u7801\u6700\u7ec8\u7684\u8986\u76d6&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/175.178.247.106\/index.php\/2024\/03\/17\/%e7%bc%96%e5%86%99%e8%83%bd%e6%8f%90%e5%8d%87%e8%a6%86%e7%9b%96%e7%8e%87%e7%9a%84%e4%bb%a3%e7%a0%81\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-14"],"_links":{"self":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":46,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions\/46"}],"wp:attachment":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}