[
  {
    "path": "LICENSE",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"{}\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright {yyyy} {name of copyright owner}\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "## C-LOG\r\n[![Release Version](https://img.shields.io/badge/release-1.0.0-red.svg)](https://github.com/0xmalloc/c-log/releases) \r\n\r\n## What is c-log\r\na fast ,stable and thread-safe log lib(logger) for C/C++ language\r\n\r\n## How to Use \r\nyou can use  \"sh build.sh\" to build the testing application and also i suggest you to `run \"./bench_test\" (single_thread)to see the performance`\r\n\r\nyou can use this log module by the follow  guiding:\r\n```cpp\r\n    log_init(LL_TRACE, \"mysql\", \"./log/\");\r\n\tLOG_NOTICE(\"%s [time:%d]\", \"test calling log\", time(NULL));\r\n\tLOG_DEBUG(\"debug msg,only write to log when the loglevel bigger than or equal to debug [time:%d]\", time(NULL));\r\n\tLOG_WARN(\"warnning msg will be writing to the error files [time:%d]\", time(NULL));\r\n\tLOG_ERROR(\"you also can change  number of output files by rewrite the macro_define.h\");\r\n```\r\nand also you don't need to call close befor your application exit\r\nif you insist to calling close,please use WARN_W.log_close() INFO_W.log_close() and so so.\r\n\r\nthis module also provide some convenient macro, you can use it in the following scene\r\n```cpp\r\n    int function()\r\n\t{\r\n\t\tint ret;\r\n\t\tif()\r\n\t\t{\r\n\t\t\tret = function_call();\r\n\t\t\tMACRO_RET(ret != 0, -1);\r\n\t\t\tMACRO_WARN(ret != 0, \"function call failed  [ret:%d]\", ret);\r\n\t\t\tMACRO_WARN_RET(ret != 0, -1, \"function call failed  [ret:%d]\", ret)\r\n\t\t\t\r\n\t\t}\r\n\t}\r\n```\r\nThe performance of this log module is really depend on how fast your disk is.\r\nhere is test result:\r\n\r\n    case 1\r\n        Record Size: 280kb\r\n    \tCost Time: 42 seconds\r\n    \tData File Size: 20GB\r\n    \tyes. we write 20GB file in 102s,that's means we write 200MB per second.and\r\n    \talso this is the limitation of SATA.\r\n\r\n    case 2 \r\n        Record Size: 56byte\r\n    \tRecord Write per Senconds:200,000\r\n    \tyes,in it's general useage,you can gain high qps from this log module\r\n\r\nwhat will be the next?\r\n\r\n\r\n## About Author\r\n\r\n```javascript\r\n  var 0xmalloc = {\r\n    nickName  : \"zealot yin\",\r\n    email : '0xmalloc@gmail.com',\r\n    site : \"http://www.cnblogs.com/Creator/\"\r\n    weibo : \"http://www.weibo.com/zealotyin\"\r\n  }\r\n```\r\n"
  },
  {
    "path": "src/log.c",
    "content": "#include \"log.h\"\n#include <sys/file.h>\n#include <stdarg.h>\n#include <time.h>\n#include <stdlib.h>\n#include <string.h>\n#include <errno.h>\n#include <unistd.h>\n#include <sys/stat.h>\n\nLog_Writer WARN_W;\nLog_Writer INFO_W;\n__thread char Log_Writer::m_buffer[_LOG_BUFFSIZE];\n\nbool log_init(LogLevel l, const char* p_modulename, const char* p_logdir)\n{\n\t//如果路径存在文件夹，则判断是否存在\n\tif (access (p_logdir, 0) == -1)\n\t{\n\t\tif (mkdir (p_logdir, S_IREAD | S_IWRITE ) < 0)\n\t\t\tfprintf(stderr, \"create folder failed\\n\");\n\t}\n\tchar _location_str[_LOG_PATH_LEN];\n\tsnprintf(_location_str, _LOG_PATH_LEN, \"%s/%s.access\", p_logdir, p_modulename);\t\n\tINFO_W.loginit(l, _location_str);\n\tsnprintf(_location_str, _LOG_PATH_LEN, \"%s/%s.error\", p_logdir, p_modulename);\n\t//warning级别以上日志去WARN_W  去向由宏决定的 请见macro_define.h\n\tif(l > LL_WARNING)\n\t\tWARN_W.loginit(l, _location_str);\n\telse\n\t\tWARN_W.loginit(LL_WARNING, _location_str);\n\treturn true;\n}\n\nconst char* Log_Writer::logLevelToString(LogLevel l) {\n        switch ( l ) {\n\t\t\tcase LL_DEBUG:\n\t\t\t\treturn \"DEBUG\";\n\t\t\tcase LL_TRACE:\n\t\t\t\treturn \"TRACE\";\n\t\t\tcase LL_NOTICE:\n\t\t\t\treturn \"NOTICE\";\n\t\t\tcase LL_WARNING:\n\t\t\t\treturn \"WARN\" ;\n\t\t\tcase LL_ERROR:\n\t\t\t\treturn \"ERROR\";\n\t\t\tdefault:\n\t\t\t\treturn \"UNKNOWN\";\n        }\n}\n\t\nbool Log_Writer::checklevel(LogLevel l)\n{\n\tif(l >= m_system_level)\n\t\treturn true;\n\telse\n\t\treturn false;\n}\n\nbool Log_Writer::loginit(LogLevel l, const  char *filelocation, bool append, bool issync)\n{\n\tMACRO_RET(NULL != fp, false);\n    m_system_level = l;\n    m_isappend = append; \n    m_issync = issync; \n\tif(strlen(filelocation) >= (sizeof(m_filelocation) -1))\n\t{\n\t\tfprintf(stderr, \"the path of log file is too long:%d limit:%d\\n\", strlen(filelocation), sizeof(m_filelocation) -1);\n\t\texit(0);\n\t}\n\t//本地存储filelocation  以防止在栈上的非法调用调用\n\tstrncpy(m_filelocation, filelocation, sizeof(m_filelocation));\n\tm_filelocation[sizeof(m_filelocation) -1] = '\\0';\n\t\n\tif('\\0' == m_filelocation[0])\n\t{\n\t\tfp = stdout;\n\t\tfprintf(stderr, \"now all the running-information are going to put to stderr\\n\");\n\t\treturn true;\n\t}\n\t\n\tfp = fopen(m_filelocation, append ? \"a\":\"w\");\n\tif(fp == NULL)\n\t{\n\t\tfprintf(stderr, \"cannot open log file,file location is %s\\n\", m_filelocation);\n\t\texit(0);\n\t}\n\t//setvbuf (fp, io_cached_buf, _IOLBF, sizeof(io_cached_buf)); //buf set _IONBF  _IOLBF  _IOFBF\n\tsetvbuf (fp,  (char *)NULL, _IOLBF, 0);\n\tfprintf(stderr, \"now all the running-information are going to the file %s\\n\", m_filelocation);\n\treturn true;\n}\n\nint Log_Writer::premakestr(char* m_buffer, LogLevel l)\n{\n    time_t now;\n\tnow = time(&now);;\n\tstruct tm vtm; \n    localtime_r(&now, &vtm);\n    return snprintf(m_buffer, _LOG_BUFFSIZE, \"%s: %02d-%02d %02d:%02d:%02d \", logLevelToString(l),\n            vtm.tm_mon + 1, vtm.tm_mday, vtm.tm_hour, vtm.tm_min, vtm.tm_sec);\n}\n\nbool Log_Writer::log(LogLevel l, char* logformat,...)\n{\n\tMACRO_RET(!checklevel(l), false);\n\tint _size;\n\tint prestrlen = 0;\n\t\n\tchar * star = m_buffer;\n\tprestrlen = premakestr(star, l);\n\tstar += prestrlen;\n\t\n\tva_list args;\n\tva_start(args, logformat);\n\t_size = vsnprintf(star, _LOG_BUFFSIZE - prestrlen, logformat, args);\n\tva_end(args);\n\t\n\tif(NULL == fp)\n\t\tfprintf(stderr, \"%s\", m_buffer);\n\telse\n\t\t_write(m_buffer, prestrlen + _size);\n\treturn true;\n}\n\nbool Log_Writer::_write(char *_pbuffer, int len)\n{\n\tif(0 != access(m_filelocation, W_OK))\n\t{\t\n\t\tpthread_mutex_lock(&m_mutex);\n\t\t//锁内校验 access 看是否在等待锁过程中被其他线程loginit了  避免多线程多次close 和init\n\t\tif(0 != access(m_filelocation, W_OK))\n\t\t{\n\t\t\tlogclose();\n\t\t\tloginit(m_system_level, m_filelocation, m_isappend, m_issync);\n\t\t}\n\t\tpthread_mutex_unlock(&m_mutex);\n\t}\n\n\tif(1 == fwrite(_pbuffer, len, 1, fp)) //only write 1 item\n\t{\n\t\tif(m_issync)\n          \tfflush(fp);\n\t\t*_pbuffer='\\0';\n    }\n    else \n\t{\n        int x = errno;\n\t    fprintf(stderr, \"Failed to write to logfile. errno:%s    message:%s\", strerror(x), _pbuffer);\n\t    return false;\n\t}\n\treturn true;\n}\n\nLogLevel Log_Writer::get_level()\n{\n\treturn m_system_level; \n}\n\nbool Log_Writer::logclose()\n{\n\tif(fp == NULL)\n\t\treturn false;\n\tfflush(fp);\n\tfclose(fp);\n\tfp = NULL;\n\treturn true;\n}\n\n\n\n"
  },
  {
    "path": "src/log.h",
    "content": "#ifndef   _MACRO_LogModule\n#define   _MACRO_LogModule\n#include <stdio.h>\n#include <pthread.h>\n#include \"macro_define.h\"\n\n/* ÿ̵߳buffer size*/\n#define   _LOG_BUFFSIZE  1024*1024*4\n/* ǰ̵ Stream IO buffer size*/\n#define   _SYS_BUFFSIZE  1024*1024*8\n/* log ļַ·󳤶*/\n#define\t  _LOG_PATH_LEN  250\n/* ־Ӧģ*/\n#define   _LOG_MODULE_LEN 32\n\ntypedef  enum LogLevel {  \n\tLL_DEBUG = 1,\n\tLL_TRACE = 2,\n\tLL_NOTICE = 3, \n\tLL_WARNING = 4, \n\tLL_ERROR = 5,\n}LogLevel;\n\n/**\n*\tLog_Writer  ־\n*/\nclass Log_Writer\n{\n\tpublic:\n\t\tLog_Writer()\n\t\t{\n\t\t\tm_system_level = LL_NOTICE;\n\t\t\t//fp = stderr;\n\t\t\tfp = NULL;\n\t\t\tm_issync = false;\n\t\t\tm_isappend = true;\n\t\t\tm_filelocation[0] ='\\0'; \n\t\t\tpthread_mutex_init(&m_mutex, NULL);\n\t\t}\n\t\t~Log_Writer(){\n\t\t\tlogclose();\n\t\t}\n\t\tbool loginit(LogLevel l, const  char *filelocation, bool append = true, bool issync = false);\n\t\tbool log(LogLevel l,char *logformat,...);\n\t\tLogLevel get_level();\n\t\tbool logclose();\n\tprivate:\n\t\tconst char* logLevelToString(LogLevel l);\n\t\tbool checklevel(LogLevel l);\n\t\tint premakestr(char* m_buffer, LogLevel l);\n\t\tbool _write(char *_pbuffer, int len);\n\tprivate:\n\t\tenum LogLevel m_system_level;\n\t\tFILE* fp;\n\t\tbool m_issync;\n\t\tbool m_isappend;\n\t\tchar m_filelocation[_LOG_PATH_LEN];\n\t\tpthread_mutex_t m_mutex;\n\t\tstatic __thread char m_buffer[_LOG_BUFFSIZE];\n\t\t//The __thread specifier may be applied to any global, file-scoped static, function-scoped static, \n\t\t//or static data member of a class. It may not be applied to block-scoped automatic or non-static data member\n\t\t//in the log  scence,It's safe!!!!\n\t\t//һԱ֮˳õ__threadԴleak,ͬʱҲõĶLog_Writerţ\n\t\t//Ϊһ߳ͬһʱֻһLog_Writerڸɻ֮m_bufferreset\n\t\t//Լһ߳ûLog_Write(Ϊһ߳ڵֻ̬д) Ҳ̰߳ȫģ\n};\n\nextern Log_Writer WARN_W;\nextern Log_Writer INFO_W;\n\n/**\n * LogLevel ־\n * p_modulename ģ mysql\n * p_logdir  ־Ŀ¼\n * */\nbool log_init(LogLevel l, const char* p_modulename, const char* p_logdir);\n#endif\n"
  },
  {
    "path": "src/macro_define.h",
    "content": "#ifndef  _MACRO_DEFINE\n#define  _MACRO_DEFINE\n//============basic===================\n\n#define LOG_ERROR(log_fmt, log_arg...) \\\n    do{ \\\n        WARN_W.log(LL_ERROR,   \"[%s:%d][%s] \" log_fmt \"\\n\", \\\n                     __FILE__, __LINE__, __FUNCTION__, ##log_arg); \\\n    } while (0) \n\n#define LOG_WARN(log_fmt, log_arg...) \\\n    do{ \\\n        WARN_W.log(LL_WARNING,   \"[%s:%d][%s] \" log_fmt \"\\n\", \\\n                     __FILE__, __LINE__, __FUNCTION__, ##log_arg); \\\n    } while (0) \n\n#define LOG_NOTICE(log_fmt, log_arg...) \\\n    do{ \\\n        INFO_W.log(LL_NOTICE,   \"[%s:%d][%s] \" log_fmt \"\\n\", \\\n                     __FILE__, __LINE__, __FUNCTION__, ##log_arg); \\\n    } while (0) \n\n#define LOG_TRACE(log_fmt, log_arg...) \\\n    do{ \\\n        INFO_W.log(LL_TRACE,   \"[%s:%d][%s] \" log_fmt \"\\n\", \\\n                     __FILE__, __LINE__, __FUNCTION__, ##log_arg); \\\n    } while (0) \n\n#define LOG_DEBUG(log_fmt, log_arg...) \\\n    do{ \\\n        INFO_W.log(LL_DEBUG,   \"[%s:%d][%s] \" log_fmt \"\\n\", \\\n                     __FILE__, __LINE__, __FUNCTION__, ##log_arg); \\\n    } while (0) \n\n//============extend===================\n#define MACRO_RET(condition, return_val) {\\\n    if (condition) {\\\n        return return_val;\\\n    }\\\n}\n\n#define MACRO_WARN(condition, log_fmt, log_arg...) {\\\n    if (condition) {\\\n        LOG_WARN( log_fmt, ##log_arg);\\\n    }\\\n}\n\n#define MACRO_WARN_RET(condition, return_val, log_fmt, log_arg...) {\\\n    if ((condition)) {\\\n        LOG_WARN( log_fmt, ##log_arg);\\\n\t\treturn return_val;\\\n    }\\\n}\n#endif\n"
  },
  {
    "path": "test/bench_test.c",
    "content": "#include \"log.h\"\n#include <string.h>\n#include <stdio.h>\n#include <signal.h>\n#include <unistd.h>\n\n/*\n * 单线程压力测试\n * */\nint common_test()\n{\n\tLOG_DEBUG(\"testing write log befor log_init: %s\", \"haha\");\n}\n\nvoid test01()\n{\n    //init  log\n\t//log_init(LL_TRACE, \"mysql\", \"./log\");\n\tlog_init(LL_TRACE, \"bench\", \"./\");\n    \n\tint i;\n\tchar _strbuf[1024*1024*4];\n\n\tchar *_log_str = \"You got something wrong here\";\t\n\tfor(i = 0; i < 10000; i++)\n\t{\n\t\tstrncat(_strbuf, _log_str, strlen(_log_str));\n\t}\n\tprintf(\"Log size of each log:%d\", strlen(_strbuf));\t\n\tfor(i = 0; i < 1000000000; i++)\n\t{\n\t\tLOG_NOTICE(\"%s [value:%d]\", _strbuf, i);\n    }\n}\n\nint main()\n{\n    test01();\n}\n"
  },
  {
    "path": "test/build.sh",
    "content": "#!/bin/bash\nSRCPATH=\"../src/\"\nrm -f test_log test_pthread_log ultimate_test\ng++ -o test_log  \"$SRCPATH\"log.c    test.c  -lpthread  -I \"$SRCPATH\"\ng++ -o bench_test  \"$SRCPATH\"log.c    bench_test.c  -lpthread  -I \"$SRCPATH\"\ng++ -o test_pthread_log  \"$SRCPATH\"log.c    thread_test.c -lpthread -I \"$SRCPATH\"\ng++ -o ultimate_test  \"$SRCPATH\"log.c    ultimate_test.c  -I \"$SRCPATH\"\n\nif ls *.access >/dev/null 2>/dev/null  \nthen\n\trm *.access\nfi\nif ls *.error >/dev/null 2>/dev/null  \nthen \n\trm *.error\nfi\n"
  },
  {
    "path": "test/test.c",
    "content": "#include \"log.h\"\n#include <string.h>\n#include <stdio.h>\n#include <signal.h>\n#include <unistd.h>\n/*\n * 基本功能测试\n */\nint common_test()\n{\n\tLOG_DEBUG(\"testing write log befor log_init: %s\", \"haha\");\n}\n\nvoid test01()\n{\n\t//测试init 之前调用log_wwarn的结果\n\tcommon_test();\n    //init  log\n\t//log_init(LL_TRACE, \"mysql\", \"./log\");\n\tlog_init(LL_DEBUG, \"mysql\", \"./\");\n    \n\tint i;\n\tchar _strbuf[200*1024];\n\n\tchar *_log_str = \"You got something wrong here\";\t\n\tfor(i = 0; i < 1; i++)\n\t{\n\t\tstrncat(_strbuf, _log_str, strlen(_log_str));\n\t}\n\tcommon_test(); \n\t\n\tfor(i = 0; i < 10; i++)\n\t{\n\t\t//sleep(1);\n\t\tif(0 == (i% 5))\n\t\t\tLOG_NOTICE(\"%s [value:%d] [xx:%d]\", _strbuf, i, 10);\n\t\telse if(1 == i % 5)\n\t\t\tLOG_DEBUG(\"%s [value:%d]\", _strbuf, i); \n\t\telse if (2 == i % 5)\n\t\t\tLOG_TRACE(\"%s [value:%d]\", _strbuf, i);\n\t\telse if (3 == i % 5)\n\t\t\tLOG_WARN(\"%s [value:%d]\", _strbuf, i);\n\t\telse \n\t\t\tLOG_ERROR(\"%s [value:%d]\", _strbuf, i);\n    }\n}\n\nint main()\n{\n    test01();\n\t\t\n}\n"
  },
  {
    "path": "test/thread_test.c",
    "content": "#include \"log.h\"\n#include <string.h>\n#include <stdio.h>\n#include <signal.h>\n#include <unistd.h>\n#include <pthread.h>\n/*\n * 测试是否多线程安全\n * */\nbool quit = false;\nvoid signal_handler(int sig){\n\tswitch(sig){\n\t\tcase SIGTERM:\n\t\tcase SIGINT:\n\t\t\tquit = true;\n\t\t\tbreak;\n\t}\n\tprintf(\"bingo sig\\n\");\n}\n\nvoid* test01(void *)\n{\n    int i;\n\tchar _strbuf[200*1024];\n\n\tchar *_log_str = \"You got something wrong here\";\t\n\tfor(i = 0; i < 1; i++)\n\t{\n\t\tstrncat(_strbuf, _log_str, strlen(_log_str));\n\t}\n\n    for(i=0;i<1000000000;i++)\n\t{\n\t\t//测试日志是否立即写入到文件\n\t\tsleep(1);\n\t\tif(0 == (i% 5)) \n            LOG_NOTICE(\"%s [value:%d]\", _strbuf, i);\n        else if(1 == i % 5)\n            LOG_DEBUG(\"%s [value:%d]\", _strbuf, i); \n        else if (2 == i % 5)\n            LOG_TRACE(\"%s [value:%d]\", _strbuf, i);\n        else if (3 == i % 5)\n            LOG_WARN(\"%s [value:%d]\", _strbuf, i);\n        else    \n            LOG_ERROR(\"%s [value:%d]\", _strbuf, i);\t\n\t\tif(quit)\n\t\t\tbreak;\n    }\n}\n\nint main()\n{\n    signal(SIGPIPE, SIG_IGN);\n    signal(SIGINT, signal_handler);\n    signal(SIGTERM, signal_handler);\n\tlog_init(LL_TRACE, \"session\", \"./\");\n\t\n\tpthread_t pid[20];\n\tint i = 0;\n\tint ret = 0;\n\tfor(; i < 20; i++)\n\t{\n\t\tret = pthread_create(&pid[i], NULL, test01, NULL);\n\t\tMACRO_WARN_RET(0 != ret, -1, \"pthread_create error[ret:%d]\", ret)\n\t}\n\tfor(i = 0; i < 20; i++)\n\t{\n\t\tpthread_join(pid[i], NULL);\n\t}\n\t\t\n}\n"
  },
  {
    "path": "test/ultimate_test.c",
    "content": "#include \"log.h\"\n#include <unistd.h>\n#include <stdlib.h>\n#include <signal.h>\n\nvoid sig_alarm (int signo)\n{\n\texit (0);\n}\nint main()\n{\n\talarm (10);\n\tif (signal (SIGALRM, sig_alarm) == SIG_ERR)\n\t{\n\t\tprintf(\"%s\\n\", \"signal error\");\n\t}\n\tlog_init (LL_DEBUG, \"ultimate_test\", \"./\");\n\twhile (1)\n\t{\n\t\tLOG_TRACE(\"This is a test message!!!\");\n\t}\n\treturn 0;\n}\n"
  },
  {
    "path": "开源c-log日志库",
    "content": "开源的c-log日志库，支持c/c++\n"
  }
]