blob: a2fb9c38b4b320483e43b6eaf7093133a5b75d00 [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
10#include "Common.h"
11#include <math.h>
12
13//
14// TPrefixType is used to centralize how info log messages start.
15// See below.
16//
17enum TPrefixType {
18 EPrefixNone,
19 EPrefixWarning,
20 EPrefixError,
21 EPrefixInternalError,
22 EPrefixUnimplemented,
23 EPrefixNote
24};
25
26enum TOutputStream {
27 ENull = 0,
alokp@chromium.org0270ef12010-04-07 19:57:20 +000028 EStdOut = 0x01,
29 EString = 0x02,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030};
31//
32// Encapsulate info logs for all objects that have them.
33//
34// The methods are a general set of tools for getting a variety of
35// messages and types inserted into the log.
36//
37class TInfoSinkBase {
38public:
alokp@chromium.org0270ef12010-04-07 19:57:20 +000039 TInfoSinkBase() : outputStream(EString) {}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 void erase() { sink.erase(); }
41 TInfoSinkBase& operator<<(const TPersistString& t) { append(t); return *this; }
42 TInfoSinkBase& operator<<(char c) { append(1, c); return *this; }
43 TInfoSinkBase& operator<<(const char* s) { append(s); return *this; }
44 TInfoSinkBase& operator<<(int n) { append(String(n)); return *this; }
45 TInfoSinkBase& operator<<(const unsigned int n) { append(String(n)); return *this; }
46 TInfoSinkBase& operator<<(float n) { char buf[40];
47 sprintf(buf, "%.8g", n);
48 append(buf);
49 return *this; }
50 TInfoSinkBase& operator+(const TPersistString& t) { append(t); return *this; }
51 TInfoSinkBase& operator+(const TString& t) { append(t); return *this; }
52 TInfoSinkBase& operator<<(const TString& t) { append(t); return *this; }
53 TInfoSinkBase& operator+(const char* s) { append(s); return *this; }
54 const char* c_str() const { return sink.c_str(); }
55 void prefix(TPrefixType message) {
56 switch(message) {
57 case EPrefixNone: break;
58 case EPrefixWarning: append("WARNING: "); break;
59 case EPrefixError: append("ERROR: "); break;
60 case EPrefixInternalError: append("INTERNAL ERROR: "); break;
61 case EPrefixUnimplemented: append("UNIMPLEMENTED: "); break;
62 case EPrefixNote: append("NOTE: "); break;
63 default: append("UNKOWN ERROR: "); break;
64 }
65 }
66 void location(TSourceLoc loc) {
67 append(FormatSourceLoc(loc).c_str());
68 append(": ");
69 }
70 void message(TPrefixType message, const char* s) {
71 prefix(message);
72 append(s);
73 append("\n");
74 }
75 void message(TPrefixType message, const char* s, TSourceLoc loc) {
76 prefix(message);
77 location(loc);
78 append(s);
79 append("\n");
80 }
81
82 void setOutputStream(int output = 4)
83 {
84 outputStream = output;
85 }
86
87protected:
88 void append(const char *s);
89
90 void append(int count, char c);
91 void append(const TPersistString& t);
92 void append(const TString& t);
93
94 void checkMem(size_t growth) { if (sink.capacity() < sink.size() + growth + 2)
95 sink.reserve(sink.capacity() + sink.capacity() / 2); }
96 void appendToStream(const char* s);
97 TPersistString sink;
98 int outputStream;
99};
100
101class TInfoSink {
102public:
103 TInfoSinkBase info;
104 TInfoSinkBase debug;
105 TInfoSinkBase obj;
106};
107
108#endif // _INFOSINK_INCLUDED_