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