This is a creation in Article, where the information may have evolved or changed.
Another week, the recent life, hhhe, the process of learning is really very slow, but slowly, not anxious, not afraid, not counseling.
Recently wrote something, encapsulated a database operation of the package, but there is a big bug, temporarily not posted, because it has not been resolved. But the package of this log package, actually did not write complete, but feel good, anyway, better than the instant log.
By the way, this operation does cache, why? In fact, the reason is very simple, this is the knowledge of the operating system. Because IO interrupts occur when we execute a write database or log to a file, an IO interrupt allows the operating system to handle these interrupts, and when our program executes, it switches from the user level to the core level, and then returns from the core level to the user level at the end of the process, which costs a lot When performing such operations frequently, the performance of the entire system will be very poor, these are assists in the previous genetic algorithm to calculate queen problem, in the process of genetic iterative use of C + + cout, so 8 queens need 5, 6 seconds, 16 queen to run for several minutes, originally always thought it was algorithmic problem, looking for a long time, Suddenly react to cout io interrupt, comment out later, all is the second execution. This is the cost of IO operations.
And this also helps with high concurrency, and after this part, I still have some problems to solve.
Next, the log log implementation, is also very simple, when the need to print a log, the log information is stored in memory, rather than immediately print, if in a period of time and other log information needs to print, all the log information together, and write files together. The efficiency of writing more than one sentence to a file at a time is nnnnnnn times higher than the efficiency of writing a single line.
Paste the log package of its own package, incomplete version, after the use of the process, perfect will be updated.
Packaged package, feel code style pretty good, hahaha
PackageMyLogImport("Log" "OS" "FMT" "Time" "Runtime" "StrConv")varFilestr LogfileinfovarLogType LogTypevarLogbuffer LogbufferfuncInit () {filestr = logfileinfo{} logtype = logtype{} logbuffer = logbuffer{} logbufferinit () Definelogtype () Defineloginfo ()GoAutoLog ()}funcLogbufferinit () {logBuffer.Log = []string{} Logbuffer.assert = []string{} logbuffer.warning = []string{} logbuffer.error = []string{} logbuffer.exception = []string{}}funcDefinelogtype () {filename: ="Mylog/logtxt.txt"Fptr, err: = Createlogfile (filename)ifErr! =Nil{FMT. PRINTLN (ERR)//Failed to do what should not be allowed to fail here} filestr.logtxt = fptr filename ="Mylog/asstxt.txt"Fptr, err = createlogfile (filename)ifErr! =Nil{FMT. PRINTLN (ERR)//Failed to do what should not be allowed to fail here} filestr.asstxt = fptr filename ="Mylog/wartxt.txt"Fptr, err = createlogfile (filename)ifErr! =Nil{FMT. PRINTLN (ERR)//Failed to do what should not be allowed to fail here} filestr.wartxt = fptr filename ="Mylog/errtxt.txt"Fptr, err = createlogfile (filename)ifErr! =Nil{FMT. PRINTLN (ERR)//Failed to do what should not be allowed to fail here} filestr.errtxt = fptr filename ="Mylog/exctxt.txt"Fptr, err = createlogfile (filename)ifErr! =Nil{FMT. PRINTLN (ERR)//Failed to do what should not be allowed to fail here} filestr.exctxt = Fptr}funcCreatelogfile (filenamestring) (*os. File, error) {if_, Err: = OS. Stat (filename); Err! =Nil{file, err: = OS. Create (filename)ifErr! =Nil{return Nil, err}returnFileNil}Else{file, err: = OS. OpenFile (filename, os. O_rdwr|os. O_append, OS. Modeperm)ifErr! =Nil{return Nil, err}returnFileNil}}funcDefineloginfo () {logtype.loglog = log. New (Filestr.logtxt,"",0) Logtype.logassert = log. New (Filestr.asstxt,"",0) Logtype.logwar = log. New (Filestr.wartxt,"",0) Logtype.logerr = log. New (Filestr.errtxt,"",0) logtype.logexception = log. New (Filestr.exctxt,"",0)}funcGetLog () *logtype {ifFilestr.logtxt = =Nil|| Filestr.asstxt = =Nil|| Filestr.wartxt = =Nil|| Filestr.errtxt = =Nil|| Filestr.exctxt = =Nil{FMT. Println ("FILESTR has invalid pointer or nil ptr", Filestr) LogType.LogWar.Println ("FILESTR has invalid pointer or nil ptr") }return&logtype}funcAutoLog () { for{bufferlength: =0Bufferlength + = Checkbuffer (&logbuffer.log,1) Bufferlength + = Checkbuffer (&logbuffer.assert,2) Bufferlength + = Checkbuffer (&logbuffer.warning,4) Bufferlength + = Checkbuffer (&logbuffer.error,8) Bufferlength + = Checkbuffer (&logbuffer.exception, -)ifBufferlength = =0{time. Sleep(4* Time. Second)}}}funcCheckbuffer (str *[]string, Parameint)int{Length: =Len((*STR))ifLength! =0{buffer: = (*STR)[0: Length] Loginfo: ="" forI: =0; i < length; i++ {loginfo = Loginfo + buffer[i] +"\ r \ n"}//@parame & Fastest ifParame &1!=0{logType.LogLog.Println (Loginfo)}Else ifParame &2!=0{logType.LogAssert.Println (Loginfo)}Else ifParame &4!=0{logType.LogWar.Println (Loginfo)}Else ifParame &8!=0{logType.LogErr.Println (Loginfo)}Else ifParame & -!=0{logType.LogException.Println (Loginfo)} length2: =Len((*STR))ifLength2! = length {(*str) = (*STR) [Length:length2]return Len(*STR)}Else{(*STR) = (*STR) [: 0] } }return0}//@logFormat//[log]: 2016/03/24 01:02:04/users/tangs/ideaprojects/moodleserve3/mylog/log.go:150:logbuffer.log//runtime. Caller (Depth)//depth should be 1, 2 or bigger. How finish it.funcAddlog (msgstring) {_, file, line, OK: = runtime. Caller(1) S: ="" ifOK {t: = time. Now () s ="[Log]:"+ t.string () +" "+ file +":"+ StrConv. Itoa (line)} msg = S +" "+ MSG LogBuffer.Log =Append(LogBuffer.Log, MSG)}funcAddassert (msgstring) {_, file, line, OK: = runtime. Caller(1) S: ="" ifOK {t: = time. Now () s ="[Assert]:"+ t.string () +" "+ file +":"+ StrConv. Itoa (line)} msg = S +" "+ MSG Logbuffer.assert =Append(Logbuffer.assert, MSG)}funcAddwarning (msgstring) {_, file, line, OK: = runtime. Caller(1) S: ="" ifOK {t: = time. Now () s ="[Warning]:"+ t.string () +" "+ file +":"+ StrConv. Itoa (line)} msg = S +" "+ MSG logbuffer.warning =Append(Logbuffer.warning, MSG)}funcAdderror (msgstring) {_, file, line, OK: = runtime. Caller(1) S: ="" ifOK {t: = time. Now () s ="[Error]:"+ t.string () +" "+ file +":"+ StrConv. Itoa (line)} msg = S +" "+ MSG Logbuffer.error =Append(Logbuffer.error, MSG)}funcAddexception (msgstring) {_, file, line, OK: = runtime. Caller(1) S: ="" ifOK {t: = time. Now () s ="[Exception]:"+ t.string () +" "+ file +":"+ StrConv. Itoa (line)} msg = S +" "+ MSG logbuffer.exception =Append(Logbuffer.exception, MSG)}funcFlush () {//todo Write cache to file immediately}
Defines the structure of the body,
Package Mylogimport ("Log" "OS") Type LogTypestruct{Loglog *Log. Logger//For the wrong log type. Logassert *Log. Logger//The type of log used to assert (assert) (these indicate a bug in unity itself). Logwar *Log. Logger//The log type used for Warning (Warning). LogErr *Log. Logger//log type for normal log messages. Logexception *Log. Logger//The log type used for the exception. }type Logfileinfostruct{Logtxt *os. File asstxt *os. File wartxt *os. File errtxt *os. File exctxt *os. File}type Logbufferstruct{Log []stringAssert []stringWarning []stringError []stringException []string}