CN112527265A - Method for automatically injecting logs and computer equipment - Google Patents

Method for automatically injecting logs and computer equipment Download PDF

Info

Publication number
CN112527265A
CN112527265A CN201910888874.8A CN201910888874A CN112527265A CN 112527265 A CN112527265 A CN 112527265A CN 201910888874 A CN201910888874 A CN 201910888874A CN 112527265 A CN112527265 A CN 112527265A
Authority
CN
China
Prior art keywords
source
file
source code
log
code
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201910888874.8A
Other languages
Chinese (zh)
Inventor
韦启龙
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Shenzhen TCL Digital Technology Co Ltd
Original Assignee
Shenzhen TCL Digital Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Shenzhen TCL Digital Technology Co Ltd filed Critical Shenzhen TCL Digital Technology Co Ltd
Priority to CN201910888874.8A priority Critical patent/CN112527265A/en
Publication of CN112527265A publication Critical patent/CN112527265A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • G06F8/427Parsing
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/70Software maintenance or management
    • G06F8/73Program documentation

Abstract

The invention discloses a method and computer equipment for automatically injecting logs, wherein the method comprises the following steps: acquiring a source file to be processed, wherein the source file to be processed comprises a source code, and analyzing the source code to determine type information corresponding to the source code; generating log information corresponding to the source code according to the source code and the type information corresponding to the source code; and injecting the log information corresponding to the source code into the line of the source code. In the invention, the source code in the source file to be processed is analyzed to determine the log information to be injected into the source code, and the log information is injected into the line of the source code, so that the whole process does not need the participation of developers, the automatic injection of the log information according to the analysis result of the source code is realized, the defect of omission when the developers write the log code manually is overcome, and the time cost of software system development is reduced.

Description

