lib: show offset and rectype in HexDumpParser
[barry.git] / src / common.cc
blob22e414896ff02cf584bdda325d0d7464daeba1c5
1 ///
2 /// \file common.cc
3 /// General Barry interface routines
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "common.h"
23 #include <usb.h>
24 #include <pthread.h>
25 #include "debug.h"
27 namespace Barry {
29 bool __data_dump_mode__;
31 std::ostream *LogStream = &std::cout;
32 pthread_mutex_t LogStreamMutex;
36 // Init
38 /// Barry library initializer. Call this before anything else.
39 /// This takes care of initializing the lower level libusb.
40 ///
41 /// This function is safe to be called multiple times. The
42 /// data_dump_mode and the log stream will be updated each time
43 /// it is called, but the USB library will not be re-initialized.
44 ///
45 /// \param[in] data_dump_mode If set to true, the protocol conversation
46 /// will be sent to the logStream specified
47 /// in the second argument.
48 /// \param[in] LogStream Pointer to std::ostream object to use for
49 /// debug output and logging. Defaults to
50 /// std::cout.
51 ///
52 void Init(bool data_dump_mode, std::ostream *logStream)
54 static bool initialized = false;
56 // set usb debug mode first, so that USB's initialization
57 // is captured too
58 if( data_dump_mode )
59 usb_set_debug(9);
61 // perform one-time initalization
62 if( !initialized ) {
63 // if the environment variable USB_DEBUG is set, that
64 // level value will be used instead of our 9 above...
65 // if you need to *force* this to 9, call Verbose(true)
66 // after Init()
67 usb_init();
69 // only need to initialize this once
70 pthread_mutex_init(&LogStreamMutex, NULL);
72 // done
73 initialized = true;
76 __data_dump_mode__ = data_dump_mode;
77 LogStream = logStream;
81 // Verbose
83 /// This API call lets the application enable / disable verbose debug
84 /// output on the fly.
85 ///
86 /// \param[in] data_dump_mode If set to true, the protocol conversation
87 /// will be sent to the logStream specified
88 /// in the Barry::Init() call.
89 ///
90 void Verbose(bool data_dump_mode)
92 __data_dump_mode__ = data_dump_mode;
94 if( data_dump_mode )
95 usb_set_debug(9);
96 else
97 usb_set_debug(0);
101 // IsVerbose
103 /// Returns true if data dump mode is enabled.
105 bool IsVerbose()
107 return __data_dump_mode__;
110 } // namespace Barry