logger.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include "defines.h"
  35. namespace cvflann
  36. {
  37. class Logger
  38. {
  39. Logger() : stream(stdout), logLevel(FLANN_LOG_WARN) {}
  40. ~Logger()
  41. {
  42. if ((stream!=NULL)&&(stream!=stdout)) {
  43. fclose(stream);
  44. }
  45. }
  46. static Logger& instance()
  47. {
  48. static Logger logger;
  49. return logger;
  50. }
  51. void _setDestination(const char* name)
  52. {
  53. if (name==NULL) {
  54. stream = stdout;
  55. }
  56. else {
  57. #ifdef _MSC_VER
  58. if (fopen_s(&stream, name, "w") != 0)
  59. stream = NULL;
  60. #else
  61. stream = fopen(name,"w");
  62. #endif
  63. if (stream == NULL) {
  64. stream = stdout;
  65. }
  66. }
  67. }
  68. int _log(int level, const char* fmt, va_list arglist)
  69. {
  70. if (level > logLevel ) return -1;
  71. int ret = vfprintf(stream, fmt, arglist);
  72. return ret;
  73. }
  74. public:
  75. /**
  76. * Sets the logging level. All messages with lower priority will be ignored.
  77. * @param level Logging level
  78. */
  79. static void setLevel(int level) { instance().logLevel = level; }
  80. /**
  81. * Sets the logging destination
  82. * @param name Filename or NULL for console
  83. */
  84. static void setDestination(const char* name) { instance()._setDestination(name); }
  85. /**
  86. * Print log message
  87. * @param level Log level
  88. * @param fmt Message format
  89. * @return
  90. */
  91. static int log(int level, const char* fmt, ...)
  92. {
  93. va_list arglist;
  94. va_start(arglist, fmt);
  95. int ret = instance()._log(level,fmt,arglist);
  96. va_end(arglist);
  97. return ret;
  98. }
  99. #define LOG_METHOD(NAME,LEVEL) \
  100. static int NAME(const char* fmt, ...) \
  101. { \
  102. va_list ap; \
  103. va_start(ap, fmt); \
  104. int ret = instance()._log(LEVEL, fmt, ap); \
  105. va_end(ap); \
  106. return ret; \
  107. }
  108. LOG_METHOD(fatal, FLANN_LOG_FATAL)
  109. LOG_METHOD(error, FLANN_LOG_ERROR)
  110. LOG_METHOD(warn, FLANN_LOG_WARN)
  111. LOG_METHOD(info, FLANN_LOG_INFO)
  112. private:
  113. FILE* stream;
  114. int logLevel;
  115. };
  116. }
  117. #endif //OPENCV_FLANN_LOGGER_H