Before using log4c or log4cpp, it is always necessary to configure complex configurations such as files and link libraries. Although the more complex the more the software supports a lot of features, can be selective, but for a small project, or to study other people's code and add a bit of log, with such a complex configuration is not necessary.
So I think, or write a simple log library, so that you do not need the complexity of the log Control tool, you do not have to configure the environment every time, do not have to write configuration files or something. So I wrote a library, because too simple, the code is not much, there is no need to make Lib library, directly compiled into the code can be.
Let's take a look at this library from a simple little example.
This library contains two files, one is. c file, and one is a. h file, which is compiled directly with your project code when you use it.
Suppose the code structure of the project is as follows:
ls easylog.c easylog.h main.c
The main code of the project is in the Main.c file:
Cat
#include <stdio.h>"easylog.h"int main (intChar * argv[]) { easylog ("hello to me "); return 0 ;}
Compile:
GCC -o main main.c easylog.c
Run:
$./lscat main.log Hello my
The above example is the simplest configuration, that is, without adding any extraneous information, what is written in the Easylog () function, and what is output. Also, in the case of nothing, the log file will be automatically created and executed with the same prefix as the file. The way to get this file name is obtained through the system file "/proc/self/exe".
If I wanted to change the name of another log file, that would require more complicated operations to add a line of code inside the code, using Easylog_file () to make the name of the log file. For example, we want to put the log in the file Test.log, then we can write:
Easylog_file
("Test.log"
);
Modify the project file, compile the execution, and view the log file:
$Catmain.c #include<stdio.h>#include"easylog.h"intMainintargcChar*argv[]) {Easylog_file ("Test.log"); Easylog ("Hello, my"); Return0;} $ GCC-o main main.c easylog.c $./Main $lseasylog.c easylog.h main main.c main.log test.log$CatTest.log Hello to my
As you can see, there is a test.log file in the current directory, to see what is in it, we can see the log output.
But if I want to get other information, such as I want to get time information. That would have to add another line, Easylog_flag_add (easylog_date | Easylog_time) This code means, add two properties to the log, Easylog_date is the date, Easylog_time is the time.
Here we modify our project file Main.c, add a line of time to date the setup code.
$ cat main.c #include <stdio.h> #include " easylog.h " int Main (int argc, char * argv[]) {easylog_file ( " test.log " | Easylog_time); Easylog ( " hello my " ); return 0 ;}
Compile, run, view logs:
gcc -o main main.c easylog.c $. /cat test.log Hello my --£º 23.847 --Hello, my
Since it is for the log, it needs more detailed information, such as my log is in which file which line is written down, in order to get this information, we can add some settings, such as Easylog_file, Easylog_line, Easylog_func, These three are recorded in the log of the file where the log statement, the file line and the function name.
Modify project files, compile, run, view logs:
$Catmain.c #include<stdio.h>#include"easylog.h"intMainintargcChar*argv[]) {Easylog_file ("Test.log");//set the log file nameEasylog_flag_add (Easylog_date |//to set the required properties for a logEasylog_time |Easylog_file|Easylog_line|Easylog_func); Easylog ("Hello, my"); Return0;} $ GCC-o main main.c easylog.c $./Main $CatTest.log Hello to my .- .-Ten Geneva: -:23.847--Hello, my .- .-Ten Geneva: -:38.438Main.c OneMain ()--Hello my
As you can see from the log file, the last line of log shows the file, the number of rows, and the function name of the log. Contrasts sharply with the two lines above it.
What if we don't want to have a property when we're halfway through the record? Then remove a property, such as now to remove the date attribute, then add this line: Easylog_flag_rm (easylog_date);
Modify project files, compile, run, view logs:
$Catmain.c #include<stdio.h>#include"easylog.h"intMainintargcChar*argv[]) {Easylog_file ("Test.log"); Easylog_flag_add (Easylog_date|Easylog_time|Easylog_file|Easylog_line|Easylog_func); Easylog ("Hello, my"); EASYLOG_FLAG_RM (easylog_date);//Remove Project PropertiesEasylog ("Hello, your"); Return0;} $ GCC-o main main.c easylog.c $./Main $CatTest.log Hello to my .- .-Ten Geneva:Panax Notoginseng:28.094--Hello, my .- .-Ten Geneva: the:42.996Main.c OneMain ()--Hello, myGeneva: the:42.997Main.c -Main ()--Hello your
As you can see from the last log, the last line of the log does not record date information.
Because Easylog code itself is very few, altogether also has more than 200 lines, so can be easily modified and used by the user.
Finally, Easylog's code is placed on GitHub, where the address is Https://github.com/fengbohello/easylog , and students are welcome to use the ^_^
Sync post:http://www.fengbohello.top/code/easylog
Easylog--A simple log library under Linux