Log printing method, electronic device and storage medium

By adding control information to Java programs to control log printing, the power consumption and overhead problem caused by log printing code blocks when users use their mobile phones, and the effect of improving mobile phone performance is achieved.

CN116521487BActive Publication Date: 2025-04-04HONOR DEVICE CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310766659.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-27
Publication Date
2025-04-04
Estimated Expiration
2043-06-27

AI Technical Summary

Technical Problem

In the development stage of Java programs, in order to analyze and locate problems, developers will add a large number of log printing code blocks to the source code, causing users to execute these code blocks when using the phone, resulting in excess power consumption and overhead and affecting the performance of the phone.

Method used

By adding control information (such as control code) to the program, when an event is detected, it decides whether to print the event based on the control information of the printing program corresponding to the event. For example, when the logical value of the first tag is set to False, the event is not printed directly, and the log printing code block and string splicing instructions are avoided.

Benefits of technology

It effectively avoids unnecessary power consumption and improves the performance of electronic devices, especially when the user does not need to print logs during use.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116521487B_ABST
    Figure CN116521487B_ABST
Patent Text Reader

Abstract

The present application provides a log printing method, an electronic device and a storage medium, and relates to the technical field of electronic devices. The log printing method can insert the control code of each log printing code block into the source code of the program, and the control code is executed before the log printing code block. In this way, when the electronic device used by the user runs the compiled program, the control code will be executed before the log printing code block is executed, and the control code will determine whether the log needs to be printed. When it is determined that the log does not need to be printed, the execution of the log printing code block can be directly skipped, thereby avoiding calling the logical judgment function in the log printing code block and executing a large number of string splicing instructions, so as to avoid unnecessary power consumption, thereby improving the performance of the electronic device.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of electronic devices, and in particular to a log printing method, electronic device and storage medium. Background Art

[0002] During the program development phase of the terminal device, such as the Java program development phase, when the developer is developing some functions, in order to analyze and locate the problems encountered during the function development process, a large number of log printing code blocks (code blocks including at least one line of log printing code) will be added to the source code of the Java program, so as to print the corresponding logs by executing the log printing code blocks, and then debug the Java program based on the printed logs. After the program development is completed, the developer will compile the Java program and transplant the compiled Java program to the user's mobile phone for operation to realize the corresponding functions.

[0003] When using a mobile phone, users do not need to analyze positioning technology issues like developers, and there is no need to print logs. However, the Java program used by users is still the version developed by developers. As mentioned above, a large number of log printing code blocks have been added to the source code of the Java program in this version. In this way, when running the Java program, the user's mobile phone will execute a large number of log printing code blocks, resulting in unnecessary power consumption, thereby affecting the performance of the mobile phone. Summary of the invention

[0004] Some embodiments of the present application provide a log printing method, an electronic device, and a computer-readable storage medium. The present application is introduced from multiple aspects below, and the embodiments and beneficial effects of the following aspects can be referenced to each other.

[0005] In a first aspect, the present application provides a log printing method for an electronic device, the method comprising: running a first program; detecting a first event generated during the running of the first program; determining whether to print the first event based on control information of the printing program corresponding to the first event; and determining not to print the first event when the control information is the first control information.

[0006] The program mentioned in the present application may be a Java program, the first event may be a log, the printing program may be a printing code block, and the control information may be a control code. The control code may include a logic judgment function, the logic judgment function may include a first mark, and the first control information may be control information when the logic value corresponding to the first mark is a first value.

[0007] According to the implementation mode of the present application, when an electronic device detects an event generated during the running of a program, it can determine whether to print the event based on the control information of the printing program corresponding to the event. For example, when the control information is the first control information, the event can be directly not printed, that is, the printing program corresponding to the event is not executed. In this way, unnecessary power consumption can be avoided, thereby improving the performance of the electronic device.

[0008] In some embodiments, the control information includes a logic judgment function, the logic judgment function includes a first mark, the logic value of the first mark represents whether to execute the printing program; the printing program is used to print the first event; the first control information is the control information when the logic value corresponding to the first mark is the first value.

[0009] Among them, the first mark mentioned in this application can be a variable parameter in the control code, the logical value can be the first value or the second value, the first value can be False, and the second value can be True.

[0010] According to the implementation mode of the present application, the printing of the log can be controlled by setting the logical value of the first mark. For example, when the logical value of the first mark is the first value, when the program is running, it is determined that the event is not printed based on the logical value of the first mark, and the printing program is not executed, thereby avoiding unnecessary power consumption.

[0011] In some embodiments, the first tag is used to instruct the printing program to print a level of the first event.

