1 Star 0 Fork 0

Ling_Boogie / mahjong-helper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
shanten_log.go 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
18520664652 提交于 2022-01-06 19:03 . release v0.1.0
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"time"
"gitee.com/ling_boogie/mahjong-helper/util"
)
func newShantenLogFilePath(nickName string, accountID string) (filePath string, err error) {
// 获取或者创建shanten日志
const logDir = "shanten"
if err = os.MkdirAll(logDir, os.ModePerm); err != nil {
return
}
fileName := fmt.Sprintf("shanten-%s-%s.log", nickName, accountID)
filePath = filepath.Join(logDir, fileName)
return filepath.Abs(filePath)
}
func PrintFirstShantenLog(nickName string, accountID string, shanten int) {
// 设置日志输出到 shanten/shanten-xxx.log
if debugMode {
fmt.Printf("%s-%s-%d\n", nickName, accountID, shanten)
}
filePath, err := newShantenLogFilePath(nickName, accountID)
if err != nil {
return
}
logFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
return
}
//及时关闭file句柄
defer func(logFile *os.File) {
err := logFile.Close()
if err != nil {
util.OrangevillePrint("[ERROR] printFirstShantenLog: log open failed.")
return
}
}(logFile)
//写入文件时,使用带缓存的 *Writer
write := bufio.NewWriter(logFile)
year, month, day := DateYmdInts()
inStr := fmt.Sprintf(
"{\"year\":%d, "+
"\"month\":%d, "+
"\"day\":%d, "+
"\"shanten\":%d}"+
"\n",
year, month, day, shanten)
_, err = write.WriteString(inStr)
if err != nil {
return
}
//Flush将缓存的文件真正写入到文件中
err = write.Flush()
if err != nil {
return
}
}
// DateYmdInts 获取日期的年月日
func DateYmdInts() (int, int, int) {
timeNow := time.Now()
year, month, day := timeNow.Date()
return year, int(month), day
}
1
https://gitee.com/ling_boogie/mahjong-helper.git
git@gitee.com:ling_boogie/mahjong-helper.git
ling_boogie
mahjong-helper
mahjong-helper
v0.1.0

搜索帮助