err-log.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 LogStr(index, " Count Query Error "+tablename+" ")
  34. }
  35. func PageRead(index string, tablename string) string {
  36. return LogStr(index, " Page Read from "+tablename+" ")
  37. }
  38. func PageQryErr(index string, tablename string) string {
  39. return LogStr(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 LogStr(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 LogStr(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 LogStr(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, " Function Run from "+funcname+" ")
  79. }
  80. func FuncRunErr(index string, funcname string) string {
  81. return LogStr(index, " Function Run Error in "+funcname+" ")
  82. }
  83. func JsonFormatErr(index string, structname string) string {
  84. return LogStr(index, " Json Format mismatches in "+structname+" ")
  85. }
  86. func LogStr(index string, s string) string { // nㅣl 아님 경우만 처리(!!중요)
  87. msg := s
  88. str := index + " @ " + msg
  89. log.Println("[Log]: " + str)
  90. return msg
  91. }
  92. func LogErr(index string, s string, err error) error { // nㅣl 아님 경우만 처리(!!중요)
  93. var errStr string
  94. if err != nil {
  95. errStr = err.Error()
  96. } else {
  97. log.Println("========= Fatal: error is nil LogErr ==========")
  98. }
  99. msg := s + " * " + errStr
  100. str := index + " @ " + msg
  101. log.Println("[Cnd]: " + str)
  102. return errors.New(msg)
  103. }
  104. func CurrFuncName() string { // nㅣl 아님 경우만 처리(!!중요)
  105. pc := make([]uintptr, 15)
  106. n := runtime.Callers(2, pc)
  107. frames := runtime.CallersFrames(pc[:n])
  108. frame, _ := frames.Next()
  109. s := frame.Function
  110. // fmt.Println(s[strings.LastIndex(s, "/")+1:])
  111. // fmt.Printf("%s:%d %s\n", frame.File, frame.Line, frame.Function)
  112. return s[strings.LastIndex(s, "/")+1:] + " "
  113. }
  114. func LogCritical(index string, s string, err error) { //에러 ㄱ계를 추적
  115. var errStr string
  116. if err != nil {
  117. errStr = err.Error()
  118. } else {
  119. log.Println("========= Fatal: error is nil LogCritical==========")
  120. }
  121. str := index + " @ " + s + " * " + errStr
  122. log.Println("[Fatal]: " + str)
  123. whereami(2)
  124. whereami(3)
  125. whereami(4)
  126. fmt.Println(strings.Repeat("=", 80))
  127. }
  128. func LogFatal(index string, s string, err error) { //Critical 동일하지만 프로세스 중단
  129. var errStr string
  130. if err != nil {
  131. errStr = err.Error()
  132. } else {
  133. log.Println("========= Fatal: error is nil LogFatal ==========")
  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. os.Exit(100)
  142. }
  143. // ==== 아래건은 모두 옛날 것이라 차츰 Deprecate 할 것 =====
  144. func OkLog(s string) error {
  145. // log.Logger
  146. log.Println("[OK]: " + s)
  147. return nil
  148. }
  149. func AokLog(s string) {
  150. log.Println("[Abango-OK]: " + s)
  151. }
  152. func ErrLog(s string, err error) error { // // nㅣl처리 아주 중요함 ( 이건 이제 더 사용하지 말것)
  153. var errStr string
  154. if err != nil {
  155. errStr = err.Error()
  156. } else {
  157. log.Println("========= Fatal: error is nil ErrLog==========")
  158. }
  159. str := "[Error]: " + s + " (Err): " + errStr
  160. log.Println(str)
  161. return errors.New(str)
  162. }
  163. // === 여기 까지 =====
  164. func ChkLog(point string, x ...interface{}) {
  165. log.Println("[CHECK:" + point + "] " + fmt.Sprintf("%v", x))
  166. }
  167. // func FatalLog(point string, err error) {
  168. // fmt.Println("[FATAL-ERROR]: "+point, err)
  169. // os.Exit(1000)
  170. // }
  171. func MyErr(s string, e error, eout bool) error {
  172. fmt.Println("[MyErr] Position -> ", s, strings.Repeat("=", 40))
  173. emsg := ""
  174. if e != nil {
  175. emsg = "Error: " + e.Error()
  176. } else {
  177. emsg = "ERROR is Nil: Wrong Error Check: Check err != OR err == is correct ! "
  178. }
  179. fmt.Println(emsg, "\n")
  180. whereami(2)
  181. whereami(3)
  182. whereami(4)
  183. fmt.Println(strings.Repeat("=", 80))
  184. if e != nil && eout == true { // quit running if it is FATAL ERROR
  185. log.Println("[FATAL-ERROR] : EXIT 100")
  186. os.Exit(100)
  187. }
  188. return errors.New(emsg)
  189. }
  190. func Tp(a ...interface{}) {
  191. fmt.Println(a)
  192. }
  193. func Atp(a ...interface{}) {
  194. fmt.Println("[Abango]->", a)
  195. }
  196. func agErr(s string, e error, amsg *string) string {
  197. fmt.Println("== agErr ", strings.Repeat("=", 90))
  198. // fpcs := make([]uintptr, 1)
  199. // n := runtime.Callers(2, fpcs)
  200. // if n == 0 {
  201. // fmt.Println("MSG: NO CALLER")
  202. // }
  203. // // caller := runtime.FuncForPC(fpcs[0] - 1)
  204. // caller := runtime.FuncForPC(fpcs[0])
  205. // // fmt.Println(caller.FileLine(fpcs[0] - 1))
  206. // fmt.Println(caller.FileLine(fpcs[0]))
  207. // fmt.Println(caller.Name())
  208. emsg := ""
  209. if e != nil {
  210. emsg = "Error: " + e.Error() + " in " + s
  211. } else {
  212. emsg = "Error: error is nil" + " in " + s // e 가 nil 인 상태에서 Error() 인용시 runtime error
  213. }
  214. fmt.Println(emsg, "\n")
  215. whereami(2)
  216. whereami(3)
  217. fmt.Println(strings.Repeat("=", 100))
  218. return emsg
  219. }
  220. func whereami(i int) {
  221. function, file, line, _ := runtime.Caller(i)
  222. fmt.Printf(" %d.File: %s - %d %s\n func: %s \n", i, chopPath(file), line, file, runtime.FuncForPC(function).Name())
  223. }
  224. func WhereAmI(depthList ...int) {
  225. var depth int
  226. if depthList == nil {
  227. depth = 1
  228. } else {
  229. depth = depthList[0]
  230. }
  231. // function, file, line, _ := runtime.Caller(depth)
  232. for i := 0; i < depth+1; i++ {
  233. function, file, line, _ := runtime.Caller(i)
  234. fmt.Printf("==Level %d==\n", i)
  235. fmt.Printf("File: %s - %d %s\nFunction: %s \n", chopPath(file), line, file, runtime.FuncForPC(function).Name())
  236. }
  237. fmt.Printf("==End==\n")
  238. return
  239. }
  240. // return the source filename after the last slash
  241. func chopPath(original string) string {
  242. i := strings.LastIndex(original, "/")
  243. if i == -1 {
  244. return original
  245. } else {
  246. return original[i+1:]
  247. }
  248. }