func RegisterData(params martini.Params, l *log.Logger, db *sql.DB) (int, string) { key := params["key"] value := params["value"] l.Println(key, value) tx, err := db.Begin() if err != nil { log.Fatal(err) } stmt, err := tx.Prepare("insert into foo(id, name) values(?, ?)") if err != nil { log.Fatal(err) } defer stmt.Close() for i := 0; i < 10; i++ { _, err = stmt.Exec(i, fmt.Sprintf("[%s = %s] ねぷねぷのぷりん%03d", key, value, i)) if err != nil { log.Fatal(err) } } tx.Commit() return 200, "Hello!" + value }
// Goroutine to handle transmitting packets from one socket to another: func (c *conn) xmit(r io.Reader, w io.Writer, stop chan bool, logger *log.Logger) { buffer := make([]byte, buffer_size) for { // 5 second read deadline so that we don't get lots of CLOSE_WAIT sockets: r.(*net.TCPConn).SetReadDeadline(time.Now().Add(time.Millisecond * time.Duration(5000))) rn, err := r.Read(buffer) if err != nil { if c.handleError(err, logger) { continue } else { break } } _, err = w.Write(buffer[:rn]) if err != nil { if c.handleError(err, logger) { continue } else { break } } } stop <- true if verbose { logger.Println("stopped") } }
func DoRequest(verb, url, payload string, headers map[string]string, outputStructPointer interface{}, logger *log.Logger) error { if logger == nil { logger = log.New(ioutil.Discard, "", 0) } req, err := http.NewRequest(verb, url, strings.NewReader(payload)) for headerKey, headerVal := range headers { req.Header.Set(headerKey, headerVal) } client := &http.Client{} resp, err := client.Do(req) if err != nil { logger.Println(err) return err } defer resp.Body.Close() // debug request and response //logger.Println(req, payload) bodyBytes, _ := ioutil.ReadAll(resp.Body) //logger.Println("\n\n", string(bodyBytes), "\n\n") resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) err = json.NewDecoder(resp.Body).Decode(outputStructPointer) if err != nil { logger.Println(err, "Got this instead: ", "Request", req, payload, "Response", string(bodyBytes)) return err } return nil }
func acceptConn(conn net.Conn, l log.Logger) { l.Println("Success: Connection accepted from ", conn.RemoteAddr()) fmt.Println("Success: Connection accepted from ", conn.RemoteAddr()) for { data := make([]byte, 4096) n, err := conn.Read(data) if err != nil { fmt.Println("Error while reading from connection: ", err.Error()) return } fmt.Println("Number of bytes read: ", n) convertData(data, n) } // Handle timeout?! /* err := conn.Close() if err != nil { l.Println(err.Error()) } else { l.Println("Connection closed.") fmt.Println("Connection closed.") }*/ }
func Register(consulURL string, logger *log.Logger, service *Service) error { var httpresponse *http.Response var err error var buf []byte buf, err = json.Marshal(service) if err != nil { log.Println(err.Error()) return err } body := bytes.NewBuffer(buf) log.Println(body.String()) httpresponse, err = http.Post(consulURL+REGISTER, "application/json", body) if err != nil { logger.Println(err.Error()) return err } logger.Printf("register status code: %d\n", httpresponse.StatusCode) if httpresponse.StatusCode != 200 { return errors.New("register: invalid status code " + httpresponse.Status) } return err }
// pring write message into logger. If console is enabled, write into too func (l Logger) print(loggerElement *log.Logger, message ...interface{}) { data := append([]interface{}{getInfo()}, message...) loggerElement.Println(data...) if l.writeConsole { log.Println(data...) } }
// ZmqPubSubProxy Implements a many-to-many device on a zmq PUB-SUB connection func ZmqPubSubProxy(host string, portIn int, portOut int, logger *log.Logger) { xsub, _ := zmq.NewSocket(zmq.SUB) xpub, _ := zmq.NewSocket(zmq.PUB) defer xsub.Close() defer xpub.Close() addrIn := fmt.Sprintf("tcp://*:%d", portIn) addrOut := fmt.Sprintf("tcp://*:%d", portOut) logger.Println("ZMQ XSUB on", addrIn) xsub.Bind(addrIn) xsub.SetSubscribe("") logger.Println("ZMQ XPUB on", addrOut) xpub.Bind(addrOut) poller := zmq.NewPoller() poller.Add(xsub, zmq.POLLIN) for { // keep looping sockets, _ := poller.Poll(5 * time.Second) for _, socket := range sockets { switch s := socket.Socket; s { case xsub: ZmqSendMulti(xpub, ZmqRecvMulti(s)) } } } }
func NewContext( localStoragePath string, args []string, logger *log.Logger, fatal *log.Logger, ) (c G5CLIExecutionContext) { l := local.New(localStoragePath) if !l.IsPersisted() { logger.Println("No locally cached data. Enhance...") if err := g5hub.PopulateLocalStorage(l); err != nil { fatal.Printf(err.Error() + "\n") os.Exit(1) } l.Persist() } if l.ShouldNag() { l.LastNagged = time.Now() l.Persist() layout := "Mon, 01/02/06" s := l.LastUpdated.Format(layout) logger.Printf("Your last update was %v. You should consider running the update command. I'll nag you again soon.\n", s) } return G5CLIExecutionContext{ LocalStorage: l, Args: args, Logger: logger, Fatal: fatal, Heroku: CLIHerokuExecutor{SuccessLogger: logger, ErrorLogger: fatal}, } }
func doSnapshot(logger *log.Logger, params martini.Params, r render.Render) { lang := params["language"] // levelDBへ保存 level, err := leveldb.Open("snapshot", &db.Options{}) if err != nil { r.Error(400) } defer level.Close() key := createSnapshotKey(lang, time.Now()) logger.Println("key: ", key) if _, err := level.Get([]byte(key), &db.ReadOptions{}); err != nil { res, err := http.Get(fmt.Sprintf(gitHubStarURL, lang)) if err != nil { r.Error(400) } data, err := ioutil.ReadAll(res.Body) if err != nil { r.Error(400) } if err := level.Set([]byte(key), data, &db.WriteOptions{}); err != nil { r.Error(400) } } r.JSON(200, nil) }
func runMain(logger *log.Logger) { parseFlags() var err error var wd string projectPath = srcPath + projectFlag + "/" if err = os.Chdir(projectPath); err != nil { logger.Printf("\n**invalid project path '%s'\n%s\n", projectFlag, err) help() os.Exit(1) } if debugFlag { wd, err = os.Getwd() if err != nil { fmt.Println(err) } logger.Println("Working DIR:", wd) } testFiles(logger) }
func waitForNotifyEvents(watcher *fsnotify.Watcher, pathname string, channel chan<- io.ReadCloser, logger *log.Logger) { if file, err := os.Open(pathname); err == nil { channel <- file } for { select { case event, ok := <-watcher.Event: if !ok { return } if event.Name != pathname { continue } if file, err := os.Open(pathname); err != nil { if os.IsNotExist(err) { continue } if logger != nil { logger.Printf("Error opening file: %s: %s\n", pathname, err) } } else { channel <- file } case err, ok := <-watcher.Error: if !ok { return } logger.Println("Error with watcher:", err) } } }
func PostIdeaHandler(req *http.Request, render render.Render, log *log.Logger) { log.Println("Receive post data.") name := req.FormValue("name") email := req.FormValue("email") link := req.FormValue("link") content := req.FormValue("content") log.Println("Name: " + name) log.Println("Email: " + email) log.Println("Link: " + link) log.Println("Content: " + content) if len(name) == 0 { render.JSON(400, map[string]interface{}{"error": 1, "message": "Empty name."}) return } if len(email) == 0 { render.JSON(400, map[string]interface{}{"error": 2, "message": "Empty email."}) return } if len(content) == 0 { render.JSON(400, map[string]interface{}{"error": 3, "message": "Empty content."}) return } AddIdea(name, email, link, content) render.JSON(200, map[string]interface{}{"error": 0}) }
func deleteUnneededFiles(srpcClient *srpc.Client, subFS *filesystem.FileSystem, imgFS *filesystem.FileSystem, logger *log.Logger) bool { startTime := showStart("compute early files to delete") pathsToDelete := make([]string, 0) imgHashToInodesTable := imgFS.HashToInodesTable() imgFilenameToInodeTable := imgFS.FilenameToInodeTable() for pathname, inum := range subFS.FilenameToInodeTable() { if inode, ok := subFS.InodeTable[inum].(*filesystem.RegularInode); ok { if inode.Size > 0 { if _, ok := imgHashToInodesTable[inode.Hash]; !ok { pathsToDelete = append(pathsToDelete, pathname) } } else { if _, ok := imgFilenameToInodeTable[pathname]; !ok { pathsToDelete = append(pathsToDelete, pathname) } } } } showTimeTaken(startTime) if len(pathsToDelete) < 1 { return false } updateRequest := sub.UpdateRequest{ Wait: true, PathsToDelete: pathsToDelete} var updateReply sub.UpdateResponse startTime = showStart("Subd.Update() for early files to delete") err := client.CallUpdate(srpcClient, updateRequest, &updateReply) showTimeTaken(startTime) if err != nil { logger.Println(err) } return true }
func incomingEmail(tc *twilio.Client, req *http.Request, log *log.Logger) { if err := verifyMailgunSig( req.FormValue("token"), req.FormValue("timestamp"), req.FormValue("signature"), ); err != nil { log.Println("Mailgun request verification failed:", err) return } dkim := req.FormValue("X-Mailgun-Spf") spf := req.FormValue("X-Mailgun-Dkim-Check-Result") sender := req.FormValue("sender") if dkim != "Pass" || spf != "Pass" || sender != emailTo { log.Printf("Email verification failed: SPF: %s, DKIM: %s, addr: %s", spf, dkim, sender) return } params := twilio.MessageParams{Body: req.FormValue("stripped-text")} dest := strings.SplitN(req.FormValue("recipient"), "@", 2)[0] _, _, err := tc.Messages.Send(smsFrom, dest, params) if err != nil { log.Println("SMS send failed:", err) return } log.Println("SMS sent to", dest) }
func LockMetrics(logger *log.Logger, dbs []string, HOSTNAME string, dbConn *sql.DB) []Metric { logger.Println("get lock metrics 3.1") var metrics = make([]Metric, 0) for i := 0; i < len(dbs); i++ { metric := Metric{} var lockCount int64 var lockType, lockMode string err := dbConn.QueryRow("select locktype,mode, count(*) from pg_locks, pg_database where pg_locks.database = pg_database.oid and pg_database.datname = '"+dbs[i]+"' group by pg_locks.locktype, pg_locks.mode").Scan(&lockType, &lockMode, &lockCount) if err != nil { logger.Println("error: " + err.Error()) return metrics } metric.Hostname = HOSTNAME metric.MetricName = "lock_count" metric.Units = "count" metric.Value = lockCount metric.LockType = lockType metric.LockMode = lockMode metric.DatabaseName = dbs[i] metrics = append(metrics, metric) } return metrics }
// WithLog is a decorator that calls Println method // of provided logger to log received requests. // Format is: // <http method> <remote addr> <requested url> func WithLog(logger *log.Logger, fn http.HandlerFunc) http.HandlerFunc { //TODO use constants to define format like in log package. return func(w http.ResponseWriter, r *http.Request) { logger.Println(r.Method, r.RemoteAddr, r.URL) fn(w, r) } }
func incomingVoicemail(tc *twilio.Client, m mailgun.Mailgun, req *http.Request, log *log.Logger) { log.Printf("%#v", req.Form) transReq, err := tc.NewRequest("GET", req.FormValue("TranscriptionUrl")+".json", nil) if err != nil { log.Println("Transcription req build error:", err) return } var trans transcription _, err = tc.Do(transReq, &trans) if err != nil { log.Println("Transcription req error:", err) return } var buf bytes.Buffer err = voicemailTemplate.Execute(&buf, &voicemailData{ From: req.FormValue("From"), Duration: trans.Duration, Recording: req.FormValue("RecordingUrl"), Text: trans.Text, }) if err != nil { log.Println("Email template error:", err) return } msg := mailgun.NewMessage("voicemail@"+emailDomain, "New voicemail from "+req.FormValue("From"), buf.String(), emailTo) msg.SetDKIM(true) _, _, err = m.Send(msg) if err != nil { log.Println("Voicemail send error:", err) return } }
// Mount the file system according to arguments in the supplied context. func mountWithArgs( bucketName string, mountPoint string, flags *flagStorage, mountStatus *log.Logger) (mfs *fuse.MountedFileSystem, err error) { // Enable invariant checking if requested. if flags.DebugInvariants { syncutil.EnableInvariantChecking() } // Grab the connection. mountStatus.Println("Opening GCS connection...") conn, err := getConn(flags) if err != nil { err = fmt.Errorf("getConn: %v", err) return } // Mount the file system. mfs, err = mountWithConn( context.Background(), bucketName, mountPoint, flags, conn, mountStatus) if err != nil { err = fmt.Errorf("mountWithConn: %v", err) return } return }
func (p *proberType) proberLoop(defaultProbeInterval uint, logger *log.Logger) { // Set the initial probe interval to the global default, if less than the // interval for this prober. The probe interval will be gradually increased // until the target probe interval is reached. This gives faster probing at // startup when higher resolution may be helpful, and then backs off. probeInterval := time.Duration(defaultProbeInterval) * time.Second if probeInterval > p.probeInterval { probeInterval = p.probeInterval } for { p.probeStartTime = time.Now() if err := p.prober.Probe(); err != nil { logger.Println(err) } probeDuration := time.Since(p.probeStartTime) p.probeTimeDistribution.Add(probeDuration) time.Sleep(probeInterval - probeDuration) // Increase the probe interval until the interval for this prober is // reached. if probeInterval < p.probeInterval { probeInterval += time.Duration(defaultProbeInterval) * time.Second } if probeInterval > p.probeInterval { probeInterval = p.probeInterval } } }
func PurgeURL(url string, log *log.Logger) error { timeout := time.Duration(2 * time.Second) client := &http.Client{ Timeout: timeout, } // Invalidate the file req, err := http.NewRequest("PURGE", url, nil) if err != nil { log.Println("Unable to purge " + url + ": " + err.Error()) return err } resp, err := client.Do(req) if err == nil { defer resp.Body.Close() } if err != nil { log.Println("Unable to purge " + url + ": " + err.Error()) return err } log.Println("Purging " + url + ": " + resp.Status) return nil }
func build(builder gin.Builder, runner gin.Runner, logger *log.Logger) { err := builder.Build() if err != nil { buildError = err logger.Println("ERROR! Build failed.") fmt.Println(builder.Errors()) } else { // print success only if there were errors before, otherwise tell when rebuilt var notificationMessage string if buildError != nil { notificationMessage = "Build Successful" logger.Println(notificationMessage) } else { var curDir1 string curDir, _ := os.Getwd() segments := strings.Split(curDir, "/") if len(segments) > 0 { curDir1 = segments[len(segments)-1] } notificationMessage = fmt.Sprintf("%v - Rebuilt at %v \n", curDir1, time.Now().Format("15:04:05.999999")) logger.Printf(notificationMessage) } fmt.Println(notificationsEnabled) if notificationMessage != "" && notificationsEnabled { mack.Notify(notificationMessage) } buildError = nil if immediate { runner.Run() } } time.Sleep(100 * time.Millisecond) }
func Index(l *log.Logger, r render.Render, level *leveldb.DB) { // Message All var response ResponseData var readOpts db.ReadOptions keys, err := level.Get([]byte("keys"), &readOpts) if err == nil { keyList := strings.Split(string(keys), ",") response.Msg = "データベースのデータ" datas := make([]Message, len(keyList)) for idx, key := range keyList { data, _ := level.Get([]byte(key), &readOpts) var message Message err := json.Unmarshal(data, &message) if err != nil { panic(err) } l.Println(key, message) datas[idx] = message } response.Datas = datas } else { // not database recodes response.Msg = "データベースのサンプル" response.Datas = []Message{ {1, "ほげ", "*****@*****.**", "ふがふが", time.Now()}, {2, "ねぷねぷ", "*****@*****.**", "ねぷてぬ", time.Now()}, } } r.HTML(200, "index", response) }
func ObjectSizeMetrics(logger *log.Logger, dbs []string, HOSTNAME string, dbConn *sql.DB) []Metric { logger.Println("ObjectSizeMetrics") var metrics = make([]Metric, 0) for i := 0; i < len(dbs); i++ { metric := Metric{} var dbSize int64 err := dbConn.QueryRow("select pg_database_size(d.datname)/1024/1024 from pg_database d where d.datname = '" + dbs[i] + "'").Scan(&dbSize) if err != nil { logger.Println("error: " + err.Error()) return metrics } metric.Hostname = HOSTNAME metric.MetricName = "database_size" metric.Units = "megabytes" metric.Value = dbSize metric.DatabaseName = dbs[i] metrics = append(metrics, metric) } return metrics }
// Println calls Println on a logger or the default logger. // Arguments are handled in the manner of fmt.Println. func Println(l *log.Logger, v ...interface{}) { if l == nil { log.Println(v...) } else { l.Println(v...) } }
func logHandler(inner http.Handler, name string, weblog *log.Logger) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now() l := &responseLogger{w: w} inner.ServeHTTP(l, r) weblog.Println(buildLogLine(l, r, start)) }) }
func PrintMetrics(logger *log.Logger, metrics []Metric) error { var err error logger.Println("writing metrics") for i := 0; i < len(metrics); i++ { metrics[i].Print() } return err }
func makeSpecialInode(fullPathname string, inode *filesystem.SpecialInode, logger *log.Logger) { if err := inode.Write(fullPathname); err != nil { logger.Println(err) } else { logger.Printf("Made special inode: %s\n", fullPathname) } }
// Glance 显示摘要 func (q CompanyDailyQuote) Glance(logger *log.Logger, location *time.Location) { logger.Printf("上市公司:%s\t%s", q.Code, q.Name) q.Pre.Glance(logger, "Pre", location) q.Regular.Glance(logger, "Regular", location) q.Post.Glance(logger, "Post", location) logger.Println("") }
func Logging(l *log.Logger) Adapter { return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { l.Println(r.Method, r.URL.Path) h.ServeHTTP(w, r) }) } }
func (blink *blink) print(output *log.Logger) { output.Println(`PRINTING B-LINK`) if blink.root == nil { return } blink.root.print(output) }