Logger.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 "Logger.hpp"
  33. namespace ZeroTier {
  34. Logger::Logger(const char *p,const char *prefix,unsigned long maxLogSize) :
  35. _path((p) ? p : ""),
  36. _prefix((prefix) ? (std::string(prefix) + " ") : ""),
  37. _maxLogSize(maxLogSize),
  38. _log_m(),
  39. _log((FILE *)0)
  40. {
  41. if (_path.length())
  42. _log = fopen(_path.c_str(),"a");
  43. else _log = stdout;
  44. }
  45. Logger::~Logger()
  46. {
  47. fflush(_log);
  48. if ((_log)&&(_log != stdout)&&(_log != stderr))
  49. fclose(_log);
  50. }
  51. void Logger::log(const char *fmt,...)
  52. {
  53. va_list ap;
  54. char tmp[128];
  55. if (_log) {
  56. Mutex::Lock _l(_log_m);
  57. _rotateIfNeeded();
  58. if (_log) {
  59. time_t now = time(0);
  60. char *nowstr = ctime_r(&now,tmp);
  61. for(char *c=nowstr;*c;++c) {
  62. if (*c == '\n')
  63. *c = '\0';
  64. }
  65. if (_prefix.length())
  66. fwrite(_prefix.data(),1,_prefix.length(),_log);
  67. fprintf(_log,"[%s] ",nowstr);
  68. va_start(ap,fmt);
  69. vfprintf(_log,fmt,ap);
  70. va_end(ap);
  71. #ifdef _WIN32
  72. fwrite("\r\n",1,2,_log);
  73. #else
  74. fwrite("\n",1,1,_log);
  75. #endif
  76. fflush(_log);
  77. }
  78. }
  79. }
  80. #ifdef ZT_TRACE
  81. void Logger::trace(const char *module,unsigned int line,const char *fmt,...)
  82. {
  83. va_list ap;
  84. char tmp[128];
  85. if (_log) {
  86. Mutex::Lock _l(_log_m);
  87. _rotateIfNeeded();
  88. if (_log) {
  89. time_t now = time(0);
  90. char *nowstr = ctime_r(&now,tmp);
  91. for(char *c=nowstr;*c;++c) {
  92. if (*c == '\n')
  93. *c = '\0';
  94. }
  95. if (_prefix.length())
  96. fwrite(_prefix.data(),1,_prefix.length(),_log);
  97. fprintf(_log,"[%s] TRACE/%s:%u ",nowstr,module,line);
  98. va_start(ap,fmt);
  99. vfprintf(_log,fmt,ap);
  100. va_end(ap);
  101. #ifdef _WIN32
  102. fwrite("\r\n",1,2,_log);
  103. #else
  104. fwrite("\n",1,1,_log);
  105. #endif
  106. fflush(_log);
  107. }
  108. }
  109. }
  110. #endif
  111. void Logger::_rotateIfNeeded()
  112. {
  113. if ((_maxLogSize)&&(_log != stdout)&&(_log != stderr)) {
  114. long pos = ftell(_log);
  115. if (pos > (long)_maxLogSize) {
  116. fclose(_log);
  117. rename(_path.c_str(),std::string(_path).append(".old").c_str());
  118. _log = fopen(_path.c_str(),"w");
  119. }
  120. }
  121. }
  122. } // namespace ZeroTier