[0012] According to the implementation mode of the present application, since the first mark can indicate the level of the log, the logs of each level can be managed and controlled by setting the logical value of the first mark, so that the program only prints logs of certain levels during operation to meet the need to print logs of certain levels during subsequent program maintenance.

[0013] In some implementations, the control information further includes a start character and an end character, the logic judgment function and the start character are located in front of the printing code block corresponding to the printing program; and the end character is located after the printing code block.

[0014] The print code block may be a log print code block.

[0015] According to the implementation mode of the present application, the logic judgment function, the start symbol and the end symbol constitute a complete control code, and the logic judgment function and the start symbol are located in front of the printing code block, so that the control code will be executed before the printing code block, so that it can be judged whether to execute the printing code block before executing the printing code block.

[0016] In some implementations, the logic judgment function, the start symbol, and the start statement of the print code block are located in the same code line; the end symbol and the end statement of the print code block are located in the same code line.

[0017] According to the implementation mode of the present application, the control code is inserted into the source code of the program after the program development is completed. When inserting, the logic judgment function, the start symbol and the start statement of the print code block are placed on the same code line, and the end symbol and the end statement of the print code block are placed on the same code line, so that the line number of each code in the program can be kept unchanged, thereby not affecting the positioning of the code in subsequent analysis.

[0018] In some implementations, when the control information is the first control information, determining not to print the first event includes: when the control information is the first control information, determining not to execute a printing program.

[0019] According to the implementation mode of the present application, when the variable parameter of the logic judgment function in the control code is set to False, it means that the log does not need to be printed. When the electronic device runs the program, the log printing code block can be directly skipped without executing the string concatenation instruction, thereby avoiding unnecessary power consumption and improving the performance of the electronic device.

[0020] In some implementations, the method further includes: when the control information is the second control information, executing a printing program to determine whether to print the first event.

[0021] In some implementations, executing a printing program to determine whether to print a first event includes: determining whether a level of the printing program to print the first event is higher than or equal to a preset level; if so, printing the first event; if not, not printing the first event.

[0022] In a second aspect, an embodiment of the present application provides an electronic device, comprising: a memory for storing instructions executed by one or more processors of the electronic device; and a processor, when the processor executes the instructions in the memory, the electronic device can execute the method described in the first aspect of the present application. The beneficial effects that can be achieved in the second aspect can refer to the beneficial effects of the method provided in any embodiment of the first aspect, and will not be repeated here.

[0023] In a third aspect, the present application provides a computer-readable storage medium on which instructions are stored, and when the instructions are executed on a computer, the computer can execute the method described in any embodiment of the first aspect. The beneficial effects that can be achieved in the third aspect can refer to the beneficial effects of the method provided in any embodiment of the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1This is an exemplary application scenario of the embodiments of the present application;

[0025] Figure 2 An example diagram of log printing code provided for some embodiments;

[0026] Figure 3 An example diagram of the process of inserting control code provided in an embodiment of the present application;

[0027] Figure 4 Examples of control codes provided for embodiments of this application Figure 1 ;

[0028] Figure 5 Examples of control codes provided for embodiments of this application Figure 2 ;

[0029] Figure 6 A flow chart of a log printing method provided in an embodiment of the present application;

[0030] Figure 7 An example diagram of the program compilation process provided in the embodiment of the present application;

[0031] Figure 8 This is a schematic diagram showing the principle of printing a log on a mobile phone when a control code is not inserted, provided in some embodiments;

[0032] Fig. 9 This is a schematic diagram showing the principle of printing a log on a mobile phone after inserting a control code according to an embodiment of the present application;

[0033] Fig.10 An example diagram of the setting principle of the log control level provided in the embodiment of the present application;

[0034] Fig.11 An example diagram of a process constructed for a version of an embodiment of the present application;

[0035] Fig.12 An example diagram of the process of inserting the log instrumentation tool into the control code provided by the embodiment of the present application is shown;

[0036] Fig.13 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION

[0037] The embodiment of the present application is used to provide a log printing method. The method provided by the present application can improve the performance of electronic equipment.

[0038] Figure 1 An exemplary application scenario of the present application is shown. Figure 1In order to meet some functional requirements, developers can develop programs, such as Java programs, on the computer 10. After the Java program is developed, the compiled Java program can be transplanted to the mobile phone 20 for running to achieve the corresponding functions.

[0039] In this application, the computer 10 and the mobile phone 20 are only used as examples and are not intended to limit this application. In other embodiments, the computer 10 may also be a server or other electronic device that supports program development, and the mobile phone 20 may also be a laptop, tablet, large-screen device, wearable device (e.g., watch, smart glasses, helmet), desktop computer, augmented reality (AR) / virtual reality (VR) device, personal digital assistant (PDA) or other electronic device that supports running the program.

