1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include <wtf/PrintStream.h>
28
29#include <stdio.h>
30#include <wtf/text/CString.h>
31#include <wtf/text/UniquedStringImpl.h>
32#include <wtf/text/WTFString.h>
33
34namespace WTF {
35
36PrintStream::PrintStream() { }
37PrintStream::~PrintStream() { } // Force the vtable to be in this module
38
39void PrintStream::printf(const char* format, ...)
40{
41 va_list argList;
42 va_start(argList, format);
43 vprintf(format, argList);
44 va_end(argList);
45}
46
47void PrintStream::printfVariableFormat(const char* format, ...)
48{
49 ALLOW_NONLITERAL_FORMAT_BEGIN
50 IGNORE_GCC_WARNINGS_BEGIN("suggest-attribute=format")
51 va_list argList;
52 va_start(argList, format);
53 vprintf(format, argList);
54 va_end(argList);
55 IGNORE_GCC_WARNINGS_END
56 ALLOW_NONLITERAL_FORMAT_END
57}
58
59void PrintStream::flush()
60{
61}
62
63PrintStream& PrintStream::begin()
64{
65 return *this;
66}
67
68void PrintStream::end()
69{
70}
71
72void printInternal(PrintStream& out, const char* string)
73{
74 out.printf("%s", string);
75}
76
77static void printExpectedCStringHelper(PrintStream& out, const char* type, Expected<CString, UTF8ConversionError> expectedCString)
78{
79 if (UNLIKELY(!expectedCString)) {
80 if (expectedCString.error() == UTF8ConversionError::OutOfMemory)
81 out.print("(Out of memory while converting ", type, " to utf8)");
82 else
83 out.print("(failed to convert ", type, " to utf8)");
84 return;
85 }
86 out.print(expectedCString.value());
87}
88
89void printInternal(PrintStream& out, const StringView& string)
90{
91 printExpectedCStringHelper(out, "StringView", string.tryGetUtf8());
92}
93
94void printInternal(PrintStream& out, const CString& string)
95{
96 out.print(string.data());
97}
98
99void printInternal(PrintStream& out, const String& string)
100{
101 printExpectedCStringHelper(out, "String", string.tryGetUtf8());
102}
103
104void printInternal(PrintStream& out, const StringImpl* string)
105{
106 if (!string) {
107 out.print("(null StringImpl*)");
108 return;
109 }
110 printExpectedCStringHelper(out, "StringImpl*", string->tryGetUtf8());
111}
112
113void printInternal(PrintStream& out, bool value)
114{
115 out.print(boolForPrinting(value));
116}
117
118void printInternal(PrintStream& out, int value)
119{
120 out.printf("%d", value);
121}
122
123void printInternal(PrintStream& out, unsigned value)
124{
125 out.printf("%u", value);
126}
127
128void printInternal(PrintStream& out, signed char value)
129{
130 out.printf("%d", static_cast<int>(value));
131}
132
133void printInternal(PrintStream& out, unsigned char value)
134{
135 out.printf("%u", static_cast<unsigned>(value));
136}
137
138void printInternal(PrintStream& out, short value)
139{
140 out.printf("%d", static_cast<int>(value));
141}
142
143void printInternal(PrintStream& out, unsigned short value)
144{
145 out.printf("%u", static_cast<unsigned>(value));
146}
147
148void printInternal(PrintStream& out, long value)
149{
150 out.printf("%ld", value);
151}
152
153void printInternal(PrintStream& out, unsigned long value)
154{
155 out.printf("%lu", value);
156}
157
158void printInternal(PrintStream& out, long long value)
159{
160 out.printf("%lld", value);
161}
162
163void printInternal(PrintStream& out, unsigned long long value)
164{
165 out.printf("%llu", value);
166}
167
168void printInternal(PrintStream& out, float value)
169{
170 out.print(static_cast<double>(value));
171}
172
173void printInternal(PrintStream& out, double value)
174{
175 out.printf("%lf", value);
176}
177
178void printInternal(PrintStream& out, RawPointer value)
179{
180 out.printf("%p", value.value());
181}
182
183void dumpCharacter(PrintStream& out, char value)
184{
185 out.printf("%c", value);
186}
187
188} // namespace WTF
189
190