Debian Codegraph @

lib/common/avm_output.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #include "avm_output.h"
2 #include "avm_stl.h"
3 #include "avm_locker.h"
4 #include "avm_map.h"
5 #include "avm_cpuinfo.h"
6
7 #include <string.h>
8 #include <stdio.h>
9 #include <errno.h>
10
11 // ALL STATIC OBJECTS are defined HERE
12
13 avm::CPU_Info freq; // will be initialized first thing
14
15 AVM_BEGIN_NAMESPACE;
16
17 AvmOutput::AvmOutput* AvmOutput::m_pSelf = 0;
18 int AvmOutput::m_iLevel = 0; // debug level
19
20 struct AvmOutput::AvmOutputPrivate
21 {
22 struct Less {
23 bool operator()(const char* p1, const char* p2) const;
24 };
25 struct Equal {
26 bool operator()(const char* p1, const char* p2) const;
27 bool operator()(const char* p1, int p2) const;
28 };
29 avm::string m_sString;
30 const char* m_sCurrentMode;
31 char tmps[1024];
32 PthreadMutex m_Mutex;
33 int m_iDebugLevel;
34 avm::avm_map<const char*, int, Less, Equal> m_sMap;
35 };
36
37 AvmOutput::AvmOutput() : priv(0)
38 {
39 assert(m_pSelf == 0);
40 m_pSelf = this;
41 resetDebugLevels();
42
43 // static initilization - runs only once
44 freq.Init();
45 }
46
47 AvmOutput::~AvmOutput()
48 {
49 delete priv;
50 m_pSelf = 0;
51 }
52
53 void AvmOutput::createAvmOutput()
54 {
55 new AvmOutput();
56 }
57
58 AvmOutput::AvmOutput* AvmOutput::singleton()
59 {
60 assert(m_pSelf != 0);
61 return m_pSelf;
62 }
63
64 void AvmOutput::setDebugLevel(const char* mode, int level)
65 {
66 if (mode)
67 *priv->m_sMap.find_insert(mode) = level;
68 }
69
70 // referenced internaly
71 void AvmOutput::vwrite(const char* format, va_list va)
72 {
73 vsnprintf(priv->tmps, sizeof(priv->tmps)-1, format, va);
74 priv->m_sString += priv->tmps;
75 if (errno && (priv->m_iDebugLevel & AVML_STRERROR)) {
76 priv->m_sString += " ";
77 priv->m_sString += strerror(errno);
78 }
79 flush();
80 }
81
82 void AvmOutput::vwrite(const char* mode, const char* format, va_list va)
83 {
84 Locker locker(priv->m_Mutex);
85 priv->m_sCurrentMode = mode;
86 priv->m_iDebugLevel = 0;
87 vwrite(format, va);
88 }
89
90 void AvmOutput::vwrite(const char* mode, int debuglevel, const char* format, va_list va)
91 {
92 Locker locker(priv->m_Mutex);
93 priv->m_sCurrentMode = mode;
94 priv->m_iDebugLevel = debuglevel;
95 vwrite(format, va);
96 }
97 /*
98 void AvmOutput::write(const char* format, ...)
99 {
100 // if(m_iDebugLevel<=m_iWriterDebugLevel)
101 // return;
102 m_iDebugLevel=0;
103 m_sCurrentMode="";
104 va_list va;
105 va_start(va, format);
106 vwrite(format, va);
107 va_end(va);
108 if((m_sString.size()>80) || (m_sString.find('\n')!=string::npos))
109 flush();
110 }
111 */
112 void AvmOutput::write(const char* mode, const char* format, ...)
113 {
114 // if(m_iDebugLevel<=m_iWriterDebugLevel)
115 // return;
116 Locker locker(priv->m_Mutex);
117 va_list va;
118 // string sOldMode=m_sCurrentMode;
119 priv->m_sCurrentMode = mode;
120 priv->m_iDebugLevel = 0;
121 va_start(va, format);
122 vwrite(format, va);
123 va_end(va);
124 // m_sCurrentMode=sOldMode;
125 }
126 /*
127 void AvmOutput::write(int debuglevel, const char* format, ...)
128 {
129 // if(m_iDebugLevel<=debuglevel)
130 // return;
131 m_sCurrentMode="";
132 m_iDebugLevel=debuglevel;
133 va_list va;
134 va_start(va, format);
135 vwrite(format, va);
136 va_end(va);
137 if((m_sString.size()>80) || (m_sString.find('\n')!=string::npos))
138 flush();
139 }
140 */
141 void AvmOutput::write(const char* mode, int debuglevel, const char* format, ...)
142 {
143 // if(m_iDebugLevel<=debuglevel)
144 // return;
145 va_list va;
146 Locker locker(priv->m_Mutex);
147 // string sOldMode=m_sCurrentMode;
148 priv->m_sCurrentMode = mode;
149 priv->m_iDebugLevel = debuglevel;
150 va_start(va, format);
151 vwrite(format, va);
152 va_end(va);
153 // m_sCurrentMode=sOldMode;
154 }
155
156 void AvmOutput::resetDebugLevels(int level)
157 {
158 if (!priv)
159 priv = new AvmOutput::AvmOutputPrivate();
160 for(avm_map<const char*, int, AvmOutput::AvmOutputPrivate::Less, AvmOutput::AvmOutputPrivate::Equal>::const_iterator i = priv->m_sMap.begin(); i != priv->m_sMap.end(); i++)
161 i->value=level;
162 }
163
164 void AvmOutput::flush()
165 {
166 if (!priv->m_sString.size())
167 return;
168 int p = *priv->m_sMap.find_default(priv->m_sCurrentMode);
169 //printf("PPP: %d leve %d\n", p, priv->m_iDebugLevel);
170 if (p >= priv->m_iDebugLevel)
171 {
172 printf("<%s> : %s", priv->m_sCurrentMode, priv->m_sString.c_str());
173 if (!priv->m_sString.find('\n'))
174 puts("");
175 }
176 priv->m_sString.erase();
177 }
178
179 bool AvmOutput::AvmOutputPrivate::Less::operator()(const char* p1, const char* p2) const
180 {
181 if(!p1)
182 return true;
183 if(!p2)
184 return false;
185 return strcmp(p1, p2);
186 }
187
188 bool AvmOutput::AvmOutputPrivate::Equal::operator()(const char* p1, const char* p2) const
189 {
190 //printf("P1: %p %s\n", p1, p1); printf("P2: %p %s\n", p2, p2); printf("done\n");
191 if (!p1)
192 return (!p2);
193 if (!p2)
194 return false;
195 return strcmp(p1, p2);
196 }
197
198 bool AvmOutput::AvmOutputPrivate::Equal::operator()(const char* p1, int p2) const
199 {
200 assert(!p2);
201 if (!p1)
202 return true;
203 return (p1[0]==0);
204 }
205
206 AVM_END_NAMESPACE;
207
208 void avm_printf(const char* mode, const char* format, ...)
209 {
210 va_list va;
211 va_start(va, format);
212 avm::AvmOutput()->vwrite(mode, format, va);
213 va_end(va);
214 }
215
216 void avm_dprintf(const char* mode, int debuglevel, const char* format, ...)
217 {
218 va_list va;
219 va_start(va, format);
220 avm::AvmOutput()->vwrite(mode, debuglevel, format, va);
221 va_end(va);
222 }