bluemonkey enhancements
[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 class DebugOut 
34 {
35 public:
36
37         static const int Error;
38         static const int Warning;
39
40         DebugOut(int debugLevel = 4)
41         {
42                 mDebugLevel = debugLevel;
43
44                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
45                 {
46                         ostream out(buf);
47                         out.precision(15);
48                         out<<bufferTime(amb::currentTime())<<" | ";
49
50                         if(mDebugLevel == Error)
51                                 out<<"ERROR ";
52                         if(mDebugLevel == Warning)
53                                 out<<"WARNING ";
54                 }
55         }
56         DebugOut const& operator << (const string &message) const
57         {
58                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
59                 {
60                         ostream out(buf);
61                         out.precision(15);
62                         out<<message<<" ";
63                 }
64                 return *this;
65         }
66
67         DebugOut const& operator << (ostream & (*manip)(std::ostream&)) const
68         {
69
70
71                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
72                 {
73                         ostream out(buf);
74                         out.precision(15);
75                         out<<endl;
76
77                         if((mDebugLevel == Error && throwErr))
78                         {
79                                 throw std::runtime_error("Abort on Error is set");
80                         }
81                         else if ((mDebugLevel == Warning && throwWarn))
82                         {
83                                 throw std::runtime_error("Abort on Warning is set");
84                         }
85                 }
86                 return *this;
87         }
88         
89         DebugOut const & operator << (double val) const
90         {
91                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
92                 {
93                         ostream out(buf);
94                         out.precision(15);
95                         out<<val<<" ";
96                 }
97                 return *this;
98         }
99
100         static void setDebugThreshhold(int th)
101         {
102                 debugThreshhold = th;
103         }
104
105         static void setOutput(ostream &o)
106         {
107                 buf = o.rdbuf();
108         }
109
110         static void setThrowWarn(bool v)
111         {
112                 throwWarn = v;
113         }
114
115         static void setThrowErr(bool v)
116         {
117                 throwErr = v;
118         }
119
120         static const int getDebugThreshhold()
121         {
122                 return debugThreshhold;
123         }
124
125 private:
126
127         std::string bufferTime(double time)
128         {
129                 ostringstream f;
130
131                 f.precision(15);
132
133                 f<<time;
134
135                 while(f.str().length() <= 15)
136                 {
137                         f<<" ";
138                 }
139
140                 return f.str();
141         }
142
143         static int debugThreshhold;
144         static std::streambuf *buf;
145         static bool throwWarn;
146         static bool throwErr;
147         int mDebugLevel;
148 };
149
150
151
152
153
154 #endif