C ++ object for string

I would like to output the contents of a C ++ object (e.g. a circular buffer implementation) to a logging string. I planned to do this manually, but then I noticed that the running print object on my object in the GDB debugger gave me fine formatted output, for example:

(gdb) print audioDebugMessageQueue $1 = { writePointer = 1, readPointer = 0, keys = {{ samples = {0.155292124, 0.106764726}, timeStamp = 1322767911, numFrames = 1024 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }, { samples = {0, 0}, timeStamp = 0, numFrames = 0 }} } 

If GDB can access a pretty dump of my object, I guess I probably can too. But how? By the way, I'm on iOS.

+4
source share
2 answers

The reason gdb can give you this good conclusion is that if specified (the -g option for gcc), the compiler generates, in addition to the normal code, some additional information on how to interpret binary structures in your program. The debugger reads and interprets this additional information, and then when you print the object, it uses it to output your object. Please note that if you do not include debugging information in your files (i.e. if you do not use -g ), then gdb will not be able to give you such beautiful prints, even if you tell exactly where this variable is located (other information - debugging information contains).

Now you can write code to read this debugging information for your program, and then use it to display classes (or, if the license allows it, even use existing code for this). However, if you do not use it for so many different types (and it’s all right with the restriction that you have to compile with debugging information, which can help people reverse engineer your code), it is almost certainly easier to just write the hand print code.

Another option would, of course, be to install gdb and call it from your program to generate output. This is probably not what you would like to do, however (it will, however, work around any licensing issues).

+2
source

Write down stream insert statements for each of the classes in your class, and then for the class itself, and then for the container.

Considering

 class C { public: int a; sometype b; } typedef std::vector< C > Cvector; 

you need something like:

 template<class E, class T> std::basic_ostream<E,T> &operator <<( std::basic_ostream<E,T> &s, const sometype & a ) { /* ... */ } template<class E, class T> std::basic_ostream<E,T> &operator <<( std::basic_ostream<E,T> &s, const C& a_c ) { s << std::hex << std::setw(8) << a_c.a << std::endl << a_c.b; } template<class E, class T> std::basic_ostream<E,T> &operator <<( std::basic_ostream<E,T> &s, const Cvector & cvec ) { for ( iterator i = cvec.begin(); i != cvec.end(); ++i ) s << *i << std::endl; } 

[Consider this pseudo code ...]

+1
source

Source: https://habr.com/ru/post/1384192/


All Articles