1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/compiler/InfoSink.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,115 @@ 1.4 +// 1.5 +// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +#ifndef _INFOSINK_INCLUDED_ 1.11 +#define _INFOSINK_INCLUDED_ 1.12 + 1.13 +#include <math.h> 1.14 +#include "compiler/Common.h" 1.15 + 1.16 +// Returns the fractional part of the given floating-point number. 1.17 +inline float fractionalPart(float f) { 1.18 + float intPart = 0.0f; 1.19 + return modff(f, &intPart); 1.20 +} 1.21 + 1.22 +// 1.23 +// TPrefixType is used to centralize how info log messages start. 1.24 +// See below. 1.25 +// 1.26 +enum TPrefixType { 1.27 + EPrefixNone, 1.28 + EPrefixWarning, 1.29 + EPrefixError, 1.30 + EPrefixInternalError, 1.31 + EPrefixUnimplemented, 1.32 + EPrefixNote 1.33 +}; 1.34 + 1.35 +// 1.36 +// Encapsulate info logs for all objects that have them. 1.37 +// 1.38 +// The methods are a general set of tools for getting a variety of 1.39 +// messages and types inserted into the log. 1.40 +// 1.41 +class TInfoSinkBase { 1.42 +public: 1.43 + TInfoSinkBase() {} 1.44 + 1.45 + template <typename T> 1.46 + TInfoSinkBase& operator<<(const T& t) { 1.47 + TPersistStringStream stream; 1.48 + stream << t; 1.49 + sink.append(stream.str()); 1.50 + return *this; 1.51 + } 1.52 + // Override << operator for specific types. It is faster to append strings 1.53 + // and characters directly to the sink. 1.54 + TInfoSinkBase& operator<<(char c) { 1.55 + sink.append(1, c); 1.56 + return *this; 1.57 + } 1.58 + TInfoSinkBase& operator<<(const char* str) { 1.59 + sink.append(str); 1.60 + return *this; 1.61 + } 1.62 + TInfoSinkBase& operator<<(const TPersistString& str) { 1.63 + sink.append(str); 1.64 + return *this; 1.65 + } 1.66 + TInfoSinkBase& operator<<(const TString& str) { 1.67 + sink.append(str.c_str()); 1.68 + return *this; 1.69 + } 1.70 + // Make sure floats are written with correct precision. 1.71 + TInfoSinkBase& operator<<(float f) { 1.72 + // Make sure that at least one decimal point is written. If a number 1.73 + // does not have a fractional part, the default precision format does 1.74 + // not write the decimal portion which gets interpreted as integer by 1.75 + // the compiler. 1.76 + TPersistStringStream stream; 1.77 + if (fractionalPart(f) == 0.0f) { 1.78 + stream.precision(1); 1.79 + stream << std::showpoint << std::fixed << f; 1.80 + } else { 1.81 + stream.unsetf(std::ios::fixed); 1.82 + stream.unsetf(std::ios::scientific); 1.83 + stream.precision(8); 1.84 + stream << f; 1.85 + } 1.86 + sink.append(stream.str()); 1.87 + return *this; 1.88 + } 1.89 + // Write boolean values as their names instead of integral value. 1.90 + TInfoSinkBase& operator<<(bool b) { 1.91 + const char* str = b ? "true" : "false"; 1.92 + sink.append(str); 1.93 + return *this; 1.94 + } 1.95 + 1.96 + void erase() { sink.clear(); } 1.97 + int size() { return static_cast<int>(sink.size()); } 1.98 + 1.99 + const TPersistString& str() const { return sink; } 1.100 + const char* c_str() const { return sink.c_str(); } 1.101 + 1.102 + void prefix(TPrefixType p); 1.103 + void location(int file, int line); 1.104 + void location(const TSourceLoc& loc); 1.105 + void message(TPrefixType p, const TSourceLoc& loc, const char* m); 1.106 + 1.107 +private: 1.108 + TPersistString sink; 1.109 +}; 1.110 + 1.111 +class TInfoSink { 1.112 +public: 1.113 + TInfoSinkBase info; 1.114 + TInfoSinkBase debug; 1.115 + TInfoSinkBase obj; 1.116 +}; 1.117 + 1.118 +#endif // _INFOSINK_INCLUDED_