Home | History | Annotate | Download | only in flann
      1 /***********************************************************************
      2  * Software License Agreement (BSD License)
      3  *
      4  * Copyright 2008-2009  Marius Muja (mariusm (at) cs.ubc.ca). All rights reserved.
      5  * Copyright 2008-2009  David G. Lowe (lowe (at) 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 {
     45     Logger() : stream(stdout), logLevel(FLANN_LOG_WARN) {}
     46 
     47     ~Logger()
     48     {
     49         if ((stream!=NULL)&&(stream!=stdout)) {
     50             fclose(stream);
     51         }
     52     }
     53 
     54     static Logger& instance()
     55     {
     56         static Logger logger;
     57         return logger;
     58     }
     59 
     60     void _setDestination(const char* name)
     61     {
     62         if (name==NULL) {
     63             stream = stdout;
     64         }
     65         else {
     66             stream = fopen(name,"w");
     67             if (stream == NULL) {
     68                 stream = stdout;
     69             }
     70         }
     71     }
     72 
     73     int _log(int level, const char* fmt, va_list arglist)
     74     {
     75         if (level > logLevel ) return -1;
     76         int ret = vfprintf(stream, fmt, arglist);
     77         return ret;
     78     }
     79 
     80 public:
     81     /**
     82      * Sets the logging level. All messages with lower priority will be ignored.
     83      * @param level Logging level
     84      */
     85     static void setLevel(int level) { instance().logLevel = level; }
     86 
     87     /**
     88      * Sets the logging destination
     89      * @param name Filename or NULL for console
     90      */
     91     static void setDestination(const char* name) { instance()._setDestination(name); }
     92 
     93     /**
     94      * Print log message
     95      * @param level Log level
     96      * @param fmt Message format
     97      * @return
     98      */
     99     static int log(int level, const char* fmt, ...)
    100     {
    101         va_list arglist;
    102         va_start(arglist, fmt);
    103         int ret = instance()._log(level,fmt,arglist);
    104         va_end(arglist);
    105         return ret;
    106     }
    107 
    108 #define LOG_METHOD(NAME,LEVEL) \
    109     static int NAME(const char* fmt, ...) \
    110     { \
    111         va_list ap; \
    112         va_start(ap, fmt); \
    113         int ret = instance()._log(LEVEL, fmt, ap); \
    114         va_end(ap); \
    115         return ret; \
    116     }
    117 
    118     LOG_METHOD(fatal, FLANN_LOG_FATAL)
    119     LOG_METHOD(error, FLANN_LOG_ERROR)
    120     LOG_METHOD(warn, FLANN_LOG_WARN)
    121     LOG_METHOD(info, FLANN_LOG_INFO)
    122 
    123 private:
    124     FILE* stream;
    125     int logLevel;
    126 };
    127 
    128 }
    129 
    130 #endif //OPENCV_FLANN_LOGGER_H
    131