Logger.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <time.h>
  32. #include "Constants.hpp"
  33. #include "Logger.hpp"
  34. namespace ZeroTier {
  35. Logger::Logger(const char *p,const char *prefix,unsigned long maxLogSize) :
  36. _path((p) ? p : ""),
  37. _prefix((prefix) ? (std::string(prefix) + " ") : ""),
  38. _maxLogSize(maxLogSize),
  39. _log_m(),
  40. _log((FILE *)0)
  41. {
  42. if (_path.length())
  43. _log = fopen(_path.c_str(),"a");
  44. else _log = stdout;
  45. }
  46. Logger::~Logger()
  47. {
  48. fflush(_log);
  49. if ((_log)&&(_log != stdout)&&(_log != stderr))
  50. fclose(_log);
  51. }
  52. void Logger::log(const char *fmt,...)
  53. {
  54. va_list ap;
  55. char tmp[128];
  56. if (_log) {
  57. Mutex::Lock _l(_log_m);
  58. _rotateIfNeeded();
  59. if (_log) {
  60. time_t now = time(0);
  61. #ifdef __WINDOWS__
  62. ctime_s(tmp,sizeof(tmp),&now);
  63. char *nowstr = tmp;
  64. #else
  65. char *nowstr = ctime_r(&now,tmp);
  66. #endif
  67. for(char *c=nowstr;*c;++c) {
  68. if (*c == '\n')
  69. *c = '\0';
  70. }
  71. if (_prefix.length())
  72. fwrite(_prefix.data(),1,_prefix.length(),_log);
  73. fprintf(_log,"[%s] ",nowstr);
  74. va_start(ap,fmt);
  75. vfprintf(_log,fmt,ap);
  76. va_end(ap);
  77. #ifdef _WIN32
  78. fwrite("\r\n",1,2,_log);
  79. #else
  80. fwrite("\n",1,1,_log);
  81. #endif
  82. fflush(_log);
  83. }
  84. }
  85. }
  86. #ifdef ZT_TRACE
  87. void Logger::trace(const char *module,unsigned int line,const char *fmt,...)
  88. {
  89. va_list ap;
  90. char tmp[128];
  91. if (_log) {
  92. Mutex::Lock _l(_log_m);
  93. _rotateIfNeeded();
  94. if (_log) {
  95. time_t now = time(0);
  96. #ifdef __WINDOWS__
  97. ctime_s(tmp,sizeof(tmp),&now);
  98. char *nowstr = tmp;
  99. #else
  100. char *nowstr = ctime_r(&now,tmp);
  101. #endif
  102. for(char *c=nowstr;*c;++c) {
  103. if (*c == '\n')
  104. *c = '\0';
  105. }
  106. if (_prefix.length())
  107. fwrite(_prefix.data(),1,_prefix.length(),_log);
  108. fprintf(_log,"[%s] TRACE/%s:%u ",nowstr,module,line);
  109. va_start(ap,fmt);
  110. vfprintf(_log,fmt,ap);
  111. va_end(ap);
  112. #ifdef _WIN32
  113. fwrite("\r\n",1,2,_log);
  114. #else
  115. fwrite("\n",1,1,_log);
  116. #endif
  117. fflush(_log);
  118. }
  119. }
  120. }
  121. #endif
  122. void Logger::_rotateIfNeeded()
  123. {
  124. if ((_maxLogSize)&&(_log != stdout)&&(_log != stderr)) {
  125. long pos = ftell(_log);
  126. if (pos > (long)_maxLogSize) {
  127. fclose(_log);
  128. rename(_path.c_str(),std::string(_path).append(".old").c_str());
  129. _log = fopen(_path.c_str(),"w");
  130. }
  131. }
  132. }
  133. } // namespace ZeroTier