A method for outputting local code logs to Java-side log files

Through the JNA framework, a single log file output from local code logs to the Java side is implemented in Java applications, which solves the problem of many log output files in Java applications and improves the efficiency of log viewing and problem solving.

CN113590135BActive Publication Date: 2025-01-03青岛本原微电子有限公司
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110660796.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-06-15
Publication Date
2025-01-03
Estimated Expiration
2041-06-15

AI Technical Summary

Technical Problem

In Java applications, the log output of local code (C/C++) can only generate two log files, resulting in inefficient efficiency in subsequent log viewing and problem solving, and two files need to be compared at the same time.

Method used

Through the JNA framework, the C/C++ side calls back the log output method of the Java side, uses the Java side to output the log, and records the local code logs to the same log file. The specific implementation includes the Java side declaring the log output interface, the C/C++ side declares the function pointer variable, the Java side converts the log output method into a function pointer and passes it to the C/C++ side, and calls the Java side's log output through the function pointer on the C/C++ side method.

Benefits of technology

It implements the output of local code logs to a single log file on the Java side, improving the efficiency of log viewing and problem solving.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113590135B_ABST
    Figure CN113590135B_ABST
Patent Text Reader

Abstract

The invention discloses a method for outputting a local code log to a Java end log file. The Java end declares a log output interface, which includes a unique log output method and is used for a C / C++ end to call back and realize a log output function; the C / C++ end declares a function pointer and a function pointer variable for storing and calling the log output method transmitted from the Java end; the Java end creates an implementation object of the log output interface, and JNA converts the log output method into a function pointer, transmits it to the C / C++ end, and stores it in the function pointer variable; log information is passed in through the function pointer, the Java end log output method is called, and the local code log is output to the Java end log file; based on the JNA framework, the invention calls back a Java end method through a C / C++ end code, outputs a log with the help of the Java end, and records the log information in the same log file.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of computers and technology, and in particular, relates to a method for outputting a local code log to a Java-side log file. Background Art

[0002] In some Java applications, you need to write some native code (C / C++) to complete the necessary work.

[0003] The execution of the code can be recorded through the output log, which is convenient for finding key information and quickly locating and solving problems later. However, only two log files can be generated. To view the logs later, you need to compare the two files at the same time, which makes it difficult to find and solve the problem. Summary of the invention

[0004] The purpose of the present invention is to provide a method for outputting local code logs to a Java-side log file. Based on the JNA (Java Native Access) framework, the Java-side method is called back by the C / C++-side code, and the log is output by the Java side, so that the log information is recorded in the same log file, thereby improving the efficiency of subsequent log viewing and problem solving.

[0005] The present invention is implemented by the following technical solutions:

[0006] A method for outputting local code logs to a Java-side log file is proposed, comprising: declaring a log output interface on the Java side, the log output interface including a unique log output method; the log output method is used to be called back by the local C / C++ side to implement the log output function; the C / C++ side declares a function pointer and a function pointer variable for storing and calling the log output method transmitted from the Java side; the Java side creates an implementation object of the log output interface, converts the log output method into a function pointer through JNA, transmits it to the C / C++ side and stores it in the function pointer variable; the C / C++ side calls the log output method of the Java side through the function pointer, and outputs the local code log to the log file of the Java side.

[0007] Furthermore, the method also includes: when declaring the log output interface on the Java side, a JNA interactive interface including at least one interface transfer method is also declared, and the interface transfer method is used to transfer the log output interface to the native code C / C++ side through JNA.

[0008] Furthermore, the method also includes: when the C / C++ side declares the function pointer variable, defining a receiving function for initializing the function pointer variable, and the receiving function assigns its parameters to the function pointer variable, thereby saving the log output method passed from the Java side to the C / C++ side through JNA.

[0009] Furthermore, the method also includes: the Java side creates an implementation object of the log output interface, calls the interface transfer method, and passes the implementation object as a parameter; calls the receiving function of the C / C++ side through JNA, and saves the log output method on the local side of the C / C++ side.

[0010] Compared with the prior art, the advantages and positive effects of the present invention are as follows: in the method for outputting local code logs to a Java-side log file proposed by the present invention, a log output interface is declared on the Java side, and the log output interface includes a unique log output method, and the log output method is used to be called back by the local C / C++ side to realize the log output function; the C / C++ side declares a function pointer and a function pointer variable for storing and calling the log output method transmitted from the Java side; the Java side creates an implementation object of the log output interface, and converts the log output method into a function pointer through JNA, passes it to the C / C++ side and stores it in the function pointer variable; when the C / C++ side needs to output the log, the log information is passed in through the function pointer, the log output method of the Java side is called, and the local code log is output to the log file of the Java side; based on the JNA framework, the present invention calls back the Java-side log output method through the C / C++ side code, and outputs the log with the help of the Java side, thereby recording the log information in the same log file, thereby improving the efficiency of subsequent log viewing and problem solving.

[0011] Other features and advantages of the present invention will become more apparent after reading the detailed description of the embodiments of the present invention in conjunction with the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] Figure 1 A flow chart of the method for outputting local code logs to Java-side log files proposed by the present invention;

[0013] Figure 2 It is a schematic diagram of the Java-side and C / C++-side programs in an embodiment of the present invention. DETAILED DESCRIPTION

[0014] The specific implementation modes of the present invention are further described in detail below in conjunction with the accompanying drawings.

[0015] like Figure 1 As shown, the method for outputting local code logs to Java-side log files proposed by the present invention includes:

[0016] Step S1: The Java side declares a log output interface, which includes a unique log output method.