[0040] However, during the program development phase, in order to analyze and locate problems, developers will add some log printing code blocks to the source code of the Java program, which will cause mobile phone 20 to print logs frequently when running the compiled Java program, thereby increasing the power consumption of the processor and affecting the performance of mobile phone 20.

[0041] It is understandable that the operation phase of the mobile phone 20 belongs to the user use phase. In this phase, the program has been developed and there is no need to print logs. Therefore, it is necessary to control the printing of logs to ensure that the mobile phone 20 no longer prints logs in this phase.

[0042] Since Java programs usually have a large amount of code and encounter many technical problems during the development process, developers will add more log printing code blocks in the source code. In this case, if the log printing code blocks are deleted manually, the efficiency is low and difficult to implement.

[0043] In some embodiments, logs can be divided into multiple levels, and the log control level can be pre-set in the system. If the log level requested to be printed is higher than or equal to the log control level, the log of this level is allowed to be printed; if the log level requested to be printed is lower than the log control level, the log of this level is not allowed to be printed.

[0044] For example, logs can be divided into the following levels from high to low: off, fatal, error, warn, info, debug, trace, all. The following is a description of each level:

[0045] off: The highest level, no log is printed.

[0046] fatal: Indicates a very serious error event that may cause the application to terminate execution.

[0047] error: Indicates an error event, but the application may continue to run.

[0048] warn: Indicates a potentially dangerous situation.

[0049] info: Specifies the description information, which describes the application running process in a coarse-grained manner.

[0050] debug: Specifies detailed event information for program debugging.

[0051] trace: indicates the program running track, which is finer-grained than the debug level.

[0052] all: The lowest log level, used to print all logs.

[0053] For example, assuming that the log control level is set to fatal level in advance, when the mobile phone 20 runs to the following Figure 2 When the log printing code block shown (recorded as "log printing code block R1") is executed, the log printing function Log.d() is called to determine whether the log level currently requested to be printed is higher than or equal to the log control level, and whether to print the log is determined based on the judgment result. Since the log level requested to be printed by the log printing code block R1 is the debug level, which is lower than the fatal level, the mobile phone 20 will not print the log.

[0054] However, the log printing function Log.d() includes string concatenation code, and after the string concatenation code is compiled, multiple string concatenation instructions will be generated. When the mobile phone 20 calls the log printing function Log.d(), these string concatenation instructions will be executed first, and then the judgment action of log printing will be executed. In this case, although the mobile phone 20 does not print the log, it will still execute a large number of string concatenation instructions, resulting in unnecessary power consumption, thereby affecting the performance of the mobile phone 20.

[0055] In order to solve the above technical problems, the present embodiment provides a log printing method. In the method provided in the present embodiment, during the program development stage, control information (for example, control code) can be added to the program, and the control information is used to determine whether to execute the corresponding printing program, that is, to determine whether to print the corresponding event (that is, log). In this way, when the electronic device runs the program, when an event generated during the running of the program is detected, it can be determined whether to print the event based on the control information of the printing program corresponding to the event. For example, when the control information is the first control information, for example, when the logical value corresponding to the variable parameter of the logic judgment function in the control code is False, the event can be directly not printed, that is, the printing program corresponding to the event is not executed, that is, the above-mentioned log printing code block will not be executed, and the string concatenation instruction will not be executed. In this way, unnecessary power consumption overhead can be avoided, thereby improving the performance of the electronic device.

[0056] It can be understood that when the logic value corresponding to the variable parameter of the logic judgment function in the control code is True, that is, when the control information is the second control information, the printing program can be executed, that is, the string concatenation instruction is first performed. Then, based on the log level indicated in the logic judgment function and the preset log control level, it is determined whether to print the event.

[0057] The following describes the solution of the present application by taking a computer 10 and a mobile phone 20 as examples.

[0058] The embodiment of the present application is that the computer 10 first inserts the control code of the log printing code block into the source code of the Java program, and then compiles the source code of the Java program after the insertion is completed, and then transplants the compiled Java program to the mobile phone 20 for operation.

[0059] The following first introduces the insertion process of the control code.

[0060] Figure 3 The insertion of the control code is performed by the computer 10, referring to Figure 3 , including the following steps:

[0061] S101, identifying a log printing code block (or referred to as a “printing code block”) in a source code of a first program and a log level requested to be printed by the log printing code block.

