Create a base GLSL front-end from the 3Dlabs glslang front-end from 20-Sep-2005.
[platform/upstream/glslang.git] / glslang / Include / InfoSink.h
1 //
2 //Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 //All rights reserved.
4 //
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
7 //are met:
8 //
9 //    Redistributions of source code must retain the above copyright
10 //    notice, this list of conditions and the following disclaimer.
11 //
12 //    Redistributions in binary form must reproduce the above
13 //    copyright notice, this list of conditions and the following
14 //    disclaimer in the documentation and/or other materials provided
15 //    with the distribution.
16 //
17 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 //    contributors may be used to endorse or promote products derived
19 //    from this software without specific prior written permission.
20 //
21 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #ifndef _INFOSINK_INCLUDED_
36 #define _INFOSINK_INCLUDED_
37
38 #include "../Include/Common.h"
39 #include <math.h>
40
41 //
42 // TPrefixType is used to centralize how info log messages start.
43 // See below.
44 //
45 enum TPrefixType {
46     EPrefixNone,
47     EPrefixWarning,
48     EPrefixError,
49     EPrefixInternalError,
50     EPrefixUnimplemented,
51     EPrefixNote
52 };
53
54 enum TOutputStream {
55     ENull = 0,
56     EDebugger = 0x01,
57     EStdOut = 0x02,
58     EString = 0x04,
59 };
60 //
61 // Encapsulate info logs for all objects that have them.
62 //
63 // The methods are a general set of tools for getting a variety of
64 // messages and types inserted into the log.
65 //
66 class TInfoSinkBase {
67 public:
68     TInfoSinkBase() : outputStream(4) {}
69     void erase() { sink.erase(); }
70     TInfoSinkBase& operator<<(const TPersistString& t) { append(t); return *this; }
71     TInfoSinkBase& operator<<(char c)                  { append(1, c); return *this; }
72     TInfoSinkBase& operator<<(const char* s)           { append(s); return *this; }
73     TInfoSinkBase& operator<<(int n)                   { append(String(n)); return *this; }
74     TInfoSinkBase& operator<<(const unsigned int n)    { append(String(n)); return *this; }
75     TInfoSinkBase& operator<<(float n)                 { char buf[40]; 
76                                                      sprintf(buf, (fabs(n) > 1e-8 && fabs(n) < 1e8) || n == 0.0f ?
77                                                              "%f" : "%g", n);
78                                                      append(buf); 
79                                                      return *this; }
80     TInfoSinkBase& operator+(const TPersistString& t)  { append(t); return *this; }
81     TInfoSinkBase& operator+(const TString& t)         { append(t); return *this; }
82     TInfoSinkBase& operator<<(const TString& t)        { append(t); return *this; }
83     TInfoSinkBase& operator+(const char* s)            { append(s); return *this; }
84     const char* c_str() const { return sink.c_str(); }
85     void prefix(TPrefixType message) {
86         switch(message) {
87         case EPrefixNone:                                      break;
88         case EPrefixWarning:       append("WARNING: ");        break;
89         case EPrefixError:         append("ERROR: ");          break;
90         case EPrefixInternalError: append("INTERNAL ERROR: "); break;
91         case EPrefixUnimplemented: append("UNIMPLEMENTED: ");  break;
92         case EPrefixNote:          append("NOTE: ");           break;
93         default:                   append("UNKOWN ERROR: ");   break;
94         }
95     }
96     void location(TSourceLoc loc) {
97         append(FormatSourceLoc(loc).c_str());
98         append(": ");
99     }
100     void message(TPrefixType message, const char* s) {
101         prefix(message);
102         append(s);
103         append("\n");
104     }
105     void message(TPrefixType message, const char* s, TSourceLoc loc) {
106         prefix(message);
107         location(loc);
108         append(s);
109         append("\n");
110     }
111     
112     void setOutputStream(int output = 4)
113     {
114         outputStream = output;
115     }
116
117 protected:
118     void append(const char *s); 
119
120     void append(int count, char c);
121     void append(const TPersistString& t);
122     void append(const TString& t);
123
124     void checkMem(size_t growth) { if (sink.capacity() < sink.size() + growth + 2)  
125                                        sink.reserve(sink.capacity() +  sink.capacity() / 2); }
126     void appendToStream(const char* s);
127     TPersistString sink;
128     int outputStream;
129 };
130
131 class TInfoSink {
132 public:
133     TInfoSinkBase info;
134     TInfoSinkBase debug;
135 };
136
137 #endif // _INFOSINK_INCLUDED_