err-log.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Author : Eric Kim
  2. // Build Date : 23 Jul 2018 Last Update 02 Aug 2018
  3. // End-Agent for Passcon Multi OS go binding with Windows, MacOS, iOS, and Android
  4. // All rights are reserved.
  5. package etc
  6. import (
  7. "errors"
  8. "fmt"
  9. "io"
  10. "log"
  11. "os"
  12. "runtime"
  13. "strings"
  14. )
  15. // var InfoLog *log.Logger
  16. func InitLog(path string, showstdout string) error {
  17. file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
  18. if err != nil {
  19. return MyErr("CXZFDREADSF-MyLog file could not be opened: ", err, true)
  20. }
  21. // defer file.Close()
  22. // 파일과 화면에 같이 출력하기 위해 MultiWriter 생성
  23. if showstdout == "Yes" {
  24. multiWriter := io.MultiWriter(file, os.Stdout)
  25. log.SetOutput(multiWriter)
  26. } else {
  27. log.SetOutput(file)
  28. }
  29. // InfoLog = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
  30. return nil
  31. }
  32. func PageCntErr(index string, tablename string) string {
  33. return ErrStr(index, " Count Query Error "+tablename+" ")
  34. }
  35. func PageRead(index string, tablename string) string {
  36. return ErrStr(index, " Page Read from "+tablename+" ")
  37. }
  38. func PageQryErr(index string, tablename string) string {
  39. return ErrStr(index, " Page Query Error "+tablename+" ")
  40. }
  41. func RecRead(index string, msgString string) string {
  42. return LogStr(index, " Read "+msgString+" ")
  43. }
  44. func RecNotFound(index string, msgString string) string {
  45. return LogStr(index, " Not Found "+msgString+" ")
  46. }
  47. func RecReadErr(index string, msgString string) string {
  48. return ErrStr(index, " B error in Reading "+msgString+" ")
  49. }
  50. func RecAdded(index string, msgString string) string {
  51. return LogStr(index, " Add "+msgString+" ")
  52. }
  53. func RecNotAdded(index string, msgString string) string {
  54. return LogStr(index, " Not Added "+msgString+" ")
  55. }
  56. func RecAddErr(index string, msgString string) string {
  57. return ErrStr(index, " DB error in Adding "+msgString+" ")
  58. }
  59. func RecEdited(index string, msgString string) string {
  60. return LogStr(index, " Edited "+msgString+" ")
  61. }
  62. func RecNotEdited(index string, msgString string) string {
  63. return LogStr(index, " Not Edited-'Same Contents Update' is NOT necessary with "+msgString+" ")
  64. }
  65. func RecEditErr(index string, msgString string) string {
  66. return ErrStr(index, " DB error in Editing "+msgString+" ")
  67. }
  68. func RecDeleted(index string, msgString string) string {
  69. return LogStr(index, " Delete "+msgString+" ")
  70. }
  71. func RecNotDeleted(index string, msgString string) string {
  72. return LogStr(index, " Not Deleted "+msgString+" ")
  73. }
  74. func RecDelErr(index string, msgString string) string {
  75. return LogStr(index, " DB error in Deleting "+msgString+" ")
  76. }
  77. func FuncRun(index string, funcname string) string {
  78. return LogStr(index, " Func : "+funcname+" ")
  79. }
  80. func FuncRunErr(index string, funcname string) string {
  81. return ErrStr(index, "-> "+funcname+" ")
  82. }
  83. func JsonFormatErr(index string, structname string) string {
  84. return ErrStr(index, " Json Format mismatches in "+structname+" ")
  85. }
  86. func ErrStr(index string, s string) string { // nㅣl 아님 경우만 처리(!!중요)
  87. msg := s
  88. str := index + " @ " + msg
  89. log.Println("[Err]: " + str)
  90. return msg
  91. }
  92. func LogStr(index string, s string) string { // nㅣl 아님 경우만 처리(!!중요)
  93. msg := s
  94. str := index + " @ " + msg
  95. log.Println("[Log]: " + str)
  96. return msg
  97. }
  98. func LogErr(index string, s string, err error) error { // nㅣl 아님 경우만 처리(!!중요)
  99. var errStr string
  100. if err != nil {
  101. errStr = err.Error()
  102. } else {
  103. log.Println("========= Fatal: error is nil LogErr ==========")
  104. }
  105. msg := s + " * " + errStr
  106. str := index + " @ " + msg
  107. log.Println("[Err]: " + str)
  108. return errors.New(msg)
  109. }
  110. func CurrFuncName() string { // nil 아닌 경우만 처리(!!중요)
  111. pc := make([]uintptr, 15)
  112. n := runtime.Callers(2, pc)
  113. frames := runtime.CallersFrames(pc[:n])
  114. frame, _ := frames.Next()
  115. fmt.Printf("FuncPath: %s:%d %s\n", frame.File, frame.Line, frame.Function)
  116. // fmt.Println("Lastindex:", s[strings.LastIndex(s, "/")+1:])
  117. s := frame.Function
  118. return s[strings.LastIndex(s, "/")+1:] + " "
  119. }
  120. func LogFuncName() string {
  121. pc := make([]uintptr, 15)
  122. n := runtime.Callers(2, pc)
  123. frames := runtime.CallersFrames(pc[:n])
  124. frame, _ := frames.Next()
  125. s := frame.Function
  126. return s[strings.LastIndex(s, "/")+1:] + " "
  127. }
  128. func LogCritical(index string, s string, err error) { //에러 ㄱ계를 추적
  129. var errStr string
  130. if err != nil {
  131. errStr = err.Error()
  132. } else {
  133. log.Println("========= Fatal: error is nil LogCritical==========")
  134. }
  135. str := index + " @ " + s + " * " + errStr
  136. log.Println("[Fatal]: " + str)
  137. whereami(2)
  138. whereami(3)
  139. whereami(4)
  140. fmt.Println(strings.Repeat("=", 80))
  141. }
  142. func LogFatal(index string, s string, err error) { //Critical 동일하지만 프로세스 중단
  143. var errStr string
  144. if err != nil {
  145. errStr = err.Error()
  146. } else {
  147. log.Println("========= Fatal: error is nil LogFatal ==========")
  148. }
  149. str := index + " @ " + s + " * " + errStr
  150. log.Println("[Fatal]: " + str)
  151. whereami(2)
  152. whereami(3)
  153. whereami(4)
  154. fmt.Println(strings.Repeat("=", 80))
  155. os.Exit(100)
  156. }
  157. // ==== 아래건은 모두 옛날 것이라 차츰 Deprecate 할 것 =====
  158. func OkLog(s string) error {
  159. // log.Logger
  160. log.Println("[OK]: " + s)
  161. return nil
  162. }
  163. func AokLog(s string) {
  164. log.Println("[Abango-OK]: " + s)
  165. }
  166. func ErrLog(s string, err error) error { // // nㅣl처리 아주 중요함 ( 이건 이제 더 사용하지 말것)
  167. var errStr string
  168. if err != nil {
  169. errStr = err.Error()
  170. } else {
  171. log.Println("========= Fatal: error is nil ErrLog==========")
  172. }
  173. str := "[Err]: " + s + " (Err): " + errStr
  174. log.Println(str)
  175. return errors.New(str)
  176. }
  177. // === 여기 까지 =====
  178. func ChkLog(point string, x ...interface{}) {
  179. log.Println("[CHECK:" + point + "] " + fmt.Sprintf("%v", x))
  180. }
  181. // func FatalLog(point string, err error) {
  182. // fmt.Println("[FATAL-ERROR]: "+point, err)
  183. // os.Exit(1000)
  184. // }
  185. func MyErr(s string, e error, eout bool) error {
  186. fmt.Println("[MyErr] Position -> ", s, strings.Repeat("=", 40))
  187. emsg := ""
  188. if e != nil {
  189. emsg = "Error: " + e.Error()
  190. } else {
  191. emsg = "ERROR is Nil: Wrong Error Check: Check err != OR err == is correct ! "
  192. }
  193. fmt.Println(emsg, "\n")
  194. whereami(2)
  195. whereami(3)
  196. whereami(4)
  197. fmt.Println(strings.Repeat("=", 80))
  198. if e != nil && eout == true { // quit running if it is FATAL ERROR
  199. log.Println("[FATAL-ERROR] : EXIT 100")
  200. os.Exit(100)
  201. }
  202. return errors.New(emsg)
  203. }
  204. func Tp(a ...interface{}) {
  205. fmt.Println(a)
  206. }
  207. func Atp(a ...interface{}) {
  208. fmt.Println("[Abango]->", a)
  209. }
  210. func agErr(s string, e error, amsg *string) string {
  211. fmt.Println("== agErr ", strings.Repeat("=", 90))
  212. // fpcs := make([]uintptr, 1)
  213. // n := runtime.Callers(2, fpcs)
  214. // if n == 0 {
  215. // fmt.Println("MSG: NO CALLER")
  216. // }
  217. // // caller := runtime.FuncForPC(fpcs[0] - 1)
  218. // caller := runtime.FuncForPC(fpcs[0])
  219. // // fmt.Println(caller.FileLine(fpcs[0] - 1))
  220. // fmt.Println(caller.FileLine(fpcs[0]))
  221. // fmt.Println(caller.Name())
  222. emsg := ""
  223. if e != nil {
  224. emsg = "Error: " + e.Error() + " in " + s
  225. } else {
  226. emsg = "Error: error is nil" + " in " + s // e 가 nil 인 상태에서 Error() 인용시 runtime error
  227. }
  228. fmt.Println(emsg, "\n")
  229. whereami(2)
  230. whereami(3)
  231. fmt.Println(strings.Repeat("=", 100))
  232. return emsg
  233. }
  234. func whereami(i int) {
  235. function, file, line, _ := runtime.Caller(i)
  236. fmt.Printf(" %d.File: %s - %d %s\n func: %s \n", i, chopPath(file), line, file, runtime.FuncForPC(function).Name())
  237. }
  238. func WhereAmI(depthList ...int) {
  239. var depth int
  240. if depthList == nil {
  241. depth = 1
  242. } else {
  243. depth = depthList[0]
  244. }
  245. // function, file, line, _ := runtime.Caller(depth)
  246. for i := 0; i < depth+1; i++ {
  247. function, file, line, _ := runtime.Caller(i)
  248. fmt.Printf("==Level %d==\n", i)
  249. fmt.Printf("File: %s - %d %s\nFunction: %s \n", chopPath(file), line, file, runtime.FuncForPC(function).Name())
  250. }
  251. fmt.Printf("==End==\n")
  252. return
  253. }
  254. // return the source filename after the last slash
  255. func chopPath(original string) string {
  256. i := strings.LastIndex(original, "/")
  257. if i == -1 {
  258. return original
  259. } else {
  260. return original[i+1:]
  261. }
  262. }