{"id":217,"date":"2006-12-24T11:37:00","date_gmt":"2006-12-24T11:37:00","guid":{"rendered":"https:\/\/ahm.basfinans.com\/index.php\/2006\/12\/24\/the-simplest-logging-class-in-c\/"},"modified":"2006-12-24T11:37:00","modified_gmt":"2006-12-24T11:37:00","slug":"the-simplest-logging-class-in-c","status":"publish","type":"post","link":"https:\/\/ahm.basfinans.com\/index.php\/2006\/12\/24\/the-simplest-logging-class-in-c\/","title":{"rendered":"The Simplest Logging Class in C++"},"content":{"rendered":"<p><a href=\"http:\/\/2.bp.blogspot.com\/_MWBL1bjt7-k\/RY5mtWp0PhI\/AAAAAAAAAD8\/R-BO32OxKjU\/s1600-h\/gccegg-65.png\"><img decoding=\"async\" alt=\"\" border=\"0\" id=\"BLOGGER_PHOTO_ID_5012056364671057426\" src=\"http:\/\/2.bp.blogspot.com\/_MWBL1bjt7-k\/RY5mtWp0PhI\/AAAAAAAAAD8\/R-BO32OxKjU\/s320\/gccegg-65.png\" style=\"cursor: pointer; float: right; margin: 0pt 0pt 10px 10px;\" \/><\/a><\/p>\n<p>I want to create the simplest ever logging class in C++, so I created a single Log.h file and created a sample application that uses it.<br \/>\nThis was very useful in the small application I am working on.<\/p>\n<p><span><\/p>\n<p>It compile with gcc and it works as expected.<\/p>\n<p>known problems:<br \/>the Log.write() function may make problems in multithreaded applications, in such case you must put a critical section around it.<\/span><br \/>\n<span><br \/><\/span><br \/>\n<span>You are completely free to use the code as you wish. No limits.<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\/\/main.cpp<br \/>\/\/logging header file<br \/>#include &lt;stdio.h&gt;<br \/>#include &#8220;log.h&#8221;<\/p>\n<p>LOG_DECLARE;<\/p>\n<p>int main()<br \/>{<br \/>\/\/initialize file logger<br \/>LOG_INIT(&#8220;main.log&#8221;);<\/p>\n<p>LOG_MSG(&#8220;Application initialization&#8221;);<\/p>\n<p>printf(&#8220;This is the applicationn&#8221;);<\/p>\n<p>LOG_MSG(&#8220;Application end&#8221;);<\/p>\n<p>}<br \/>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<\/p>\n<p>Look at the Log.h file<\/p>\n<p>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\/\/log.h<\/p>\n<p>#ifndef  __LOG_H<br \/>#define __LOG_H<br \/>\/\/It is bad to include files within other include files, but this is an exception to simplify the code<br \/>#include &lt;stdio.h&gt;<\/p>\n<p>\/\/must be put in global scope and used once in all files<br \/>#define LOG_DECLARE               Log __log;<br \/>\/\/used in any file need to access the log files, except the file that has LOG_DECLARE statment<br \/>#define LOG_USE();                    extern Log __log;<br \/>\/\/init log files, called once in program initialization<br \/>#define LOG_INIT(fileName)        do{if(fileName!= NULL)__log.init(fileName); else printf(&#8220;error init log file %s&#8221;, fileName);}while(0);<\/p>\n<p>\/\/here are the different levels of logging<br \/>#define LOG_VERBOSE(msg)    __log.write(&#8220;verbose&#8221;, msg, __FILE__, __LINE__);  \/\/detaild info<br \/>#define LOG_MSG(msg)              __log.write(&#8220;msg&#8221;, msg, __FILE__, __LINE__);         \/\/brief info<br \/>#define LOG_WARN(msg)           __log.write(&#8220;warn&#8221;, msg, __FILE__, __LINE__);         \/\/warning<br \/>#define LOG_ERR(msg)               __log.write(&#8220;error&#8221;, msg, __FILE__, __LINE__);         \/\/error<br \/>#define LOG_FATAL(msg)           __log.write(&#8220;fatal&#8221;, msg, __FILE__, __LINE__);         \/\/fatal error<\/p>\n<p>class Log<br \/>{<br \/>FILE *fp;<br \/>bool logOk;<\/p>\n<p>public:<\/p>\n<p>Log()<br \/>   {fp = NULL;}<br \/>~Log() \/\/the destructor closes the file<br \/>   {close();}<\/p>\n<p>void init(char *pfileName)<br \/>   { if(pfileName != NULL) {fp = fopen(pfileName, &#8220;a+&#8221;); if(fp != NULL) fseek(fp, 0, SEEK_END);} }<\/p>\n<p>void close()<br \/>   {if(fp != NULL) fclose(fp);  fp = NULL;}<\/p>\n<p>\/\/FIXME: A critical section is required to protect the file writing function in multithreading programms<br \/>void write(char *pType, char *pMsg, char *pFileName, int lineNo)<br \/>   { if(fp != NULL) fprintf(fp, &#8220;%st%st%st%st%dt%sn&#8221;,  pType, __DATE__, __TIME__, pFileName, lineNo, pMsg);     }<br \/>};<\/p>\n<p>#endif<br \/><\/span><\/p>\n<div>From ahm507.blogspot.com<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I want to create the simplest ever logging class in C++, so I created a single Log.h file and created a sample application that uses it. This was very useful in the small application I am working on. It compile with gcc and it works as expected. known problems:the Log.write() function may make problems in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,24],"tags":[],"_links":{"self":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts\/217"}],"collection":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":0,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"wp:attachment":[{"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahm.basfinans.com\/index.php\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}