PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/logger/logger.go

https://github.com/YapheetS/btcrobot
Go | 278 lines | 225 code | 30 blank | 23 comment | 81 complexity | 5ea0daa0e2af05c51ab8a631347ab6f8 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. btcbot is a Bitcoin trading bot for HUOBI.com written
  3. in golang, it features multiple trading methods using
  4. technical analysis.
  5. Disclaimer:
  6. USE AT YOUR OWN RISK!
  7. The author of this project is NOT responsible for any damage or loss caused
  8. by this software. There can be bugs and the bot may not perform as expected
  9. or specified. Please consider testing it first with paper trading /
  10. backtesting on historical data. Also look at the code to see what how
  11. it's working.
  12. Weibo:http://weibo.com/bocaicfa
  13. */
  14. package logger
  15. import (
  16. . "config"
  17. "fmt"
  18. "io"
  19. "log"
  20. "os"
  21. "path/filepath"
  22. "runtime"
  23. )
  24. var (
  25. // 日志文件
  26. trade_file = ROOT + "/log/trade.log"
  27. override_file = ROOT + "/log/report"
  28. info_file = ROOT + "/log/info.log"
  29. debug_file = ROOT + "/log/debug.log"
  30. trace_file = ROOT + "/log/trace.log"
  31. error_file = ROOT + "/log/error.log"
  32. fatal_file = ROOT + "/log/fatal.log"
  33. )
  34. func init() {
  35. os.Mkdir(ROOT+"/log/", 0644)
  36. os.Mkdir(ROOT+"/cache/", 0777)
  37. if Config["env"] == "test" {
  38. override_file += "_test"
  39. }
  40. }
  41. type logger struct {
  42. *log.Logger
  43. }
  44. func New(out io.Writer) *logger {
  45. return &logger{
  46. Logger: log.New(out, "", log.LstdFlags),
  47. }
  48. }
  49. func NewReport(out io.Writer) *logger {
  50. return &logger{
  51. Logger: log.New(out, "", log.LstdFlags),
  52. }
  53. }
  54. func Tradef(format string, args ...interface{}) {
  55. file, err := os.OpenFile(trade_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  56. if err != nil {
  57. return
  58. }
  59. defer file.Close()
  60. New(file).Printf(format, args...)
  61. if Config["infoconsole"] == "1" {
  62. log.Printf(format, args...)
  63. }
  64. }
  65. func Tradeln(args ...interface{}) {
  66. file, err := os.OpenFile(trade_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  67. if err != nil {
  68. return
  69. }
  70. defer file.Close()
  71. New(file).Println(args...)
  72. if Config["infoconsole"] == "1" {
  73. log.Println(args...)
  74. }
  75. }
  76. func Infof(format string, args ...interface{}) {
  77. file, err := os.OpenFile(info_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  78. if err != nil {
  79. return
  80. }
  81. defer file.Close()
  82. New(file).Printf(format, args...)
  83. if Config["infoconsole"] == "1" {
  84. log.Printf(format, args...)
  85. }
  86. }
  87. func Infoln(args ...interface{}) {
  88. file, err := os.OpenFile(info_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  89. if err != nil {
  90. return
  91. }
  92. defer file.Close()
  93. New(file).Println(args...)
  94. if Config["infoconsole"] == "1" {
  95. log.Println(args...)
  96. }
  97. }
  98. func Errorf(format string, args ...interface{}) {
  99. file, err := os.OpenFile(error_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  100. if err != nil {
  101. return
  102. }
  103. defer file.Close()
  104. New(file).Printf(format, args...)
  105. if Config["errorconsole"] == "1" {
  106. log.Printf(format, args...)
  107. }
  108. }
  109. func Errorln(args ...interface{}) {
  110. file, err := os.OpenFile(error_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  111. if err != nil {
  112. return
  113. }
  114. defer file.Close()
  115. // 加上文件调用和行号
  116. _, callerFile, line, ok := runtime.Caller(1)
  117. if ok {
  118. args = append([]interface{}{"[", filepath.Base(callerFile), "]", line}, args...)
  119. }
  120. New(file).Println(args...)
  121. if Config["errorconsole"] == "1" {
  122. log.Println(args...)
  123. }
  124. }
  125. func Fatalf(format string, args ...interface{}) {
  126. file, err := os.OpenFile(fatal_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  127. if err != nil {
  128. return
  129. }
  130. defer file.Close()
  131. New(file).Printf(format, args...)
  132. if Config["fatalconsole"] == "1" {
  133. log.Printf(format, args...)
  134. }
  135. }
  136. func Fatalln(args ...interface{}) {
  137. file, err := os.OpenFile(fatal_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  138. if err != nil {
  139. return
  140. }
  141. defer file.Close()
  142. // 加上文件调用和行号
  143. _, callerFile, line, ok := runtime.Caller(1)
  144. if ok {
  145. args = append([]interface{}{"[", filepath.Base(callerFile), "]", line}, args...)
  146. }
  147. New(file).Println(args...)
  148. if Config["fatalconsole"] == "1" {
  149. log.Println(args...)
  150. }
  151. }
  152. func Fatal(args ...interface{}) {
  153. file, err := os.OpenFile(fatal_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  154. if err != nil {
  155. return
  156. }
  157. defer file.Close()
  158. // 加上文件调用和行号
  159. _, callerFile, line, ok := runtime.Caller(1)
  160. if ok {
  161. args = append([]interface{}{"[", filepath.Base(callerFile), "]", line}, args...)
  162. }
  163. New(file).Println(args...)
  164. if Config["fatalconsole"] == "1" {
  165. log.Println(args...)
  166. }
  167. }
  168. func Debugf(format string, args ...interface{}) {
  169. if Config["debug"] == "1" {
  170. file, err := os.OpenFile(debug_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  171. if err != nil {
  172. return
  173. }
  174. defer file.Close()
  175. New(file).Printf(format, args...)
  176. if Config["debugconsole"] == "1" {
  177. log.Printf(format, args...)
  178. }
  179. }
  180. }
  181. func Debugln(args ...interface{}) {
  182. if Config["debug"] == "1" {
  183. file, err := os.OpenFile(debug_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  184. if err != nil {
  185. return
  186. }
  187. defer file.Close()
  188. // 加上文件调用和行号
  189. _, callerFile, line, ok := runtime.Caller(1)
  190. if ok {
  191. args = append([]interface{}{"[", filepath.Base(callerFile), "]", line}, args...)
  192. }
  193. New(file).Println(args...)
  194. if Config["debugconsole"] == "1" {
  195. log.Println(args...)
  196. }
  197. }
  198. }
  199. func Tracef(format string, args ...interface{}) {
  200. file, err := os.OpenFile(trace_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  201. if err != nil {
  202. return
  203. }
  204. defer file.Close()
  205. New(file).Printf(format, args...)
  206. }
  207. func Traceln(args ...interface{}) {
  208. file, err := os.OpenFile(trace_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  209. if err != nil {
  210. return
  211. }
  212. defer file.Close()
  213. // 加上文件调用和行号
  214. _, callerFile, line, ok := runtime.Caller(1)
  215. if ok {
  216. args = append([]interface{}{"[", filepath.Base(callerFile), "]", line}, args...)
  217. }
  218. New(file).Println(args...)
  219. }
  220. var what string
  221. func OverrideStart(Peroid int) {
  222. what = fmt.Sprintf("%03d", Peroid)
  223. file, err := os.OpenFile(override_file+what+".log", os.O_CREATE|os.O_TRUNC, 0644)
  224. if err != nil {
  225. return
  226. }
  227. defer file.Close()
  228. }
  229. func Overridef(format string, args ...interface{}) {
  230. file, err := os.OpenFile(override_file+what+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  231. if err != nil {
  232. return
  233. }
  234. defer file.Close()
  235. NewReport(file).Printf(format, args...)
  236. }
  237. func Overrideln(args ...interface{}) {
  238. file, err := os.OpenFile(override_file+what+".log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
  239. if err != nil {
  240. return
  241. }
  242. defer file.Close()
  243. NewReport(file).Println(args...)
  244. }