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 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 10 | #include <math.h> |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | #include "compiler/Common.h" |
| 12 | |
alokp@chromium.org | b892fc6 | 2010-05-06 19:10:34 +0000 | [diff] [blame] | 13 | // Returns the fractional part of the given floating-point number. |
| 14 | inline float fractionalPart(float f) { |
alokp@chromium.org | 92984ef | 2010-05-06 22:49:37 +0000 | [diff] [blame] | 15 | float intPart = 0.0f; |
| 16 | return modff(f, &intPart); |
alokp@chromium.org | b892fc6 | 2010-05-06 19:10:34 +0000 | [diff] [blame] | 17 | } |
| 18 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 19 | // |
| 20 | // TPrefixType is used to centralize how info log messages start. |
| 21 | // See below. |
| 22 | // |
| 23 | enum TPrefixType { |
| 24 | EPrefixNone, |
| 25 | EPrefixWarning, |
| 26 | EPrefixError, |
| 27 | EPrefixInternalError, |
| 28 | EPrefixUnimplemented, |
| 29 | EPrefixNote |
| 30 | }; |
| 31 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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: |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 40 | TInfoSinkBase() {} |
| 41 | |
| 42 | template <typename T> |
| 43 | TInfoSinkBase& operator<<(const T& t) { |
| 44 | TPersistStringStream stream; |
| 45 | stream << t; |
| 46 | sink.append(stream.str()); |
alokp@chromium.org | b892fc6 | 2010-05-06 19:10:34 +0000 | [diff] [blame] | 47 | return *this; |
| 48 | } |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 49 | // Override << operator for specific types. It is faster to append strings |
| 50 | // and characters directly to the sink. |
| 51 | TInfoSinkBase& operator<<(char c) { |
| 52 | sink.append(1, c); |
| 53 | return *this; |
| 54 | } |
| 55 | TInfoSinkBase& operator<<(const char* str) { |
| 56 | sink.append(str); |
| 57 | return *this; |
| 58 | } |
| 59 | TInfoSinkBase& operator<<(const TPersistString& str) { |
| 60 | sink.append(str); |
| 61 | return *this; |
| 62 | } |
| 63 | TInfoSinkBase& operator<<(const TString& str) { |
| 64 | sink.append(str.c_str()); |
| 65 | return *this; |
| 66 | } |
| 67 | // Make sure floats are written with correct precision. |
| 68 | TInfoSinkBase& operator<<(float f) { |
| 69 | // Make sure that at least one decimal point is written. If a number |
| 70 | // does not have a fractional part, the default precision format does |
| 71 | // not write the decimal portion which gets interpreted as integer by |
| 72 | // the compiler. |
| 73 | TPersistStringStream stream; |
| 74 | if (fractionalPart(f) == 0.0f) { |
| 75 | stream.precision(2); |
| 76 | stream << std::showpoint << f; |
| 77 | } else { |
| 78 | stream << f; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 79 | } |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 80 | sink.append(stream.str()); |
| 81 | return *this; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 82 | } |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 83 | // Write boolean values as their names instead of integral value. |
| 84 | TInfoSinkBase& operator<<(bool b) { |
| 85 | const char* str = b ? "true" : "false"; |
| 86 | sink.append(str); |
| 87 | return *this; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | } |
| 89 | |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 90 | void erase() { sink.clear(); } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 92 | const TPersistString& str() const { return sink; } |
| 93 | const char* c_str() const { return sink.c_str(); } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame^] | 95 | void prefix(TPrefixType message); |
| 96 | void location(TSourceLoc loc); |
| 97 | void message(TPrefixType message, const char* s); |
| 98 | void message(TPrefixType message, const char* s, TSourceLoc loc); |
| 99 | |
| 100 | private: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | TPersistString sink; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | class TInfoSink { |
| 105 | public: |
| 106 | TInfoSinkBase info; |
| 107 | TInfoSinkBase debug; |
| 108 | TInfoSinkBase obj; |
| 109 | }; |
| 110 | |
| 111 | #endif // _INFOSINK_INCLUDED_ |