diff --git a/os/gcron/gcron_entry.go b/os/gcron/gcron_entry.go index 68fd6d14c..0c4952faf 100644 --- a/os/gcron/gcron_entry.go +++ b/os/gcron/gcron_entry.go @@ -109,8 +109,10 @@ func (entry *Entry) Close() { // gcron.Entry relies on gtimer to implement a scheduled task check for gcron.Entry per second. func (entry *Entry) check() { if entry.schedule.meet(time.Now()) { - path := entry.cron.GetLogPath() - level := entry.cron.GetLogLevel() + var ( + path = entry.cron.GetLogPath() + level = entry.cron.GetLogLevel() + ) switch entry.cron.status.Val() { case StatusStopped: return @@ -122,6 +124,23 @@ func (entry *Entry) check() { case StatusReady: fallthrough case StatusRunning: + defer func() { + if err := recover(); err != nil { + glog.Path(path).Level(level).Errorf( + "[gcron] %s(%s) %s end with error: %+v", + entry.Name, entry.schedule.pattern, entry.jobName, err, + ) + } else { + glog.Path(path).Level(level).Debugf( + "[gcron] %s(%s) %s end", + entry.Name, entry.schedule.pattern, entry.jobName, + ) + } + if entry.entry.Status() == StatusClosed { + entry.Close() + } + }() + // Running times check. times := entry.times.Add(-1) if times <= 0 { @@ -133,16 +152,7 @@ func (entry *Entry) check() { entry.times.Set(defaultTimes) } glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s start", entry.Name, entry.schedule.pattern, entry.jobName) - defer func() { - if err := recover(); err != nil { - glog.Path(path).Level(level).Errorf("[gcron] %s(%s) %s end with error: %v", entry.Name, entry.schedule.pattern, entry.jobName, err) - } else { - glog.Path(path).Level(level).Debugf("[gcron] %s(%s) %s end", entry.Name, entry.schedule.pattern, entry.jobName) - } - if entry.entry.Status() == StatusClosed { - entry.Close() - } - }() + entry.Job() } diff --git a/os/gcron/gcron_schedule.go b/os/gcron/gcron_schedule.go index bd50c4c1e..146c78547 100644 --- a/os/gcron/gcron_schedule.go +++ b/os/gcron/gcron_schedule.go @@ -31,7 +31,7 @@ type cronSchedule struct { const ( // regular expression for cron pattern, which contains 6 parts of time units. - gREGEX_FOR_CRON = `^([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,A-Za-z]+)\s+([\-/\d\*\?,A-Za-z]+)$` + regexForCron = `^([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,]+)\s+([\-/\d\*\?,A-Za-z]+)\s+([\-/\d\*\?,A-Za-z]+)$` ) var ( @@ -95,7 +95,7 @@ func newSchedule(pattern string) (*cronSchedule, error) { } // Handle the common cron pattern, like: // 0 0 0 1 1 2 - if match, _ := gregex.MatchString(gREGEX_FOR_CRON, pattern); len(match) == 7 { + if match, _ := gregex.MatchString(regexForCron, pattern); len(match) == 7 { schedule := &cronSchedule{ create: time.Now().Unix(), every: 0, diff --git a/os/glog/glog_api.go b/os/glog/glog_api.go index 243a7b993..4a26d84b7 100644 --- a/os/glog/glog_api.go +++ b/os/glog/glog_api.go @@ -6,14 +6,14 @@ package glog -// Print prints with newline using fmt.Sprintln. -// The parameter can be multiple variables. +// Print prints `v` with newline using fmt.Sprintln. +// The parameter `v` can be multiple variables. func Print(v ...interface{}) { logger.Print(v...) } -// Printf prints with format using fmt.Sprintf. -// The parameter can be multiple variables. +// Printf prints `v` with format `format` using fmt.Sprintf. +// The parameter `v` can be multiple variables. func Printf(format string, v ...interface{}) { logger.Printf(format, v...) } diff --git a/os/glog/glog_chaining.go b/os/glog/glog_chaining.go index 900e3bbea..90d93c0af 100644 --- a/os/glog/glog_chaining.go +++ b/os/glog/glog_chaining.go @@ -18,31 +18,31 @@ func Expose() *Logger { // Ctx is a chaining function, // which sets the context for current logging. -// The parameter specifies the context keys for retrieving values. +// The parameter `keys` specifies the context keys for retrieving values. func Ctx(ctx context.Context, keys ...interface{}) *Logger { return logger.Ctx(ctx, keys...) } // To is a chaining function, -// which redirects current logging content output to the sepecified . +// which redirects current logging content output to the sepecified `writer`. func To(writer io.Writer) *Logger { return logger.To(writer) } // Path is a chaining function, -// which sets the directory path to for current logging content output. +// which sets the directory path to `path` for current logging content output. func Path(path string) *Logger { return logger.Path(path) } // Cat is a chaining function, -// which sets the category to for current logging content output. +// which sets the category to `category` for current logging content output. func Cat(category string) *Logger { return logger.Cat(category) } // File is a chaining function, -// which sets file name for the current logging content output. +// which sets file name `pattern` for the current logging content output. func File(pattern string) *Logger { return logger.File(pattern) } @@ -94,7 +94,7 @@ func Header(enabled ...bool) *Logger { // Line is a chaining function, // which enables/disables printing its caller file along with its line number. -// The parameter specified whether print the long absolute file path, eg: /a/b/c/d.go:23. +// The parameter `long` specified whether print the long absolute file path, eg: /a/b/c/d.go:23. func Line(long ...bool) *Logger { return logger.Line(long...) } diff --git a/os/glog/glog_config.go b/os/glog/glog_config.go index 6e4402dee..e50c3218f 100644 --- a/os/glog/glog_config.go +++ b/os/glog/glog_config.go @@ -31,8 +31,8 @@ func GetPath() string { return logger.GetPath() } -// SetFile sets the file name for file logging. -// Datetime pattern can be used in , eg: access-{Ymd}.log. +// SetFile sets the file name `pattern` for file logging. +// Datetime pattern can be used in `pattern`, eg: access-{Ymd}.log. // The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log func SetFile(pattern string) { logger.SetFile(pattern) @@ -48,9 +48,9 @@ func GetLevel() int { return logger.GetLevel() } -// SetWriter sets the customized logging for logging. -// The object should implements the io.Writer interface. -// Developer can use customized logging to redirect logging output to another service, +// SetWriter sets the customized logging `writer` for logging. +// The `writer` object should implements the io.Writer interface. +// Developer can use customized logging `writer` to redirect logging output to another service, // eg: kafka, mysql, mongodb, etc. func SetWriter(writer io.Writer) { logger.SetWriter(writer) @@ -113,13 +113,13 @@ func GetCtxKeys() []interface{} { } // PrintStack prints the caller stack, -// the optional parameter specify the skipped stack offset from the end point. +// the optional parameter `skip` specify the skipped stack offset from the end point. func PrintStack(skip ...int) { logger.PrintStack(skip...) } // GetStack returns the caller stack content, -// the optional parameter specify the skipped stack offset from the end point. +// the optional parameter `skip` specify the skipped stack offset from the end point. func GetStack(skip ...int) string { return logger.GetStack(skip...) } diff --git a/os/glog/glog_instance.go b/os/glog/glog_instance.go index 42c741a03..49ca83264 100644 --- a/os/glog/glog_instance.go +++ b/os/glog/glog_instance.go @@ -19,7 +19,7 @@ var ( ) // Instance returns an instance of Logger with default settings. -// The parameter is the name for the instance. +// The parameter `name` is the name for the instance. func Instance(name ...string) *Logger { key := DefaultName if len(name) > 0 && name[0] != "" { diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 3a87e9aab..fea141fd4 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -93,7 +93,7 @@ func (l *Logger) getFilePath(now time.Time) string { return file } -// print prints to defined writer, logging file or passed . +// print prints `s` to defined writer, logging file or passed `std`. func (l *Logger) print(ctx context.Context, level int, values ...interface{}) { // Lazy initialize for rotation feature. // It uses atomic reading operation to enhance the performance checking. @@ -309,12 +309,12 @@ func (l *Logger) getCtx() context.Context { return context.TODO() } -// printStd prints content without stack. +// printStd prints content `s` without stack. func (l *Logger) printStd(level int, value ...interface{}) { l.print(l.getCtx(), level, value...) } -// printStd prints content with stack check. +// printStd prints content `s` with stack check. func (l *Logger) printErr(level int, value ...interface{}) { if l.config.StStatus == 1 { if s := l.GetStack(); s != "" { @@ -325,13 +325,13 @@ func (l *Logger) printErr(level int, value ...interface{}) { l.print(l.getCtx(), level, value...) } -// format formats using fmt.Sprintf. +// format formats `values` using fmt.Sprintf. func (l *Logger) format(format string, value ...interface{}) string { return fmt.Sprintf(format, value...) } // PrintStack prints the caller stack, -// the optional parameter specify the skipped stack offset from the end point. +// the optional parameter `skip` specify the skipped stack offset from the end point. func (l *Logger) PrintStack(skip ...int) { if s := l.GetStack(skip...); s != "" { l.Println("Stack:\n" + s) @@ -341,7 +341,7 @@ func (l *Logger) PrintStack(skip ...int) { } // GetStack returns the caller stack content, -// the optional parameter specify the skipped stack offset from the end point. +// the optional parameter `skip` specify the skipped stack offset from the end point. func (l *Logger) GetStack(skip ...int) string { stackSkip := l.config.StSkip if len(skip) > 0 { diff --git a/os/glog/glog_logger_api.go b/os/glog/glog_logger_api.go index 321da0752..8b211bc1c 100644 --- a/os/glog/glog_logger_api.go +++ b/os/glog/glog_logger_api.go @@ -11,14 +11,14 @@ import ( "os" ) -// Print prints with newline using fmt.Sprintln. -// The parameter can be multiple variables. +// Print prints `v` with newline using fmt.Sprintln. +// The parameter `v` can be multiple variables. func (l *Logger) Print(v ...interface{}) { l.printStd(LEVEL_NONE, v...) } -// Printf prints with format using fmt.Sprintf. -// The parameter can be multiple variables. +// Printf prints `v` with format `format` using fmt.Sprintf. +// The parameter `v` can be multiple variables. func (l *Logger) Printf(format string, v ...interface{}) { l.printStd(LEVEL_NONE, l.format(format, v...)) } @@ -145,7 +145,7 @@ func (l *Logger) Criticalf(format string, v ...interface{}) { } } -// checkLevel checks whether the given could be output. +// checkLevel checks whether the given `level` could be output. func (l *Logger) checkLevel(level int) bool { return l.config.Level&level > 0 } diff --git a/os/glog/glog_logger_chaining.go b/os/glog/glog_logger_chaining.go index c0213b982..89351df3f 100644 --- a/os/glog/glog_logger_chaining.go +++ b/os/glog/glog_logger_chaining.go @@ -34,7 +34,7 @@ func (l *Logger) Ctx(ctx context.Context, keys ...interface{}) *Logger { } // To is a chaining function, -// which redirects current logging content output to the specified . +// which redirects current logging content output to the specified `writer`. func (l *Logger) To(writer io.Writer) *Logger { logger := (*Logger)(nil) if l.parent == nil { @@ -47,9 +47,9 @@ func (l *Logger) To(writer io.Writer) *Logger { } // Path is a chaining function, -// which sets the directory path to for current logging content output. +// which sets the directory path to `path` for current logging content output. // -// Note that the parameter is a directory path, not a file path. +// Note that the parameter `path` is a directory path, not a file path. func (l *Logger) Path(path string) *Logger { logger := (*Logger)(nil) if l.parent == nil { @@ -67,8 +67,8 @@ func (l *Logger) Path(path string) *Logger { } // Cat is a chaining function, -// which sets the category to for current logging content output. -// Param can be hierarchical, eg: module/user. +// which sets the category to `category` for current logging content output. +// Param `category` can be hierarchical, eg: module/user. func (l *Logger) Cat(category string) *Logger { logger := (*Logger)(nil) if l.parent == nil { @@ -86,7 +86,7 @@ func (l *Logger) Cat(category string) *Logger { } // File is a chaining function, -// which sets file name for the current logging content output. +// which sets file name `pattern` for the current logging content output. func (l *Logger) File(file string) *Logger { logger := (*Logger)(nil) if l.parent == nil { @@ -181,7 +181,7 @@ func (l *Logger) Stdout(enabled ...bool) *Logger { } else { logger = l } - // stdout printing is enabled if is not passed. + // stdout printing is enabled if `enabled` is not passed. if len(enabled) > 0 && !enabled[0] { logger.config.StdoutPrint = false } else { @@ -200,7 +200,7 @@ func (l *Logger) Header(enabled ...bool) *Logger { } else { logger = l } - // header is enabled if is not passed. + // header is enabled if `enabled` is not passed. if len(enabled) > 0 && !enabled[0] { logger.SetHeaderPrint(false) } else { @@ -211,7 +211,7 @@ func (l *Logger) Header(enabled ...bool) *Logger { // Line is a chaining function, // which enables/disables printing its caller file path along with its line number. -// The parameter specified whether print the long absolute file path, eg: /a/b/c/d.go:23, +// The parameter `long` specified whether print the long absolute file path, eg: /a/b/c/d.go:23, // or else short one: d.go:23. func (l *Logger) Line(long ...bool) *Logger { logger := (*Logger)(nil) @@ -237,7 +237,7 @@ func (l *Logger) Async(enabled ...bool) *Logger { } else { logger = l } - // async feature is enabled if is not passed. + // async feature is enabled if `enabled` is not passed. if len(enabled) > 0 && !enabled[0] { logger.SetAsync(false) } else { diff --git a/os/glog/glog_logger_config.go b/os/glog/glog_logger_config.go index 6cc95ef05..33ed77043 100644 --- a/os/glog/glog_logger_config.go +++ b/os/glog/glog_logger_config.go @@ -188,9 +188,9 @@ func (l *Logger) GetCtxKeys() []interface{} { return l.config.CtxKeys } -// SetWriter sets the customized logging for logging. -// The object should implements the io.Writer interface. -// Developer can use customized logging to redirect logging output to another service, +// SetWriter sets the customized logging `writer` for logging. +// The `writer` object should implements the io.Writer interface. +// Developer can use customized logging `writer` to redirect logging output to another service, // eg: kafka, mysql, mongodb, etc. func (l *Logger) SetWriter(writer io.Writer) { l.config.Writer = writer @@ -222,8 +222,8 @@ func (l *Logger) GetPath() string { return l.config.Path } -// SetFile sets the file name for file logging. -// Datetime pattern can be used in , eg: access-{Ymd}.log. +// SetFile sets the file name `pattern` for file logging. +// Datetime pattern can be used in `pattern`, eg: access-{Ymd}.log. // The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log func (l *Logger) SetFile(pattern string) { l.config.File = pattern