daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #ifndef _INFOSINK_INCLUDED_ |
| 8 | #define _INFOSINK_INCLUDED_ |
| 9 | |
| 10 | #include "Common.h" |
| 11 | #include <math.h> |
| 12 | |
| 13 | // |
| 14 | // TPrefixType is used to centralize how info log messages start. |
| 15 | // See below. |
| 16 | // |
| 17 | enum TPrefixType { |
| 18 | EPrefixNone, |
| 19 | EPrefixWarning, |
| 20 | EPrefixError, |
| 21 | EPrefixInternalError, |
| 22 | EPrefixUnimplemented, |
| 23 | EPrefixNote |
| 24 | }; |
| 25 | |
| 26 | enum TOutputStream { |
| 27 | ENull = 0, |
| 28 | EDebugger = 0x01, |
| 29 | EStdOut = 0x02, |
| 30 | EString = 0x04, |
| 31 | }; |
| 32 | // |
| 33 | // Encapsulate info logs for all objects that have them. |
| 34 | // |
| 35 | // The methods are a general set of tools for getting a variety of |
| 36 | // messages and types inserted into the log. |
| 37 | // |
| 38 | class TInfoSinkBase { |
| 39 | public: |
| 40 | TInfoSinkBase() : outputStream(4) {} |
| 41 | void erase() { sink.erase(); } |
| 42 | TInfoSinkBase& operator<<(const TPersistString& t) { append(t); return *this; } |
| 43 | TInfoSinkBase& operator<<(char c) { append(1, c); return *this; } |
| 44 | TInfoSinkBase& operator<<(const char* s) { append(s); return *this; } |
| 45 | TInfoSinkBase& operator<<(int n) { append(String(n)); return *this; } |
| 46 | TInfoSinkBase& operator<<(const unsigned int n) { append(String(n)); return *this; } |
| 47 | TInfoSinkBase& operator<<(float n) { char buf[40]; |
| 48 | sprintf(buf, "%.8g", n); |
| 49 | append(buf); |
| 50 | return *this; } |
| 51 | TInfoSinkBase& operator+(const TPersistString& t) { append(t); return *this; } |
| 52 | TInfoSinkBase& operator+(const TString& t) { append(t); return *this; } |
| 53 | TInfoSinkBase& operator<<(const TString& t) { append(t); return *this; } |
| 54 | TInfoSinkBase& operator+(const char* s) { append(s); return *this; } |
| 55 | const char* c_str() const { return sink.c_str(); } |
| 56 | void prefix(TPrefixType message) { |
| 57 | switch(message) { |
| 58 | case EPrefixNone: break; |
| 59 | case EPrefixWarning: append("WARNING: "); break; |
| 60 | case EPrefixError: append("ERROR: "); break; |
| 61 | case EPrefixInternalError: append("INTERNAL ERROR: "); break; |
| 62 | case EPrefixUnimplemented: append("UNIMPLEMENTED: "); break; |
| 63 | case EPrefixNote: append("NOTE: "); break; |
| 64 | default: append("UNKOWN ERROR: "); break; |
| 65 | } |
| 66 | } |
| 67 | void location(TSourceLoc loc) { |
| 68 | append(FormatSourceLoc(loc).c_str()); |
| 69 | append(": "); |
| 70 | } |
| 71 | void message(TPrefixType message, const char* s) { |
| 72 | prefix(message); |
| 73 | append(s); |
| 74 | append("\n"); |
| 75 | } |
| 76 | void message(TPrefixType message, const char* s, TSourceLoc loc) { |
| 77 | prefix(message); |
| 78 | location(loc); |
| 79 | append(s); |
| 80 | append("\n"); |
| 81 | } |
| 82 | |
| 83 | void setOutputStream(int output = 4) |
| 84 | { |
| 85 | outputStream = output; |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | void append(const char *s); |
| 90 | |
| 91 | void append(int count, char c); |
| 92 | void append(const TPersistString& t); |
| 93 | void append(const TString& t); |
| 94 | |
| 95 | void checkMem(size_t growth) { if (sink.capacity() < sink.size() + growth + 2) |
| 96 | sink.reserve(sink.capacity() + sink.capacity() / 2); } |
| 97 | void appendToStream(const char* s); |
| 98 | TPersistString sink; |
| 99 | int outputStream; |
| 100 | }; |
| 101 | |
| 102 | class TInfoSink { |
| 103 | public: |
| 104 | TInfoSinkBase info; |
| 105 | TInfoSinkBase debug; |
| 106 | TInfoSinkBase obj; |
| 107 | }; |
| 108 | |
| 109 | #endif // _INFOSINK_INCLUDED_ |