| 66 | } |
| 67 | |
| 68 | func (d *stdLogDispatcher) Read() { |
| 69 | buf := make([]byte, d.bufferLength+1) |
| 70 | next := 0 // offset |
| 71 | lnTabCnt := 0 // tab count in recent line |
| 72 | var lnReqID string |
| 73 | var ls LogStatStore |
| 74 | for { |
| 75 | |
| 76 | // bytes count from connection |
| 77 | read, err := d.conn.Read(buf[next:d.bufferLength]) |
| 78 | d.logger.V(9).Infof("recv res: read %d, data %s, err %+v", read, string(buf[next:next+read]), err) |
| 79 | if err != nil { |
| 80 | d.logger.V(5).Errorf("recv failed: %s", err.Error()) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | // if log store map is empty, discard the data |
| 85 | if len(d.logStoreMap.storeMap) == 0 { |
| 86 | d.logger.Warn("can't find log store map") |
| 87 | next = 0 |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | // if can't get the log store of the last request, discard the data |
| 92 | ls, err = d.logStoreMap.getLast() |
| 93 | if err != nil { |
| 94 | d.logger.Warnf("get last log store of %s failed", d.logStoreMap.String()) |
| 95 | next = 0 |
| 96 | continue |
| 97 | } |
| 98 | if ls == nil { |
| 99 | next = 0 |
| 100 | continue |
| 101 | } |
| 102 | |
| 103 | next += read |
| 104 | |
| 105 | nzero := 0 |
| 106 | lastln := -1 |
| 107 | lastTab := -1 |
| 108 | |
| 109 | // parse data |
| 110 | for i := 0; i < next; i++ { |
| 111 | if buf[i] == ZeroByte { // \0 means the end of the request |
| 112 | d.logger.V(9).Infof("next %d lastln %d i %d nzero %d", next, lastln, i, nzero) |
| 113 | nzero++ |
| 114 | copy(buf[i:], buf[i+1:next]) // remove \0 |
| 115 | i-- |
| 116 | next-- |
| 117 | nwrite, err := ls.WriteStdLog(d.logFrom, buf[lastln+1:i+1], true) |
| 118 | // write error: discard the log data, start the new line process |
| 119 | // write all success: start the new line process |
| 120 | if err != nil || nwrite == i-lastln { |
| 121 | if err != nil { |
| 122 | d.logger.Errorf("write std log %s len %d with eof lastln %d i %d failed: %s", ls.String(), nwrite, lastln, i, err) |
| 123 | } else { |
| 124 | d.logger.V(6).Infof("write std log %s len %d with eof lastln %d i %d", ls.String(), nwrite, lastln, i) |
| 125 | } |