• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 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 // InfoLog.h: Defines the gl::InfoLog class to handle the logs generated when
7 // compiling/linking shaders so useful error messages can be returned to the caller.
8 
9 #ifndef LIBANGLE_INFOLOG_H_
10 #define LIBANGLE_INFOLOG_H_
11 
12 namespace gl
13 {
14 
15 class InfoLog : angle::NonCopyable
16 {
17   public:
18     InfoLog();
19     ~InfoLog();
20 
21     size_t getLength() const;
22     void getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const;
23 
24     void appendSanitized(const char *message);
25     void reset();
26 
27     // This helper class ensures we append a newline after writing a line.
28     class StreamHelper : angle::NonCopyable
29     {
30       public:
StreamHelper(StreamHelper && rhs)31         StreamHelper(StreamHelper &&rhs) : mStream(rhs.mStream) { rhs.mStream = nullptr; }
32 
33         StreamHelper &operator=(StreamHelper &&rhs)
34         {
35             std::swap(mStream, rhs.mStream);
36             return *this;
37         }
38 
~StreamHelper()39         ~StreamHelper()
40         {
41             // Write newline when destroyed on the stack
42             if (mStream)
43             {
44                 (*mStream) << std::endl;
45             }
46         }
47 
48         template <typename T>
49         StreamHelper &operator<<(const T &value)
50         {
51             (*mStream) << value;
52             return *this;
53         }
54 
55       private:
56         friend class InfoLog;
57 
StreamHelper(std::stringstream * stream)58         StreamHelper(std::stringstream *stream) : mStream(stream) { ASSERT(stream); }
59 
60         std::stringstream *mStream;
61     };
62 
63     template <typename T>
64     StreamHelper operator<<(const T &value)
65     {
66         ensureInitialized();
67         StreamHelper helper(mLazyStream.get());
68         helper << value;
69         return helper;
70     }
71 
str()72     std::string str() const { return mLazyStream ? mLazyStream->str() : ""; }
73 
74     bool empty() const;
75 
76   private:
ensureInitialized()77     void ensureInitialized()
78     {
79         if (!mLazyStream)
80         {
81             mLazyStream.reset(new std::stringstream());
82         }
83     }
84 
85     std::unique_ptr<std::stringstream> mLazyStream;
86 };
87 
88 }  // namespace gl
89 
90 #endif  // LIBANGLE_INFOLOG_H_
91