Software log processing method supporting thread security, storage medium and equipment
By introducing a message queue mechanism and a reasonable log file management strategy in the log system, the blocking and loss of log records under high concurrency is solved, and an efficient, secure, cross-platform log processing solution is achieved.
Patent Information
- Application Number
- CN202411682609.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-22
- Publication Date
- 2025-03-14
AI Technical Summary
Existing logging systems may cause data blocking or loss under high concurrency conditions, and are too large to be suitable for small embedded software use, and are difficult to port across hardware platforms.
A software log processing method that supports thread safety is designed, using the message queue mechanism to record logs, ensuring the orderly and efficient log recording, and optimizing log storage space and read and write efficiency through reasonable log file creation and deletion mechanisms.
It realizes logging fluency under high concurrency conditions, ensures that the latest log content is not lost, and reduces system overhead, suitable for small embedded software and cross-platform applications.
Smart Images

Figure CN119621146A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software log recording, and in particular to a software log processing method, storage medium and device supporting thread safety. Background Art
[0002] Software logs are text information generated by software operation, including software operation records and program debugging information. During software development, log information can provide powerful assistance for software troubleshooting and performance optimization. After the software is solidified, the log will also record a large amount of information generated by the software every day, providing data for software security monitoring and operation maintenance. At present, there are many excellent open source log systems on the market, but the framework of such open source software is usually too large and complex, which is not convenient for the team to use in a unified and standardized manner, nor is it easy to transplant across hardware platforms.
[0003] In the entire software system, the log system essentially plays a supporting role, so its system overhead should be reduced as much as possible, while its efficiency and throughput should be improved. Especially in the field of embedded development, the simplicity and efficiency of the log system is an extremely important software quality indicator. In addition, the current mainstream log systems in the industry have many pain points in design, such as log output blocking normal business execution, large amounts of burst data affecting the overall performance of the software, and the log system being too large to be convenient for small embedded software to use.
[0004] Therefore, from the perspective of ease of use, efficiency and performance, designing and implementing a cross-platform logging system that is simple, easy to maintain, efficient, high-performance and easy to use has become the current key task. Summary of the invention
[0005] The purpose of the present invention is to overcome the problems existing in the prior art and provide a software log system processing solution with a small amount of code, convenient coding and use, asynchronous processing and innovatively designed log text creation and deletion mechanism to ensure that the log system has smooth data recording under high concurrency conditions, progressive deletion of log content with adjustable size, and no loss of the latest log content.
[0006] The objective of the present invention is achieved through the following technical solutions:
[0007] In a first aspect, a software log processing method supporting thread safety is provided, comprising the following steps:
[0008] S1. Create a log class; the log data structure uses a message queue mechanism;
[0009] S2. Create a log system running thread according to the log class thread pointer passed in by the software, and start the log system running thread; wherein the log thread runs in an independent thread when recording logs;
[0010] S3, create a new current log file path and a historical log file path, open the current log file path, and if it is successfully opened, execute step S4;
[0011] S4, determine whether the current log file capacity exceeds the set log file capacity, if it exceeds, determine whether there is a historical log file, if so, execute step S5;
[0012] S5, delete the historical log file, rename the current log file to the historical log file, and then create a new log file;
[0013] S6. When a log needs to be written, the log content is transmitted as an input parameter to the log writing function, and then the log writing function is called to record the log content in the log file.
[0014] In some embodiments, creating a log class includes:
[0015] Create a new log function class, in which the input parameters of the constructor include thread pointer, message queue variable name, log file capacity and single log capacity; static private variables include thread lock variable, log file pointer variable, log file name variable, thread pointer variable, message queue handle and message queue variable name; private functions include log thread function and log creation function; public functions include class constructor, log thread start function and log write function.
[0016] In some embodiments, the class constructor assigns the thread pointer and the message queue variable name in the input parameters to the corresponding static variables, and creates a message queue according to the handle of the log message queue and the message queue name.
[0017] In some embodiments, the message queue mechanism includes:
[0018] The message queue entry policy is set to queue according to the first-in-first-out policy.
[0019] In some embodiments, other software threads only need to include the header file of the log class, the log message queue name and the log class object name when designing. When recording is needed, the information only needs to be sent to the log message queue through the message queue.
[0020] In some embodiments, the determination of successful opening in step S3 includes:
[0021] If the current log file path is successfully opened within three times, the log creation function is executed, otherwise the log file creation failure information is returned to the caller.
[0022] In some embodiments, the log creation function is used to call the software to record the first log record when it is first run, and the input parameters include the current file pointer, the historical log path, and the current file log path.
[0023] In some embodiments, the first statement of the log writing function is locked with a thread lock, and the thread lock is opened at the last statement.
[0024] In a second aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, any one of the software log processing methods described above is implemented.
[0025] According to a third aspect, an electronic device is provided, including a memory and a processor, wherein the memory stores computer instructions executable on the processor, and the processor executes any one of the software log processing methods described above when executing the computer instructions.
[0026] It should be further explained that the technical features corresponding to the above options can be combined or replaced with each other to form a new technical solution if there is no conflict.
[0027] Compared with the prior art, the present invention has the following beneficial effects:
[0028] (1) The present invention aims to solve the problems of data blockage or data loss that may be caused by high data concurrency in traditional log systems when recording logs. By designing a specific log data structure and using a message queue mechanism, the present invention can support multi-threaded programs to record log information reliably, orderly and efficiently.
[0029] (2) To address the pain points that the software log storage space cannot be increased infinitely and the read and write latency of log files is increasing due to their large size, a log file creation and deletion mechanism is reasonably designed according to the software hardware environment to improve the log read and write efficiency and ensure that the latest log content is not lost under limited hardware resources;
[0030] (3) In view of the problem that the open source log system is too large and complex, not convenient for use in small software such as embedded systems, and not easy to transplant across operating system platforms, the log class function (ThreadLog function) created by the present invention is composed of ThreadLog.cpp and ThreadLog.h texts. ThreadLog.cpp can generate ThreadLog.lib static link library. When in use, just add ThreadLog.lib and ThreadLog.h files to the project file. Therefore, the log system of the present invention designs a log class text with less than 200 lines of code based on C++ language and POSIX message queue interface function. When in use, only Lib files and header files need to be added to be applied across operating system platforms. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 A flow chart of a software log processing method supporting thread safety shown in an embodiment of the present invention;
[0032] Figure 2 A logic diagram of log file creation and deletion processing shown in an embodiment of the present invention;
[0033] Figure 3 This is a log thread operation logic diagram shown in an embodiment of the present invention. DETAILED DESCRIPTION
[0034] The technical solution of the present invention is clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. The components of the embodiments of the present application described and shown in the accompanying drawings can be arranged and designed in various configurations. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0035] It should be noted that the defects existing in the solutions in the above-mentioned prior art are the results obtained by the inventor after practice and careful research. Therefore, the discovery process of the above-mentioned problems and the solutions proposed in the embodiments of the present application for the above-mentioned problems below should all be the contributions made by the inventor to the present application in the process of invention and creation, and should not be understood as technical contents known to technical personnel in this field.
[0036] In view of the technical problems pointed out in the background technology, the embodiments provided by the present invention are as follows:
[0037] Example 1
[0038] Reference Figure 1 In an exemplary embodiment, a software log processing method supporting thread safety is provided, comprising the following steps:
[0039] S1. Create a log class; the log data structure uses a message queue mechanism;
[0040] S2. Create a log system running thread according to the log class thread pointer passed in by the software, and start the log system running thread; wherein the log thread runs in an independent thread when recording logs;
[0041] S3, create a new current log file path and a historical log file path, open the current log file path, and if it is successfully opened, execute step S4;
[0042] S4, determine whether the current log file capacity exceeds the set log file capacity, if it exceeds, determine whether there is a historical log file, if so, execute step S5;
[0043] S5, delete the historical log file, rename the current log file to the historical log file, and then create a new log file;
[0044] S6. When a log needs to be written, the log content is transmitted as an input parameter to the log writing function, and then the log writing function is called to record the log content in the log file.
[0045] Specifically, the following is the specific content of creating a log class, including the following parts:
[0046] 1. Create a new log function class (ThreadLog). The ThreadLog function consists of ThreadLog.cpp and ThreadLog.h texts. ThreadLog.cpp can generate the ThreadLog.lib static link library. When using it, add the ThreadLog.lib and ThreadLog.h files to the project file. The input parameters of the constructor are thread pointer (pthread_t*ptr), message queue variable name (char*logName), log file capacity (Total_Size=500000byte (unit: byte)), single log capacity (singelSize<=200byte (unit: byte)); static private variables include thread lock variable (pthread_mutex_t logLock), log file pointer variable (FILE*fp), log file name variable (char fileName_str
[100] ), thread pointer variable (pthread_t*m_pthread_tid), message queue handle (MSG_Q_ID msg_log) and the message queue variable name (char* m_msgq_name_log), private functions include log thread function (Run_LOG(void*arg), log creation function logCreate(FILE*fp, char*fileNameOld_str, char*fileName_str); public functions include class constructor (pthread_t*pthread_tid, char*msgq_name_log), log thread start function (bool ThreadInit()) and write log function (voidWriteLog(char*)).
[0047] 2. The class constructor (ThreadLog(pthread_t*pthread_tid, char*msgq_name_log)) is a function that is triggered when a new log class is created, and is used to initialize the log class. In the constructor of the log class, the thread pointer (pthread_t*m_pthread_tid) and the message queue variable name (char*logName) in the input parameters are assigned to the corresponding static variables, and a message queue is created based on the handle of the log message queue (MSG_Q_ID msg_log) and the message queue name (char*m_msgq_name_log). The queue entry strategy of the message queue is set to queue and wait according to the first-in-first-out strategy, so as to keep the order of log content generation consistent with the actual generation order; finally, the thread lock is initialized.
[0048] 3. The log thread start function (bool ThreadInit()) is a function that is called as soon as the software that uses this log class for logging is started. It is usually called in the main thread of the software. After the call is successful, all subsequent log operations of the software will be recorded in the log file. The log thread start function creates the thread successfully and starts the log thread according to the log class thread pointer (pthread_t*m_pthread_tid) passed in by the software; creates a new current log file path (. / log.txt) and a historical log file path (. / logOld.txt), opens the current log file path (. / log.txt), and if it is not opened successfully, it will be opened again, up to three times. If it is not opened successfully for three times, the log file creation failure information will be returned to the caller. If it is opened successfully, the log creation function will be executed.
[0049] 4. Reference Figure 2 , the log creation function is used to call the first log record when the software is run for the first time. The input parameters are: current file pointer (FILE*fp), historical log file path (. / logOld.txt), current file log path (. / log.txt); when the software starts, if the current log file capacity exceeds the set log file capacity size (Total_Size), first determine whether the historical log file path (. / logOld.txt) exists. If it does, delete the historical log file path (. / logOld.txt), and then rename the current log file path (. / log.txt) to the historical log file path (. / logOld.txt). Through this operation, the most recent historical log file will be retained; then create a new log file path (. / log.txt), record the usage record when the software is first started in the new log file, and add the time record of the new entry of the software log.
[0050] 5. Reference Figure 3 , the log thread function runs in an independent thread when recording logs. When designing other threads of the software, you only need to include the header file of the log class ("ThreadLog.h"), the log message queue name (char*logName) and the log class object name (myLog). When you need to record, you only need to send the information to the log message queue through the message queue; the message queue receiving function of the log thread function is always in a blocked state. When the log message queue receives a message sent by other queues, the message queue receiving function immediately transmits the received information to the log thread, and then determines the current log file capacity (now_Total_Size) and the newly received message capacity ( When the sum of now_singelSize) exceeds the set log file capacity size (Total_Size), first determine whether there is a historical log file (. / logOld.txt). If so, delete the historical log file (. / logOld.txt), then rename the current log file (. / log.txt) as the historical log file (. / logOld.txt), and then create a new log file (. / log.txt) to record the currently received information in the log file. Through this operation, the most recent old log file will be retained, and the log information can be recorded one by one in the order of time sent to the message queue.
[0051] 6. The write log function (WriteLog(char*message)) is the function used by the caller, which is actually the message queue sending function. Its input data is the message address (char*message). To ensure that multiple calls can be made without blocking, the thread lock (pthread_mutex_lock(&logLock)) is used in the first sentence of the function, and the thread lock (pthread_mutex_unlock(&logLock)) is opened in the last sentence of the function.
[0052] Through the present invention, other threads of the software only need to include the header file of the log class, the log message queue name and the log class object name when designing. When recording is needed, it is only necessary to send the information to the log message queue through the message queue. It is simple and easy to use and occupies little space. When logging, it runs in a separate thread and does not block the information sending end, thereby ensuring efficient operation of the software. When the current log file exceeds the maximum capacity, the current file is renamed as the old file through setting, the latest valid data is retained, and the current log file is newly created, thereby ensuring that the current log file can continue to record information and ensuring efficient and low-load operation of the software.
[0053] Example 2
[0054] Based on the same inventive concept as Example 1, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the software log processing method provided by the embodiment of the present invention is implemented. Based on such an understanding, the technical solution of this embodiment is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0055] Example 3
[0056] Based on the same inventive concept as Example 1, an electronic device is provided, including a memory and a processor, wherein the memory stores computer instructions that can be executed on the processor, and when the processor executes the computer instructions, the software log processing method provided in the embodiment of the present invention is executed.
[0057] The processor may be a single-core or multi-core central processing unit or a specific integrated circuit, or one or more integrated circuits configured to implement the present invention.
[0058] Embodiments of the subject matter and functional operations described in this specification may be implemented in: tangibly embodied computer software or firmware, computer hardware including the structures disclosed in this specification and their structural equivalents, or a combination of one or more of them. Embodiments of the subject matter described in this specification may be implemented as one or more computer programs, i.e., one or more modules of computer program instructions encoded on a tangible, non-transitory program carrier to be executed by a data processing device or to control the operation of the data processing device. Alternatively or additionally, the program instructions may be encoded on an artificially generated propagated signal, such as a machine-generated electrical, optical, or electromagnetic signal, which is generated to encode and transmit information to a suitable receiver device for execution by the data processing device.
[0059] The processes and logic flows described in this specification can be performed by one or more programmable computers executing one or more computer programs to perform corresponding functions by operating on input data and generating output. The processes and logic flows can also be performed by special purpose logic circuits, such as FPGAs (field programmable gate arrays) or ASICs (application-specific integrated circuits), and the apparatus can also be implemented as special purpose logic circuits.
[0060] Processors suitable for executing computer programs include, for example, general and / or special microprocessors, or any other type of central processing unit. Typically, the central processing unit will receive instructions and data from a read-only memory and / or a random access memory. The basic components of a computer include a central processing unit for implementing or executing instructions and one or more memory devices for storing instructions and data. Typically, the computer will also include one or more large-capacity storage devices for storing data, such as magnetic disks, magneto-optical disks, or optical disks, or the computer will be operably coupled to this large-capacity storage device to receive data from it or to transmit data to it, or both. However, the computer does not necessarily have such a device. In addition, the computer can be embedded in another device, such as a mobile phone, a personal digital assistant (PDA), a mobile audio or video player, a game console, a global positioning system (GPS) receiver, or a portable storage device such as a universal serial bus (USB) flash drive, just to name a few.
[0061] It should be understood that each box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flow chart, and the combination of the boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0062] The above specific implementation methods are detailed descriptions of the present invention. It cannot be determined that the specific implementation methods of the present invention are limited to these descriptions. For ordinary technicians in the technical field to which the present invention belongs, several simple deductions and substitutions can be made without departing from the concept of the present invention, which should be regarded as belonging to the protection scope of the present invention.
Claims
1. A software log processing method supporting thread safety, characterized in that: The following steps are involved: S1. Create a log class; the log data structure uses a message queue mechanism; S2. Create a log system running thread according to the log class thread pointer passed in by the software, and start the log system running thread; wherein the log thread runs in an independent thread when recording logs; S3, create a new current log file path and a historical log file path, open the current log file path, and if it is successfully opened, execute step S4; S4, determine whether the current log file capacity exceeds the set log file capacity, if it exceeds, determine whether there is a historical log file, if so, execute step S5; S5, delete the historical log file, rename the current log file to the historical log file, and then create a new log file; S6. When a log needs to be written, the log content is transmitted as an input parameter to the log writing function, and then the log writing function is called to record the log content in the log file.
2. The software log processing method according to claim 1, characterized in that: The creation of a log class includes: Create a new log function class, in which the input parameters of the constructor include thread pointer, message queue variable name, log file capacity and single log capacity; static private variables include thread lock variable, log file pointer variable, log file name variable, thread pointer variable, message queue handle and message queue variable name; private functions include log thread function and log creation function; public functions include class constructor, log thread start function and log write function.
3. The software log processing method according to claim 2, characterized in that: The class constructor assigns the thread pointer and the message queue variable name in the input parameters to the corresponding static variables, and creates a message queue according to the handle of the log message queue and the message queue name.
4. The software log processing method according to claim 3, characterized in that: The message queue mechanism includes: The message queue entry policy is set to queue according to the first-in-first-out policy.
5. The software log processing method according to claim 1, characterized in that: When designing other threads of the software, they only need to include the header file of the log class, the log message queue name and the log class object name. When recording is needed, they only need to send the information to the log message queue through the message queue.
6. The software log processing method according to claim 2, characterized in that: The judgment of successful opening in step S3 includes: If the current log file path is successfully opened within three times, the log creation function is executed, otherwise the log file creation failure information is returned to the caller.
7. The software log processing method according to claim 2, characterized in that: The log creation function is used to call the software to record the first log record when it is first run, and the input parameters include the current file pointer, the historical log path, and the current file log path.
8. The software log processing method according to claim 2, characterized in that: The first sentence of the log writing function is locked with a thread lock, and the thread lock is opened at the last sentence.
9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the software log processing method described in any one of claims 1 to 8 is implemented.
10. An electronic device comprising a memory and a processor, wherein the memory stores computer instructions that can be executed on the processor, wherein: When the processor runs the computer instructions, it executes the software log processing method described in any one of claims 1-8.