File: log.h

package info (click to toggle)
android-file-transfer 4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,496 kB
  • sloc: cpp: 12,909; python: 140; lex: 47; xml: 26; sh: 13; makefile: 6
file content (128 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download | duplicates (3)
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
/*
    This file is part of Android File Transfer For Linux.
    Copyright (C) 2015-2020  Vladimir Menshakov

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License,
    or (at your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef AFTL_MTP_LOG_H
#define AFTL_MTP_LOG_H

#include <mtp/ByteArray.h>
#include <mtp/types.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>

//I love libstdc++!
inline std::ostream & operator << (std::ostream & stream, unsigned char v)
{
	stream << (mtp::u16)v;
	return stream;
}

namespace mtp
{
	class InputStream;

	inline std::ostream & operator << (std::ostream & stream, unsigned char v)
	{
		stream << (u16)v;
		return stream;
	}

	namespace impl
	{
		template<typename T, bool Hex>
		struct Format
		{
			T			Value;
			unsigned	Width;
			Format(const T & value, unsigned width = 0): Value(value), Width(width) { }

			template<typename Stream>
			Stream & operator >> (Stream & stream) const
			{
				std::ios::fmtflags oldFlags = stream.flags();
				char oldFill = stream.fill();
				if (Hex)
					stream << std::setw(Width) << std::setfill('0') << std::hex << Value;
				else
					stream << std::setw(Width) << std::setfill(' ') << Value;
				stream.flags(oldFlags);
				stream.fill(oldFill);
				return stream;
			}

			std::string ToString() const
			{
				std::stringstream ss;
				(*this) >> ss;
				return ss.str();
			}
		};
	}

	template<typename Stream, typename T, bool H>
	Stream & operator << (Stream &stream, const impl::Format<T, H> &format)
	{
		format >> stream;
		return stream;
	}

	template<typename T>
	impl::Format<T, true> hex(const T & value, unsigned width = 0)
	{ return impl::Format<T, true>(value, width); }

	template<typename T>
	impl::Format<T, false> width(const T & value, unsigned width)
	{ return impl::Format<T, false>(value, width); }

	inline void print()
	{ std::cout << std::endl; }

	template<typename ValueType, typename ... Args>
	void print(const ValueType & value, Args ... args)
	{
		std::cout << value;
		print(args...);
	}

	extern bool g_debug;

	inline void error()
	{ std::cerr << std::endl; }

	template<typename ValueType, typename ... Args>
	void error(const ValueType & value, Args ... args)
	{
		std::cerr << value;
		error(args...);
	}

	template<typename ValueType, typename ... Args>
	void debug(const ValueType & value, Args ... args)
	{
		if (g_debug)
			error(value, args...);
	}

	//! output hex dump to debug channel
	void HexDump(const std::string &prefix, const ByteArray &data, bool force = false);
	void HexDump(std::stringstream & ss, const std::string &prefix, size_t size, InputStream & is);
}

#endif