Method for automatically injecting logs and computer equipment
Technical Field
The invention relates to the technical field of Internet of things, in particular to a method for automatically injecting logs and computer equipment.
Background
Software developers write a large number of programs, but the programs always have exceptions, the repair of software defects occupies a considerable part of time in the development cycle of a software system, the logging is a method for tracking the time of software running, and the steps of using a logging module to record the execution of the programs in detail can quickly find out the place and the reason of errors when the programs have faults, so as to help to track and debug the programs.
However, logs generated in a software system are usually added in codes by software developers, but the software developers mainly pay attention to service codes in the encoding process, often ignore the codes added with printed logs, when a fault of software does occur due to log codes, a place and a reason for making a fault cannot be quickly found according to the logs at the first time, and the fault may need to be analyzed and solved when the fault occurs, so that the fault which occurs only when the system runs for a long time or the fault which occurs under some special conditions cannot be solved for a long time, and time is greatly wasted.
Accordingly, the prior art is in need of improvement and development.
Disclosure of Invention
The technical problem to be solved by the invention is to provide a method and computer equipment for automatically injecting logs, so that a developer is not required to edit log codes in the process of writing codes, the work of the developer is reduced, and the development period of a software system is shortened.
In a first aspect, the present invention provides a method for log automatic injection, where the method includes:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
As a further improved technical solution, the analyzing the source code to determine the type information corresponding to the source code includes:
analyzing the source code, and if the source code comprises a preset key code, acquiring type information corresponding to the key code from a preset log file, wherein the log file comprises a plurality of key codes and type information corresponding to each key code.
As a further improved technical solution, the generating of the log information corresponding to the source code according to the source code and the type information corresponding to the source code further includes:
determining a log frame corresponding to the type information according to the type information corresponding to the source code;
and generating log information corresponding to the source code according to the key code and the log frame.
As a further improved technical solution, the type information includes a function entry, a pointer return, and a loop, and when the type information corresponding to the source code is any one of the function entry, the pointer return, and the loop, the injecting the log information corresponding to the source code into the line where the source code is located respectively includes:
and injecting the log information corresponding to the source code before the first character of the line where the source code is positioned.
As a further improved technical solution, the type information further includes non-pointer return, and when the type information corresponding to the source code is the non-pointer return, the injecting the log information corresponding to the source code into the line where the source code is located respectively includes:
and injecting the log information corresponding to the source code into the tail character of the line of the source code.
As a further improved technical solution, before obtaining a source file to be processed, the method includes:
acquiring all source files in a project to be processed;
for each source file, determining whether the source file has been injected with log information;
and if the log information is not injected into the source file, taking the source file as a source file to be processed.
As a further improved technical solution, before the obtaining of the source file to be processed, the method further includes:
acquiring all source files in a project to be processed;
for each source file, judging whether the source file is a source file to be ignored or not according to a preset ignore list, wherein the ignore list comprises a path of the source file to be ignored and a regular expression representing the source file to be ignored;
if the source file is not the source file to be ignored, judging whether the source file is injected into log information or not;
and if the log information is not injected into the source file, taking the source file as a source file to be processed.
As a further improved technical solution, the determining whether the source file has been injected with log information includes:
judging whether a history database has a path of the source file;
if the history database does not have the path of the source file, judging whether the history database has the file fingerprint of the source file, and if the history database does not have the file fingerprint of the source file, indicating that the source file is not injected with log information.
In a second aspect, the present invention provides a computer device comprising a memory and a processor, the memory storing a computer program, the processor implementing the following steps when executing the computer program:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
In a third aspect, the present invention provides a computer readable storage medium having stored thereon a computer program which, when executed by a processor, performs the steps of:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
Compared with the prior art, the embodiment of the invention has the following advantages:
the method provided by the embodiment of the invention comprises the following steps: acquiring a source file to be processed, wherein the source file to be processed comprises a source code, and analyzing the source code to determine type information corresponding to the source code; generating log information corresponding to the source code according to the source code and the type information corresponding to the source code; and injecting the log information corresponding to the source code into the line of the source code. In the invention, the source code in the source file to be processed is analyzed to determine the log information to be injected into the source code, and the log information is injected into the line of the source code, so that the whole process does not need the participation of developers, the automatic injection of the log information according to the analysis result of the source code is realized, the defect of omission when the developers write the log code manually is overcome, and the time cost of software system development is reduced.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only some embodiments described in the present invention, and for those skilled in the art, other drawings can be obtained according to the drawings without creative efforts.
FIG. 1 is a flow chart of a method for log auto-injection in an embodiment of the present invention;
FIG. 2 is a software flow diagram of log injection for all source files in a project according to an embodiment of the present invention;
fig. 3 is an internal structural diagram of a computer device in an embodiment of the present invention.
Detailed Description
In order to make the technical solutions of the present invention better understood, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The embodiment provides a method for automatically injecting logs, which analyzes a source file, and injects injection type information corresponding to an analysis result conforming to an injection rule into log information of the source file when the analysis result conforms to a certain injection rule, so that log information is automatically injected according to the analysis result of the source file, the defect that a software developer omits a log code when writing the log code manually is overcome, and the time cost of software system development is reduced.
Referring to fig. 1, the method may include the steps of:
s1, obtaining a source file to be processed, wherein the source file to be processed comprises a source code.
In the embodiment of the invention, the source file is a file formed by source programs and data, and is obtained after source codes written by using a high-level language or an assembly language are saved as the file, wherein each source file comprises a plurality of lines of source codes. In the development stage, log injection needs to be carried out on a plurality of source files in a project, so that when a program fails, the place and the reason of an error can be quickly found according to log information. First, a pending source file needs to be determined among a plurality of source files, and specifically, before step S1, the method further includes:
m1a, acquiring all source files in the project to be processed.
In the embodiment of the invention, aiming at the project to be processed, the project to be processed comprises at least one source file, and in the development phase, the source file which needs to be subjected to log injection in the project to be processed is subjected to log injection. For example, obtaining all source files in a project to be processed is: source file 1(F1), source file 2(F2), source file 3(F3), and source file 4 (F4).
M2a, for each source file, determines whether the source file has been injected with log information.
In the embodiment of the present invention, in order to not repeatedly perform log injection on the same source file, it is necessary to determine whether the acquired source file has already been subjected to log injection, specifically, step M2a includes:
m2a1, judging whether the history database has a path of the source file.
In the embodiment of the invention, when the log information is injected, the history database records the path of the source file injected with the log information, so that when judging whether the source file is injected with the log information or not, if the source file has the path of the source file in the history database, the source file can be determined to be injected with the log information. For example, retrieving a source file: f1, in the history database, the file path of F1 is found: e \\ \ trunk \ TIF _ LiveTV \ app \ src \ main \ java \ com \ weiqilong \ TVActivity. java, it can be determined that F1 has been log injected.
M2a2, if the history database does not have the path of the source file, judging whether the history database has the file fingerprint of the source file, and if the history database does not have the file fingerprint of the source file, indicating that the source file is not injected with log information.
In the embodiment of the present invention, if the history database does not have the path of the source file, it cannot be determined that the source file is not injected with the log information, because the source file may have been injected with the log information, but the position of the source file is shifted, which may cause a situation that the path of the current source file is inconsistent with the path saved in the history database. Therefore, it is necessary to determine whether the file fingerprint of the source file exists in the history database.
In the embodiment of the invention, when the log information is injected, the history database also records the file fingerprint of the source file injected with the log information. File fingerprints create small numbers, i.e., fingerprints, from any file through a hashing algorithm, also known as a hashing algorithm. For example, for source file F2, the fingerprint generated for it using the hashing algorithm is "a 71165e95 a", and after log injection for F2, the file fingerprint for F2 is: "a 71165e95 a" is saved to the history database. File fingerprints are unique markers of a file that are associated with every byte of the file and it is difficult to find the reverse regularity. If the file fingerprint of the source file exists in the historical database, the source file is indicated to have been injected with log information, and if the file fingerprint of the source file does not exist in the historical database, the source file is indicated to have not been injected with log information.
In one implementation, if the history database does not have the file fingerprint of the source file, it is determined whether the history database has the file name of the source file, and if the history database has the file name of the source file, it indicates that log information has been injected into the source file.
In one implementation, since the file fingerprint is associated with each byte in the source file, if the content of the source file has changed, it will cause the file password to change; if a source file which has been subjected to log injection not only moves to a position but also changes content, a file path and a file fingerprint of the source file cannot be found in a history database, so that when the history database does not have the file path of the source file and the file fingerprint of the source file, it is also necessary to determine whether a file name of the source file exists in the history database, and if the history database has the file name of the source file, it indicates that log information has been injected into the source file. For example, source file F1 has a name test. java, and if there is a file name in the history database: java, it means that log information has been injected into test. If the file name of the source file is not in the history database, the source file is indicated to be injected with log information.
In the embodiment of the invention, when judging whether the source file is injected with the log information, firstly judging through the file path, secondly judging through the file fingerprint, and finally judging through the file name, wherein the priority of the file path is higher than that of the file fingerprint, and the priority of the file fingerprint is higher than that of the file name.
M3a, if the log information is not injected into the source file, the source file is used as a pending source file.
In the embodiment of the invention, through the steps M2a to M3a, the source file which is not injected with the log can be screened out as the source file to be processed.
In another implementation, in a pending item, not all source files need to be log-injected, the source files that do not need to be log-injected in the item may be filtered first, and then source files that need to be log-injected and have not been log-injected are screened, specifically, step S1 includes:
m1b, acquiring all source files in the project to be processed;
m2b1, for each source file, judging whether the source file is a source file to be ignored or not according to a preset ignore list, wherein the ignore list comprises a path of the source file to be ignored and a regular expression representing the source file to be ignored;
m2b2, if the source file is not the source file to be ignored, judging whether the source file is injected with log information;
m3b, if the log information is not injected into the source file, the source file is used as a pending source file.
Through the above steps, the source files which are not in the ignore list and are not injected with logs can be screened out as the files to be processed, and the above steps are described in detail after step S4.
S2, analyzing the source code to determine the type information corresponding to the source code.
In the embodiment of the invention, the type information corresponding to the source code is determined by analyzing the source code. The type information needs to be searched in a pre-stored log file according to the result obtained by analysis. Specifically, step S2 includes:
s21, analyzing the source code, and if the source code comprises a preset key code, acquiring type information corresponding to the key code from a preset log file, wherein the log file comprises a plurality of key codes and type information corresponding to each key code.
In the embodiment of the invention, a log file is required to be preset, the log file comprises a log file code, and the log file code realizes the corresponding relation between a plurality of key codes and a plurality of types of information. The type information comprises a function entry, a pointer return, a non-pointer return and a loop, and correspondingly, the key codes comprise a function name, a return value type code, a loop code and a loop element.
The function name in the key code and the type information corresponding to the function name are function entries; the type information corresponding to the return value type code in the key code is pointer return and non-pointer return, and the type corresponding to the loop code and the loop element is a loop.
In this embodiment of the present invention, if the source code includes a key code, the key code included in the source code is obtained, and the type information corresponding to the key code is determined in the log file, for example, when a certain line of source codes is analyzed as: "public void calc (int a, int b)", where the line code includes a function name calc and a variable name, and then it is determined that the type information of the line source code is a function entry in the log file. For example, when parsing into a certain line of source code includes loop code: for, the type information of the line source code is determined to be a loop in the log file.
In the embodiment of the present invention, if the source code does not include a preset key code, the next line is parsed. For example, there is one behavior in the source code: and an extension ViewController, namely WKScriptMessageHandler, analyzing the line code, wherein the line code does not comprise a key code, and analyzing the next line of source code without performing log injection operation on the line code.
And S3, generating log information corresponding to the source code according to the source code and the type information corresponding to the source code.
In the embodiment of the present invention, the type information corresponding to each line of source codes is determined through step S2, the log file further includes a log frame corresponding to each type information, the log frame corresponding to each type information can be determined according to the type information obtained through parsing, and then the log frame is filled with the parsed key codes to generate the log information corresponding to the source codes.
Specifically, step S3 includes:
s31, according to the type information corresponding to the source code, determining a log frame corresponding to the type information.
In the embodiment of the invention, the type information corresponds to a log frame, and the type information comprises a function entry, a pointer return, a non-pointer return and a loop; the log frame corresponding to the function entry comprises an entry frame and a first time frame; the log frame corresponding to the pointer return (void) comprises a first exit frame and a second time frame; the log frames corresponding to the non-pointer return (return) are a second exit frame and a second time frame; and the log frame corresponding to the loop is a loop frame.
For example, when a certain line of source code is resolved: "public void calc (int a, int b)", wherein the key code includes: the function name calc, the function parameter name a and the function parameter name b can determine that the type corresponding to the line of codes is a function entry, and further determine that a frame corresponding to the function entry comprises an entry frame and a first time frame, wherein the entry frame is as follows: "log.d (TAG," function name: function parameter name ═ function parameter name); ", the first time frame is: "long startTime ═ system. ", the first time frame being a time log frame injected at the entry to the function.
For example, when parsing into a certain line of source code includes key code: "void" and "calc" indicate that the return value type of the function calc is viod, then it can be determined that the type information corresponding to the line code is pointer return, and void corresponds to the first exit frame: "Log.d (TAG," function name end "); and a second time frame: "log.d (TAG," function name cost time ═ "+ system. "the second timeframe is a timelog frame injected at the exit of the function.
For example, when parsing into a certain line of source code includes key code: and "return", wherein the line code belongs to a calc function, and indicates that the type of the return value is a non-void type, it may be determined that the type information corresponding to the line source code is a non-pointer return, and return corresponds to a second exit frame: "log.d (TAG," function name: return ═ return value); and a second time frame: "log.d (TAG," function name cost time ═ "+ system. ".
For example, when parsing into a certain line of source code includes key code: "for (list: lists)", where for is a loop code, list is a loop element, then the type corresponding to the line code is a loop, and the frame corresponding to the line code is a loop frame: "log.d (TAG," for cycle element ═ cycle element); ".
And S32, generating log information corresponding to the source code according to the key code and the log frame.
In the embodiment of the present invention, generating the source code for each line of the source code is implemented by injecting the log code written in the log file, which can be regarded as a process of frame filling.
For example, a certain line of source code has been resolved: "public void calc (int a, int b); ", wherein the key code is related to, including: the function name calc, the function parameter name a and the function parameter name b can determine that the log frame corresponding to the line of code is an entry frame: "log.d (TAG," function name: function parameter name ═ function parameter name); "and the first time frame is: "long startTime ═ system. "determining, according to the parsed function name and function parameter, that the log information corresponding to the source code includes entry information" log.d "(TAG," calc () "+", a ═ a + ", b ═ b +"); "and runtime information: "longstartTime ═ system. ".
For example, in the above example, when a certain line of source code is parsed to include the key code: "void" and "calc", which have been determined to correspond to the first exit frame as: "Log.d (TAG," function name end "); and a second time frame: "log.d (TAG," function name cost time ═ "+ system. ", according to the key code calc obtained by analysis, determining the log information corresponding to the line code includes: egress log information "log.d (TAG," calc end "); "and runtime information: "log.d (TAG," calc cost time "+ system. currenttimemillis () -startTime); ".
For example, in the above example, when a certain line of source code is parsed to include the key code: when the type of the return value in a certain row of source code is resolved into a non-void type, and the row of source code belongs to a calc function, it can be determined that the type information of the return is a non-pointer return, and the return corresponds to a second exit frame: "log.d (TAG," function name: return ═ return value); and a second time frame: "log.d (TAG," function name cost time ═ "+ system. "according to the parsed function name and the return value, the return log information can be determined: "log.d (TAG," return "+", a ═ a); "and runtime information: "log.d (TAG," calc cost time "+ (system. currenttimemillis () -startTime) +" ms "); ".
For example, in the above example, when a certain line of source code is parsed to include the key code: "for (list: lists)", it is confirmed that the frame corresponding to the line code is a loop frame: "log.d (TAG," for cycle element ═ cycle element); determining the log information corresponding to the line code as follows according to the analyzed cycle elements: "log.d (TAG," loop lists, element ═ plus); ".
And S4, injecting the log information corresponding to the source code into the line where the source code is located.
In the embodiment of the invention, one source file comprises a plurality of lines of source codes, not every line of source codes needs to be subjected to log injection, and for every line of source codes, when key codes are obtained through analysis, log information corresponding to the line of source codes is generated; the injection manners of the log information corresponding to different types of information are different, specifically, step S4 includes:
and S41, when the type information corresponding to the source code is any one of a function entry, a pointer return and a loop, injecting log information corresponding to the source code before the first character of the line where the source code is located.
In the embodiment of the present invention, when the type information corresponding to the source code is any one of a function entry, a pointer return, and a loop, a first character of the source code is acquired, and before the log information corresponding to the source code generated in the preceding step is injected into the first character, the first character is a first character arranged in all characters of the source code.
For example, the source code is: "public void calc (int a, int b); ", the log information corresponding to the line of source code, which has been generated through the foregoing steps, includes entry information" log.d (TAG, "calc ()" + ", a ═ a +", b ═ b + "); "and runtime information: "long startTime ═ system. "then obtain the first character in all the characters in the line source code, i.e. obtain the first character in the line source code, as follows: and p, injecting the log information corresponding to the line source code generated in the previous step into the first character of the line: before "p" we obtain: "log.d (TAG," calc () "+", a ═ a + ", b ═ b +"; current timemills (); public void calc (int a, int b); ".
For example, the source code is: the "return a" is analyzed that the type information of the source code is returned as a non-pointer, and the log information corresponding to the source code of the row obtained through the above steps includes: "log.d (TAG," return "+", a ═ a); "and" log.d (TAG, "calc cost time ═ plus (system. currenttimemillis () -startTime) +" ms "); ", get the first character of the line source code: "r", in the first character: before "r", log information corresponding to the line code is injected to obtain: "log.d (TAG," return "+", a ═ a); log.d (TAG, "calccost time" + (system. currenttimemillis () -startTime) + "ms"); return a; ".
And S42, when the type information corresponding to the source code is non-pointer return, injecting the log information corresponding to the source code into the tail character of the line where the source code is located.
In the embodiment of the invention, when the type information corresponding to the source code is returned by a non-pointer, the tail character of the source code is obtained, and the log information corresponding to the source code of the line is injected behind the tail character, wherein the tail character is the last character in all the characters arranged in the source code.
For example, the source code is: "void calc (void)", and it is analyzed that the type information of the line source code is returned as a non-pointer, and the log information corresponding to the line source code obtained through the above steps includes: "Log.d (TAG," calc end "); "and" log.d "(TAG," c cost time "+ system current time millis () -startTime)", obtaining the last character in all characters of the source code, i.e. obtaining the tail character of the line of source code: ")", after injecting the log information into the tail character of the line code, we get: "void calc (void); log.d (TAG, "calc end"); log.d (TAG, "calc cost time" + system. currenttimemillis () -startTime) ".
When the software fails during operation, a developer can know the type of the source code of the line immediately behind the source code by seeing the log information in front of the first character of the line where the source code is located, and the developer can conveniently locate the position where the source code fails.
In the embodiment of the invention, after the log injection of one source file to be processed is completed, the file path, the file fingerprint and the file name of the source file to be processed are all stored in the historical database, so that the source file which is injected with the log can not be injected repeatedly when the next source file to be processed is obtained.
Detailed description in another implementation, source files that are not in the ignore list and are not logged are screened out as details of the pending file.
In another implementation manner, not all source files in a to-be-processed project need to be subjected to log injection, for example, in an Android Studio project, a build file generated in a compiling process does not need to be subjected to log injection, and for such source files that do not need to be subjected to log injection, parsing processing is not performed on the source files. Specifically, step S1 includes:
m1b, acquiring all source files in the project to be processed.
In the embodiment of the present invention, step M1b is the same as step M1a, and is not repeated here.
M2b1, for each source file, judging whether the source file is a source file to be ignored or not according to a preset ignore list, wherein the ignore list comprises a path of the source file to be ignored and a regular expression representing the source file to be ignored.
In another implementation manner, one to-be-processed item corresponds to one ignore list, and the ignore list represents the source file to be ignored through a file path or a regular expression. When the log is automatically injected, a source file in a project to be processed is read, and when the source file is in an ignore list, the source file is not analyzed, namely, the log is not required to be injected into the source file, so that the ignore list is required to be prestored before the automatic log injection is carried out.
Specifically, step M1b is preceded by:
m01, presetting an ignore list, wherein the ignore list comprises a path of a source file to be ignored and a regular expression representing the source file to be ignored.
In the embodiment of the invention, the path of the source file which needs to be ignored can be written into the ignore list, and when the log is automatically injected, the source file under the path does not need to be processed. The source files to be ignored generally include a referenced third-party library file, a configuration file of the project, and an intermediate file generated by the compiling process, for example, in the Android Studio project, the intermediate file build generated by the compiling process does not need to be subjected to log injection, and the path of the build file can be written into an ignore list. In order to improve the efficiency of the log automatic injection process, the source file to be ignored can also be a source file which is not focused in the project, for example, for an Android Studio project, the project only needs to focus on files with suffixes of java, the files with the suffixes of java are stored in the scr directory, and the source files in the non-scr directory need to be ignored, so that the path of the files in the non-scr directory can be used as the path of the ignored files and added to the ignore list. Furthermore, java files stored in the scr directory may have other referenced codes, the other referenced codes do not need log injection, and the other referenced codes are written in a tester folder in the scr directory, so that the path of the tester folder is also added to the ignore list.
In the embodiment of the invention, the regular expression can be used for expressing the source file corresponding to the regular expression without processing, the regular expression is written into the ignore list, and the source file corresponding to the regular expression is ignored when the log is automatically injected. A regular expression is a logical formula represented using a combination of characters, for example, intended to represent: a non-empty string containing only letters a and b can be described as follows using a regular expression: "(a | b) (a | b) ×". In the embodiment of the present invention, for example, if a file ending with a ". log" needs to be ignored, the regular expression representing the file ending with the ". log" may be written into the ignore list, and the regular expression representing the file ending with the ". log" is ". prime \ log".
M2b2, if the source file is not the source file to be ignored, judging whether the source file is injected into the log information.
In the embodiment of the present invention, if the source file is not in the ignore list, it indicates that the source file needs to perform log injection processing, and to avoid repeatedly injecting logs, it is determined whether the source file has been injected.
M3b, if the log information is not injected into the source file, the source file is used as a pending source file.
In the embodiment of the present invention, M3b is the same as in the foregoing step M3a, and is not described herein again; through steps M2b 1-M3 b, source files that are not in the ignore list and are not logged can be screened out as pending source files.
In one implementation, log injecting all source files in an item may include: firstly, acquiring all source files in a project; filtering all source files to be ignored out according to a preset ignore list; and judging whether the source files except the source files to be ignored in all the source files are subjected to log injection one by one or simultaneously, analyzing the source codes of the source files line by line for the source files which are not subjected to the log injection, and injecting log information according to the analysis result, wherein the specific method is shown in the steps from S1 to S4 so as to realize the log injection of all the source files in one project.
In another implementation, referring to fig. 2, the step of performing log injection on all source files in a project may further include:
a. one source file in the project is read.
b. And c, judging whether the source file is in an ignore list, if the source file is in the ignore list and the source file does not need log injection, returning to the step a, and obtaining a source file again, and if the source file is not in the ignore list, entering the step c.
c. And c, judging whether the log is injected into the source file or not, wherein the judging mode is to judge whether the file path of the source file or the file fingerprint or the file name is in the historical database or not, if the file path or the file fingerprint or the file name in the active file is in the historical database, the log is injected into the source file, the injection is not required to be repeated, the step a is returned, one source file is obtained again, and if the source file is not subjected to the log injection, the step d is carried out.
d. The source code of the source file is parsed line by line, and log information is injected according to the parsing result, and the specific method is shown in the steps S1 to S4.
e. And adding the source file subjected to log injection into the historical database, specifically, adding a file path, a file fingerprint and a file name of the source file into the historical database.
f. And c, judging whether a next source file exists in the project, if so, returning to the step a, continuously processing the rest source files, and if not, further, indicating that the source files in the project all finish log injection.
It can be seen that embodiments of the present invention provide a method, comprising: acquiring a source file to be processed, wherein the source file to be processed comprises a source code, and analyzing the source code to determine type information corresponding to the source code; generating log information corresponding to the source code according to the source code and the type information corresponding to the source code; and injecting the log information corresponding to the source code into the line of the source code. In the invention, the source code in the source file to be processed is analyzed to determine the log information to be injected into the source code, and the log information is injected into the line of the source code, so that the whole process does not need the participation of developers, the automatic injection of the log information according to the analysis result of the source code is realized, the defect of omission when the developers write the log code manually is overcome, and the time cost of software system development is reduced.
Referring to fig. 3, a computer device, which may be a terminal, having an internal structure as shown in fig. 3 according to an embodiment of the present invention is shown. The computer device includes a processor, a memory, a network interface, a display screen, and an input device connected by a system bus. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to implement a method of log auto-injection. The display screen of the computer equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, a key, a track ball or a touch pad arranged on the shell of the computer equipment, an external keyboard, a touch pad or a mouse and the like.
Those skilled in the art will appreciate that the illustration in fig. 3 is merely a block diagram of a portion of the structure associated with the disclosed aspects and is not intended to limit the computing devices to which the disclosed aspects apply, as particular computing devices may include more or less components than those shown, or may combine certain components, or have a different arrangement of components.
The embodiment of the invention provides computer equipment, which comprises a memory and a processor, wherein the memory stores a computer program, and the computer equipment is characterized in that the processor executes the computer program and realizes the following steps:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, where the computer program, when executed by a processor, implements the following steps:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above-mentioned embodiments only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.
It is to be understood that the invention is not limited to the examples described above, but that modifications and variations may be effected thereto by those of ordinary skill in the art in light of the foregoing description, and that all such modifications and variations are intended to be within the scope of the invention as defined by the appended claims.

