{"id":54,"date":"2011-04-05T13:59:21","date_gmt":"2011-04-05T05:59:21","guid":{"rendered":"http:\/\/blog.vscz.net\/?p=54"},"modified":"2014-07-28T22:15:01","modified_gmt":"2014-07-28T14:15:01","slug":"%e5%88%9b%e5%bb%ba%e5%8d%95%e4%bb%b6%e6%a8%a1%e5%bc%8f%e7%9a%84logger%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/bylang.cn\/?p=54","title":{"rendered":"\u521b\u5efa\u5355\u4ef6\u6a21\u5f0f\u7684Logger\u7c7b"},"content":{"rendered":"<p>\u300aC++\u9ad8\u7ea7\u7f16\u7a0b\u300b\u7b2c\u4e8c\u5341\u516d\u7ae0\u7684\u4e00\u4e2a\u4f8b\u5b50\u3002<br \/>\n\/**<br \/>\n* Logger.h<br \/>\n*<br \/>\n* Definition of a singleton logger class, implemented with static methods<br \/>\n*\/<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;fstream&gt;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<br \/>\nclass Logger<br \/>\n{<br \/>\npublic:<br \/>\nstatic const std::string kLogLevelDebug;<br \/>\nstatic const std::string kLogLevelInfo;<br \/>\nstatic const std::string kLogLevelError;<br \/>\n\/\/ Return a reference to the singleton logger object<br \/>\nstatic Logger&amp; instance();<br \/>\n\/\/ Log a single message at the given log level<br \/>\nvoid log(const std::string&amp; inMessage, const std::string&amp; inLogLevel);<br \/>\n\/\/ Log a vector of messages at the given log level<br \/>\nvoid log(const std::vector&lt;std::string&gt;&amp; inMessage, const std::string&amp; inLogLevel);<br \/>\nprotected:<br \/>\n\/\/ static variable for the one-and-only instance<br \/>\nstatic Logger sInstance;<br \/>\n\/\/ Constant for the filename<br \/>\nstatic const char* const kLogFileName;<br \/>\n\/\/ Data member for the output stream<br \/>\nstd::ofstream mOutputStream;<br \/>\nprivate:<br \/>\n\/\/ \u5173\u952e\uff1a\u79c1\u6709\u7684\u6784\u9020\u51fd\u6570\uff0c\u4e0d\u80fd\u5b9e\u4f8b\u5316\uff0c\u56e0\u4e3a\u5b9e\u4f8b\u5316\u65f6\u7c7b\u5916\u90e8\u65e0\u6cd5\u8bbf\u95ee\u5176\u5185\u90e8\u7684\u79c1\u6709\u7684\u6784\u9020\u51fd\u6570<br \/>\nLogger(void);<br \/>\n~Logger(void);<br \/>\n};<\/p>\n<p>\/**<br \/>\n* Logger.cpp<br \/>\n*<br \/>\n* Implementation of a singleton logger class<br \/>\n*\/<br \/>\n#include &#8220;Logger.h&#8221;<br \/>\n#include &lt;string&gt;<br \/>\nusing namespace std;<br \/>\nconst string Logger::kLogLevelDebug = &#8220;DEBUG&#8221;;<br \/>\nconst string Logger::kLogLevelInfo = &#8220;INFO&#8221;;<br \/>\nconst string Logger::kLogLevelError = &#8220;Error&#8221;;<br \/>\nconst char* const Logger::kLogFileName = &#8220;log.out&#8221;;<br \/>\n\/\/ The static instance will be construct when the program starts and destructed when it ends.<br \/>\nLogger Logger::sInstance;<br \/>\nLogger::Logger(void)<br \/>\n{<br \/>\nmOutputStream.open(kLogFileName, ios_base::app);<br \/>\nif (!(mOutputStream.good())) {<br \/>\ncerr &lt;&lt; &#8220;Unable to initialize the Logger!&#8221; &lt;&lt; endl;<br \/>\n}<br \/>\n}<br \/>\nLogger::~Logger(void)<br \/>\n{<br \/>\nmOutputStream.close();<br \/>\n}<br \/>\nLogger&amp; Logger::instance()<br \/>\n{<br \/>\nreturn sInstance;<br \/>\n}<br \/>\nvoid Logger::log(const std::string&amp; inMessage, const std::string&amp; inLogLevel)<br \/>\n{<br \/>\nmOutputStream &lt;&lt; inLogLevel &lt;&lt; &#8220;:&#8221; &lt;&lt; inMessage &lt;&lt; endl;<br \/>\n}<br \/>\nvoid Logger::log(const std::vector&lt;std::string&gt;&amp; inMessage, const std::string&amp; inLogLevel)<br \/>\n{<br \/>\nfor (size_t i=0; i&lt;inMessage.size(); i++) {<br \/>\nlog(inMessage[i], inLogLevel);<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ TestTrueSingletonLogger.cpp<br \/>\n#include &#8220;Logger.h&#8221;<br \/>\n#include &lt;vector&gt;<br \/>\n#include &lt;string&gt;<br \/>\nint main(int argc, char **argv)<br \/>\n{<br \/>\nLogger::instance().log(&#8220;test message&#8221;, Logger::kLogLevelDebug);<br \/>\nvector&lt;string&gt; items;<br \/>\nitems.push_back(&#8220;item1&#8221;);<br \/>\nitems.push_back(&#8220;item2&#8221;);<br \/>\nLogger::instance().log(items, Logger::kLogLevelError);<br \/>\nreturn 0;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300aC++\u9ad8\u7ea7\u7f16\u7a0b\u300b\u7b2c\u4e8c\u5341\u516d\u7ae0\u7684\u4e00\u4e2a\u4f8b\u5b50\u3002 \/** * Logger.h * * Definition of a singleton logger class, implemented with static methods *\/ #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;vector&gt; #include &lt;string&gt; class Logger { public: static const std::string kLogLevelDebug; static const std::string kLogLevelInfo; static const std::string kLogLevelError; \/\/ Return a reference to the singleton logger object static Logger&amp; instance(); \/\/ Log [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bylang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":3,"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":57,"href":"https:\/\/bylang.cn\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/57"}],"wp:attachment":[{"href":"https:\/\/bylang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bylang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bylang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}