LogWriter.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "LogWriter.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #ifdef WIN32
  6. #include <direct.h>
  7. #include <io.h> //C (Windows) access
  8. #define R_OK 0
  9. #else
  10. #include <unistd.h> //C (Linux) access
  11. #endif
  12. #include "MoudleConfig.h"
  13. #if defined(WIN32)
  14. #include <WinSock2.h>
  15. #include <Windows.h>
  16. static DWORD WINAPI thread_Func(LPVOID pM)
  17. #else
  18. #include <sys/time.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. static void *thread_Func(void *pM)
  24. #endif
  25. {
  26. LogWriter *pointer = (LogWriter*)pM;
  27. pointer->run();
  28. return 0;
  29. }
  30. #define TMPBUFFERLEN (1024 * 1024 * 3)
  31. LogWriter::LogWriter()
  32. {
  33. mCondition = new Cond;
  34. mTmpBuffer = new char[TMPBUFFERLEN];
  35. #if defined(WIN32)
  36. HANDLE handle = CreateThread(NULL, 0, thread_Func, this, 0, NULL);
  37. #else
  38. pthread_t thread1;
  39. pthread_create(&thread1,NULL,thread_Func,this);
  40. #endif
  41. }
  42. LogWriter::~LogWriter()
  43. {
  44. if (mTmpBuffer != NULL)
  45. {
  46. delete mTmpBuffer;
  47. mTmpBuffer = NULL;
  48. }
  49. if (mCondition != NULL)
  50. {
  51. delete mCondition;
  52. mCondition = NULL;
  53. }
  54. }
  55. void LogWriter::addLogNode(const LogInfoNode &node)
  56. {
  57. mCondition->Lock();
  58. mLogNodeList.push_back(node);
  59. mCondition->Signal();
  60. mCondition->Unlock();
  61. }
  62. void LogWriter::writeLog(int cameraId, const std::string &str)
  63. {
  64. LogInfoNode node;
  65. node.cameraId = cameraId;
  66. node.mCreateTime = MoudleConfig::getTimeStamp_MilliSecond();
  67. #if defined(WIN32)
  68. SYSTEMTIME sys;
  69. GetLocalTime( &sys );
  70. memset(mTmpBuffer, 0x0, TMPBUFFERLEN);
  71. sprintf(mTmpBuffer,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
  72. sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, str.c_str());
  73. node.logStr = mTmpBuffer;
  74. #else
  75. struct timeval tv;
  76. struct timezone tz;
  77. struct tm *p;
  78. gettimeofday(&tv, &tz);
  79. p = localtime(&tv.tv_sec);
  80. memset(mTmpBuffer, 0x0, TMPBUFFERLEN);
  81. // memset(node.time,0x0,32);
  82. // sprintf(node.time,"%d-%02d-%02d %02d:%02d:%02d.%03d",1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
  83. sprintf(mTmpBuffer,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
  84. 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec, str.c_str());
  85. node.logStr = mTmpBuffer;
  86. #endif
  87. addLogNode(node);
  88. // if (cameraId == WRITE_LOG_ID_MAIN)
  89. // {
  90. // fprintf(stderr, "******:");
  91. // }
  92. // if (cameraId == WRITE_LOG_ID_MAIN)
  93. {
  94. #if defined(WIN32)
  95. fprintf(stderr,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s\n",
  96. sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, str.c_str());
  97. #else
  98. fprintf(stderr,"[%d-%02d-%02d %02d:%02d:%02d.%03d] %s",
  99. 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec, str.c_str());
  100. #endif
  101. }
  102. }
  103. void LogWriter::run()
  104. {
  105. while(1)
  106. {
  107. mCondition->Lock();
  108. if (mLogNodeList.empty())
  109. {
  110. mCondition->Wait();
  111. }
  112. bool isNeedWriteToFile = false;
  113. if (mLogNodeList.size() >= 10)//日志文件超过10条 则写入文件
  114. {
  115. isNeedWriteToFile = true;
  116. }
  117. else
  118. {
  119. uint64_t startTime = mLogNodeList.front().mCreateTime;
  120. uint64_t currentTime = MoudleConfig::getTimeStamp_MilliSecond();
  121. if ((currentTime - startTime) > (10000)) //日志数据最迟10秒写入文件
  122. {
  123. isNeedWriteToFile = true;
  124. }
  125. }
  126. if(isNeedWriteToFile)
  127. {
  128. std::list<LogInfoNode> LogNodeList = mLogNodeList;
  129. mLogNodeList.clear();
  130. mCondition->Unlock();
  131. while(!LogNodeList.empty())
  132. {
  133. LogInfoNode node = LogNodeList.front();
  134. LogNodeList.pop_front();
  135. #ifdef WIN32
  136. char logDirName[20] = {0};
  137. sprintf(logDirName,"log\\%d",node.cameraId);
  138. ///如果log目录不存在 则创建
  139. if (access(logDirName, R_OK)!=0)
  140. {
  141. // _mkdir(logDirName);
  142. char cmd[32] = {0};
  143. sprintf(cmd,"mkdir %s",logDirName);
  144. system(cmd);
  145. }
  146. #else
  147. char logDirName[20] = {0};
  148. sprintf(logDirName,"log/%d",node.cameraId);
  149. ///如果log目录不存在 则创建
  150. if (access(logDirName,R_OK)!=0)
  151. {
  152. char cmd[32] = {0};
  153. sprintf(cmd,"mkdir %s -p",logDirName);
  154. system(cmd);
  155. }
  156. #endif
  157. ///一个文件最多5M 超过5M则创建下一个文件
  158. int index = 0;
  159. char fileName[36];
  160. while(1)
  161. {
  162. memset(fileName,0x0,36);
  163. sprintf(fileName,"log/%d/logfile_%d",node.cameraId,index++);
  164. if (access(fileName, R_OK)==0)
  165. {
  166. FILE * fp = fopen(fileName, "r");
  167. fseek(fp, 0L, SEEK_END);
  168. int size = ftell(fp);
  169. fclose(fp);
  170. if (size < 5*1024*1024) //小于5M则可以写
  171. {
  172. break;
  173. }
  174. }
  175. else
  176. {
  177. break;
  178. }
  179. }
  180. FILE * fp = fopen(fileName, "at+");
  181. if (fp == NULL)
  182. {
  183. fprintf(stderr,"写日志失败,请确保你有足够的权限写!\n");
  184. }
  185. else
  186. {
  187. fwrite(node.logStr.c_str(),1,node.logStr.size(),fp);
  188. fclose(fp);
  189. }
  190. }
  191. }
  192. else
  193. {
  194. mCondition->Unlock();
  195. MoudleConfig::mSleep(5000);
  196. continue;
  197. }
  198. }
  199. }