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:
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
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
Use something like Log4cxx to read the s开发者_运维百科ame 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.
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.
精彩评论