* formatHeader generates the header information for a log entry. t: The current time. file: The file name of the source code invoking the log function. line: The line number of the source code invoking the log function. level: The log level of the current log entry. */
(t time.Time, file string, line int, level int)
| 129 | level: The log level of the current log entry. |
| 130 | */ |
| 131 | func (log *ZinxLoggerCore) formatHeader(t time.Time, file string, line int, level int) { |
| 132 | var buf *bytes.Buffer = &log.buf |
| 133 | // If the current prefix string is not empty, write the prefix first. |
| 134 | if log.prefix != "" { |
| 135 | buf.WriteByte('<') |
| 136 | buf.WriteString(log.prefix) |
| 137 | buf.WriteByte('>') |
| 138 | } |
| 139 | |
| 140 | // If the time-related flags are set, add the time information to the log header. |
| 141 | if log.flag&(BitDate|BitTime|BitMicroSeconds) != 0 { |
| 142 | // Date flag is set |
| 143 | if log.flag&BitDate != 0 { |
| 144 | year, month, day := t.Date() |
| 145 | itoa(buf, year, 4) |
| 146 | buf.WriteByte('/') // "2019/" |
| 147 | itoa(buf, int(month), 2) |
| 148 | buf.WriteByte('/') // "2019/04/" |
| 149 | itoa(buf, day, 2) |
| 150 | buf.WriteByte(' ') // "2019/04/11 " |
| 151 | } |
| 152 | |
| 153 | // Time flag is set |
| 154 | if log.flag&(BitTime|BitMicroSeconds) != 0 { |
| 155 | hour, min, sec := t.Clock() |
| 156 | itoa(buf, hour, 2) |
| 157 | buf.WriteByte(':') // "11:" |
| 158 | itoa(buf, min, 2) |
| 159 | buf.WriteByte(':') // "11:15:" |
| 160 | itoa(buf, sec, 2) // "11:15:33" |
| 161 | // Microsecond flag is set |
| 162 | if log.flag&BitMicroSeconds != 0 { |
| 163 | buf.WriteByte('.') |
| 164 | itoa(buf, t.Nanosecond()/1e3, 6) // "11:15:33.123123 |
| 165 | } |
| 166 | buf.WriteByte(' ') |
| 167 | } |
| 168 | |
| 169 | // Log level flag is set |
| 170 | if log.flag&BitLevel != 0 { |
| 171 | buf.WriteString(levels[level]) |
| 172 | } |
| 173 | |
| 174 | // Short file name flag or long file name flag is set |
| 175 | if log.flag&(BitShortFile|BitLongFile) != 0 { |
| 176 | // Short file name flag is set |
| 177 | if log.flag&BitShortFile != 0 { |
| 178 | short := file |
| 179 | for i := len(file) - 1; i > 0; i-- { |
| 180 | if file[i] == '/' { |
| 181 | // Get the file name after the last '/' character, e.g. "zinx.go" from "/home/go/src/zinx.go" |
| 182 | short = file[i+1:] |
| 183 | break |
| 184 | } |
| 185 | } |
| 186 | file = short |
| 187 | } |
| 188 | buf.WriteString(file) |