Logger.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef ZT_LOGGER_HPP
  28. #define ZT_LOGGER_HPP
  29. #include <stdio.h>
  30. #include <string>
  31. #include <stdexcept>
  32. #include "NonCopyable.hpp"
  33. #include "Mutex.hpp"
  34. #undef LOG
  35. #define LOG(f,...) if (_r->log) _r->log->log(f,##__VA_ARGS__)
  36. #undef TRACE
  37. #ifdef ZT_TRACE
  38. #define TRACE(f,...) if (_r->log) _r->log->trace(__FILE__,__LINE__,f,##__VA_ARGS__)
  39. #else
  40. #define TRACE(f,...) {}
  41. #endif
  42. namespace ZeroTier {
  43. /**
  44. * Utility for outputting logs to a file or stdout/stderr
  45. */
  46. class Logger : NonCopyable
  47. {
  48. public:
  49. /**
  50. * Construct a logger to log to a file or stdout
  51. *
  52. * If a path is supplied to log to a file, maxLogSize indicates the size
  53. * at which this file is closed, renamed to .old, and then a new log is
  54. * opened (essentially a log rotation). If stdout is used, this is ignored.
  55. *
  56. * @param p Path to log to or NULL to use stdout
  57. * @param prefix Prefix to prepend to log lines or NULL for none
  58. * @param maxLogSize Maximum log size (0 for no limit)
  59. */
  60. Logger(const char *p,const char *prefix,unsigned long maxLogSize);
  61. ~Logger();
  62. void log(const char *fmt,...);
  63. #ifdef ZT_TRACE
  64. void trace(const char *module,unsigned int line,const char *fmt,...);
  65. #else
  66. inline void trace(const char *module,unsigned int line,const char *fmt,...) {}
  67. #endif
  68. private:
  69. void _rotateIfNeeded();
  70. std::string _path;
  71. std::string _prefix;
  72. unsigned long _maxLogSize;
  73. Mutex _log_m;
  74. FILE *_log;
  75. };
  76. } // namespace ZeroTier
  77. #endif