[0062] The present application does not limit the program type of the first program, and the first program may be a program of a language type such as a Java program, a C language program, a C++ program, etc. In this embodiment, a Java program is taken as an example of the first program.

[0063] After the developer completes the development of the Java program on the computer 10, in the pre-compilation stage, the computer 10 can call a log instrumentation tool (a tool integrated in the Android compilation system that can automatically insert code) to perform a cyclic scan of the source code of the Java program to identify the log printing code block in the source code and the log level requested to be printed by the log printing code block.

[0064] S102: Generate a control code for the log printing code block based on the log level requested to be printed by the log printing code block.

[0065] For example, after the log instrumentation tool identifies the log printing code block R1, it can generate the control code "if (LogControl.D) {}" according to the log level (debug level) requested to be printed by the log printing code block R1. Wherein, if (LogControl.D) is a logic judgment function, LogControl.D is a variable parameter (as the first marker), "{" is the start symbol, and "}" is the end symbol.

[0066] The variable parameter LogControl.D of the logic judgment function is determined according to the log level requested to be printed by the log printing code block R1. For example, if the log level requested to be printed by the log printing code block R1 is the warn level, and its specific log printing function is Log.w(), then the variable parameter of the logic judgment function is LogControl.W.

[0067] The value of LogControl.D can be set according to the actual scenario requirements. For example, when it is necessary to print logs, the logical value of LogControl.D is set to True (as an example of the first value). When it is not necessary to print logs, the logical value of LogControl.D is set to False (as an example of the second value).

[0068] It can be understood that in this embodiment, the mobile phone 20 does not need to print logs. Therefore, the logical value of LogControl.D can be set to False to skip the log printing code block R1, avoid calling the log printing function Log.d(), and avoid executing the string concatenation instruction.

[0069] It can be understood that the above logic value method is only an example and is not limited in this application.

[0070] S103, inserting a control code into the source code of the first program.

[0071] It is understandable that after the Java program is developed, the code line number of its source code is determined. Therefore, the symbol table generated after compiling the source code of the Java program is also determined. Among them, the symbol table is used to parse the dump file (a memory image file of a process, used for debugging the program) exported by the mobile phone 20. When parsing the dump file, the code line number is determined according to the symbol table, and then the code line with the problem is determined according to the code line number, and the problem code can be located.

[0072] In some embodiments, the instrumentation tool may move the log printing code block R1 down by one line. At this time, the starting line number of the log printing code block R1 is updated from 22582 to 22583, and the ending line number is updated from 22584 to 22585. Then, refer to Figure 4 , the instrumentation tool then inserts "if (LogControl.D) {" in the previous line of the log printing code block R1 (the line number is 22582), and inserts "}" in the next line of the log printing code block R1 (the line number is 22586).

[0073] After inserting the control code, the line number of the log printing code block R1 changes. In addition, the line number of the code below the log printing code block R1 also changes. The change of the code line number will affect the location of the code, making it difficult to locate the problematic code when maintaining the program later. Therefore, when inserting the control code, the code line number should be kept unchanged.

[0074] In this embodiment, reference Figure 5 , the instrumentation tool can obtain the starting line number and the ending line number of the log printing code block R1, and then determine the starting statement and the ending statement of the log printing code block according to the starting line number and the ending line number, and insert "if (LogControl.D) {" before the starting statement, and insert "}" after the ending statement. In this way, it can be ensured that the code line number of the source code remains unchanged after the control code is inserted, so as not to affect the subsequent maintenance of the program.

[0075] In summary, in this embodiment, the computer 10 inserts the control code in front of the log printing code block, so that when the mobile phone 20 runs the program, the control code in the program can execute the judgment action of log printing in advance, without entering the log printing code block for judgment. In addition, when the control code determines not to print the log, the log printing code block can be directly skipped, without entering the log printing code block to call the log printing function and execute the string concatenation instruction, thereby improving the running speed of the mobile phone 20 and further improving the performance of the mobile phone 20.

[0076] The above embodiment introduces the insertion process of the control code. The following introduces the operation process of the control code.

[0077] Figure 6 This is a flow chart of the log printing method provided in the embodiment of the present application. The method is executed by the mobile phone 20, refer to Figure 6 , the method comprises the following steps:

[0078] S201, obtaining an installation package of a first program, wherein the source code of the first program includes a log printing code block and a control code of the log printing code block, the control code includes a first mark, the first mark indicates whether log printing is required, and the control code is executed before the log printing code block.

[0079] The following description will continue with the Java program as the first program as an example.

[0080] After the log plugging tool inserts the control code into the source code of the Java program, the computer 10 compiles the source code of the Java program, and after the compilation is completed, the compiled Java program is transplanted to the mobile phone 20 for running.

