logger.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. stream = fopen(name,"w");
  58. if (stream == NULL) {
  59. stream = stdout;
  60. }
  61. }
  62. }
  63. int _log(int level, const char* fmt, va_list arglist)
  64. {
  65. if (level > logLevel ) return -1;
  66. int ret = vfprintf(stream, fmt, arglist);
  67. return ret;
  68. }
  69. public:
  70. /**
  71. * Sets the logging level. All messages with lower priority will be ignored.
  72. * @param level Logging level
  73. */
  74. static void setLevel(int level) { instance().logLevel = level; }
  75. /**
  76. * Sets the logging destination
  77. * @param name Filename or NULL for console
  78. */
  79. static void setDestination(const char* name) { instance()._setDestination(name); }
  80. /**
  81. * Print log message
  82. * @param level Log level
  83. * @param fmt Message format
  84. * @return
  85. */
  86. static int log(int level, const char* fmt, ...)
  87. {
  88. va_list arglist;
  89. va_start(arglist, fmt);
  90. int ret = instance()._log(level,fmt,arglist);
  91. va_end(arglist);
  92. return ret;
  93. }
  94. #define LOG_METHOD(NAME,LEVEL) \
  95. static int NAME(const char* fmt, ...) \
  96. { \
  97. va_list ap; \
  98. va_start(ap, fmt); \
  99. int ret = instance()._log(LEVEL, fmt, ap); \
  100. va_end(ap); \
  101. return ret; \
  102. }
  103. LOG_METHOD(fatal, FLANN_LOG_FATAL)
  104. LOG_METHOD(error, FLANN_LOG_ERROR)
  105. LOG_METHOD(warn, FLANN_LOG_WARN)
  106. LOG_METHOD(info, FLANN_LOG_INFO)
  107. private:
  108. FILE* stream;
  109. int logLevel;
  110. };
  111. }
  112. #endif //OPENCV_FLANN_LOGGER_H