err-log.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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:] + "\n"
  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. func ChkLog(point string, x ...interface{}) {
  178. log.Println("[CHECK:" + point + "] " + fmt.Sprintf("%v", x))
  179. }
  180. func MyErr(s string, e error, eout bool) error {
  181. fmt.Println("[MyErr] Position -> ", s, strings.Repeat("=", 40))
  182. emsg := ""
  183. if e != nil {
  184. emsg = "Error: " + e.Error()
  185. } else {
  186. emsg = "ERROR is Nil: Wrong Error Check: Check err != OR err == is correct ! "
  187. }
  188. fmt.Println(emsg, "\n")
  189. whereami(2)
  190. whereami(3)
  191. whereami(4)
  192. fmt.Println(strings.Repeat("=", 80))
  193. if e != nil && eout == true { // quit running if it is FATAL ERROR
  194. log.Println("[FATAL-ERROR] : EXIT 100")
  195. os.Exit(100)
  196. }
  197. return errors.New(emsg)
  198. }
  199. func agErr(s string, e error, amsg *string) string {
  200. fmt.Println("== agErr ", strings.Repeat("=", 90))
  201. // fpcs := make([]uintptr, 1)
  202. // n := runtime.Callers(2, fpcs)
  203. // if n == 0 {
  204. // fmt.Println("MSG: NO CALLER")
  205. // }
  206. // // caller := runtime.FuncForPC(fpcs[0] - 1)
  207. // caller := runtime.FuncForPC(fpcs[0])
  208. // // fmt.Println(caller.FileLine(fpcs[0] - 1))
  209. // fmt.Println(caller.FileLine(fpcs[0]))
  210. // fmt.Println(caller.Name())
  211. emsg := ""
  212. if e != nil {
  213. emsg = "Error: " + e.Error() + " in " + s
  214. } else {
  215. emsg = "Error: error is nil" + " in " + s // e 가 nil 인 상태에서 Error() 인용시 runtime error
  216. }
  217. fmt.Println(emsg, "\n")
  218. whereami(2)
  219. whereami(3)
  220. fmt.Println(strings.Repeat("=", 100))
  221. return emsg
  222. }
  223. func whereami(i int) {
  224. function, file, line, _ := runtime.Caller(i)
  225. fmt.Printf(" %d.File: %s - %d %s\n func: %s \n", i, ChopPath(file), line, file, runtime.FuncForPC(function).Name())
  226. }
  227. func WhereAmI(depthList ...int) {
  228. var depth int
  229. if depthList == nil {
  230. depth = 1
  231. } else {
  232. depth = depthList[0]
  233. }
  234. // function, file, line, _ := runtime.Caller(depth)
  235. for i := 0; i < depth+1; i++ {
  236. function, file, line, _ := runtime.Caller(i)
  237. fmt.Printf("==Level %d==\n", i)
  238. fmt.Printf("File: %s - %d %s\nFunction: %s \n", ChopPath(file), line, file, runtime.FuncForPC(function).Name())
  239. }
  240. fmt.Printf("==End==\n")
  241. return
  242. }
  243. func QryPathSql(path string, sql string) string { // // nㅣl처리 아주 중요함 ( 이건 이제 더 사용하지 말것)
  244. return "====" + path + "====\n" + sql + "\n" + "===================\n"
  245. }