[0081] For example, refer to Figure 7 In the pre-compilation stage, the log instrumentation tool can statically insert the control code into A1.java, B1.java, and C1.java (as the source code of the Java program) to obtain A2.java, B2.java, and C2.java. Then, in the compilation stage, the computer 10 can call the compiler javac to compile A2.java, B2.java, and C2.java into their corresponding class files: A2.class, B2.class, and C2.class, and then call the compilation tool dx or d8 to merge A2.class, B2.class, and C2.class together according to the dex file format to obtain a class.dex file. Then, the class.dex file is packaged into an Android installation package (Android application package, APK).

[0082] After the computer 10 packages the compiled Java program into an APK, the APK can be uploaded to the network, and the mobile phone 20 can download it on the uploaded network, or the APK can be directly sent to the mobile phone 20.

[0083] S202: Install and run the first program based on the installation package.

[0084] After the mobile phone 20 obtains the APK of the Java program, it installs and runs the Java program based on the APK.

[0085] S203, based on the first program executing the control code, determine whether to execute the log printing code block according to the preset value of the first mark of the control code. If not, execute step S204; if yes, execute step S205.

[0086] When the mobile phone 20 executes the control code, the if (LogControl.D) function is called to determine whether the logical value (or "preset value") of LogControl.D is True. If so, the log printing code block is executed; if not, the log printing code block is not executed.

[0087] S204, skip the log printing code block, and execute the next line of code in the log printing code block.

[0088] When the logical value of LogControl.D is False, the log printing code block is skipped and the next line of code in the log printing code block is executed.

[0089] S205, execute the log printing code block to determine whether the log level requested to be printed is higher than or equal to the preset log control level. If yes, execute step S206; if no, execute step S207.

[0090] When the logical value of LogControl.D is True, the log printing code block is executed. For the sake of narrative coherence, the specific execution process of the log printing code block is described below.

[0091] S206, printing logs of corresponding log levels.

[0092] For example, if the log control level is the trace level, and the log printing code block requests to print the log level at the debug level (higher than the trace level), then the debug level log is printed.

[0093] S207: Do not print the log, but execute the next line of code in the log printing code block.

[0094] For example, if the log control level is info, and the log level requested to be printed by the log printing code block is debug (lower than info), the debug level log is not printed, and the next line of code in the log printing code block is executed.

[0095] It is understandable that in order to ensure that the mobile phone 20 does not call the log printing function during operation and avoid unnecessary power consumption, in this embodiment, the logical value of LogControl.D can be set to False. In this way, in the above step S202, it will be determined that the mobile phone 20 needs to print a log, thereby skipping the log printing code block and executing the subsequent code.

[0096] In summary, in this embodiment, after inserting the control code into the source code of the program, when the mobile phone 20 is running the program, when the control code determines that the mobile phone 20 does not need to print the log, the log printing code block can be directly skipped, thereby avoiding the call of the log printing function and avoiding the execution of the string concatenation instruction. In this way, the performance consumption caused by the execution of the log printing code block can be reduced, thereby improving the performance of the mobile phone 20.

[0097] Figure 8 The process of printing a log by the mobile phone 20 when the control code is not inserted is shown. Figure 8 The Android system includes the application layer, application framework layer, local service layer and kernel layer.

[0098] Among them, the application layer may include application A, application B, application C, application D and other applications.

[0099] The application framework layer may include log printing interfaces such as Log.java printing interface, Slog.java printing interface and Rlog.java printing interface.

[0100] The local service layer may include the libog library (a log library) and the Java NativeInterface (JNI) interface. The JNI interface may call the writeEvent function, the Android_uitil_EventLog_writeEvent_Integer function, and the write_to_log function to execute related printing instructions (for example, string concatenation instructions).

[0101] The kernel layer can include a logger device and some callable functions, such as __write_to_log_kernel function, logger_init function, init_log function, etc. When the JNI interface calls the kernel layer, the kernel layer's __write_to_log_kernel function can be called to write logs to the logger, and the logger_init function, init_log function, etc. can be called to execute corresponding instructions, such as log level control, opening device files, writing logs to device files, and Log driver writing data.

[0102] The following describes the log printing process when the control code is not inserted.

[0103] When mobile phone 20 executes the log printing code block in the application layer and calls the log printing function, the application layer will call the pre-packaged log printing interface in the application framework layer (for example, Log.java printing interface, Slog.java printing interface, Rlog.java printing interface, etc.) (that is, call the log printing function), and then call the Java local (Java Native Interface, JNI) interface in the local service layer through the log printing interface, and then call the liblog library through the JNI interface, and then call the __write_to_log_kernel function in the kernel layer through the liblog library to write the log to the log recorder.

