tags:

views:

71

answers:

1

I have a C++ application and a Java application that need to log messages in the same way. My Java application uses Apache Commons Logging, backed by a Log4j configuration. I need a single log4j configuration so I can change my logging preferences in one location. In my C++ application, I have captured all calls to printf() and fprintf(std***) and am thinking I have the following options:

  1. Fork in my C++ app, create a pipe from (f)printf() calls to the new processes stdin, and start a Java program that reads from stdin and logs using Commons Logging

  2. Create a JVM in the C++ app using JNI's JNI_CreateJVM() and invoke a Java logging method when the (f)printf() calls are made

  3. Use something like Log4cxx to read the same configuration as the Java app and log natively in C++

I'd like to avoid option 3 as much as possible because I don't want to add another third-party dependency to my applications. I understand there is a performance cost to crossing from C++ to Java, but I'm not sure if it will matter that much.

+2  A: 

In addition to the performance cost, anything except option 3 is also horribly complicated (*). Also, I am not sure that there is a Java library that reads an InputStream and transforms it into Commons Logging calls. Even if there is, in order to be able to control filtering completely with the Java-side configuration, you would need to log everything at trace level into stdout (because the C++ code does not know about the configured log levels), which also sounds excessive .

Go with Log4cxx, or make some C++ code that can read the configuration file yourself.

(*) Okay, option 4 (have a wrapper script that redirects stderr/stdout from your unmodified C++ program to a Java program that translates the output into log entries) would not be very complex.

Thilo
+1 for option 4--that's what I'd do, at least until it proves to be unworkable.
Drew Hall