logger.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /***********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
  5. * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
  6. *
  7. * THE BSD LICENSE
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *************************************************************************/
  30. #ifndef OPENCV_FLANN_LOGGER_H
  31. #define OPENCV_FLANN_LOGGER_H
  32. //! @cond IGNORED
  33. #include <stdio.h>
  34. #include <stdarg.h>
  35. #include "defines.h"
  36. namespace cvflann
  37. {
  38. class Logger
  39. {
  40. Logger() : stream(stdout), logLevel(FLANN_LOG_WARN) {}
  41. ~Logger()
  42. {
  43. if ((stream!=NULL)&&(stream!=stdout)) {
  44. fclose(stream);
  45. }
  46. }
  47. static Logger& instance()
  48. {
  49. static Logger logger;
  50. return logger;
  51. }
  52. void _setDestination(const char* name)
  53. {
  54. if (name==NULL) {
  55. stream = stdout;
  56. }
  57. else {
  58. #ifdef _MSC_VER
  59. if (fopen_s(&stream, name, "w") != 0)
  60. stream = NULL;
  61. #else
  62. stream = fopen(name,"w");
  63. #endif
  64. if (stream == NULL) {
  65. stream = stdout;
  66. }
  67. }
  68. }
  69. int _log(int level, const char* fmt, va_list arglist)
  70. {
  71. if (level > logLevel ) return -1;
  72. int ret = vfprintf(stream, fmt, arglist);
  73. return ret;
  74. }
  75. public:
  76. /**
  77. * Sets the logging level. All messages with lower priority will be ignored.
  78. * @param level Logging level
  79. */
  80. static void setLevel(int level) { instance().logLevel = level; }
  81. /**
  82. * Sets the logging destination
  83. * @param name Filename or NULL for console
  84. */
  85. static void setDestination(const char* name) { instance()._setDestination(name); }
  86. /**
  87. * Print log message
  88. * @param level Log level
  89. * @param fmt Message format
  90. * @return
  91. */
  92. static int log(int level, const char* fmt, ...)
  93. {
  94. va_list arglist;
  95. va_start(arglist, fmt);
  96. int ret = instance()._log(level,fmt,arglist);
  97. va_end(arglist);
  98. return ret;
  99. }
  100. #define LOG_METHOD(NAME,LEVEL) \
  101. static int NAME(const char* fmt, ...) \
  102. { \
  103. va_list ap; \
  104. va_start(ap, fmt); \
  105. int ret = instance()._log(LEVEL, fmt, ap); \
  106. va_end(ap); \
  107. return ret; \
  108. }
  109. LOG_METHOD(fatal, FLANN_LOG_FATAL)
  110. LOG_METHOD(error, FLANN_LOG_ERROR)
  111. LOG_METHOD(warn, FLANN_LOG_WARN)
  112. LOG_METHOD(info, FLANN_LOG_INFO)
  113. private:
  114. FILE* stream;
  115. int logLevel;
  116. };
  117. }
  118. //! @endcond
  119. #endif //OPENCV_FLANN_LOGGER_H