A simple log system C code implementation, log c code
Problem description
To implement a simple Windows-based log system, the style of the content required to be written into the log file is as follows:
[Time] + [file name] + [log level] + log Content
For example:
[2015.02.2514: 35: 13.143] [WriteLog. c] [INFO] This is a test!
"14:35:13. 143 "indicates the current time (accurate to milliseconds)," WriteLog. c "indicates the name of the printed log Content," INFO "indicates the Log Level, and" This is a test!" Is the specific log Content.
Algorithm flow
Figure 1 Overall procedure
Figure 2 General execution process of log writing
C code implementation
Configuration File Content
The configuration file is named Config. ini. Its content is shown in the following figure:
[LOG]
; LogLevel, 0-Fatal 1-Error 2-Warn 3-Info 4-Trace 5-Debug 6-All
LogLevel = 5
; Log dir
LogDir = D :\\ Test
"LogLevel" indicates the Log Level, and "LogDir" indicates the path where the log file is stored (Note: do not end with \ at the end ). You must change the values of each configuration item according to your actual needs.
Program description
(1) The program is directly compiled and run in VC ++.
(2) the configuration file Config. ini is directly placed in the project directory at the same level as the WriteLog. c file.
(3) You can selectively print some log information by changing the configuration value of "LogLevel.
Program running result
(1) set "LogLevel" to 3. The log file content is as follows:
(2) Set "LogLevel" to 5. The log file content is as follows:
It can be seen that the program can output the corresponding log information according to the configured log level.
(My microblogging: http://weibo.com/zhouzxi? Topnav = 1 & wvr = 5, No.: 245924426, welcome !)