blob: 698a8b454b3cfa9d4ed2d00b86167e8c132d1ffe [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.com4f39fd92010-03-08 20:26:45 +000010#include <math.h>
Jamie Madilld7f21352013-10-30 17:53:15 -040011#include <stdlib.h>
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/Common.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000013
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000014// Returns the fractional part of the given floating-point number.
15inline float fractionalPart(float f) {
alokp@chromium.org92984ef2010-05-06 22:49:37 +000016 float intPart = 0.0f;
17 return modff(f, &intPart);
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000018}
19
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020//
21// TPrefixType is used to centralize how info log messages start.
22// See below.
23//
24enum TPrefixType {
25 EPrefixNone,
26 EPrefixWarning,
27 EPrefixError,
28 EPrefixInternalError,
29 EPrefixUnimplemented,
30 EPrefixNote
31};
32
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033//
34// Encapsulate info logs for all objects that have them.
35//
36// The methods are a general set of tools for getting a variety of
37// messages and types inserted into the log.
38//
39class TInfoSinkBase {
40public:
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000041 TInfoSinkBase() {}
42
43 template <typename T>
44 TInfoSinkBase& operator<<(const T& t) {
45 TPersistStringStream stream;
46 stream << t;
47 sink.append(stream.str());
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000048 return *this;
49 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000050 // Override << operator for specific types. It is faster to append strings
51 // and characters directly to the sink.
52 TInfoSinkBase& operator<<(char c) {
53 sink.append(1, c);
54 return *this;
55 }
56 TInfoSinkBase& operator<<(const char* str) {
57 sink.append(str);
58 return *this;
59 }
60 TInfoSinkBase& operator<<(const TPersistString& str) {
61 sink.append(str);
62 return *this;
63 }
64 TInfoSinkBase& operator<<(const TString& str) {
65 sink.append(str.c_str());
66 return *this;
67 }
68 // Make sure floats are written with correct precision.
69 TInfoSinkBase& operator<<(float f) {
70 // Make sure that at least one decimal point is written. If a number
71 // does not have a fractional part, the default precision format does
72 // not write the decimal portion which gets interpreted as integer by
73 // the compiler.
74 TPersistStringStream stream;
75 if (fractionalPart(f) == 0.0f) {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000076 stream.precision(1);
77 stream << std::showpoint << std::fixed << f;
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000078 } else {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000079 stream.unsetf(std::ios::fixed);
80 stream.unsetf(std::ios::scientific);
81 stream.precision(8);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000082 stream << f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000084 sink.append(stream.str());
85 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000087 // Write boolean values as their names instead of integral value.
88 TInfoSinkBase& operator<<(bool b) {
89 const char* str = b ? "true" : "false";
90 sink.append(str);
91 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000092 }
93
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000094 void erase() { sink.clear(); }
alokp@chromium.org7beea402010-09-15 21:18:34 +000095 int size() { return static_cast<int>(sink.size()); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000097 const TPersistString& str() const { return sink; }
98 const char* c_str() const { return sink.c_str(); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
Jamie Madill075edd82013-07-08 13:30:19 -0400100 void prefix(TPrefixType p);
101 void location(int file, int line);
102 void location(const TSourceLoc& loc);
103 void message(TPrefixType p, const TSourceLoc& loc, const char* m);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +0000104
105private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106 TPersistString sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107};
108
109class TInfoSink {
110public:
111 TInfoSinkBase info;
112 TInfoSinkBase debug;
113 TInfoSinkBase obj;
114};
115
116#endif // _INFOSINK_INCLUDED_