[0017] The Java side declares a log output interface inherited from com.sun.jna.Callback. The log output interface contains a unique log output method, which is used to be called back by the native code C / C++ side to implement the log output function. Its parameters are log-related information.

[0018] At the same time, the Java side declares a JNA interactive interface inherited from com.sun.jna.Library, which includes at least one interface transfer method, which is used to transfer the implementation object of the above log output interface to the native code C / C++ segment through JNA, and the parameter of the interface transfer method is the above log output interface.

[0019] Step S2: The C / C++ side declares a function pointer and a function pointer variable for storing and calling the log output method passed from the Java side.

[0020] Declare a function pointer to call the Java-side log output method. Its parameters and return values ​​correspond to the above log output method. The corresponding rules can refer to the JNA default type mapping.

[0021] Declare a function pointer variable to save and call the log output method passed from the Java side.

[0022] When declaring the function pointer variable on the C / C++ side, a receiving function is defined to initialize the function pointer variable. The function name of the receiving function is the same as the above interface transfer method, and the parameter type is the above function pointer. The receiving function assigns its parameter to the function pointer variable, thereby saving the log output method passed from the Java side to the C / C++ side through JNA.

[0023] Step S3: The Java side creates an implementation object of the log output interface, converts the log output method into a function pointer through JNA, passes it to the C / C++ side and saves it in the function pointer variable.

[0024] On the Java side, create an implementation object of the log output interface and call the above interface transfer method to pass the implementation object as a parameter.

[0025] JNA converts the implementation object passed by the Java side into a function pointer and passes it to the native code side. The C / C++ side saves the function pointer passed by JNA to the native C / C++ side in the above-mentioned function pointer variable through the above-mentioned receiving function.

[0026] Step S4: The C / C++ side calls the Java side's log output method through a function pointer, and outputs the local code log to the Java side's log file.

[0027] Combination Figure 2 As shown, the present invention provides a specific embodiment of the method for outputting the proposed local code log to the Java-side log file.

[0028] In this embodiment, the operating platform is Windows 7, the Java-side log output framework uses Log4j, and the local code side is developed in C / C++ language and compiled into a .dll file.

[0029] 1) Declare the log output interface on the Java side.

[0030] The log output interface inherits from com.sun.jna.Callback and contains the only log output method. The parameters of the log output method are the log information level and the specific log content, and the return value is empty.

[0031] 2) Declare the JNA interaction interface on the Java side.

[0032] The JNA interactive interface inherits from com.sun.jna.Library. The interface transfer method is declared in the JNA interactive interface to pass the log output interface declared in 1) to the C / C++ end code through JNA. The parameter is the log output interface.

[0033] 3) C / C++ declares a function pointer type, with the parameters being the log information level and the specific log content, and its return value being void. Also, declares a global variable of the above type to save and call the log output method passed from the Java side.

[0034] 4) Define a receiving function on the C / C++ side to initialize the function pointer variable declared in 3). The function name is the same as the interface transfer method in 2), and the parameter type is the function pointer in 3). In the receiving function, assign the parameter to the global variable declared in 3).

[0035] 5) On the Java side, create an anonymous class that implements the log output interface in 1). In the log output method, use the Log4j framework to output the log information to the .log log file. Call the interface transfer method in 2), use the anonymous class as its parameter, call the receiving function in 4) through JNA, and save the log output method on the local side.

[0036] 6) When log output is required on the C / C++ side, determine whether the function pointer is empty, pass in the log information level and the specific log content as parameters, call the Java side log output method through the function pointer, and output the log to the log file on the Java side.

[0037] It should be pointed out that the above description is not a limitation of the present invention, and the present invention is not limited to the above examples. Changes, modifications, additions or substitutions made by ordinary technicians in this technical field within the essential scope of the present invention should also fall within the protection scope of the present invention.

Claims

1. A method for outputting local code logs to Java-side log files, characterized in that: include: Step S1: The Java side declares a log output interface, which contains a unique log output method; The Java side declares the log output interface inherited from com.sun.jna.Callback. The log output interface contains a unique log output method, which is used to be called back by the native code C / C++ side to implement the log output function. Its parameters are log related information. At the same time, the Java side declares a JNA interactive interface inherited from com.sun.jna.Library, and the JNA interactive interface includes at least one interface transfer method, which is used to transfer the implementation object of the above log output interface to the native code C / C++ segment through JNA, and the parameter of the interface transfer method is the above log output interface; Step S2: The C / C++ side declares a function pointer and a function pointer variable for storing and calling the log output method passed from the Java side; Declare a function pointer to call the Java-side log output method. Its parameters and return values ​​correspond to the above log output method. The corresponding rules refer to the JNA default type mapping. Declare a function pointer variable to save and call the log output method passed from the Java end; When declaring the function pointer variable on the C / C++ side, a receiving function is defined to initialize the function pointer variable. The function name of the receiving function is the same as the above interface transfer method, and the parameter type is the above function pointer. The receiving function assigns its parameter to the function pointer variable, thereby saving the log output method passed from the Java side to the C / C++ side through JNA. Step S3: The Java side creates an implementation object of the log output interface, converts the log output method into a function pointer through JNA, passes it to the C / C++ side and saves it in the function pointer variable; On the Java side, create an implementation object of the log output interface and call the above interface transfer method to pass the implementation object as a parameter; JNA converts the implementation object passed by the Java side into a function pointer and passes it to the native code side. The C / C++ side saves the function pointer passed by JNA to the native C / C++ side in the above-mentioned function pointer variable through the above-mentioned receiving function. Step S4: The C / C++ side calls the Java side's log output method through a function pointer, and outputs the local code log to the Java side's log file.