[0104] Finally, the logger determines whether the log level requested to be printed by the log printing code block is higher than or equal to the preset log control level. If so, the log of the log level requested to be printed by the log printing code block is written to the corresponding device file. If not, no operation is performed. It can be understood that when the control code is not inserted, the control of log printing only controls whether the log is written to the device file at the kernel layer (that is, controls whether the log is written to the disk), but does not control the execution of the log printing code block, and it is impossible to avoid the power consumption caused by the execution of the log printing code block.

[0105] Fig. 9 The process of printing a log by the mobile phone 20 after inserting the control code is shown.

[0106] refer to Fig. 9 After inserting the control code, the log printing interface can be controlled at the application framework layer.

[0107] Specifically, when the mobile phone 20 executes the control code at the application layer, it can call the logic judgment function in the control code to determine whether to execute the log printing code block at the application framework layer. If yes, the log printing code block is executed, and at this time, the log printing interface is allowed to be called; if no, the log printing code block is skipped, and at this time, the log printing interface is not allowed to be called.

[0108] In some cases, when the logical value of the variable parameter (such as LogControl.D) in the logic judgment function is True, the control code determines to execute the log printing code block. At this time, the mobile phone 20 will call the log printing interface in the application framework layer, and then call the JNI interface in the local service layer through the log printing interface, and then call the liblog library through the JNI interface, and then call the __write_to_log_kernel function of the kernel layer through the liblog library to write the log to the log recorder.

[0109] Finally, the logger determines whether the log level requested to be printed by the log printing code block is higher than or equal to the preset log control level. If so, the log of the log level requested to be printed by the log printing code block is written into the corresponding device file; if not, no operation is performed.

[0110] In other cases, when the logical value of the variable parameter in the logic judgment function is False, the control code determines not to execute the log printing code block. At this time, the log printing code block is skipped and the next line of code in the log printing code block is executed. In order to avoid executing the log printing code block, the variable parameter in the logic judgment function can be set to False. In this way, when the application framework layer makes a judgment, the result of the judgment will be to skip the log printing code block. Figure 8 and Fig. 9 From the described process, it can be seen that inserting the control code of the log printing code can advance the judgment action of log printing from the kernel layer to the application framework layer, thereby controlling the execution of the log printing code block and avoiding the power consumption caused by the execution of the log printing code block.

[0111] In this application, in order to meet the needs of different scenarios, the Java program can be built into different versions, for example, a user version, a debugging version, a performance test version, etc.

[0112] refer to Fig.10 You can set persist.log.tag (log control level flag) to different log levels according to the version type as the log control level. For example, for the performance debugging version, you can set persist.log.tag to info level.

[0113] When the mobile phone 20 runs the log printing code block, the log control level can be judged according to persist.log.tag, and the log of the corresponding level can be printed according to the log control level. For example, if the log control level is error, logs of error and above are printed. If the log control level is warn, logs of warn and above are printed. If the log control level is info, logs of info and above are printed.

[0114] In some embodiments, the log level corresponding to persist.log.tag can be controlled by setting the property value of prop.

[0115] Fig.11 shows the build process of the version. Fig.11 , including the following steps:

[0116] S301, start the compilation system.

[0117] After the developer has completed developing the Java program on the computer 10 , the computer 10 may start the compilation system and enter the pre-compilation stage.

[0118] S302, parsing compilation command parameters.

[0119] For different versions, different compilation command parameters can be set to determine whether to insert the control code into the source code of the Java program according to the compilation command parameters.

[0120] S303, determine whether to insert the control code. If yes, execute step S304; if no, execute step S306.

[0121] Whether to insert the control code is determined according to the compilation command parameter. For example, if the compilation command parameter is True, it is determined that the control code needs to be inserted, and if the compilation command parameter is False, it is determined that the control code does not need to be inserted.

[0122] S304, calling the log instrumentation tool to perform the instrumentation operation.

[0123] When it is determined that the control code needs to be inserted, the log plugging tool in the compilation system can be called to insert the control code of the log printing code block into the first source code of the Java program.

[0124] S305: Use a checking tool to check the instrumentation result.

[0125] After the control code is inserted, you can call the inspection tool to check the inserted control code.

[0126] S306, executing a compile operation.

[0127] After checking the inserted control code, the compilation system is called to compile the source code of the Java program.

[0128] S307, version building is completed.

[0129] After the compilation is complete, the corresponding version is built.

