MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdbOut.cpp
1 /*
2  Copyright (C) 2003-2006, 2008 MySQL AB, 2008-2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <ndb_global.h>
20 
21 #include <NdbOut.hpp>
22 #include <OutputStream.hpp>
23 
24 /* Initialized in ndb_init() */
25 NdbOut ndbout;
26 NdbOut ndberr;
27 
28 static const char * fms[] = {
29  "%d", "0x%02x", // Int8
30  "%u", "0x%02x", // Uint8
31  "%d", "0x%04x", // Int16
32  "%u", "0x%04x", // Uint16
33  "%d", "0x%08x", // Int32
34  "%u", "0x%08x", // Uint32
35  "%lld", "0x%016llx", // Int64
36  "%llu", "0x%016llx", // Uint64
37  "%llu", "0x%016llx" // UintPtr
38 };
39 
40 NdbOut&
41 NdbOut::operator<<(Int8 v) { m_out->print(fms[0+isHex],(int)v); return *this;}
42 NdbOut&
43 NdbOut::operator<<(Uint8 v) { m_out->print(fms[2+isHex],(int)v); return *this;}
44 NdbOut&
45 NdbOut::operator<<(Int16 v) { m_out->print(fms[4+isHex],(int)v); return *this;}
46 NdbOut&
47 NdbOut::operator<<(Uint16 v) { m_out->print(fms[6+isHex],(int)v); return *this;}
48 NdbOut&
49 NdbOut::operator<<(Int32 v) { m_out->print(fms[8+isHex], v); return *this;}
50 NdbOut&
51 NdbOut::operator<<(Uint32 v) { m_out->print(fms[10+isHex], v); return *this;}
52 NdbOut&
53 NdbOut::operator<<(Int64 v) { m_out->print(fms[12+isHex], v); return *this;}
54 NdbOut&
55 NdbOut::operator<<(Uint64 v) { m_out->print(fms[14+isHex], v); return *this;}
56 NdbOut&
57 NdbOut::operator<<(unsigned long int v) { return *this << (Uint64) v; }
58 
59 NdbOut&
60 NdbOut::operator<<(const char* val){ m_out->print("%s", val ? val : "(null)"); return * this; }
61 NdbOut&
62 NdbOut::operator<<(const void* val){ m_out->print("%p", val); return * this; }
63 NdbOut&
64 NdbOut::operator<<(BaseString &val){ return *this << val.c_str(); }
65 
66 NdbOut&
67 NdbOut::operator<<(float val){ m_out->print("%f", (double)val); return * this;}
68 NdbOut&
69 NdbOut::operator<<(double val){ m_out->print("%f", val); return * this; }
70 
71 NdbOut& NdbOut::endline()
72 {
73  isHex = 0; // Reset hex to normal, if user forgot this
74  m_out->println("%s", "");
75  if (m_autoflush)
76  m_out->flush();
77  return *this;
78 }
79 
80 NdbOut& NdbOut::flushline()
81 {
82  m_out->flush();
83  return *this;
84 }
85 
86 NdbOut& NdbOut::setHexFormat(int _format)
87 {
88  isHex = (_format == 0 ? 0 : 1);
89  return *this;
90 }
91 
92 NdbOut::NdbOut(OutputStream & out, bool autoflush)
93  : m_out(& out), isHex(0), m_autoflush(autoflush)
94 {
95 }
96 
97 NdbOut::NdbOut()
98  : m_out(NULL), isHex(0)
99 {
103 }
104 
105 NdbOut::~NdbOut()
106 {
111 }
112 
113 void
114 NdbOut::print(const char * fmt, ...){
115  va_list ap;
116  char buf[1000];
117 
118  va_start(ap, fmt);
119  if (fmt != 0)
120  BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
121  *this << buf;
122  va_end(ap);
123 }
124 
125 void
126 NdbOut::println(const char * fmt, ...){
127  va_list ap;
128  char buf[1000];
129 
130  va_start(ap, fmt);
131  if (fmt != 0)
132  BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
133  *this << buf << endl;
134  va_end(ap);
135 }
136 
137 extern "C"
138 void
139 vndbout_c(const char * fmt, va_list ap){
140  char buf[1000];
141 
142  if (fmt != 0)
143  {
144  BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
145  }
146  ndbout << buf << endl;
147 }
148 
149 extern "C"
150 void
151 ndbout_c(const char * fmt, ...){
152  va_list ap;
153 
154  va_start(ap, fmt);
155  vndbout_c(fmt, ap);
156  va_end(ap);
157 }
158 
159 extern "C" int ndbout_printer(const char * fmt, ...)
160 {
161  va_list ap;
162 
163  va_start(ap, fmt);
164  vndbout_c(fmt, ap);
165  va_end(ap);
166  return 1;
167 }
168 
169 
170 FilteredNdbOut::FilteredNdbOut(OutputStream & out,
171  int threshold, int level)
172  : NdbOut(out) {
173  m_level = level;
174  m_threshold = threshold;
175  m_org = &out;
176  m_null = new NullOutputStream();
177  setLevel(level);
178 }
179 
180 FilteredNdbOut::~FilteredNdbOut(){
181  delete m_null;
182 }
183 
184 void
185 FilteredNdbOut::setLevel(int i){
186  m_level = i;
187  if(m_level >= m_threshold){
188  m_out = m_org;
189  } else {
190  m_out = m_null;
191  }
192 }
193 
194 void
195 FilteredNdbOut::setThreshold(int i){
196  m_threshold = i;
197  setLevel(m_level);
198 }
199 
200 int
201 FilteredNdbOut::getLevel() const {
202  return m_level;
203 }
204 int
205 FilteredNdbOut::getThreshold() const {
206  return m_threshold;
207 }
208 
209 static FileOutputStream ndbouts_fileoutputstream(0);
210 static FileOutputStream ndberrs_fileoutputstream(0);
211 
212 void
213 NdbOut_Init()
214 {
215  new (&ndbouts_fileoutputstream) FileOutputStream(stdout);
216  new (&ndbout) NdbOut(ndbouts_fileoutputstream);
217 
218  new (&ndberrs_fileoutputstream) FileOutputStream(stderr);
219  new (&ndberr) NdbOut(ndberrs_fileoutputstream);
220 }