Skip to content

Commit d344247

Browse files
committed
log summary #26
1 parent 05dc9e3 commit d344247

File tree

2 files changed

+76
-65
lines changed

2 files changed

+76
-65
lines changed

cmd/chkbit/main.go

+72-63
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,45 @@ type Main struct {
108108
bps *util.RateCalc
109109
}
110110

111-
func (m *Main) log(text string) {
111+
func (m *Main) logFile(text string) {
112112
m.logger.Println(time.Now().UTC().Format("2006-01-02 15:04:05"), text)
113113
}
114114

115+
func (m *Main) logInfo(col, text string) {
116+
if m.progress != Quiet {
117+
if m.progress == Fancy {
118+
lterm.Printline(col, text, lterm.Reset)
119+
} else {
120+
fmt.Println(text)
121+
}
122+
}
123+
m.logFile(text)
124+
}
125+
126+
func (m *Main) logError(text string) {
127+
text = chkbit.STATUS_PANIC.String() + " " + text
128+
129+
if m.progress == Fancy {
130+
lterm.Write(termAlertFG)
131+
fmt.Fprintln(os.Stderr, text)
132+
lterm.Write(lterm.Reset)
133+
} else {
134+
fmt.Fprintln(os.Stderr, text)
135+
}
136+
137+
m.logFile(text)
138+
}
139+
140+
func (m *Main) printError(text string) {
141+
if m.progress == Fancy {
142+
lterm.Write(termAlertFG)
143+
fmt.Fprintln(os.Stderr, text)
144+
lterm.Write(lterm.Reset)
145+
} else {
146+
fmt.Fprintln(os.Stderr, text)
147+
}
148+
}
149+
115150
func (m *Main) logStatus(stat chkbit.Status, message string) bool {
116151
if stat == chkbit.STATUS_UPDATE_INDEX {
117152
return false
@@ -124,7 +159,7 @@ func (m *Main) logStatus(stat chkbit.Status, message string) bool {
124159
}
125160

126161
if m.logVerbose || !stat.IsVerbose() {
127-
m.log(stat.String() + " " + message)
162+
m.logFile(stat.String() + " " + message)
128163
}
129164

130165
if m.verbose || !stat.IsVerbose() {
@@ -203,17 +238,17 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
203238
switch cmd {
204239
case Check:
205240
pathList = cli.Check.Paths
206-
m.log("chkbit check " + strings.Join(pathList, ", "))
241+
m.logFile("chkbit check " + strings.Join(pathList, ", "))
207242
case Update:
208243
pathList = cli.Update.Paths
209244
m.context.UpdateIndex = true
210245
m.context.AddOnly = cli.Update.AddOnly
211246
m.context.ForceUpdateDmg = cli.Update.Force
212-
m.log("chkbit update " + strings.Join(pathList, ", "))
247+
m.logFile("chkbit update " + strings.Join(pathList, ", "))
213248
case Show:
214249
pathList = cli.ShowIgnoredOnly.Paths
215250
m.context.ShowIgnoredOnly = true
216-
m.log("chkbit show-ignored-only " + strings.Join(pathList, ", "))
251+
m.logFile("chkbit show-ignored-only " + strings.Join(pathList, ", "))
217252
}
218253

219254
m.context.ShowMissing = cli.ShowMissing
@@ -228,10 +263,7 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
228263
if err == nil {
229264
// pathList is relative to root
230265
err = os.Chdir(root)
231-
if m.progress != Quiet {
232-
fmt.Println("Using indexdb in " + root)
233-
}
234-
m.log("using indexdb in " + root)
266+
m.logInfo("", "Using indexdb in "+root)
235267
}
236268
if err != nil {
237269
return false, err
@@ -251,81 +283,58 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
251283
}
252284

253285
func (m *Main) printResult() error {
254-
cprint := func(col, text string) {
255-
if m.progress != Quiet {
256-
if m.progress == Fancy {
257-
lterm.Printline(col, text, lterm.Reset)
258-
} else {
259-
fmt.Println(text)
260-
}
261-
}
262-
}
263-
264-
eprint := func(col, text string) {
265-
if m.progress == Fancy {
266-
lterm.Write(col)
267-
fmt.Fprintln(os.Stderr, text)
268-
lterm.Write(lterm.Reset)
269-
} else {
270-
fmt.Fprintln(os.Stderr, text)
271-
}
272-
}
273286

274287
if m.progress != Quiet {
275288
mode := ""
276289
if !m.context.UpdateIndex {
277290
mode = " in readonly mode"
278291
}
279-
status := fmt.Sprintf("Processed %s%s.", util.LangNum1MutateSuffix(m.context.NumTotal, "file"), mode)
280-
cprint(termOKFG, status)
281-
m.log(status)
292+
status := fmt.Sprintf("Processed %s%s", util.LangNum1MutateSuffix(m.context.NumTotal, "file"), mode)
293+
m.logInfo(termOKFG, status)
282294

283295
if m.progress == Fancy && m.context.NumTotal > 0 {
284296
elapsed := time.Since(m.fps.Start)
285297
elapsedS := elapsed.Seconds()
286-
fmt.Println("-", elapsed.Truncate(time.Second), "elapsed")
287-
fmt.Printf("- %.2f files/second\n", (float64(m.fps.Total)+float64(m.fps.Current))/elapsedS)
288-
fmt.Printf("- %.2f MB/second\n", (float64(m.bps.Total)+float64(m.bps.Current))/float64(sizeMB)/elapsedS)
298+
m.logInfo("", fmt.Sprintf("- %s elapsed", elapsed.Truncate(time.Second)))
299+
m.logInfo("", fmt.Sprintf("- %.2f files/second", (float64(m.fps.Total)+float64(m.fps.Current))/elapsedS))
300+
m.logInfo("", fmt.Sprintf("- %.2f MB/second", (float64(m.bps.Total)+float64(m.bps.Current))/float64(sizeMB)/elapsedS))
289301
}
290302

291-
del := ""
292303
if m.context.UpdateIndex {
293304
if m.context.NumIdxUpd > 0 {
305+
m.logInfo(termOKFG, fmt.Sprintf("- %s updated", util.LangNum1Choice(m.context.NumIdxUpd, "directory was", "directories were")))
306+
m.logInfo(termOKFG, fmt.Sprintf("- %s added", util.LangNum1Choice(m.context.NumNew, "file hash was", "file hashes were")))
307+
m.logInfo(termOKFG, fmt.Sprintf("- %s updated", util.LangNum1Choice(m.context.NumUpd, "file hash was", "file hashes were")))
294308
if m.context.NumDel > 0 {
295-
del = fmt.Sprintf("\n- %s been removed", util.LangNum1Choice(m.context.NumDel, "file/directory has", "files/directories have"))
309+
m.logInfo(termOKFG, fmt.Sprintf("- %s been removed", util.LangNum1Choice(m.context.NumDel, "file/directory has", "files/directories have")))
296310
}
297-
cprint(termOKFG, fmt.Sprintf("- %s updated\n- %s added\n- %s updated%s",
298-
util.LangNum1Choice(m.context.NumIdxUpd, "directory was", "directories were"),
299-
util.LangNum1Choice(m.context.NumNew, "file hash was", "file hashes were"),
300-
util.LangNum1Choice(m.context.NumUpd, "file hash was", "file hashes were"),
301-
del))
302311
}
303312
} else if m.context.NumNew+m.context.NumUpd+m.context.NumDel > 0 {
313+
m.logInfo(termAlertFG, "No changes were made")
314+
m.logInfo(termAlertFG, fmt.Sprintf("- %s would have been added", util.LangNum1MutateSuffix(m.context.NumNew, "file")))
315+
m.logInfo(termAlertFG, fmt.Sprintf("- %s would have been updated", util.LangNum1MutateSuffix(m.context.NumUpd, "file")))
304316
if m.context.NumDel > 0 {
305-
del = fmt.Sprintf("\n- %s would have been removed", util.LangNum1Choice(m.context.NumDel, "file/directory", "files/directories"))
317+
m.logInfo(termAlertFG, fmt.Sprintf("- %s would have been removed", util.LangNum1Choice(m.context.NumDel, "file/directory", "files/directories")))
306318
}
307-
cprint(termAlertFG, fmt.Sprintf("No changes were made:\n- %s would have been added\n- %s would have been updated%s",
308-
util.LangNum1MutateSuffix(m.context.NumNew, "file"),
309-
util.LangNum1MutateSuffix(m.context.NumUpd, "file"),
310-
del))
311319
}
312320
}
313321

322+
// summarize errors
314323
if len(m.dmgList) > 0 {
315-
eprint(termAlertFG, "chkbit detected damage in these files:")
324+
m.printError("chkbit detected damage in these files:")
316325
for _, err := range m.dmgList {
317326
fmt.Fprintln(os.Stderr, err)
318327
}
319328
n := len(m.dmgList)
320329
status := fmt.Sprintf("error: detected %s with damage!", util.LangNum1MutateSuffix(n, "file"))
321-
m.log(status)
322-
eprint(termAlertFG, status)
330+
m.logFile(status)
331+
m.printError(status)
323332
}
324333

325334
if len(m.errList) > 0 {
326335
status := "chkbit ran into errors"
327-
m.log(status + "!")
328-
eprint(termAlertFG, status+":")
336+
m.logFile(status + "!")
337+
m.printError(status + ":")
329338
for _, err := range m.errList {
330339
fmt.Fprintln(os.Stderr, err)
331340
}
@@ -368,6 +377,16 @@ func (m *Main) run() int {
368377
)
369378
}
370379

380+
if cli.Quiet {
381+
m.progress = Quiet
382+
} else if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
383+
m.progress = Summary
384+
} else if cli.Plain {
385+
m.progress = Plain
386+
} else {
387+
m.progress = Fancy
388+
}
389+
371390
switch ctx.Command() {
372391
case "check <paths>":
373392
cmd = Check
@@ -376,9 +395,9 @@ func (m *Main) run() int {
376395
case "show-ignored-only <paths>":
377396
cmd = Show
378397
case "init-db <path>":
379-
m.log("chkbit init-db " + cli.InitDb.Path)
398+
m.logInfo("", "chkbit init-db "+cli.InitDb.Path)
380399
if err := chkbit.InitializeIndexDb(cli.InitDb.Path, cli.IndexName, cli.InitDb.Force); err != nil {
381-
fmt.Println("error: " + err.Error())
400+
m.logError(err.Error())
382401
return 1
383402
}
384403
return 0
@@ -406,16 +425,6 @@ func (m *Main) run() int {
406425
m.logger = log.New(f, "", 0)
407426
}
408427

409-
if cli.Quiet {
410-
m.progress = Quiet
411-
} else if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
412-
m.progress = Summary
413-
} else if cli.Plain {
414-
m.progress = Plain
415-
} else {
416-
m.progress = Fancy
417-
}
418-
419428
if showRes, err := m.process(cmd, cli); err == nil {
420429
if showRes && cmd != Show {
421430
if m.printResult() != nil {

store.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (s *store) exportCache(dbFile, suffix string) error {
250250
return err
251251
}
252252

253-
err = connR.View(func(tx *bolt.Tx) error {
253+
if err = connR.View(func(tx *bolt.Tx) error {
254254
b := tx.Bucket([]byte("data"))
255255
c := b.Cursor()
256256
var ierr error
@@ -287,7 +287,9 @@ func (s *store) exportCache(dbFile, suffix string) error {
287287
}
288288
}
289289
return ierr
290-
})
290+
}); err != nil {
291+
return err
292+
}
291293

292294
if _, err = file.WriteString(chkbitDbSuffix); err != nil {
293295
return err

0 commit comments

Comments
 (0)