diff --git a/common.go b/common.go index 4a0beae..33d3ee9 100644 --- a/common.go +++ b/common.go @@ -9,8 +9,7 @@ import ( ) var ( - debugMode bool = true - funcs = template.FuncMap{ + funcs = template.FuncMap{ "sub": func(a, b int) int { return a - b }, diff --git a/config.go b/config.go index 14d9808..a411961 100644 --- a/config.go +++ b/config.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "fmt" "log" "os" "strconv" @@ -18,7 +17,7 @@ func initConfig() error { return createConfig() } - fmt.Println("Configuration file already exists.") + printInfo("Configuration file already exists.") config = loadConfig() return nil } @@ -26,12 +25,12 @@ func initConfig() error { func createConfig() error { reader := bufio.NewReader(os.Stdin) - fmt.Println("Configuration file not found.") - fmt.Print("Do you want to use default values? (yes/no): ") + printMessage("Configuration file not found.") + printMessage("Do you want to use default values? (yes/no): ") useDefaults, _ := reader.ReadString('\n') if strings.TrimSpace(useDefaults) != "yes" { - fmt.Print("Enter port (default 5000): ") + printMessage("Enter port (default 5000): ") portStr, _ := reader.ReadString('\n') if portStr != "\n" { port, err := strconv.Atoi(strings.TrimSpace(portStr)) @@ -42,7 +41,7 @@ func createConfig() error { } } - fmt.Print("Enter your domain address (default localhost): ") + printMessage("Enter your domain address (default localhost): ") domain, _ := reader.ReadString('\n') if domain != "\n" { config.Domain = strings.TrimSpace(domain) @@ -53,10 +52,13 @@ func createConfig() error { if config.AuthCode == "" { config.AuthCode = generateStrongRandomString(64) - fmt.Printf("Generated connection code: %s\n", config.AuthCode) + printMessage("Generated connection code: %s\n", config.AuthCode) } config.NodesEnabled = len(config.Peers) > 0 + config.CrawlerEnabled = true + config.WebsiteEnabled = true + config.LogLevel = 1 saveConfig(config) return nil @@ -73,17 +75,20 @@ func saveConfig(config Config) { sec.Key("Peers").SetValue(peers) sec.Key("Domain").SetValue(config.Domain) sec.Key("NodesEnabled").SetValue(strconv.FormatBool(config.NodesEnabled)) + sec.Key("CrawlerEnabled").SetValue(strconv.FormatBool(config.CrawlerEnabled)) + sec.Key("WebsiteEnabled").SetValue(strconv.FormatBool(config.WebsiteEnabled)) + sec.Key("LogLevel").SetValue(strconv.Itoa(config.LogLevel)) err := cfg.SaveTo(configFilePath) if err != nil { - fmt.Println("Error writing to config file:", err) + printErr("Error writing to config file: %v", err) } } func loadConfig() Config { cfg, err := ini.Load(configFilePath) if err != nil { - log.Fatalf("Error opening config file: %v", err) + printErr("Error opening config file: %v", err) } port, err := cfg.Section("").Key("Port").Int() @@ -110,13 +115,31 @@ func loadConfig() Config { nodesEnabled = len(peers) > 0 // Enable nodes if peers are configured } + crawlerEnabled, err := cfg.Section("").Key("CrawlerEnabled").Bool() + if err != nil { // Default to true if not found + crawlerEnabled = true + } + + websiteEnabled, err := cfg.Section("").Key("WebsiteEnabled").Bool() + if err != nil { // Default to true if not found + websiteEnabled = true + } + + logLevel, err := cfg.Section("").Key("LogLevel").Int() + if err != nil || logLevel < 0 || logLevel > 4 { // Default to 1 if not found or out of range + logLevel = 1 + } + config = Config{ - Port: port, - AuthCode: cfg.Section("").Key("AuthCode").String(), - PeerID: cfg.Section("").Key("PeerID").String(), - Peers: peers, - Domain: domain, - NodesEnabled: nodesEnabled, + Port: port, + AuthCode: cfg.Section("").Key("AuthCode").String(), + PeerID: cfg.Section("").Key("PeerID").String(), + Peers: peers, + Domain: domain, + NodesEnabled: nodesEnabled, + CrawlerEnabled: crawlerEnabled, + WebsiteEnabled: websiteEnabled, + LogLevel: logLevel, } return config @@ -127,7 +150,7 @@ var configLock sync.RWMutex func startFileWatcher() { watcher, err := fsnotify.NewWatcher() if err != nil { - log.Fatal(err) + printErr("%v", err) } go func() { @@ -139,7 +162,7 @@ func startFileWatcher() { return } if event.Op&fsnotify.Write == fsnotify.Write { - log.Println("Modified file:", event.Name) + printInfo("Modified file: %v", event.Name) configLock.Lock() config = loadConfig() configLock.Unlock() @@ -148,7 +171,7 @@ func startFileWatcher() { if !ok { return } - log.Println("Error watching configuration file:", err) + printWarn("Error watching configuration file: %v", err) } } }() diff --git a/init.go b/init.go index 2f6d273..8074dd5 100644 --- a/init.go +++ b/init.go @@ -1,25 +1,30 @@ package main import ( - "fmt" - "log" "time" ) type Config struct { - Port int - AuthCode string - PeerID string - Peers []string - Domain string - NodesEnabled bool + Port int + AuthCode string + PeerID string + Peers []string + Domain string + NodesEnabled bool + CrawlerEnabled bool + WebsiteEnabled bool + LogLevel int } var defaultConfig = Config{ - Port: 5000, - Domain: "localhost", - Peers: []string{}, - AuthCode: generateStrongRandomString(64), + Port: 5000, + Domain: "localhost", + Peers: []string{}, + AuthCode: generateStrongRandomString(64), + NodesEnabled: true, + CrawlerEnabled: true, + WebsiteEnabled: true, + LogLevel: 1, } const configFilePath = "config.ini" @@ -29,7 +34,7 @@ var config Config func main() { err := initConfig() if err != nil { - fmt.Println("Error during initialization:", err) + printErr("Error during initialization:") return } @@ -39,14 +44,14 @@ func main() { if config.AuthCode == "" { config.AuthCode = generateStrongRandomString(64) - fmt.Printf("Generated connection code: %s\n", config.AuthCode) + printInfo("Generated connection code: %s\n", config.AuthCode) saveConfig(config) } // Generate Host ID hostID, nodeErr := generateHostID() if nodeErr != nil { - log.Fatalf("Failed to generate host ID: %v", nodeErr) + printErr("Failed to generate host ID: %v", nodeErr) } config.PeerID = hostID diff --git a/node.go b/node.go index dc0a4eb..c317946 100644 --- a/node.go +++ b/node.go @@ -104,7 +104,7 @@ func handleNodeRequest(w http.ResponseWriter, r *http.Request) { } defer r.Body.Close() - log.Printf("Received message: %+v\n", msg) + printDebug("Received message: %+v\n", msg) w.Write([]byte("Message received")) interpretMessage(msg) @@ -133,9 +133,9 @@ func startNodeClient() { func interpretMessage(msg Message) { switch msg.Type { case "test": - fmt.Println("Received test message:", msg.Content) + printDebug("Received test message: %v", msg.Content) case "update": - fmt.Println("Received update message:", msg.Content) + printDebug("Received update message: %v", msg.Content) go update() case "heartbeat": handleHeartbeat(msg.Content) @@ -162,6 +162,6 @@ func interpretMessage(msg Message) { case "file-results": handleFileResultsMessage(msg) default: - fmt.Println("Received unknown message type:", msg.Type) + printWarn("Received unknown message type: %v", msg.Type) } } diff --git a/open-search.go b/open-search.go index 86d7e4e..280fe3d 100644 --- a/open-search.go +++ b/open-search.go @@ -36,7 +36,7 @@ func generateOpenSearchXML(config Config) { file, err := os.Create("static/opensearch.xml") if err != nil { - fmt.Println("Error creating OpenSearch file:", err) + printErr("Error creating OpenSearch file: %v", err) return } defer file.Close() @@ -44,9 +44,9 @@ func generateOpenSearchXML(config Config) { enc := xml.NewEncoder(file) enc.Indent(" ", " ") if err := enc.Encode(opensearch); err != nil { - fmt.Println("Error encoding OpenSearch XML:", err) + printErr("Error encoding OpenSearch XML: %v", err) return } - fmt.Println("OpenSearch description file generated successfully.") + printInfo("OpenSearch description file generated successfully.") } diff --git a/printing.go b/printing.go new file mode 100644 index 0000000..1a547c1 --- /dev/null +++ b/printing.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "time" +) + +// printDebug logs debug-level messages when LogLevel is set to 4. +func printDebug(format string, args ...interface{}) { + if config.LogLevel >= 4 { + logMessage("DEBUG", format, args...) + } +} + +// printInfo logs info-level messages when LogLevel is set to 3 or higher. +func printInfo(format string, args ...interface{}) { + if config.LogLevel >= 3 { + logMessage("INFO", format, args...) + } +} + +// printWarn logs warning-level messages when LogLevel is set to 2 or higher. +func printWarn(format string, args ...interface{}) { + if config.LogLevel >= 2 { + logMessage("WARN", format, args...) + } +} + +// printErr logs error-level messages regardless of LogLevel. +func printErr(format string, args ...interface{}) { + if config.LogLevel >= 1 { + logMessage("ERROR", format, args...) + } +} + +// printMessage logs messages without a specific log level (e.g., general output). +func printMessage(format string, args ...interface{}) { + logMessage("", format, args...) +} + +// logMessage handles the actual logging logic without using the default logger's timestamp. +func logMessage(level string, format string, args ...interface{}) { + timestamp := time.Now().Format("2006-01-02|15:04:05") + message := fmt.Sprintf(format, args...) + if level != "" { + fmt.Printf("[%s|%s] %s\n", timestamp, level, message) + } else { + fmt.Printf("[%s] %s\n", timestamp, message) + } +} + +///////////// + +func printErrf(format string, args ...interface{}) { + if config.LogLevel >= 1 { + logMessage("ERROR", fmt.Sprintf(format, args...)) + } +} + +func printWarnf(format string, args ...interface{}) { + if config.LogLevel >= 2 { + logMessage("WARN", fmt.Sprintf(format, args...)) + } +} + +func printInfof(format string, args ...interface{}) { + if config.LogLevel >= 3 { + logMessage("INFO", fmt.Sprintf(format, args...)) + } +} diff --git a/text-google.go b/text-google.go index aa424f6..ce7646f 100644 --- a/text-google.go +++ b/text-google.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "log" "net/http" "net/url" "strings" @@ -53,9 +52,7 @@ func PerformGoogleTextSearch(query, safe, lang string, page int) ([]TextSearchRe duration := time.Since(startTime) // Calculate the duration if len(results) == 0 { - if debugMode { - log.Println("No results found from Google") - } + printDebug("No results found from Google Search") } return results, duration, nil @@ -83,9 +80,7 @@ func parseResults(doc *goquery.Document) []TextSearchResult { link := s.Find("a") href, exists := link.Attr("href") if !exists { - if debugMode { - log.Printf("No href attribute found for result %d\n", i) - } + printDebug("No href attribute found for result %d\n", i) return }