Debian Codegraph @

lib/common/avm_output.cpp

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