err-log.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 { // nㅣl 아님 경우만 처리(!!중요)
  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 LogCritical(index string, s string, err error) { //에러 ㄱ계를 추적
  121. var errStr string
  122. if err != nil {
  123. errStr = err.Error()
  124. } else {
  125. log.Println("========= Fatal: error is nil LogCritical==========")
  126. }
  127. str := index + " @ " + s + " * " + errStr
  128. log.Println("[Fatal]: " + str)
  129. whereami(2)
  130. whereami(3)
  131. whereami(4)
  132. fmt.Println(strings.Repeat("=", 80))
  133. }
  134. func LogFatal(index string, s string, err error) { //Critical 동일하지만 프로세스 중단
  135. var errStr string
  136. if err != nil {
  137. errStr = err.Error()
  138. } else {
  139. log.Println("========= Fatal: error is nil LogFatal ==========")
  140. }
  141. str := index + " @ " + s + " * " + errStr
  142. log.Println("[Fatal]: " + str)
  143. whereami(2)
  144. whereami(3)
  145. whereami(4)
  146. fmt.Println(strings.Repeat("=", 80))
  147. os.Exit(100)
  148. }
  149. // ==== 아래건은 모두 옛날 것이라 차츰 Deprecate 할 것 =====
  150. func OkLog(s string) error {
  151. // log.Logger
  152. log.Println("[OK]: " + s)
  153. return nil
  154. }
  155. func AokLog(s string) {
  156. log.Println("[Abango-OK]: " + s)
  157. }
  158. func ErrLog(s string, err error) error { // // nㅣl처리 아주 중요함 ( 이건 이제 더 사용하지 말것)
  159. var errStr string
  160. if err != nil {
  161. errStr = err.Error()
  162. } else {
  163. log.Println("========= Fatal: error is nil ErrLog==========")
  164. }
  165. str := "[Error]: " + s + " (Err): " + errStr
  166. log.Println(str)
  167. return errors.New(str)
  168. }
  169. // === 여기 까지 =====
  170. func ChkLog(point string, x ...interface{}) {
  171. log.Println("[CHECK:" + point + "] " + fmt.Sprintf("%v", x))
  172. }
  173. // func FatalLog(point string, err error) {
  174. // fmt.Println("[FATAL-ERROR]: "+point, err)
  175. // os.Exit(1000)
  176. // }
  177. func MyErr(s string, e error, eout bool) error {
  178. fmt.Println("[MyErr] Position -> ", s, strings.Repeat("=", 40))
  179. emsg := ""
  180. if e != nil {
  181. emsg = "Error: " + e.Error()
  182. } else {
  183. emsg = "ERROR is Nil: Wrong Error Check: Check err != OR err == is correct ! "
  184. }
  185. fmt.Println(emsg, "\n")
  186. whereami(2)
  187. whereami(3)
  188. whereami(4)
  189. fmt.Println(strings.Repeat("=", 80))
  190. if e != nil && eout == true { // quit running if it is FATAL ERROR
  191. log.Println("[FATAL-ERROR] : EXIT 100")
  192. os.Exit(100)
  193. }
  194. return errors.New(emsg)
  195. }
  196. func Tp(a ...interface{}) {
  197. fmt.Println(a)
  198. }
  199. func Atp(a ...interface{}) {
  200. fmt.Println("[Abango]->", a)
  201. }
  202. func agErr(s string, e error, amsg *string) string {
  203. fmt.Println("== agErr ", strings.Repeat("=", 90))
  204. // fpcs := make([]uintptr, 1)
  205. // n := runtime.Callers(2, fpcs)
  206. // if n == 0 {
  207. // fmt.Println("MSG: NO CALLER")
  208. // }
  209. // // caller := runtime.FuncForPC(fpcs[0] - 1)
  210. // caller := runtime.FuncForPC(fpcs[0])
  211. // // fmt.Println(caller.FileLine(fpcs[0] - 1))
  212. // fmt.Println(caller.FileLine(fpcs[0]))
  213. // fmt.Println(caller.Name())
  214. emsg := ""
  215. if e != nil {
  216. emsg = "Error: " + e.Error() + " in " + s
  217. } else {
  218. emsg = "Error: error is nil" + " in " + s // e 가 nil 인 상태에서 Error() 인용시 runtime error
  219. }
  220. fmt.Println(emsg, "\n")
  221. whereami(2)
  222. whereami(3)
  223. fmt.Println(strings.Repeat("=", 100))
  224. return emsg
  225. }
  226. func whereami(i int) {
  227. function, file, line, _ := runtime.Caller(i)
  228. fmt.Printf(" %d.File: %s - %d %s\n func: %s \n", i, chopPath(file), line, file, runtime.FuncForPC(function).Name())
  229. }
  230. func WhereAmI(depthList ...int) {
  231. var depth int
  232. if depthList == nil {
  233. depth = 1
  234. } else {
  235. depth = depthList[0]
  236. }
  237. // function, file, line, _ := runtime.Caller(depth)
  238. for i := 0; i < depth+1; i++ {
  239. function, file, line, _ := runtime.Caller(i)
  240. fmt.Printf("==Level %d==\n", i)
  241. fmt.Printf("File: %s - %d %s\nFunction: %s \n", chopPath(file), line, file, runtime.FuncForPC(function).Name())
  242. }
  243. fmt.Printf("==End==\n")
  244. return
  245. }
  246. // return the source filename after the last slash
  247. func chopPath(original string) string {
  248. i := strings.LastIndex(original, "/")
  249. if i == -1 {
  250. return original
  251. } else {
  252. return original[i+1:]
  253. }
  254. }