logfile.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include <pthread.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <errno.h>
  5. #include "logfile.h"
  6. #include "fileutil.h"
  7. /////////////////////////////////////////////////////////////////////////////
  8. CLogfile::CLogfile(void) : m_pf(NULL),
  9. m_bAttached(false),
  10. m_bClose(false),
  11. m_vb(VB_Off),
  12. m_nFileSize(0),
  13. m_nMaxFileSize(0),
  14. m_nMaxBackupFiles(0)
  15. {
  16. m_lsync = PTHREAD_MUTEX_INITIALIZER;
  17. }
  18. CLogfile::CLogfile(FILE *pf, bool bClose, int verb) : m_pf(pf),
  19. m_bAttached(false),
  20. m_bClose(false),
  21. m_vb(VB_Off),
  22. m_nFileSize(0),
  23. m_nMaxFileSize(0),
  24. m_nMaxBackupFiles(0)
  25. {
  26. m_lsync = PTHREAD_MUTEX_INITIALIZER;
  27. if(!Attach(pf, bClose, verb))
  28. {
  29. StdErr("CLogfile::Attach failed!\n");
  30. throw EINVAL;
  31. }
  32. }
  33. CLogfile::CLogfile(const char *pszFilename, bool bAppend, int verb, size_t nMaxFileSize, unsigned int nMaxBackupFiles) : m_pf(NULL),
  34. m_bAttached(false),
  35. m_bClose(false),
  36. m_vb(VB_Off),
  37. m_nFileSize(0),
  38. m_nMaxFileSize(0),
  39. m_nMaxBackupFiles(0)
  40. {
  41. m_lsync = PTHREAD_MUTEX_INITIALIZER;
  42. if(!Open(pszFilename, bAppend, verb, nMaxFileSize, nMaxBackupFiles))
  43. {
  44. StdErr("CLogfile::Open failed!\n");
  45. throw errno;
  46. }
  47. }
  48. CLogfile::~CLogfile(void)
  49. {
  50. Close();
  51. ::pthread_mutex_destroy(&m_lsync);
  52. }
  53. void CLogfile::SetVerbosity(int verb)
  54. {
  55. if(verb < VB_Off)
  56. verb = VB_Off;
  57. else if(verb >= VB_Dbg)
  58. verb = VB_Dbg;
  59. m_vb = (Verbosity)verb;
  60. }
  61. /////////////////////////////////////////////////////////////////////////////
  62. bool CLogfile::Open(const char *pszFilename, bool bAppend, int verb, size_t nMaxFileSize, unsigned int nMaxBackupFiles)
  63. {
  64. if(!pszFilename || !*pszFilename)
  65. return false;
  66. if(nMaxBackupFiles < 1)
  67. nMaxBackupFiles = 1;
  68. Close();
  69. if((m_pf = fopen(pszFilename, bAppend ? "ab" : "wb")))
  70. {
  71. char szBuf[PATH_MAX];
  72. m_strFilePath = ::realpath(pszFilename, szBuf);
  73. m_nFileSize = GetFileSize();
  74. m_bAttached = false;
  75. m_bClose = true;
  76. m_nMaxFileSize = nMaxFileSize;
  77. m_nMaxBackupFiles = nMaxBackupFiles;
  78. SetVerbosity(verb);
  79. ProcessFileSizeLimit(m_pf);
  80. }
  81. return m_pf != NULL;
  82. }
  83. void CLogfile::Close(void)
  84. {
  85. CsLock();
  86. if(m_pf)
  87. {
  88. if(!m_bAttached || m_bClose)
  89. fclose(m_pf);
  90. m_pf = NULL;
  91. }
  92. m_bClose = false;
  93. m_bAttached = false;
  94. m_nFileSize = 0;
  95. m_nMaxFileSize = 0;
  96. m_nMaxBackupFiles = 0;
  97. m_vb = VB_Off;
  98. m_strFilePath.clear();
  99. CsUnlock();
  100. }
  101. void CLogfile::Flush(void)
  102. {
  103. if(m_pf)
  104. fflush(m_pf);
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. bool CLogfile::Attach(FILE *pf, bool bClose, int verb)
  108. {
  109. if(!pf)
  110. {
  111. errno = EINVAL;
  112. return false;
  113. }
  114. Close();
  115. m_pf = pf;
  116. m_bAttached = true;
  117. m_bClose = bClose;
  118. SetVerbosity(verb);
  119. return true;
  120. }
  121. void CLogfile::Detach(void)
  122. {
  123. CsLock();
  124. if(m_bAttached)
  125. {
  126. m_pf = NULL;
  127. m_bClose = false;
  128. m_bAttached = false;
  129. }
  130. CsUnlock();
  131. }
  132. /////////////////////////////////////////////////////////////////////////////
  133. void CLogfile::Log(FILE *pf, const char *pszFormat, va_list args)
  134. {
  135. int nLen;
  136. CsLock();
  137. if((nLen = vfprintf(pf, pszFormat, args)) > 0)
  138. {
  139. fflush(pf);
  140. m_nFileSize += nLen;
  141. ProcessFileSizeLimit(pf);
  142. }
  143. CsUnlock();
  144. }
  145. void CLogfile::Log(const char *pszFormat, ...)
  146. {
  147. if(m_pf && pszFormat && *pszFormat)
  148. {
  149. va_list args;
  150. va_start(args, pszFormat);
  151. Log(m_pf, pszFormat, args);
  152. va_end(args);
  153. }
  154. }
  155. /////////////////////////////////////////////////////////////////////////////
  156. void CLogfile::Debug(const char *pszFormat, ...)
  157. {
  158. if(m_vb >= VB_Dbg)
  159. {
  160. const char *pszFmt;
  161. std::string sFormat;
  162. if((pszFmt = CreateFormatString(VB_Dbg, pszFormat, sFormat)))
  163. {
  164. va_list args;
  165. va_start(args, pszFormat);
  166. Log(m_pf, pszFmt, args);
  167. va_end(args);
  168. }
  169. }
  170. }
  171. void CLogfile::Info(const char *pszFormat, ...)
  172. {
  173. if(m_vb >= VB_Inf)
  174. {
  175. const char *pszFmt;
  176. std::string sFormat;
  177. if((pszFmt = CreateFormatString(VB_Inf, pszFormat, sFormat)))
  178. {
  179. va_list args;
  180. va_start(args, pszFormat);
  181. Log(m_pf, pszFmt, args);
  182. va_end(args);
  183. }
  184. }
  185. }
  186. void CLogfile::Warning(const char *pszFormat, ...)
  187. {
  188. if(m_vb >= VB_War)
  189. {
  190. const char *pszFmt;
  191. std::string sFormat;
  192. if((pszFmt = CreateFormatString(VB_War, pszFormat, sFormat)))
  193. {
  194. va_list args;
  195. va_start(args, pszFormat);
  196. Log(m_pf, pszFmt, args);
  197. va_end(args);
  198. }
  199. }
  200. }
  201. void CLogfile::Error(const char *pszFormat, ...)
  202. {
  203. if(m_vb >= VB_Err)
  204. {
  205. const char *pszFmt;
  206. std::string sFormat;
  207. if((pszFmt = CreateFormatString(VB_Err, pszFormat, sFormat)))
  208. {
  209. va_list args;
  210. va_start(args, pszFormat);
  211. Log(m_pf, pszFmt, args);
  212. va_end(args);
  213. }
  214. }
  215. }
  216. size_t CLogfile::GetFileSize(void)
  217. {
  218. CsLock();
  219. size_t s = m_pf ? ::GetFileSize(m_pf) : 0;
  220. CsUnlock();
  221. return s;
  222. }
  223. bool CLogfile::ProcessFileSizeLimit(FILE *pf)
  224. {
  225. if(m_nMaxFileSize && !m_bAttached && pf && (pf == m_pf))
  226. {
  227. if(m_nFileSize >= m_nMaxFileSize)
  228. {
  229. char szBuf1[PATH_MAX], szBuf2[PATH_MAX];
  230. std::string strFilePath = m_strFilePath;
  231. Verbosity vb = m_vb;
  232. size_t nMaxFileSize = m_nMaxFileSize;
  233. int nMaxBackupFiles = m_nMaxBackupFiles;
  234. fclose(m_pf);
  235. m_pf = NULL;
  236. m_bClose = false;
  237. m_bAttached = false;
  238. m_nFileSize = 0;
  239. m_nMaxFileSize = 0;
  240. m_nMaxBackupFiles = 0;
  241. m_vb = VB_Off;
  242. m_strFilePath.clear();
  243. snprintf(szBuf1, sizeof(szBuf1), "%s.%03u", strFilePath.c_str(), nMaxBackupFiles);
  244. remove(szBuf1);
  245. for(int i = nMaxBackupFiles; i > 1; --i)
  246. {
  247. snprintf(szBuf2, sizeof(szBuf2), "%s.%03u", strFilePath.c_str(), i);
  248. snprintf(szBuf1, sizeof(szBuf1), "%s.%03u", strFilePath.c_str(), i - 1);
  249. rename(szBuf1, szBuf2);
  250. }
  251. rename(strFilePath.c_str(), szBuf1);
  252. return Open(strFilePath.c_str(), false, vb, nMaxFileSize, nMaxBackupFiles);
  253. }
  254. }
  255. return false;
  256. }
  257. /////////////////////////////////////////////////////////////////////////////
  258. void CLogfile::StdOut(const char *pszFormat, ...)
  259. {
  260. if(pszFormat && *pszFormat)
  261. {
  262. va_list args;
  263. va_start(args, pszFormat);
  264. vfprintf(stdout, pszFormat, args);
  265. va_end(args);
  266. fflush(stdout);
  267. }
  268. }
  269. void CLogfile::StdErr(const char *pszFormat, ...)
  270. {
  271. if(pszFormat && *pszFormat)
  272. {
  273. va_list args;
  274. va_start(args, pszFormat);
  275. vfprintf(stderr, pszFormat, args);
  276. va_end(args);
  277. fflush(stderr);
  278. }
  279. }
  280. /////////////////////////////////////////////////////////////////////////////
  281. const char* CLogfile::CreateFormatString(Verbosity verb, const char *pszFormat, std::string &ret) const
  282. {
  283. time_t ts = time(NULL);
  284. char szBuf[128], szTime[64];
  285. struct tm *pm = localtime(&ts);
  286. strftime(szTime, sizeof(szTime), "%d.%m.%Y %H:%M:%S", pm);
  287. switch(verb)
  288. {
  289. case VB_Dbg:
  290. sprintf(szBuf, "%s - DEBUG: ", szTime);
  291. break;
  292. case VB_Inf:
  293. sprintf(szBuf, "%s - INFO: ", szTime);
  294. break;
  295. case VB_War:
  296. sprintf(szBuf, "%s - WARNING: ", szTime);
  297. break;
  298. case VB_Err:
  299. sprintf(szBuf, "%s - ERROR: ", szTime);
  300. break;
  301. default:
  302. return NULL;
  303. }
  304. ret = szBuf;
  305. ret += pszFormat;
  306. return ret.c_str();
  307. }
  308. /////////////////////////////////////////////////////////////////////////////
  309. void CLogfile::CsLock(void)
  310. {
  311. ::pthread_mutex_lock(&m_lsync);
  312. }
  313. void CLogfile::CsUnlock(void)
  314. {
  315. ::pthread_mutex_unlock(&m_lsync);
  316. }