There is a small application for generating pseudo-logs. Under the terms of the TOR, log files are created every hour with the name of the form app_d-m-Y_H:i.log

I solved this problem "in the forehead":

  func main() { var err error var i int t := time.Now() time_layout := fmt.Sprintf("%02d-%02d-%02d_%02d_%02d", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute()) log_file := fmt.Sprintf(cfgvars.LogFile, time_layout) /* связываем вывод log-сообщений с файлом */ logTo := os.Stderr if logTo, err = os.OpenFile(log_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600); err != nil { log.Fatal(err) } defer logTo.Close() log.SetOutput(logTo) c := 0 for { i++ if c == 1202 { t = time.Now() time_layout = fmt.Sprintf("%02d-%02d-%02d_%02d_%02d", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute()) log_file = fmt.Sprintf(cfgvars.LogFile, time_layout) /* связываем вывод log-сообщений с файлом */ logTo = os.Stderr if logTo, err = os.OpenFile(log_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600); err != nil { log.Fatal(err) } defer logTo.Close() log.SetOutput(logTo) c = 0 } log.Printf("%s %s[%d]:\t%d\t%s\n", "host", os.Args[0], os.Getpid(), i, "some message") time.Sleep(999 * time.Millisecond) c++ } } 

Can all this lead to a more elegant look? Maybe using channels and other splendor?

Thank.

  • If the code is working, then it is better to ask a question on CodeReview . - user227465
  • @GreenDragon: worker. I'll try. Thanks for the tip. - Boris
  • If it is possible to use cron to launch a function with a parameter every hour, than send it to sleep constantly, all the more unclear whether it is all or not =) - Oma

0