[0130] Fig.12 The following is an example flow chart showing the process of inserting a log instrumentation tool into the control code.

[0131] refer to Fig.12When inserting control code, the instrumentation tool can parse the instrumentation directory from the system configuration file, traverse the Java files (files of the first source code) in the directory to be scanned, and then use the Javaparser library to parse the source code into a syntax tree. Determine whether the system calls the log printing tool (for example, import android.tuil.Log, import android.tuil.Sog, import android.tuil.Rog). If not, traverse the next Java file; if so, use the Javaparser library to traverse the MethodCallExpr node in the syntax tree.

[0132] Then determine whether the node.Scope of the current traversal node is Log, Slog or Rlog. If not, traverse the next MethodCallExpr node; if so, determine whether the node.Name of the current node is the log level identifier (for example, t (trace level), d (debug level), i (info level), w (trace level), e (error level), etc.). If not, traverse the next MethodCallExpr node, if so, use the current node to rebuild an ifStmt statement. Then, replace the original node with the rebuilt ifStmt statement.

[0133] Next, determine whether the syntax tree is traversed. If not, traverse the next MethodCallExpr node. If yes, use the syntax tree after the insertion to generate a new Java file. Then, generate the diff information before and after the Java file is inserted to check the correctness of the insertion operation. Finally, determine whether the Java file is traversed. If not, traverse the next Java file in the next directory to be scanned. If yes, end and complete the insertion of the Java file.

[0134] Fig.131 shows a schematic diagram of the structure of the electronic device 100. The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) connector 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0135] It is to be understood that the structure illustrated in the embodiment of the present invention does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or separate some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0136] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU). In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, and a subscriber identity module (SIM) interface.

[0137] The charging management module 140 is used to receive charging input from a charger. The charger may be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from a wired charger through the USB connector 130. In some wireless charging embodiments, the charging management module 140 may receive wireless charging input through a wireless charging coil of the electronic device 100. While the charging management module 140 is charging the battery 142, it may also power the electronic device through the power management module 141.

[0138] The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera 193, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle number, battery health status (leakage, impedance), etc. In some other embodiments, the power management module 141 can also be set in the processor 110. In other embodiments, the power management module 141 and the charging management module 140 can also be set in the same device.

[0139] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.

[0140] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.

[0141] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100. The mobile communication module 150 can receive electromagnetic waves through the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.

[0142] The modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device (not limited to a speaker 170A, a receiver 170B, etc.), or displays an image or video through a display screen 194. In some embodiments, the modem processor may be an independent device. In other embodiments, the modem processor may be independent of the processor 110 and be set in the same device as the mobile communication module 150 or other functional modules.

[0143] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0144] In some embodiments, antenna 1 of electronic device 100 is coupled to mobile communication module 150, and antenna 2 is coupled to wireless communication module 160, so that electronic device 100 can communicate with the network and other devices through wireless communication technology.

[0145] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0146] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.

[0147] The electronic device 100 can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor.

[0148] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in the external memory card.

[0149] The internal memory 121 can be used to store computer executable program codes, which include instructions. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 100 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, etc. The processor 110 executes various functional applications and data processing of the electronic device 100 by running instructions stored in the internal memory 121 and / or instructions stored in a memory provided in the processor.

[0150] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.

[0151] The embodiments disclosed in the present application may be implemented in hardware, software, firmware or a combination of these implementation methods. The embodiments of the present application may be implemented as a computer program or program code executed on a programmable system, the programmable system including at least one processor, a storage system (including volatile and non-volatile memory and / or storage elements), at least one input device and at least one output device.

[0152] Program code can be applied to input instructions to perform each function described in the present application and generate output information. Output information can be applied to one or more output devices in a known manner. For the purpose of the present application, the processing system includes any system with a processor such as, for example, a digital signal processor (DSP), a microcontroller, an application specific integrated circuit (ASIC) or a microprocessor.

[0153] Program code can be implemented with high-level programming language or object-oriented programming language to communicate with the processing system. When necessary, program code can also be implemented with assembly language or machine language. In fact, the mechanism described in this application is not limited to the scope of any specific programming language. In either case, the language can be a compiled language or an interpreted language.