Claims (10)

1. A method for log auto-injection, the method comprising:
acquiring a source file to be processed, wherein the source file to be processed comprises a source code;
analyzing the source code to determine type information corresponding to the source code;
generating log information corresponding to the source code according to the source code and the type information corresponding to the source code;
and injecting the log information corresponding to the source code into the line of the source code.
2. The method of claim 1, wherein parsing the source code to determine type information corresponding to the source code comprises:
analyzing the source code, and if the source code comprises a preset key code, acquiring type information corresponding to the key code from a preset log file, wherein the log file comprises a plurality of key codes and type information corresponding to each key code.
3. The method according to claim 2, wherein the log file further includes a log frame corresponding to each type of information, and the generating the log information corresponding to the source code according to the source code and the type information corresponding to the source code includes:
determining a log frame corresponding to the type information according to the type information corresponding to the source code;
and generating log information corresponding to the source code according to the key code and the log frame.
4. The method according to claim 2, wherein the type information includes a function entry, a pointer return and a loop, and when the type information corresponding to the source code is any one of the function entry, the pointer return and the loop, the injecting the log information corresponding to the source code into the line where the source code is located respectively includes:
and injecting the log information corresponding to the source code before the first character of the line where the source code is positioned.
5. The method according to claim 2, wherein the type information further includes a non-pointer return, and when the type information corresponding to the source code is the non-pointer return, the injecting the log information corresponding to the source code into the row where the source code is located respectively includes:
and injecting the log information corresponding to the source code into the tail character of the line of the source code.
6. The method according to any one of claims 1 to 5, wherein the obtaining a source file to be processed is preceded by:
acquiring all source files in a project to be processed;
for each source file, determining whether the source file has been injected with log information;
and if the log information is not injected into the source file, taking the source file as a source file to be processed.
7. The method according to any one of claims 1 to 5, wherein before the obtaining the source file to be processed, further comprising:
acquiring all source files in a project to be processed;
for each source file, judging whether the source file is a source file to be ignored or not according to a preset ignore list, wherein the ignore list comprises a path of the source file to be ignored and a regular expression representing the source file to be ignored;
if the source file is not the source file to be ignored, judging whether the source file is injected into log information or not;
and if the log information is not injected into the source file, taking the source file as a source file to be processed.
8. The method of claim 6, wherein the determining whether the source file has been injected with log information comprises:
judging whether a history database has a path of the source file;
if the history database does not have the path of the source file, judging whether the history database has the file fingerprint of the source file, and if the history database does not have the file fingerprint of the source file, indicating that the source file is not injected with log information.
9. A computer device comprising a memory and a processor, the memory storing a computer program, wherein the processor implements the steps of the method of any one of claims 1 to 8 when executing the computer program.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the steps of the method of any one of claims 1 to 8.
CN201910888874.8A 2019-09-19 2019-09-19 Method for automatically injecting logs and computer equipment Pending CN112527265A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910888874.8A CN112527265A (en) 2019-09-19 2019-09-19 Method for automatically injecting logs and computer equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910888874.8A CN112527265A (en) 2019-09-19 2019-09-19 Method for automatically injecting logs and computer equipment

