1. Use "in-place" variables for if statements to limit their scope to
the respectful `if` block.
2. Report the error returned from sd_journal_* by using CErr().
3. Use errors.New() instead of fmt.Errorf().
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit 20a0e58a794cfb9b1a1f757d222248e22555f7f0)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
| ... | ... |
@@ -105,7 +105,6 @@ import "C" |
| 105 | 105 |
|
| 106 | 106 |
import ( |
| 107 | 107 |
"errors" |
| 108 |
- "fmt" |
|
| 109 | 108 |
"strings" |
| 110 | 109 |
"time" |
| 111 | 110 |
"unsafe" |
| ... | ... |
@@ -282,16 +281,14 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon |
| 282 | 282 |
) |
| 283 | 283 |
|
| 284 | 284 |
// Get a handle to the journal. |
| 285 |
- rc := C.sd_journal_open(&j, C.int(0)) |
|
| 286 |
- if rc != 0 {
|
|
| 287 |
- logWatcher.Err <- fmt.Errorf("error opening journal")
|
|
| 285 |
+ if rc := C.sd_journal_open(&j, C.int(0)); rc != 0 {
|
|
| 286 |
+ logWatcher.Err <- errors.New("error opening journal: " + CErr(rc))
|
|
| 288 | 287 |
close(logWatcher.Msg) |
| 289 | 288 |
return |
| 290 | 289 |
} |
| 291 | 290 |
if config.Follow {
|
| 292 | 291 |
// Initialize library inotify watches early |
| 293 |
- rc = C.sd_journal_get_fd(j) |
|
| 294 |
- if rc < 0 {
|
|
| 292 |
+ if rc := C.sd_journal_get_fd(j); rc < 0 {
|
|
| 295 | 293 |
logWatcher.Err <- errors.New("error getting journald fd: " + CErr(rc))
|
| 296 | 294 |
close(logWatcher.Msg) |
| 297 | 295 |
return |
| ... | ... |
@@ -309,17 +306,15 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon |
| 309 | 309 |
C.sd_journal_close(j) |
| 310 | 310 |
}() |
| 311 | 311 |
// Remove limits on the size of data items that we'll retrieve. |
| 312 |
- rc = C.sd_journal_set_data_threshold(j, C.size_t(0)) |
|
| 313 |
- if rc != 0 {
|
|
| 314 |
- logWatcher.Err <- fmt.Errorf("error setting journal data threshold")
|
|
| 312 |
+ if rc := C.sd_journal_set_data_threshold(j, C.size_t(0)); rc != 0 {
|
|
| 313 |
+ logWatcher.Err <- errors.New("error setting journal data threshold: " + CErr(rc))
|
|
| 315 | 314 |
return |
| 316 | 315 |
} |
| 317 | 316 |
// Add a match to have the library do the searching for us. |
| 318 | 317 |
cmatch = C.CString("CONTAINER_ID_FULL=" + s.vars["CONTAINER_ID_FULL"])
|
| 319 | 318 |
defer C.free(unsafe.Pointer(cmatch)) |
| 320 |
- rc = C.sd_journal_add_match(j, unsafe.Pointer(cmatch), C.strlen(cmatch)) |
|
| 321 |
- if rc != 0 {
|
|
| 322 |
- logWatcher.Err <- fmt.Errorf("error setting journal match")
|
|
| 319 |
+ if rc := C.sd_journal_add_match(j, unsafe.Pointer(cmatch), C.strlen(cmatch)); rc != 0 {
|
|
| 320 |
+ logWatcher.Err <- errors.New("error setting journal match: " + CErr(rc))
|
|
| 323 | 321 |
return |
| 324 | 322 |
} |
| 325 | 323 |
// If we have a cutoff time, convert it to Unix time once. |
| ... | ... |
@@ -335,11 +330,13 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon |
| 335 | 335 |
if config.Tail >= 0 {
|
| 336 | 336 |
// If until time provided, start from there. |
| 337 | 337 |
// Otherwise start at the end of the journal. |
| 338 |
- if untilUnixMicro != 0 && C.sd_journal_seek_realtime_usec(j, C.uint64_t(untilUnixMicro)) < 0 {
|
|
| 339 |
- logWatcher.Err <- fmt.Errorf("error seeking provided until value")
|
|
| 340 |
- return |
|
| 341 |
- } else if C.sd_journal_seek_tail(j) < 0 {
|
|
| 342 |
- logWatcher.Err <- fmt.Errorf("error seeking to end of journal")
|
|
| 338 |
+ if untilUnixMicro != 0 {
|
|
| 339 |
+ if rc := C.sd_journal_seek_realtime_usec(j, C.uint64_t(untilUnixMicro)); rc != 0 {
|
|
| 340 |
+ logWatcher.Err <- errors.New("error seeking provided until value: " + CErr(rc))
|
|
| 341 |
+ return |
|
| 342 |
+ } |
|
| 343 |
+ } else if rc := C.sd_journal_seek_tail(j); rc != 0 {
|
|
| 344 |
+ logWatcher.Err <- errors.New("error seeking to end of journal: " + CErr(rc))
|
|
| 343 | 345 |
return |
| 344 | 346 |
} |
| 345 | 347 |
// (Try to) skip backwards by the requested number of lines... |
| ... | ... |
@@ -353,17 +350,19 @@ func (s *journald) readLogs(logWatcher *logger.LogWatcher, config logger.ReadCon |
| 353 | 353 |
} |
| 354 | 354 |
} else {
|
| 355 | 355 |
// Start at the beginning of the journal. |
| 356 |
- if C.sd_journal_seek_head(j) < 0 {
|
|
| 357 |
- logWatcher.Err <- fmt.Errorf("error seeking to start of journal")
|
|
| 356 |
+ if rc := C.sd_journal_seek_head(j); rc != 0 {
|
|
| 357 |
+ logWatcher.Err <- errors.New("error seeking to start of journal: " + CErr(rc))
|
|
| 358 | 358 |
return |
| 359 | 359 |
} |
| 360 | 360 |
// If we have a cutoff date, fast-forward to it. |
| 361 |
- if sinceUnixMicro != 0 && C.sd_journal_seek_realtime_usec(j, C.uint64_t(sinceUnixMicro)) != 0 {
|
|
| 362 |
- logWatcher.Err <- fmt.Errorf("error seeking to start time in journal")
|
|
| 363 |
- return |
|
| 361 |
+ if sinceUnixMicro != 0 {
|
|
| 362 |
+ if rc := C.sd_journal_seek_realtime_usec(j, C.uint64_t(sinceUnixMicro)); rc != 0 {
|
|
| 363 |
+ logWatcher.Err <- errors.New("error seeking to start time in journal: " + CErr(rc))
|
|
| 364 |
+ return |
|
| 365 |
+ } |
|
| 364 | 366 |
} |
| 365 |
- if C.sd_journal_next(j) < 0 {
|
|
| 366 |
- logWatcher.Err <- fmt.Errorf("error skipping to next journal entry")
|
|
| 367 |
+ if rc := C.sd_journal_next(j); rc < 0 {
|
|
| 368 |
+ logWatcher.Err <- errors.New("error skipping to next journal entry: " + CErr(rc))
|
|
| 367 | 369 |
return |
| 368 | 370 |
} |
| 369 | 371 |
} |