@@ -108,10 +108,45 @@ type Main struct {
108
108
bps * util.RateCalc
109
109
}
110
110
111
- func (m * Main ) log (text string ) {
111
+ func (m * Main ) logFile (text string ) {
112
112
m .logger .Println (time .Now ().UTC ().Format ("2006-01-02 15:04:05" ), text )
113
113
}
114
114
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
+
115
150
func (m * Main ) logStatus (stat chkbit.Status , message string ) bool {
116
151
if stat == chkbit .STATUS_UPDATE_INDEX {
117
152
return false
@@ -124,7 +159,7 @@ func (m *Main) logStatus(stat chkbit.Status, message string) bool {
124
159
}
125
160
126
161
if m .logVerbose || ! stat .IsVerbose () {
127
- m .log (stat .String () + " " + message )
162
+ m .logFile (stat .String () + " " + message )
128
163
}
129
164
130
165
if m .verbose || ! stat .IsVerbose () {
@@ -203,17 +238,17 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
203
238
switch cmd {
204
239
case Check :
205
240
pathList = cli .Check .Paths
206
- m .log ("chkbit check " + strings .Join (pathList , ", " ))
241
+ m .logFile ("chkbit check " + strings .Join (pathList , ", " ))
207
242
case Update :
208
243
pathList = cli .Update .Paths
209
244
m .context .UpdateIndex = true
210
245
m .context .AddOnly = cli .Update .AddOnly
211
246
m .context .ForceUpdateDmg = cli .Update .Force
212
- m .log ("chkbit update " + strings .Join (pathList , ", " ))
247
+ m .logFile ("chkbit update " + strings .Join (pathList , ", " ))
213
248
case Show :
214
249
pathList = cli .ShowIgnoredOnly .Paths
215
250
m .context .ShowIgnoredOnly = true
216
- m .log ("chkbit show-ignored-only " + strings .Join (pathList , ", " ))
251
+ m .logFile ("chkbit show-ignored-only " + strings .Join (pathList , ", " ))
217
252
}
218
253
219
254
m .context .ShowMissing = cli .ShowMissing
@@ -228,10 +263,7 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
228
263
if err == nil {
229
264
// pathList is relative to root
230
265
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 )
235
267
}
236
268
if err != nil {
237
269
return false , err
@@ -251,81 +283,58 @@ func (m *Main) process(cmd Command, cli CLI) (bool, error) {
251
283
}
252
284
253
285
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
- }
273
286
274
287
if m .progress != Quiet {
275
288
mode := ""
276
289
if ! m .context .UpdateIndex {
277
290
mode = " in readonly mode"
278
291
}
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 )
282
294
283
295
if m .progress == Fancy && m .context .NumTotal > 0 {
284
296
elapsed := time .Since (m .fps .Start )
285
297
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 ) )
289
301
}
290
302
291
- del := ""
292
303
if m .context .UpdateIndex {
293
304
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" )))
294
308
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" ) ))
296
310
}
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 ))
302
311
}
303
312
} 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" )))
304
316
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" ) ))
306
318
}
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 ))
311
319
}
312
320
}
313
321
322
+ // summarize errors
314
323
if len (m .dmgList ) > 0 {
315
- eprint ( termAlertFG , "chkbit detected damage in these files:" )
324
+ m . printError ( "chkbit detected damage in these files:" )
316
325
for _ , err := range m .dmgList {
317
326
fmt .Fprintln (os .Stderr , err )
318
327
}
319
328
n := len (m .dmgList )
320
329
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 )
323
332
}
324
333
325
334
if len (m .errList ) > 0 {
326
335
status := "chkbit ran into errors"
327
- m .log (status + "!" )
328
- eprint ( termAlertFG , status + ":" )
336
+ m .logFile (status + "!" )
337
+ m . printError ( status + ":" )
329
338
for _ , err := range m .errList {
330
339
fmt .Fprintln (os .Stderr , err )
331
340
}
@@ -368,6 +377,16 @@ func (m *Main) run() int {
368
377
)
369
378
}
370
379
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
+
371
390
switch ctx .Command () {
372
391
case "check <paths>" :
373
392
cmd = Check
@@ -376,9 +395,9 @@ func (m *Main) run() int {
376
395
case "show-ignored-only <paths>" :
377
396
cmd = Show
378
397
case "init-db <path>" :
379
- m .log ( " chkbit init-db " + cli .InitDb .Path )
398
+ m .logInfo ( "" , " chkbit init-db "+ cli .InitDb .Path )
380
399
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 ())
382
401
return 1
383
402
}
384
403
return 0
@@ -406,16 +425,6 @@ func (m *Main) run() int {
406
425
m .logger = log .New (f , "" , 0 )
407
426
}
408
427
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
-
419
428
if showRes , err := m .process (cmd , cli ); err == nil {
420
429
if showRes && cmd != Show {
421
430
if m .printResult () != nil {
0 commit comments