views:

219

answers:

6

I am implementing a log server in C++; that accepts log messages from a Java program (via log4j socket appender). How do I read these java logging objects in C++?

+7  A: 

You should configure the log4j appender to send XML format messages. Then it is simply a matter of reading XML in C++.

Stephen C
+2  A: 

Serialized java objects is a byte stream which need meta information from the Java Runtime to be able to reconstruct the java objects. Without that meta information available in the system you must add that information yourself, which is tedious and error prone. I second the idea of sending XML instead - that is what XML serialization was invented for :)

Thorbjørn Ravn Andersen
A: 

JSON! JSON! JSON! JSON!

DevDevDev
log4j does not directly have a JSON appender.
Thorbjørn Ravn Andersen
I think they should use JSON.
drewh
A: 

json is the best way to go for this kind of problems.

A: 

Log4cxx is a Log4j port to C++, perhaps you can glean some ideas from that or even use it directly?

MadKeithV
+1  A: 

Another very fast way of language-agnostic serialization is protobuf. proto-files (meta-files that describe your data-structures) are compiled using protoc which writes IO-code for various target languages.

I'm using it in my app and did some benchmarking which might give you a clue if it serves your purpose. The only downside I'm aware of is that protobuf does not handle references at all. If one of your objects contain the same object twice it will be written twice instead of just once with a reference to the previous instance (which is the case with Java serialization).

Concerning your original question, I agree with Thorbjørn that reading and writing of serialized Java objects will be too hard and error prone.

If you consider going the protobuf way, feel free to use this logging event protobuf file as a starter.

Huxi