internal/curd.go
55 lines · 1506 bytes
1package internal
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "runtime"
8 "time"
9 // "log"
10)
11
12// LogData logs the input data into a specified log file with the format [LOG] time lineNumber: logData
13func Log(data interface{}, logFile string) error {
14 // Open or create the log file
15 file, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
16 if err != nil {
17 return err
18 }
19 defer file.Close() // Ensure the file is closed when done
20
21 // Attempt to marshal the data into JSON
22 jsonData, err := json.Marshal(data)
23 if err != nil {
24 return err
25 }
26
27 // Get the caller information
28 _, filename, lineNumber, ok := runtime.Caller(1) // Caller 1 gives the caller of LogData
29 if !ok {
30 return fmt.Errorf("unable to get caller information")
31 }
32
33 // Log the current time and the JSON representation along with caller info
34 currentTime := time.Now().Format("2006/01/02 15:04:05")
35 logMessage := fmt.Sprintf("[LOG] %s %s %d: %s\n", currentTime, filename, lineNumber, jsonData)
36 _, err = fmt.Fprint(file, logMessage) // Write to the file
37 if err != nil {
38 return err
39 }
40
41 return nil
42}
43
44
45// ClearScreen clears the terminal screen and saves the state
46func ClearScreen() {
47 fmt.Print("\033[?1049h") // Switch to alternate screen buffer
48 fmt.Print("\033[2J") // Clear the entire screen
49 fmt.Print("\033[H") // Move cursor to the top left
50}
51
52// RestoreScreen restores the original terminal state
53func RestoreScreen() {
54 fmt.Print("\033[?1049l") // Switch back to the main screen buffer
55}