| 
									
										
										
										
											2024-04-17 17:10:51 +01:00
										 |  |  | // Copyright 2023 The Libc Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-27 22:53:36 +00:00
										 |  |  | //go:build linux && (amd64 || arm64 || loong64) | 
					
						
							| 
									
										
										
										
											2024-04-17 17:10:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-27 22:53:36 +00:00
										 |  |  | package libc // import "modernc.org/libc" | 
					
						
							| 
									
										
										
										
											2024-04-17 17:10:51 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"runtime" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // origin returns caller's short position, skipping skip frames. | 
					
						
							|  |  |  | func origin(skip int) string { | 
					
						
							|  |  |  | 	pc, fn, fl, _ := runtime.Caller(skip) | 
					
						
							|  |  |  | 	f := runtime.FuncForPC(pc) | 
					
						
							|  |  |  | 	var fns string | 
					
						
							|  |  |  | 	if f != nil { | 
					
						
							|  |  |  | 		fns = f.Name() | 
					
						
							|  |  |  | 		if x := strings.LastIndex(fns, "."); x > 0 { | 
					
						
							|  |  |  | 			fns = fns[x+1:] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if strings.HasPrefix(fns, "func") { | 
					
						
							|  |  |  | 			num := true | 
					
						
							|  |  |  | 			for _, c := range fns[len("func"):] { | 
					
						
							|  |  |  | 				if c < '0' || c > '9' { | 
					
						
							|  |  |  | 					num = false | 
					
						
							|  |  |  | 					break | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if num { | 
					
						
							|  |  |  | 				return origin(skip + 2) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // todo prints and return caller's position and an optional message tagged with TODO. Output goes to stderr. | 
					
						
							|  |  |  | func todo(s string, args ...interface{}) string { | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case s == "": | 
					
						
							|  |  |  | 		s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...) | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		s = fmt.Sprintf(s, args...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	r := fmt.Sprintf("%s\n\tTODO %s", origin(2), s) | 
					
						
							|  |  |  | 	// fmt.Fprintf(os.Stderr, "%s\n", r) | 
					
						
							|  |  |  | 	// os.Stdout.Sync() | 
					
						
							|  |  |  | 	return r | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // trc prints and return caller's position and an optional message tagged with TRC. Output goes to stderr. | 
					
						
							|  |  |  | func trc(s string, args ...interface{}) string { | 
					
						
							|  |  |  | 	switch { | 
					
						
							|  |  |  | 	case s == "": | 
					
						
							|  |  |  | 		s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...) | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		s = fmt.Sprintf(s, args...) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	r := fmt.Sprintf("%s: TRC %s", origin(2), s) | 
					
						
							|  |  |  | 	fmt.Fprintf(os.Stderr, "%s\n", r) | 
					
						
							|  |  |  | 	os.Stderr.Sync() | 
					
						
							|  |  |  | 	return r | 
					
						
							|  |  |  | } |