File: avm_output.cpp

package info (click to toggle)
avifile 1%3A0.7.48~20090503.ds-19
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 8,264 kB
  • sloc: cpp: 57,239; ansic: 56,968; sh: 5,370; perl: 1,548; makefile: 1,263; asm: 569; awk: 234
file content (230 lines) | stat: -rw-r--r-- 5,407 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "avm_output.h"
#include "avm_stl.h"
#include "avm_locker.h"
#include "avm_map.h"
#include "avm_cpuinfo.h"

#include <string.h>
#include <stdio.h>
#include <errno.h>

// ALL STATIC OBJECTS are defined HERE

avm::CPU_Info freq; // will be initialized first thing

AVM_BEGIN_NAMESPACE;

#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || __GNUC__ >= 5
typename AvmOutput::AvmOutput* AvmOutput::m_pSelf = 0;
#else
AvmOutput::AvmOutput* AvmOutput::m_pSelf = 0;
#endif
int AvmOutput::m_iLevel = 0; // debug level

struct AvmOutput::AvmOutputPrivate
{
    struct Less {
	bool operator()(const char* p1, const char* p2) const;
    };
    struct Equal {
	bool operator()(const char* p1, const char* p2) const;
	bool operator()(const char* p1, int p2) const;
    };
    avm::string m_sString;
    const char* m_sCurrentMode;
    char tmps[1024];
    PthreadMutex m_Mutex;
    int m_iDebugLevel;
    avm::avm_map<const char*, int, Less, Equal> m_sMap;
};

AvmOutput::AvmOutput() : priv(0)
{
    assert(m_pSelf == 0);
    m_pSelf = this;
    resetDebugLevels();

    // static initilization - runs only once
    freq.Init();
}

AvmOutput::~AvmOutput()
{
    delete priv;
    m_pSelf = 0;
}

void AvmOutput::createAvmOutput()
{
    new AvmOutput();
}

#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) || __GNUC__ >= 5
typename AvmOutput::AvmOutput* AvmOutput::singleton()
#else
AvmOutput::AvmOutput* AvmOutput::singleton()
#endif
{
    assert(m_pSelf != 0);
    return m_pSelf;
}

void AvmOutput::setDebugLevel(const char* mode, int level)
{
    if (mode)
	*priv->m_sMap.find_insert(mode) = level;
}

// referenced internaly
void AvmOutput::vwrite(const char* format, va_list va)
{
    vsnprintf(priv->tmps, sizeof(priv->tmps)-1, format, va);
    priv->m_sString += priv->tmps;
    if (errno && (priv->m_iDebugLevel & AVML_STRERROR)) {
	priv->m_sString += " ";
	priv->m_sString += strerror(errno);
    }
    flush();
}

void AvmOutput::vwrite(const char* mode, const char* format, va_list va)
{
    Locker locker(priv->m_Mutex);
    priv->m_sCurrentMode = mode;
    priv->m_iDebugLevel = 0;
    vwrite(format, va);
}

void AvmOutput::vwrite(const char* mode, int debuglevel, const char* format, va_list va)
{
    Locker locker(priv->m_Mutex);
    priv->m_sCurrentMode = mode;
    priv->m_iDebugLevel = debuglevel;
    vwrite(format, va);
}
/*
void AvmOutput::write(const char* format, ...)
{
//    if(m_iDebugLevel<=m_iWriterDebugLevel)
//	return;
    m_iDebugLevel=0;
    m_sCurrentMode="";
    va_list va;
    va_start(va, format);
    vwrite(format, va);
    va_end(va);
    if((m_sString.size()>80) || (m_sString.find('\n')!=string::npos))
	flush();
}
*/
void AvmOutput::write(const char* mode, const char* format, ...)
{
//    if(m_iDebugLevel<=m_iWriterDebugLevel)
//	return;
    Locker locker(priv->m_Mutex);
    va_list va;
//    string sOldMode=m_sCurrentMode;
    priv->m_sCurrentMode = mode;
    priv->m_iDebugLevel = 0;
    va_start(va, format);
    vwrite(format, va);
    va_end(va);
//    m_sCurrentMode=sOldMode;
}
/*
void AvmOutput::write(int debuglevel, const char* format, ...)
{
//    if(m_iDebugLevel<=debuglevel)
//	return;
    m_sCurrentMode="";
    m_iDebugLevel=debuglevel;
    va_list va;
    va_start(va, format);
    vwrite(format, va);
    va_end(va);
    if((m_sString.size()>80) || (m_sString.find('\n')!=string::npos))
	flush();
}
*/
void AvmOutput::write(const char* mode, int debuglevel, const char* format, ...)
{
//    if(m_iDebugLevel<=debuglevel)
//	return;
    va_list va;
    Locker locker(priv->m_Mutex);
//    string sOldMode=m_sCurrentMode;
    priv->m_sCurrentMode = mode;
    priv->m_iDebugLevel = debuglevel;
    va_start(va, format);
    vwrite(format, va);
    va_end(va);
//    m_sCurrentMode=sOldMode;
}

void AvmOutput::resetDebugLevels(int level)
{
    if (!priv)
	priv = new AvmOutput::AvmOutputPrivate();
    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++)
	i->value=level;
}

void AvmOutput::flush()
{
    if (!priv->m_sString.size())
	return;
    int p = *priv->m_sMap.find_default(priv->m_sCurrentMode);
    //printf("PPP: %d  leve %d\n", p, priv->m_iDebugLevel);
    if (p >= priv->m_iDebugLevel)
    {
	printf("<%s> : %s", priv->m_sCurrentMode, priv->m_sString.c_str());
	if (!priv->m_sString.find('\n'))
	    puts("");
    }
    priv->m_sString.erase();
}

bool AvmOutput::AvmOutputPrivate::Less::operator()(const char* p1, const char* p2) const
{
    if(!p1)
	return true;
    if(!p2)
	return false;
    return strcmp(p1, p2);
}

bool AvmOutput::AvmOutputPrivate::Equal::operator()(const char* p1, const char* p2) const
{
    //printf("P1: %p %s\n", p1, p1); printf("P2: %p %s\n", p2, p2); printf("done\n");
    if (!p1)
	return (!p2);
    if (!p2)
	return false;
    return strcmp(p1, p2);
}

bool AvmOutput::AvmOutputPrivate::Equal::operator()(const char* p1, int p2) const
{
    assert(!p2);
    if (!p1)
	return true;
    return (p1[0]==0);
}

AVM_END_NAMESPACE;

void avm_printf(const char* mode, const char* format, ...)
{
    va_list va;
    va_start(va, format);
    avm::AvmOutput()->vwrite(mode, format, va);
    va_end(va);
}

void avm_dprintf(const char* mode, int debuglevel, const char* format, ...)
{
    va_list va;
    va_start(va, format);
    avm::AvmOutput()->vwrite(mode, debuglevel, format, va);
    va_end(va);
}