1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 
42 #ifndef __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
43 #define __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
44 #include <cstdlib>
45 #include <iostream>
46 #include <sstream>
47 #include <opencv2/core.hpp>
48 
49 #define CHECK(cond)     for(cv::dnn::GLogWrapper _logger(__FILE__, CV_Func, __LINE__, "CHECK", #cond, cond); _logger.exit(); _logger.check()) _logger.stream()
50 #define CHECK_EQ(a, b)  for(cv::dnn::GLogWrapper _logger(__FILE__, CV_Func, __LINE__, "CHECK", #a"="#b, ((a) == (b))); _logger.exit(); _logger.check()) _logger.stream()
51 #define LOG(TYPE)       for(cv::dnn::GLogWrapper _logger(__FILE__, CV_Func, __LINE__, #TYPE); _logger.exit(); _logger.check()) _logger.stream()
52 
53 namespace cv
54 {
55 namespace dnn
56 {
57 
58 class GLogWrapper
59 {
60     const char *file, *func, *type, *cond_str;
61     int line;
62     bool cond_status, exit_loop;
63     std::stringstream sstream;
64 
65 public:
66 
GLogWrapper(const char * _file,const char * _func,int _line,const char * _type,const char * _cond_str=NULL,bool _cond_status=true)67     GLogWrapper(const char *_file, const char *_func, int _line,
68           const char *_type,
69           const char *_cond_str = NULL, bool _cond_status = true
70     ) :
71         file(_file), func(_func), type(_type), cond_str(_cond_str),
72         line(_line), cond_status(_cond_status), exit_loop(true) {}
73 
stream()74     std::iostream &stream()
75     {
76         return sstream;
77     }
78 
exit()79     bool exit()
80     {
81         return exit_loop;
82     }
83 
check()84     void check()
85     {
86         exit_loop = false;
87 
88         if (cond_str && !cond_status)
89         {
90             cv::error(cv::Error::StsError, "FAILED: " + String(cond_str) + ". " + sstream.str(), func, file, line);
91         }
92         else if (!cond_str && strcmp(type, "CHECK"))
93         {
94             #ifndef NDEBUG
95             if (!std::strcmp(type, "INFO"))
96                 std::cout << sstream.str() << std::endl;
97             else
98                 std::cerr << sstream.str() << std::endl;
99             #endif
100         }
101     }
102 };
103 
104 }
105 }
106 #endif
107