Redirect std *** from C ++ to Java for logging

I have a C ++ application and a Java application that should write messages in the same way. My Java application uses Apache Commons Logging, which is supported by the Log4j configuration. I need one log4j configuration, so I can change my logging settings in one place. In my C ++ application, I fixed all printf () and fprintf (std ***) calls and I think I have the following parameters:

  • Fork in my C ++ application, create a pipe from (f) printf () calls for new stdin processes and run a Java program that reads from stdin and logs using Commons Logging

  • Create a JVM in a C ++ application using JNI JNI_CreateJVM () and call the Java logging method when (f) printf () is made

  • Use something like Log4cxx to read the same configuration as a Java application, and logically in C ++

I would like to avoid option 3 as much as possible, because I do not want to add another third-party application. I understand that there is performance for the transition from C ++ to Java, but I'm not sure if this will make a big difference.

+4
source share
1 answer

In addition to the cost of execution, nothing but option 3 is also terribly difficult (*). Also, I'm not sure if there is a Java library that reads an InputStream and converts it into Commons Logging calls. Even if there is, in order to be able to fully control filtering using the configuration on the Java side, you will need to write everything at the trace level to stdout (since C ++ code does not know about the configured log levels), which also sounds excessive.

Go with Log4cxx or create C ++ code that can independently read the configuration file.

(*) Well, option 4 (has a shell script that redirects stderr / stdout from your unmodified C ++ program to a Java program that converts the output to log entries) will not be very complicated.

+3
source

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


All Articles