Publications (1)

Publication Number Publication Date
CN112527265A true CN112527265A (en) 2021-03-19

Family

ID=74974373

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910888874.8A Pending CN112527265A (en) 2019-09-19 2019-09-19 Method for automatically injecting logs and computer equipment

Country Status (1)

Country Link
CN (1) CN112527265A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113419773A (en) * 2021-05-21 2021-09-21 北京达佳互联信息技术有限公司 Log file generation method and device, electronic equipment, storage medium and product

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20130111451A1 (en) * 2011-10-31 2013-05-02 International Business Machines Corporation Program Log Record Optimization
CN103559121A (en) * 2013-09-23 2014-02-05 清华大学 Drive configuration debugging method based on log injection
CN106970789A (en) * 2017-03-07 2017-07-21 武汉斗鱼网络科技有限公司 A kind of Android end groups bury point methods and system automatically in AOP and AspectJ daily record
CN107145341A (en) * 2017-03-31 2017-09-08 武汉斗鱼网络科技有限公司 A kind of method and device that print log function is added in Flash
CN109240736A (en) * 2018-07-28 2019-01-18 安徽捷兴信息安全技术有限公司 It is a kind of that log method for implanting and system are applied based on Dalvik instruction
CN110134401A (en) * 2019-04-12 2019-08-16 深圳壹账通智能科技有限公司 Code snippet localization method, device, computer equipment and storage medium

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20130111451A1 (en) * 2011-10-31 2013-05-02 International Business Machines Corporation Program Log Record Optimization
CN103559121A (en) * 2013-09-23 2014-02-05 清华大学 Drive configuration debugging method based on log injection
CN106970789A (en) * 2017-03-07 2017-07-21 武汉斗鱼网络科技有限公司 A kind of Android end groups bury point methods and system automatically in AOP and AspectJ daily record
CN107145341A (en) * 2017-03-31 2017-09-08 武汉斗鱼网络科技有限公司 A kind of method and device that print log function is added in Flash
CN109240736A (en) * 2018-07-28 2019-01-18 安徽捷兴信息安全技术有限公司 It is a kind of that log method for implanting and system are applied based on Dalvik instruction
CN110134401A (en) * 2019-04-12 2019-08-16 深圳壹账通智能科技有限公司 Code snippet localization method, device, computer equipment and storage medium

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113419773A (en) * 2021-05-21 2021-09-21 北京达佳互联信息技术有限公司 Log file generation method and device, electronic equipment, storage medium and product
CN113419773B (en) * 2021-05-21 2023-11-28 北京达佳互联信息技术有限公司 Log file generation method and device, electronic equipment, storage medium and product

