mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / common / util / NdbOut.cpp
blob5069231f673fb0ff73f3db295e74f5215c066f96
1 /* Copyright (c) 2003-2005, 2008 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include <ndb_global.h>
18 #include <NdbOut.hpp>
19 #include <OutputStream.hpp>
21 static FileOutputStream ndbouts_fileoutputstream(stdout);
22 NdbOut ndbout(ndbouts_fileoutputstream);
24 static const char * fms[] = {
25 "%d", "0x%02x", // Int8
26 "%u", "0x%02x", // Uint8
27 "%d", "0x%04x", // Int16
28 "%u", "0x%04x", // Uint16
29 "%d", "0x%08x", // Int32
30 "%u", "0x%08x", // Uint32
31 "%lld", "0x%016llx", // Int64
32 "%llu", "0x%016llx", // Uint64
33 "%llu", "0x%016llx" // UintPtr
36 NdbOut&
37 NdbOut::operator<<(Int8 v) { m_out->print(fms[0+isHex],(int)v); return *this;}
38 NdbOut&
39 NdbOut::operator<<(Uint8 v) { m_out->print(fms[2+isHex],(int)v); return *this;}
40 NdbOut&
41 NdbOut::operator<<(Int16 v) { m_out->print(fms[4+isHex],(int)v); return *this;}
42 NdbOut&
43 NdbOut::operator<<(Uint16 v) { m_out->print(fms[6+isHex],(int)v); return *this;}
44 NdbOut&
45 NdbOut::operator<<(Int32 v) { m_out->print(fms[8+isHex], v); return *this;}
46 NdbOut&
47 NdbOut::operator<<(Uint32 v) { m_out->print(fms[10+isHex], v); return *this;}
48 NdbOut&
49 NdbOut::operator<<(Int64 v) { m_out->print(fms[12+isHex], v); return *this;}
50 NdbOut&
51 NdbOut::operator<<(Uint64 v) { m_out->print(fms[14+isHex], v); return *this;}
52 NdbOut&
53 NdbOut::operator<<(unsigned long int v) { return *this << (Uint64) v; }
55 NdbOut&
56 NdbOut::operator<<(const char* val){ m_out->print("%s", val ? val : "(null)"); return * this; }
57 NdbOut&
58 NdbOut::operator<<(const void* val){ m_out->print("%p", val); return * this; }
59 NdbOut&
60 NdbOut::operator<<(BaseString &val){ return *this << val.c_str(); }
62 NdbOut&
63 NdbOut::operator<<(float val){ m_out->print("%f", (double)val); return * this;}
64 NdbOut&
65 NdbOut::operator<<(double val){ m_out->print("%f", val); return * this; }
67 NdbOut& NdbOut::endline()
69 isHex = 0; // Reset hex to normal, if user forgot this
70 m_out->println("");
71 m_out->flush();
72 return *this;
75 NdbOut& NdbOut::flushline()
77 m_out->flush();
78 return *this;
81 NdbOut& NdbOut::setHexFormat(int _format)
83 isHex = (_format == 0 ? 0 : 1);
84 return *this;
87 NdbOut::NdbOut(OutputStream & out)
88 : m_out(& out)
90 isHex = 0;
93 NdbOut::~NdbOut()
97 void
98 NdbOut::print(const char * fmt, ...){
99 va_list ap;
100 char buf[1000];
102 va_start(ap, fmt);
103 if (fmt != 0)
104 BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
105 ndbout << buf;
106 va_end(ap);
109 void
110 NdbOut::println(const char * fmt, ...){
111 va_list ap;
112 char buf[1000];
114 va_start(ap, fmt);
115 if (fmt != 0)
116 BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
117 ndbout << buf << endl;
118 va_end(ap);
121 extern "C"
122 void
123 ndbout_c(const char * fmt, ...){
124 va_list ap;
125 char buf[1000];
127 va_start(ap, fmt);
128 if (fmt != 0)
129 BaseString::vsnprintf(buf, sizeof(buf)-1, fmt, ap);
130 ndbout << buf << endl;
131 va_end(ap);
134 FilteredNdbOut::FilteredNdbOut(OutputStream & out,
135 int threshold, int level)
136 : NdbOut(out) {
137 m_level = level;
138 m_threshold = threshold;
139 m_org = &out;
140 m_null = new NullOutputStream();
141 setLevel(level);
144 FilteredNdbOut::~FilteredNdbOut(){
145 delete m_null;
148 void
149 FilteredNdbOut::setLevel(int i){
150 m_level = i;
151 if(m_level >= m_threshold){
152 m_out = m_org;
153 } else {
154 m_out = m_null;
158 void
159 FilteredNdbOut::setThreshold(int i){
160 m_threshold = i;
161 setLevel(m_level);
165 FilteredNdbOut::getLevel() const {
166 return m_level;
169 FilteredNdbOut::getThreshold() const {
170 return m_threshold;