fix logger path bug
This commit is contained in:
		
							parent
							
								
									f82e9cfdbe
								
							
						
					
					
						commit
						095e3e9d4c
					
				@ -3,8 +3,8 @@ package utils
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"database/sql/driver"
 | 
						"database/sql/driver"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"regexp"
 | 
					 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
@ -16,7 +16,18 @@ var gormSourceDir string
 | 
				
			|||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	_, file, _, _ := runtime.Caller(0)
 | 
						_, file, _, _ := runtime.Caller(0)
 | 
				
			||||||
	// compatible solution to get gorm source directory with various operating systems
 | 
						// compatible solution to get gorm source directory with various operating systems
 | 
				
			||||||
	gormSourceDir = regexp.MustCompile(`gorm.utils.utils\.go`).ReplaceAllString(file, "")
 | 
						gormSourceDir = sourceDir(file)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func sourceDir(file string) string {
 | 
				
			||||||
 | 
						dir := filepath.Dir(file)
 | 
				
			||||||
 | 
						dir = filepath.Dir(dir)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						parent := filepath.Dir(dir)
 | 
				
			||||||
 | 
						if filepath.Base(parent) == "gorm.io" {
 | 
				
			||||||
 | 
							return parent
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return dir
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FileWithLineNum return the file name and line number of the current file
 | 
					// FileWithLineNum return the file name and line number of the current file
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										33
									
								
								utils/utils_unix_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								utils/utils_unix_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSourceDir(t *testing.T) {
 | 
				
			||||||
 | 
						cases := []struct {
 | 
				
			||||||
 | 
							file string
 | 
				
			||||||
 | 
							want string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: "/Users/name/go/pkg/mod/gorm.io/gorm@v1.24.1/utils/utils.go",
 | 
				
			||||||
 | 
								want: "/Users/name/go/pkg/mod/gorm.io",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: "/go/work/proj/gorm/utils/utils.go",
 | 
				
			||||||
 | 
								want: "/go/work/proj/gorm",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: "/go/work/proj/gorm_alias/utils/utils.go",
 | 
				
			||||||
 | 
								want: "/go/work/proj/gorm_alias",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: "/go/work/proj/my.gorm.io/gorm@v1.24.1/utils/utils.go",
 | 
				
			||||||
 | 
								want: "/go/work/proj/my.gorm.io/gorm@v1.24.1",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, c := range cases {
 | 
				
			||||||
 | 
							s := sourceDir(c.file)
 | 
				
			||||||
 | 
							if s != c.want {
 | 
				
			||||||
 | 
								t.Fatalf("%s: expected %s, got %s", c.file, c.want, s)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										33
									
								
								utils/utils_windows_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								utils/utils_windows_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package utils
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestSourceDir(t *testing.T) {
 | 
				
			||||||
 | 
						cases := []struct {
 | 
				
			||||||
 | 
							file string
 | 
				
			||||||
 | 
							want string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: `C:\Users\name\go\pkg\mod\gorm.io\gorm@v1.20.8\utils\utils.go`,
 | 
				
			||||||
 | 
								want: `C:\Users\name\go\pkg\mod\gorm.io`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: `C:\go\work\proj\gorm\utils\utils.go`,
 | 
				
			||||||
 | 
								want: `C:\go\work\proj\gorm`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: `C:\go\work\proj\gorm_alias\utils\utils.go`,
 | 
				
			||||||
 | 
								want: `C:\go\work\proj\gorm_alias`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								file: `C:\go\work\proj\my.gorm.io\gorm\utils\utils.go`,
 | 
				
			||||||
 | 
								want: `C:\go\work\proj\my.gorm.io\gorm`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for _, c := range cases {
 | 
				
			||||||
 | 
							s := sourceDir(c.file)
 | 
				
			||||||
 | 
							if s != c.want {
 | 
				
			||||||
 | 
								t.Fatalf("%s: expected %s, got %s", c.file, c.want, s)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user