[0154] In some cases, the disclosed embodiments may be implemented in hardware, firmware, software, or any combination thereof. The disclosed embodiments may also be implemented as instructions carried or stored on one or more temporary or non-temporary machine-readable (e.g., computer-readable) storage media, which may be read and executed by one or more processors. For example, the instructions may be distributed over a network or through other computer-readable media. Therefore, machine-readable media may include any mechanism for storing or transmitting information in a form readable by a machine (e.g., a computer), including, but not limited to, floppy disks, optical disks, optical discs, read-only memories (CD-ROMs), magneto-optical disks, read-only memories (ROMs), random access memories (RAMs), erasable programmable read-only memories (EPROMs), electrically erasable programmable read-only memories (EEPROMs), magnetic or optical cards, flash memory, or a tangible machine-readable memory for transmitting information (e.g., carrier waves, infrared signals, digital signals, etc.) using the Internet in electrical, optical, acoustic, or other forms of propagation signals. Therefore, machine-readable media include any type of machine-readable media suitable for storing or transmitting electronic instructions or information in a form readable by a machine (e.g., a computer).

[0155] In the accompanying drawings, some structural or method features may be shown in a specific arrangement and / or order. However, it should be understood that such a specific arrangement and / or order may not be required. Instead, in some embodiments, these features may be arranged in a manner and / or order different from that shown in the illustrative drawings. In addition, the inclusion of structural or method features in a particular figure does not mean that such features are required in all embodiments, and in some embodiments, these features may not be included or may be combined with other features.

[0156] It should be noted that the units / modules mentioned in the various device embodiments of the present application are all logical units / modules. Physically, a logical unit / module can be a physical unit / module, or a part of a physical unit / module, or can be implemented as a combination of multiple physical units / modules. The physical implementation method of these logical units / modules themselves is not the most important. The combination of functions implemented by these logical units / modules is the key to solving the technical problems proposed by the present application. In addition, in order to highlight the innovative part of the present application, the above-mentioned device embodiments of the present application do not introduce units / modules that are not closely related to solving the technical problems proposed by the present application, which does not mean that there are no other units / modules in the above-mentioned device embodiments.

[0157] It should be noted that in the examples and description of this patent, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "including one" do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0158] Although the present application has been illustrated and described with reference to certain preferred embodiments thereof, it will be understood by those skilled in the art that various changes in form and details may be made therein without departing from the scope of the present application.

Claims

1. A log printing method, used in electronic equipment, characterized in that: The method comprises: In the pre-compilation stage, the source code of the first program is obtained, and the source code is in Java language. The code line number of the source code after development is determined, and the code line number is used to locate the code line; identifying a printing code block of a printing program in the source code; Generate control information of the printing program according to the log level requested to be printed by the printing code block, wherein the control information includes a logic judgment function, a start character, and an end character; In the code line where the start statement of the print code block is located, the start symbol and the logic judgment function are inserted before the start statement, and in the code line where the end statement of the print code block is located, the end symbol is inserted after the end statement, so as to insert the control information into the source code; Compiling the source code, and running the first program after compiling; Based on the execution of the control information at the application layer, a first event corresponding to the printing program generated during the running of the first program is detected; Calling the logic judgment function based on the management and control information to determine whether to print the first event at the application framework layer; When the control information is the first control information, it is determined not to print the first event, and calling the printing interface corresponding to the printing code block in the application framework layer is not allowed; When the control information is the second control information, the printing program is executed to call the printing interface corresponding to the code block in the application framework layer, call the JNI interface in the local service layer through the printing interface, call the liblog library through the JNI interface, and call the write function of the kernel layer through the liblog library to write the log corresponding to the first event into the log recorder; Determining by the logger whether the log level requested to be printed by the print code block is higher than or equal to a preset level, so as to determine whether to print the first event; If yes, write the log of the log level requested to be printed by the log printing code block into the corresponding device file to print the first event; If not, no operation is performed, so as not to print the first event.

2. The method according to claim 1, characterized in that The logic judgment function includes a first mark, and the logic value of the first mark represents whether to execute the printing program; The printing program is used to print the first event; The first control information is the control information when the logic value corresponding to the first mark is a first value.

3. The method according to claim 2, characterized in that The first mark is used to instruct the printing program to print the level of the first event.

4. The method according to claim 2, characterized in that: When the control information is the first control information, determining not to print the first event includes: When the control information is the first control information, it is determined not to execute the printing program.

5. The method according to claim 1, characterized in that The step of judging by the logger whether the log level requested to be printed by the print code block is higher than or equal to a preset level to determine whether to print the first event includes: Determining, by the log recorder, whether the level of printing the first event by the printing program is higher than or equal to the preset level; If yes, print the first event; If not, the first event is not printed.

6. An electronic device comprising: a memory for storing instructions executed by one or more processors of the electronic device; The processor, when executing the instructions in the memory, can enable the electronic device to execute the log printing method according to any one of claims 1 to 5.

7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed on a computer, the computer executes the log printing method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Log printing method, device and apparatus and storage medium

    CN110377244A