summaryrefslogtreecommitdiffstats
path: root/debian/mp4v2/mp4v2-2.0.0~dfsg0/src/log.cpp
blob: 26eea6b5d5c535dd7f870d7f9f8f3d2bd8e59c76 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
///////////////////////////////////////////////////////////////////////////////
//
//  The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in
//  compliance with the License. You may obtain a copy of the License at
//  http://www.mozilla.org/MPL/
//
//  Software distributed under the License is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//  License for the specific language governing rights and limitations
//  under the License.
// 
//  The Original Code is MP4v2.
// 
//  The Initial Developer of the Original Code is David Byron.
//  Portions created by David Byron are Copyright (C) 2009, 2010, 2011.
//  All Rights Reserved.
//
//  Contributors:
//      David Byron, [email protected]
//
///////////////////////////////////////////////////////////////////////////////

#include <iomanip>
#include <iostream>
#include "src/impl.h"

namespace mp4v2 { namespace impl {

MP4LogCallback Log::_cb_func = NULL;

// There's no mechanism to set the log level at runtime at
// the moment so construct this so it only logs important
// stuff.
Log log(MP4_LOG_WARNING);

///////////////////////////////////////////////////////////////////////////////

/**
 * Log class constructor
 */
Log::Log( MP4LogLevel verbosity_ /* = MP4_LOG_NONE */ )
    : _verbosity ( verbosity_ )
    , verbosity  ( _verbosity )
{
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log class destructor
 */
Log::~Log()
{
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Mutator for the callback function
 *
 * @param value the function to call
 */
void
Log::setLogCallback( MP4LogCallback value )
{
    Log::_cb_func = value;
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Mutator for the verbosity
 *
 * @param value the verbosity to use
 */
void
Log::setVerbosity( MP4LogLevel value )
{
    _verbosity = value;
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log an error message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::errorf( const char* format,
             ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_ERROR,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a warning message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::warningf( const char* format,
               ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_WARNING,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log an info message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::infof( const char* format,
            ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_INFO,format,ap);
    va_end(ap);
}

/**
 * Log a verbose1 message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::verbose1f( const char* format,
                ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_VERBOSE1,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a verbose2 message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::verbose2f( const char* format,
                ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_VERBOSE2,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a verbose3 message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::verbose3f( const char* format,
                ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_VERBOSE3,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a verbose4 message
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::verbose4f( const char* format,
                ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(MP4_LOG_VERBOSE4,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Dump info to the console or a callback
 *
 * @param indent the number of spaces to indent the info
 *
 * @param verbosity the level of detail the message contains
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::dump ( uint8_t       indent,
            MP4LogLevel   verbosity_,
            const char*   format, ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vdump(indent,verbosity,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Dump info if it has appropriate verbosity, either to
 * standard out (with a newline appended to @p format) or to
 * the callback function (with no newline appended).
 *
 * @param indent the number of spaces to indent the info
 *
 * @param verbosity the level of detail the message contains
 *
 * @param format the format string to use to process @p ap.
 * @p format should not contain a newline.
 *
 * @param ap varargs to build the message
 */
void
Log::vdump( uint8_t     indent,
            MP4LogLevel verbosity_,
            const char* format,
            va_list     ap )
{
    // Make sure nothing gets logged with MP4_LOG_NONE.
    // That way people who ask for nothing to get logged
    // won't get anything logged.
    ASSERT(verbosity_ != MP4_LOG_NONE);
    ASSERT(format);
    ASSERT(format[0] != '\0');

    if (verbosity_ > this->_verbosity)
    {
        // We're not set verbose enough to log this
        return;
    }

    if (Log::_cb_func)
    {
        ostringstream   new_format;

        if (indent > 0)
        {
            string      indent_str(indent,' ');
            // new_format << setw(indent) << setfill(' ') << "" << setw(0);
            // new_format << format;
            new_format << indent_str << format;
            Log::_cb_func(verbosity_,new_format.str().c_str(),ap);
            return;
        }

        Log::_cb_func(verbosity_,format,ap);
        return;
    }

    // No callback set so log to standard out.  
    if (indent > 0)
    {
        ::fprintf(stdout,"%*c",indent,' ');
    }
    ::vfprintf(stdout,format,ap);
    ::fprintf(stdout,"\n");
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a message
 *
 * @param verbosity the level of detail the message contains
 *
 * @param format the format string to use to process the
 * remaining arguments.  @p format should not contain a
 * newline.
 */
void
Log::printf( MP4LogLevel        verbosity,
             const char*        format,
             ... )
{
    va_list     ap;

    va_start(ap,format);
    this->vprintf(verbosity,format,ap);
    va_end(ap);
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a message if it has appropriate verbosity, either to
 * standard out (with a newline appended to @p format) or to
 * the callback function (with no newline appended).
 *
 * @param verbosity the level of detail the message contains
 *
 * @param format the format string to use to process @p ap.
 * @p format should not contain a newline.
 *
 * @param ap varargs to build the message
 */
void
Log::vprintf( MP4LogLevel       verbosity_,
              const char*       format,
              va_list           ap )
{
    // Make sure nothing gets logged with MP4_LOG_NONE.
    // That way people who ask for nothing to get logged
    // won't get anything logged.
    ASSERT(verbosity_ != MP4_LOG_NONE);
    ASSERT(format);

    if (verbosity_ > this->_verbosity)
    {
        // We're not set verbose enough to log this
        return;
    }

    if (Log::_cb_func)
    {
        Log::_cb_func(verbosity_,format,ap);
        return;
    }

    // No callback set so log to standard out.  
    ::vfprintf(stdout,format,ap);
    ::fprintf(stdout,"\n");
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log a buffer as ascii-hex
 *
 * @param indent the number of spaces to indent the buffer
 *
 * @param verbosity the level of detail the message contains
 *
 * @param pBytes the buffer to log
 *
 * @param numBytes the number of bytes to log
 *
 * @param format the format string to use to process the
 * remaining arguments, where the format + remaining args
 * describe @p pBytes.  The resulting string should not
 * contain a newline.  Only the first 255 characters of the
 * resulting string (not including the NUL terminator) make
 * it to the log callback or stdout.
 */
void
Log::hexDump( uint8_t           indent,
              MP4LogLevel       verbosity_,
              const uint8_t*    pBytes,
              uint32_t          numBytes,
              const char*       format,
              ... )
{
    va_list     ap;

    ASSERT(pBytes || (numBytes == 0));
    ASSERT(format);

    if (verbosity_ > this->_verbosity)
    {
        // We're not set verbose enough to log this
        return;
    }

    // Build the description by processing format and the
    // remaining args.  Since we don't have asprintf, pick
    // an arbitrary length for the string and use snprintf.
    // To save a memory allocation, only do this if there's
    // a non-empty format string or non-zero indent
    char *desc = NULL;
    if (format[0] || indent)
    {
        desc = (char *)MP4Calloc(256 + indent);
        sprintf(desc,"%*c",indent,' ');
        va_start(ap,format);
        vsnprintf(desc + indent,255,format,ap);
        va_end(ap);
    }

    // From here we can use the C++ standard lib classes and
    // build a string for each line
    for (uint32_t i = 0;(i < numBytes);i += 16)
    {
        // ios_base::ate means at end.  With out this desc
        // gets overwritten with each << operation
        ostringstream oneLine(desc ? desc : "",ios_base::ate);

        // Append the byte offset this line starts with as
        // an 8 character, leading 0, hex number.  Leave the
        // fill character set to 0 for the remaining
        // operations
        oneLine << ':' << hex << setw(8) << setfill('0') <<
            std::right << i << setw(0) << setfill(' ') << ": ";

        uint32_t curlen = min((uint32_t)16,numBytes - i);
        const uint8_t *b = pBytes + i;
        uint32_t j;

        for (j = 0;(j < curlen);j++)
        {
            oneLine << hex << setw(2) << setfill('0') << right << static_cast<uint32_t>(b[j]);
            oneLine << setw(0) << setfill(' ') << ' ';
        }

        for (; j < 16; j++)
        {
            oneLine << "   ";
        }

        b = pBytes + i;
        for (j = 0;(j < curlen);j++)
        {
            if (isprint(static_cast<int>(b[j])))
            {
                oneLine << static_cast<char>(b[j]);
            }
            else
            {
                oneLine << '.';
            }
        }

        // We can either call the callback directly or use
        // the Log::printf function.  To call the callback
        // directly, we need a va_list.  (I think) we need
        // and extra function call to build that, so we may
        // as well call Log::printf.  It's going to
        // double-check the verbosity and the callback
        // function pointer, but that seems OK (13-feb-09,
        // dbyron)
        this->printf(verbosity_,"%s",oneLine.str().c_str());
    }

    if (desc)
    {
        MP4Free(desc);
        desc = NULL;
    }
}

///////////////////////////////////////////////////////////////////////////////

/**
 * Log an Exception as an error
 *
 * @param x the exception to log
 */
void
Log::errorf ( const Exception&      x )
{
    this->printf(MP4_LOG_ERROR,"%s",x.msg().c_str());
}

///////////////////////////////////////////////////////////////////////////////

}} // namespace mp4v2::impl

using namespace mp4v2::impl;

extern "C"
void MP4SetLogCallback( MP4LogCallback cb_func )
{
    Log::setLogCallback(cb_func);
}

extern "C"
MP4LogLevel MP4LogGetLevel(void)
{
    return mp4v2::impl::log.verbosity;
}

extern "C"
void MP4LogSetLevel( MP4LogLevel verbosity )
{
    try
    {
        mp4v2::impl::log.setVerbosity(verbosity);
    }
    catch( Exception* x ) {
        mp4v2::impl::log.errorf(*x);
        delete x;
    }
    catch( ... ) {
        mp4v2::impl::log.errorf( "%s: failed", __FUNCTION__ );
    }
}