added error and warning options to debugout
[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
27 #include "timestamp.h"
28
29 using namespace std;
30
31 void debugOut(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                 ostream out(buf);
44
45                 out.precision(15);
46
47                 if(mDebugLevel <= debugThreshhold || mDebugLevel == Error || mDebugLevel == Warning)
48                 {
49                         out<<bufferTime(amb::currentTime())<<" | ";
50
51                         if(mDebugLevel == Error)
52                                 out<<" ERROR ";
53                         if(mDebugLevel == Warning)
54                                 out<<" WARNING ";
55                 }
56         }
57
58         DebugOut const& operator << (string message) const
59         {
60                 ostream out(buf);
61
62                 out.precision(15);
63
64                 if(mDebugLevel <= debugThreshhold)
65                          out<<message<<" ";
66                 return *this;
67         }
68
69         DebugOut const& operator << (ostream & (*manip)(std::ostream&)) const
70         {
71                 ostream out(buf);
72
73                 out.precision(15);
74
75                 if(mDebugLevel <= debugThreshhold)
76                          out<<endl;
77                 return *this;
78         }
79         
80         DebugOut const & operator << (double val) const
81         {
82                 ostream out(buf);
83
84                 out.precision(15);
85
86                 if(mDebugLevel <= debugThreshhold)
87                          out<<val<<" ";
88                 return *this;
89         }
90
91         static void setDebugThreshhold(int th)
92         {
93                 debugThreshhold = th;
94         }
95
96         static void setOutput(ostream &o)
97         {
98                 buf = o.rdbuf();
99         }
100
101 private:
102
103         std::string bufferTime(double time)
104         {
105                 ostringstream f;
106
107                 f.precision(15);
108
109                 f<<time;
110
111                 while(f.str().length() <= 15)
112                 {
113                         f<<" ";
114                 }
115
116                 return f.str();
117         }
118
119         static int debugThreshhold;
120         static std::streambuf *buf;
121         int mDebugLevel;
122 };
123
124
125
126
127
128 #endif