blob: 2705f48d599ffabac21113a603b1497adad56d4b [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
Geoff Lang0a73dd82014-11-19 16:18:08 -05007#ifndef COMPILER_TRANSLATOR_INFOSINK_H_
8#define COMPILER_TRANSLATOR_INFOSINK_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009
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"
Olli Etuaho77ba4082016-12-16 12:01:18 +000013#include "compiler/translator/Severity.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000014
Jamie Madill45bcc782016-11-07 13:58:48 -050015namespace sh
16{
17
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000018// Returns the fractional part of the given floating-point number.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050019inline float fractionalPart(float f)
20{
21 float intPart = 0.0f;
22 return modff(f, &intPart);
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000023}
24
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Encapsulate info logs for all objects that have them.
27//
28// The methods are a general set of tools for getting a variety of
29// messages and types inserted into the log.
30//
Jamie Madilld7b1ab52016-12-12 14:42:19 -050031class TInfoSinkBase
32{
33 public:
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000034 TInfoSinkBase() {}
35
36 template <typename T>
Jamie Madilld7b1ab52016-12-12 14:42:19 -050037 TInfoSinkBase &operator<<(const T &t)
38 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000039 TPersistStringStream stream;
40 stream << t;
41 sink.append(stream.str());
alokp@chromium.orgb892fc62010-05-06 19:10:34 +000042 return *this;
43 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000044 // Override << operator for specific types. It is faster to append strings
45 // and characters directly to the sink.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050046 TInfoSinkBase &operator<<(char c)
47 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000048 sink.append(1, c);
49 return *this;
50 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050051 TInfoSinkBase &operator<<(const char *str)
52 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000053 sink.append(str);
54 return *this;
55 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050056 TInfoSinkBase &operator<<(const TPersistString &str)
57 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000058 sink.append(str);
59 return *this;
60 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -050061 TInfoSinkBase &operator<<(const TString &str)
62 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000063 sink.append(str.c_str());
64 return *this;
65 }
66 // Make sure floats are written with correct precision.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050067 TInfoSinkBase &operator<<(float f)
68 {
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000069 // 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;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050074 if (fractionalPart(f) == 0.0f)
75 {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000076 stream.precision(1);
77 stream << std::showpoint << std::fixed << f;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050078 }
79 else
80 {
daniel@transgaming.comb0a1dcc2010-06-07 02:06:26 +000081 stream.unsetf(std::ios::fixed);
82 stream.unsetf(std::ios::scientific);
83 stream.precision(8);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000084 stream << f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000085 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000086 sink.append(stream.str());
87 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088 }
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000089 // Write boolean values as their names instead of integral value.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050090 TInfoSinkBase &operator<<(bool b)
91 {
92 const char *str = b ? "true" : "false";
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000093 sink.append(str);
94 return *this;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 }
96
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000097 void erase() { sink.clear(); }
alokp@chromium.org7beea402010-09-15 21:18:34 +000098 int size() { return static_cast<int>(sink.size()); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500100 const TPersistString &str() const { return sink; }
101 const char *c_str() const { return sink.c_str(); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
Olli Etuaho77ba4082016-12-16 12:01:18 +0000103 void prefix(Severity severity);
Jamie Madill075edd82013-07-08 13:30:19 -0400104 void location(int file, int line);
alokp@chromium.org4e4facd2010-06-02 15:21:22 +0000105
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500106 private:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107 TPersistString sink;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108};
109
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500110class TInfoSink
111{
112 public:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 TInfoSinkBase info;
114 TInfoSinkBase debug;
115 TInfoSinkBase obj;
116};
117
Jamie Madill45bcc782016-11-07 13:58:48 -0500118} // namespace sh
119
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500120#endif // COMPILER_TRANSLATOR_INFOSINK_H_