From f0668bfe73c443c9fcf387693a0e816721198960 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 29 May 2018 13:40:36 +0800 Subject: [PATCH] =?UTF-8?q?gfile=E5=8C=85=E5=A2=9E=E5=8A=A0GoRootOfBuild?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=97=B6=E7=9A=84GOROOT=E6=95=B0=E5=80=BC?= =?UTF-8?q?=EF=BC=9B=E5=B9=B6=E6=94=B9=E8=BF=9Bglog=E5=8C=85=E4=B8=ADbackt?= =?UTF-8?q?race=E7=9A=84GOROOT=E8=B7=AF=E5=BE=84=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=9BTODO++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 ++ g/os/gfile/gfile.go | 41 +++++++++++++++++++++++++++++++++++----- g/os/glog/glog_logger.go | 2 +- geg/other/test.go | 19 ++----------------- 4 files changed, 41 insertions(+), 23 deletions(-) diff --git a/TODO b/TODO index cb8ad6a2b..4902d4d64 100644 --- a/TODO +++ b/TODO @@ -8,6 +8,8 @@ orm增加更多数据库支持; 增加可选择性的orm tag特性,用以数据表记录与struct对象转换的键名属性映射; ghttp.Response增加输出内容后自动退出当前请求机制,不需要用户手动return,参考beego如何实现; 对grpool进行优化改进,包括属性原子操作封装采用gtype实现,修正设计BUG:https://github.com/johng-cn/gf/issues/6; +平滑重启机制改进,去掉多进程模式,改为单进程模式,以便于开发阶段调试; + DONE: 1. gconv完善针对不同类型的判断,例如:尽量减少sprintf("%v", xxx)来执行string类型的转换; diff --git a/g/os/gfile/gfile.go b/g/os/gfile/gfile.go index 7275f6729..ee2f9a8d7 100644 --- a/g/os/gfile/gfile.go +++ b/g/os/gfile/gfile.go @@ -33,7 +33,10 @@ const ( ) // 源码的main包所在目录,仅仅会设置一次 -var mainPkgPath = gtype.NewInterface() +var mainPkgPath = gtype.NewString() + +// 编译时的 GOROOT 数值 +var goRootOfBuild = gtype.NewString() // 给定文件的绝对路径创建文件 func Mkdir(path string) error { @@ -457,17 +460,18 @@ func GetBinContentByTwoOffsets(file *os.File, start int64, end int64) []byte { return buffer } -// 获取入口函数文件所在目录(main包文件目录),仅对源码开发环境有效(即仅对生成该可执行文件的系统下有效) +// 获取入口函数文件所在目录(main包文件目录), +// **仅对源码开发环境有效(即仅对生成该可执行文件的系统下有效)** func MainPkgPath() string { path := mainPkgPath.Val() - if path != nil { - return path.(string) + if path != "" { + return path } f := "" for i := 1; i < 10000; i++ { if _, file, _, ok := runtime.Caller(i); ok { // 不包含go源码路径 - if !gregx.IsMatchString("^" + runtime.GOROOT(), file) { + if !gregx.IsMatchString("^" + GoRootOfBuild(), file) { f = file } } else { @@ -482,6 +486,33 @@ func MainPkgPath() string { return "" } +// 编译时环境的GOROOT数值 +func GoRootOfBuild() string { + if v := goRootOfBuild.Val(); v != "" { + return v + } + firstEntry := "" + for i := 0; i < 10000; i++ { + if _, file, _, ok := runtime.Caller(i); ok { + firstEntry = file + } else { + break + } + } + if len(firstEntry) > 0 { + sep := "/" + array := strings.Split(firstEntry, sep) + if len(array) == 1 { + sep = "\\" + array = strings.Split(firstEntry, sep) + } + root := strings.Join(array[0 : len(array) - 3], sep) + goRootOfBuild.Set(root) + return root + } + return "" +} + // 系统临时目录 func TempDir() string { return os.TempDir() diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index b00f56a14..5117b04f7 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -146,7 +146,7 @@ func (l *Logger) backtrace() string { for i := 1; i < 10000; i++ { if _, cfile, cline, ok := runtime.Caller(i + l.btSkip.Val()); ok { // 不打印出go源码路径 - if !gregx.IsMatchString("^" + runtime.GOROOT(), cfile) { + if !gregx.IsMatchString("^" + gfile.GoRootOfBuild(), cfile) { backtrace += strconv.Itoa(index) + ". " + cfile + ":" + strconv.Itoa(cline) + ln index++ } diff --git a/geg/other/test.go b/geg/other/test.go index fdcf1f6c8..6e4f24752 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,22 +1,7 @@ package main -import ( - "fmt" - "time" - "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/os/gproc" -) +import "gitee.com/johng/gf/g/os/glog" func main() { - if !gproc.IsChild() { - go func() { - for { - fmt.Println("test") - time.Sleep(2 * time.Second) - } - }() - } - s := g.Server() - s.SetPort(9000) - s.Run() + glog.Error(1) } \ No newline at end of file