Similar Documents

Publication Publication Date Title
US20230094191A1 (en) Scalable execution tracing for large program codebases
CN108459962B (en) Code normalization detection method and device, terminal equipment and storage medium
CN110704297A (en) Code evaluation method and device, computer equipment and storage medium
US8943480B2 (en) Setting breakpoints in optimized instructions
CN113127347A (en) Interface testing method, device, equipment and readable storage medium
CN111897727A (en) Software testing method and device, computer equipment and storage medium
US20110029953A1 (en) System and Method for Scalable Handling of Debug Information
EP3514680B1 (en) Identification of changes in functional behavior and runtime behavior of a system during maintenance cycles
CN106933642B (en) Application program processing method and processing device
US9117017B2 (en) Debugger with previous version feature
CN108446224B (en) Performance analysis method of application program on mobile terminal and storage medium
CN112181388B (en) Initializing method and device of SDK (software development kit) component, storage medium and control terminal
CN117076338B (en) Method and system for dynamically debugging Linux kernel based on kprobe
CN112789602A (en) Reverse debugging of software failures
US20070150866A1 (en) Displaying parameters associated with call statements
CN112527265A (en) Method for automatically injecting logs and computer equipment
CN104145249B (en) Method and system for debugging computer program
US11520682B2 (en) Code coverage method for embedded system on chip
CN110908869B (en) Application program data monitoring method, device, equipment and storage medium
CN115705297A (en) Code call detection method, device, computer equipment and storage medium
CN111045891B (en) Monitoring method, device, equipment and storage medium based on java multithreading
CN113220586A (en) Automatic interface pressure test execution method, device and system
CN113467815A (en) Application repair method and device for hot update, terminal equipment and storage medium
CN111736806A (en) Control injection method and device and terminal equipment
CN112783759B (en) White box test task execution method and device, storage medium and computer equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination