ambd: remove redundant code in core
[profile/ivi/automotive-message-broker.git] / lib / debugout.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef DEBUGOUT__H__
20 #define DEBUGOUT__H__
21
22 #include <string>
23 #include <iostream>
24 #include <fstream>
25 #include <sstream>
26 #include <stdexcept>
27 #include "timestamp.h"
28
29 using namespace std;
30
31 void debugOut(const string &message);
32
33 /*! \addtogroup libamb
34  *  @{
35  */
36
37 /*!
38  * \file debugout.h
39  * \brief The DebugOut class represents a class used for outputing debug information
40  * The specified debug level will only be outputed if the debug level is => the debug threshhold
41  * Here's a simple example:
42  * \code
43  * DebugOut::setDebugThreshhold(3);
44  * DebugOut(DebugOut::Warning) << "This is a warning" << std::endl;
45  * DebugOut(3) << "This will only show if the threshhold is 3 or lower." << std::endl;
46  *
47  * /// Start logging to a file:
48  * ofstream logfile;
49  * logfile.open("amb.log", ios::out | ios::trunc);
50  * DebugOut::setOutput(logfile)
51  *
52  * /// Throw exception on warning or error:
53  * DebugOut::setThrowErr(true);
54  * DebugOut::setThrowWarn(true);
55  * DebugOut(DebugOut::Error) << "This will throw an exception." << std::endl;
56  *
57  * /// Log to stderr:
58  * DebugOut::setOutput(std::cerr);
59  * DebugOut() << "This will log to stderr." << std::endl;
60  * \endcode
61  */
62
63 namespace amb
64 {
65 /*!
66  * \brief deprecateMethod prints warning if method is used.  Throws if version >= PROJECT_SERIES
67  * \param methodName name of method being deprecated.
68  * \param version version in which this method will no longer function in.
69  */
70 void deprecateMethod(const string &methodName, const std::string & version);
71 }
72
73 class DebugOut
74 {
75 public:
76
77         /*!
78          * \brief Error use when essential functionality is blocked
79          */
80         static const int Error;
81
82         /*!
83          * \brief Warning use when non-essential functionality is bocked, or when workarounds exist.
84          */
85         static const int Warning;
86
87         DebugOut(int debugLevel = 4)
88         {
89                 mDebugLevel = debugLevel;
90
91                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
92                 {
93                         ostream out(buf);
94                         out.precision(15);
95                         out<<bufferTime(amb::currentTime())<<" | ";
96
97                         if(mDebugLevel == Error)
98                                 out<<"ERROR ";
99                         if(mDebugLevel == Warning)
100                                 out<<"WARNING ";
101                 }
102         }
103         DebugOut const& operator << (const string &message) const
104         {
105                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
106                 {
107                         ostream out(buf);
108                         out.precision(15);
109                         out<<message;
110                 }
111                 return *this;
112         }
113
114         DebugOut const& operator << (ostream & (*manip)(std::ostream&)) const
115         {
116
117
118                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
119                 {
120                         ostream out(buf);
121                         out.precision(15);
122                         out<<endl;
123
124                         if((mDebugLevel == Error && throwErr))
125                         {
126                                 throw std::runtime_error("Abort on Error is set");
127                         }
128                         else if ((mDebugLevel == Warning && throwWarn))
129                         {
130                                 throw std::runtime_error("Abort on Warning is set");
131                         }
132                 }
133                 return *this;
134         }
135
136         DebugOut const & operator << (double val) const
137         {
138                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
139                 {
140                         ostream out(buf);
141                         out.precision(15);
142                         out<<val<<" ";
143                 }
144                 return *this;
145         }
146
147         static void setDebugThreshhold(int th)
148         {
149                 debugThreshhold = th;
150         }
151
152         static void setOutput(ostream &o)
153         {
154                 buf = o.rdbuf();
155         }
156
157         static void setThrowWarn(bool v)
158         {
159                 throwWarn = v;
160         }
161
162         static void setThrowErr(bool v)
163         {
164                 throwErr = v;
165         }
166
167         static const int getDebugThreshhold()
168         {
169                 return debugThreshhold;
170         }
171
172 private:
173
174         std::string bufferTime(double time)
175         {
176                 ostringstream f;
177
178                 f.precision(15);
179
180                 f<<time;
181
182                 while(f.str().length() <= 15)
183                 {
184                         f<<" ";
185                 }
186
187                 return f.str();
188         }
189
190         static int debugThreshhold;
191         static std::streambuf *buf;
192         static bool throwWarn;
193         static bool throwErr;
194         int mDebugLevel;
195 };
196
197 #endif
198
199 /** @} */