dsi_progress_logger/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
/*
 * SPDX-FileCopyrightText: 2023 Inria
 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
 * SPDX-FileCopyrightText: 2024 Fondation Inria
 *
 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
 */

#![doc = include_str!("../README.md")]

use log::info;
use num_format::{Locale, ToFormattedString};
use pluralizer::pluralize;
use std::fmt::{Arguments, Display, Formatter, Result};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, ProcessesToUpdate, RefreshKind, System};
mod utils;
pub use utils::*;

/// Logging trait.
///
/// To log the progress of an activity, you call [`start`](ProgressLog::start).
/// Then, each time you want to mark progress, you call
/// [`update`](ProgressLog::update), which increases the item counter, and will
/// log progress information if enough time has passed since the last log.
/// [`light_update`](ProgressLog::light_update) will perform a time check only
///  on updates multiples of
/// [`LIGHT_UPDATE_MASK`](ProgressLogger::LIGHT_UPDATE_MASK) + 1; it  should be
/// used when the activity has an extremely low cost that is comparable to that
/// of the time check (a call to [`Instant::now()`]) itself.
///
/// A few setters can be called at any time to customize the logger (e.g.,
/// [`item_name`](ProgressLog::item_name),
/// [`log_interval`](ProgressLog::log_interval),
/// [`expected_updates`](ProgressLog::expected_updates), etc.). The setters take
/// and return a mutable reference to the logger, so you must first assign the
/// logger to a variable, and then you can chain-call the setters on the
/// variable in fluent style. The disadvantage of this approach is that you must
/// assign the logger to a variable, but the advantage is that you can call any
/// setter without having to reassign the variable holding the logger.
///
/// It is also possible to log used and free memory at each log interval by
/// calling [`display_memory`](ProgressLog::display_memory). Memory is read from
/// system data by the [`sysinfo`] crate, and will be updated at each log
/// interval (note that this will slightly slow down the logging process).
///
/// At any time, displaying the progress logger will give you time information
/// up to the present. However,  since it is impossible to update the memory
/// information from the [`Display::fmt`] implementation, you should call
/// [`refresh`](ProgressLog::refresh) before displaying the logger on your own.
///
/// When the activity is over, you call [`stop`](ProgressLog::stop), which fixes
/// the final time, and possibly display again the logger.
///  [`done`](ProgressLog::done) will stop the logger, print `Completed.`, and
/// display the final stats.
///
/// After you finish a run of the progress logger, can call
/// [`start`](ProgressLog::start) again measure another activity.
///
/// As explained in the [crate documentation](crate), we suggest using
/// `&mut impl ProgressLog` to pass a logger as an argument, to be
/// able to use optional logging.
///
/// # Examples
///
/// See the [`ProgressLogger`] documentation.
pub trait ProgressLog {
    /// The type returned by [`concurrent`](ProgressLog::concurrent).
    type Concurrent: ConcurrentProgressLog;

    /// Force a log of `self` assuming `now` is the current time.
    ///
    /// This is a low-level method that should not be called directly.
    fn log(&mut self, now: Instant);

    /// Log `self` if it is time to log.
    ///
    /// This is a low-level method that should not be called directly.
    fn log_if(&mut self);

    /// Set the display of memory information.
    ///
    /// Memory information include:
    /// - the [resident-set size](sysinfo::Process::memory) of the process that
    ///   created the logger;
    /// - the [virtual-memory size](sysinfo::Process::virtual_memory) of the
    ///   process that created the logger;
    /// - the [available memory](sysinfo::System::available_memory);
    /// - the [free memory](`sysinfo::System::free_memory);
    /// - the [total amount](sysinfo::System::total_memory) of memory.
    fn display_memory(&mut self, display_memory: bool) -> &mut Self;

    /// Set the name of an item.
    fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self;

    /// Set the log interval.
    fn log_interval(&mut self, log_interval: Duration) -> &mut Self;

    /// Set the expected number of updates.
    ///
    /// If not [`None`], the logger will display the percentage of completion
    /// and an estimate of the time to completion.
    fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self;

    /// Set the time unit to use for speed.
    ///
    /// If not [`None`], the logger will always display the speed in this unit
    /// instead of making a choice of readable unit based on the elapsed time.
    /// Moreover, large numbers will not be thousands separated. This behavior
    /// is useful when the output of the logger must be parsed.
    fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self;

    /// Set whether to display additionally the speed achieved during the last
    /// log interval.
    fn local_speed(&mut self, local_speed: bool) -> &mut Self;

    /// Set the [`log`] target.
    ///
    /// This should often be the path of the module logging progress, which is
    /// obtained with [`std::module_path!`].
    ///
    /// Note that the macro [`progress_logger!`] sets this field automatically
    /// to [`std::module_path!`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use dsi_progress_logger::prelude::*;
    ///
    ///    env_logger::builder()
    ///        .filter_level(log::LevelFilter::Info)
    ///        .try_init()?;
    ///
    /// let mut pl = ProgressLogger::default();
    /// pl.item_name("pumpkin");
    /// pl.log_target(std::module_path!());
    /// pl.start("Smashing pumpkins from a module...");
    /// for _ in 0..100 {
    ///    // do something on each pumpkin
    ///    pl.update();
    /// }
    /// pl.done();
    /// #     Ok(())
    /// # }
    /// ```
    fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self;

    /// Add a value to the counter.
    ///
    /// This method is mainly useful for wrappers or to implement a custom
    /// update strategy.
    fn add_to_count(&mut self, count: usize);

    /// Start the logger, displaying the given message.
    ///
    /// You can pass the empty string to display nothing.
    fn start(&mut self, msg: impl AsRef<str>);

    /// Increase the count and check whether it is time to log.
    fn update(&mut self);

    /// Set the count and check whether it is time to log.
    fn update_with_count(&mut self, count: usize);

    /// Increase the count but checks whether it is time to log only after an
    /// implementation-defined number of calls.
    ///
    /// Useful for very short activities with respect to which  checking the
    /// time is expensive.
    fn light_update(&mut self);

    /// Increase the count and forces a log.
    fn update_and_display(&mut self);

    /// Stop the logger, fixing the final time.
    fn stop(&mut self);

    /// Stop the logger, print `Completed.`, and display the final stats. The
    /// number of expected updates will be cleared.
    fn done(&mut self);

    /// Stop the logger, sets the count, prints `Completed.`, and displays the
    /// final stats. The number of expected updates will be cleared.
    ///
    /// This method is particularly useful in two circumstances:
    /// * you have updated the logger with some approximate values (e.g., in a
    ///   multicore computation) but before printing the final stats you want
    ///   the internal counter to contain an exact value;
    /// * you have used the logger as a handy timer, calling just
    ///   [`start`](#fields.start) and this method.
    fn done_with_count(&mut self, count: usize);

    /// Return the elapsed time since the logger was started, or `None` if the
    /// logger has not been started.
    fn elapsed(&self) -> Option<Duration>;

    /// Refreshe memory information, if previously requested with
    /// [`display_memory`](#method.display_memory). You do not need to call this
    /// method unless you display the logger manually.
    fn refresh(&mut self);

    /// Output the given message.
    ///
    /// For maximum flexibility, this method takes as argument the result of a
    /// [`std::format_args!`] macro. Note that there will be no output if the
    /// logger is the [`None`] variant.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use dsi_progress_logger::*;
    ///
    /// env_logger::builder()
    ///     .filter_level(log::LevelFilter::Info)
    ///     .try_init()?;
    ///
    /// let logger_name = "my_logger";
    /// let mut pl = progress_logger![];
    /// pl.info(format_args!("My logger named {}", logger_name));
    /// #     Ok(())
    /// # }
    /// ```
    fn info(&self, args: Arguments<'_>);

    /// Return a concurrent copy of the logger.
    ///
    /// Some methods require both sequential and concurrent logging. To keep
    /// optional logging efficient, we suggest in this cases to use `&mut impl
    /// ProgressLog` to pass a logger as an argument, and then creating a
    /// concurrent copy of the logger with this method. If the original logger
    /// is `None`, the concurrent copy will be `None` as well.
    ///
    /// Concurrent logger implementations can just return
    /// [`dup`](ConcurrentProgressLog::dup).
    fn concurrent(&self) -> Self::Concurrent;
}

/// Concurrent logging trait.
///
/// This trait extends [`ProgressLog`] by adding a
/// [`dup`](ConcurrentProgressLog::dup) method that duplicates the logger
/// and adding the [`Clone`], [`Sync`], and [`Send`] traits.
///
/// By contract, [`Clone`] implementations must return a new logger
/// updating the same internal state, so you can easily use a
/// [`ConcurrentProgressLog`] in methods like
/// [`rayon::ParallelIterator::for_each_with`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.for_each_with),
/// [`rayon::ParallelIterator::map_with`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.map_with),
/// and so on.
///
/// Note that [`ProgressLogger`]'s [`Clone`
/// implementation](ProgressLogger#impl-Clone-for-ProgressLogger) has a
/// completely different semantics.
///
/// As explained in the [crate documentation](crate), we suggest using `&mut
/// Self::Concurrent` to pass a concurrent logger as an argument, to
/// be able to use optional logging.
///
/// # Examples
///
/// See the [`ConcurrentWrapper`] documentation.
///     type Concurrent = Option<P::Concurrent>;
///
pub trait ConcurrentProgressLog: ProgressLog + Sync + Send + Clone {
    /// The type returned by [`dup`](ConcurrentProgressLog::dup).
    type Duplicated: ConcurrentProgressLog;

    /// Duplicate the concurrent progress logger, obtaning a new one.
    ///
    /// Note that the this method has the same sematics of [`ProgressLogger`'s
    /// `Clone` implementation](ProgressLogger#impl-Clone-for-ProgressLogger),
    /// but in a [`ConcurrentProgressLog`] by contract [cloning must generate
    /// copies with the same underlying logger](ConcurrentProgressLog).
    fn dup(&self) -> Self::Duplicated;
}

impl<P: ProgressLog> ProgressLog for &mut P {
    type Concurrent = P::Concurrent;

    fn log(&mut self, now: Instant) {
        (**self).log(now);
    }

    fn log_if(&mut self) {
        (**self).log_if();
    }

    fn add_to_count(&mut self, count: usize) {
        (**self).add_to_count(count);
    }

    fn display_memory(&mut self, display_memory: bool) -> &mut Self {
        (**self).display_memory(display_memory);
        self
    }

    fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self {
        (**self).item_name(item_name);
        self
    }

    fn log_interval(&mut self, log_interval: Duration) -> &mut Self {
        (**self).log_interval(log_interval);
        self
    }

    fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self {
        (**self).expected_updates(expected_updates);
        self
    }

    fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self {
        (**self).time_unit(time_unit);
        self
    }

    fn local_speed(&mut self, local_speed: bool) -> &mut Self {
        (**self).local_speed(local_speed);
        self
    }

    fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self {
        (**self).log_target(target);
        self
    }

    fn start(&mut self, msg: impl AsRef<str>) {
        (**self).start(msg);
    }

    fn update(&mut self) {
        (**self).update();
    }

    fn update_with_count(&mut self, count: usize) {
        (**self).update_with_count(count);
    }

    fn light_update(&mut self) {
        (**self).light_update();
    }

    fn update_and_display(&mut self) {
        (**self).update_and_display();
    }

    fn stop(&mut self) {
        (**self).stop();
    }

    fn done(&mut self) {
        (**self).done();
    }

    fn done_with_count(&mut self, count: usize) {
        (**self).done_with_count(count);
    }

    fn elapsed(&self) -> Option<Duration> {
        (**self).elapsed()
    }

    fn refresh(&mut self) {
        (**self).refresh();
    }

    fn info(&self, args: Arguments<'_>) {
        (**self).info(args);
    }

    fn concurrent(&self) -> Self::Concurrent {
        (**self).concurrent()
    }
}

impl<P: ProgressLog> ProgressLog for Option<P> {
    type Concurrent = Option<P::Concurrent>;

    fn log(&mut self, now: Instant) {
        if let Some(pl) = self {
            pl.log(now);
        }
    }

    fn log_if(&mut self) {
        if let Some(pl) = self {
            pl.log_if();
        }
    }

    fn add_to_count(&mut self, count: usize) {
        if let Some(pl) = self {
            pl.add_to_count(count);
        }
    }

    fn display_memory(&mut self, display_memory: bool) -> &mut Self {
        if let Some(pl) = self {
            pl.display_memory(display_memory);
        }
        self
    }

    fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self {
        if let Some(pl) = self {
            pl.item_name(item_name);
        }
        self
    }

    fn log_interval(&mut self, log_interval: Duration) -> &mut Self {
        if let Some(pl) = self {
            pl.log_interval(log_interval);
        }
        self
    }

    fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self {
        if let Some(pl) = self {
            pl.expected_updates(expected_updates);
        }
        self
    }

    fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self {
        if let Some(pl) = self {
            pl.time_unit(time_unit);
        }
        self
    }

    /// Sets whether to display additionally the speed achieved during the last
    /// log interval.
    fn local_speed(&mut self, local_speed: bool) -> &mut Self {
        if let Some(pl) = self {
            pl.local_speed(local_speed);
        }
        self
    }

    fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self {
        if let Some(pl) = self {
            pl.log_target(target);
        }
        self
    }

    fn start(&mut self, msg: impl AsRef<str>) {
        if let Some(pl) = self {
            pl.start(msg);
        }
    }

    fn update(&mut self) {
        if let Some(pl) = self {
            pl.update();
        }
    }

    fn update_with_count(&mut self, count: usize) {
        if let Some(pl) = self {
            pl.update_with_count(count);
        }
    }

    fn light_update(&mut self) {
        if let Some(pl) = self {
            pl.light_update();
        }
    }

    fn update_and_display(&mut self) {
        if let Some(pl) = self {
            pl.update_and_display();
        }
    }

    fn stop(&mut self) {
        if let Some(pl) = self {
            pl.stop();
        }
    }

    fn done(&mut self) {
        if let Some(pl) = self {
            pl.done();
        }
    }

    fn done_with_count(&mut self, count: usize) {
        if let Some(pl) = self {
            pl.done_with_count(count);
        }
    }

    fn elapsed(&self) -> Option<Duration> {
        self.as_ref().and_then(|pl| pl.elapsed())
    }

    fn refresh(&mut self) {
        if let Some(pl) = self {
            pl.refresh();
        }
    }

    fn info(&self, args: Arguments<'_>) {
        if let Some(pl) = self {
            pl.info(args);
        }
    }

    fn concurrent(&self) -> Self::Concurrent {
        self.as_ref().map(|pl| pl.concurrent())
    }
}

impl<P: ConcurrentProgressLog> ConcurrentProgressLog for Option<P> {
    type Duplicated = Option<P::Duplicated>;

    fn dup(&self) -> Self::Duplicated {
        self.as_ref().map(|pl| pl.dup())
    }
}

/// An implementation of [`ProgressLog`] with output generated using the
/// [`log`](https://docs.rs/log) crate at the `info` level.
///
/// Instances can be created by using fluent setters, or by using the
/// [`progress_logger`] macro.
///
/// You can [clone](#impl-Clone-for-ProgressLogger) a logger to create a new one
/// with the same setup but with all the counters reset. This behavior is useful
/// when you want to configure a logger and then use its configuration for other
/// loggers.
///
/// # Examples
///
/// A typical call sequence to a progress logger is as follows:
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dsi_progress_logger::prelude::*;
///
/// env_logger::builder().filter_level(log::LevelFilter::Info).try_init()?;
///
/// let mut pl = ProgressLogger::default();
/// pl.item_name("pumpkin");
/// pl.start("Smashing pumpkins...");
/// for _ in 0..100 {
///    // do something on each pumpkin
///    pl.update();
/// }
/// pl.done();
/// #     Ok(())
/// # }
/// ```
///
/// The [`progress_logger`] macro will create the progress logger for you and
/// set its [`log_target`](ProgressLog::log_target) to [`std::module_path!()`],
/// which is usually what you want. You can also call any setter with a
/// key-value syntax:
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dsi_progress_logger::prelude::*;
///
/// env_logger::builder().filter_level(log::LevelFilter::Info).try_init()?;
///
/// let mut pl = progress_logger![item_name="pumpkin"];
/// pl.start("Smashing pumpkins...");
/// for _ in 0..100 {
///    // do something on each pumpkin
///    pl.update();
/// }
/// pl.done();
/// #     Ok(())
/// # }
/// ```
///
/// A progress logger can also be used as a handy timer:
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dsi_progress_logger::prelude::*;
///
/// env_logger::builder().filter_level(log::LevelFilter::Info).try_init()?;
///
/// let mut pl = progress_logger![item_name="pumpkin"];
/// pl.start("Smashing pumpkins...");
/// for _ in 0..100 {
///    // do something on each pumpkin
/// }
/// pl.done_with_count(100);
/// #     Ok(())
/// # }
/// ```
///
/// This progress logger will display information about  memory usage:
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use dsi_progress_logger::prelude::*;
///
/// env_logger::builder().filter_level(log::LevelFilter::Info).try_init()?;
///
/// let mut pl = progress_logger![display_memory=true];
/// #     Ok(())
/// # }
/// ```
pub struct ProgressLogger {
    /// The name of an item. Defaults to `item`.
    item_name: String,
    /// The log interval. Defaults to 10 seconds.
    log_interval: Duration,
    /// The expected number of updates. If set, the logger will display the percentage of completion and
    /// an estimate of the time to completion.
    expected_updates: Option<usize>,
    /// The time unit to use for speed. If set, the logger will always display the speed in this unit
    /// instead of making a choice of readable unit based on the elapsed time. Moreover, large numbers
    /// will not be thousands separated. This is useful when the output of the logger must be parsed.
    time_unit: Option<TimeUnit>,
    /// Display additionally the speed achieved during the last log interval.
    local_speed: bool,
    /// [`log`] target
    ///
    /// This is often the path of the module logging progress.
    log_target: String,
    /// When the logger was started.
    start_time: Option<Instant>,
    /// The last time we logged the activity (to compute speed).
    last_log_time: Instant,
    /// The next time we will log the activity.
    next_log_time: Instant,
    /// When the logger was stopped.
    stop_time: Option<Instant>,
    /// The number of items.
    count: usize,
    /// The number of items at the last log (to compute speed).
    last_count: usize,
    /// Display additionally the amount of used and free memory using this [`sysinfo::System`]
    system: Option<System>,
    /// The pid of the current process
    pid: Pid,
}

impl Default for ProgressLogger {
    /// Create a default [`ProgressLogger`] with a log interval of 10 seconds and
    /// item name set to “item”.
    fn default() -> Self {
        Self {
            item_name: "item".into(),
            log_interval: Duration::from_secs(10),
            expected_updates: None,
            time_unit: None,
            local_speed: false,
            log_target: std::env::current_exe()
                .ok()
                .and_then(|path| {
                    path.file_name()
                        .and_then(|s| s.to_owned().into_string().ok())
                })
                .unwrap_or_else(|| "main".to_string()),
            start_time: None,
            last_log_time: Instant::now(),
            next_log_time: Instant::now(),
            stop_time: None,
            count: 0,
            last_count: 0,
            system: None,
            pid: Pid::from(std::process::id() as usize),
        }
    }
}

impl Clone for ProgressLogger {
    /// Clone the logger, returning a logger with the same setup but with all the
    /// counters reset.
    #[allow(clippy::manual_map)]
    fn clone(&self) -> Self {
        Self {
            item_name: self.item_name.clone(),
            log_interval: self.log_interval,
            time_unit: self.time_unit,
            local_speed: self.local_speed,
            system: match self.system {
                Some(_) => Some(System::new_with_specifics(
                    RefreshKind::nothing().with_memory(MemoryRefreshKind::nothing().with_ram()),
                )),
                None => None,
            },
            ..ProgressLogger::default()
        }
    }
}

/// Macro to create a [`ProgressLogger`] with default log target set to
/// [`std::module_path!`], and key-value pairs instead of setters.
///
/// # Examples
///
///
/// ```rust
/// use dsi_progress_logger::prelude::*;
///
/// let mut pl = progress_logger![item_name="pumpkin", display_memory=true];
/// ```
#[macro_export]
macro_rules! progress_logger {
    ($($method:ident = $arg:expr),* $(,)?) => {
        {
            let mut pl = ::dsi_progress_logger::ProgressLogger::default();
            ::dsi_progress_logger::ProgressLog::log_target(&mut pl, ::std::module_path!());
            $(
                ::dsi_progress_logger::ProgressLog::$method(&mut pl, $arg);
            )*
            pl
        }
    }
}

impl ProgressLogger {
    /// Calls to [light_update](ProgressLog::light_update) will cause a call to
    /// [`Instant::now`] only if the current count is a multiple of this mask
    /// plus one.
    pub const LIGHT_UPDATE_MASK: usize = (1 << 20) - 1;

    fn fmt_timing_speed(&self, f: &mut Formatter<'_>, seconds_per_item: f64) -> Result {
        let items_per_second = 1.0 / seconds_per_item;

        let time_unit_timing = self
            .time_unit
            .unwrap_or_else(|| TimeUnit::nice_time_unit(seconds_per_item));

        let time_unit_speed = self
            .time_unit
            .unwrap_or_else(|| TimeUnit::nice_speed_unit(seconds_per_item));

        f.write_fmt(format_args!(
            "{:.2} {}/{}, {:.2} {}/{}",
            items_per_second * time_unit_speed.as_seconds(),
            pluralize(&self.item_name, 2, false),
            time_unit_speed.label(),
            seconds_per_item / time_unit_timing.as_seconds(),
            time_unit_timing.label(),
            self.item_name
        ))?;

        Ok(())
    }
}

impl ProgressLog for ProgressLogger {
    type Concurrent = ConcurrentWrapper<Self>;

    fn log(&mut self, now: Instant) {
        self.refresh();
        info!(target: &self.log_target, "{}", self);
        self.last_count = self.count;
        self.last_log_time = now;
        self.next_log_time = now + self.log_interval;
    }

    fn log_if(&mut self) {
        let now = Instant::now();
        if self.next_log_time <= now {
            self.log(now);
        }
    }

    fn add_to_count(&mut self, count: usize) {
        self.count += count;
    }

    fn display_memory(&mut self, display_memory: bool) -> &mut Self {
        match (display_memory, &self.system) {
            (true, None) => {
                self.system = Some(System::new_with_specifics(
                    RefreshKind::nothing().with_memory(MemoryRefreshKind::nothing().with_ram()),
                ));
            }
            (false, Some(_)) => {
                self.system = None;
            }
            _ => (),
        }
        self
    }

    fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self {
        self.item_name = item_name.as_ref().into();
        self
    }

    fn log_interval(&mut self, log_interval: Duration) -> &mut Self {
        self.log_interval = log_interval;
        self
    }

    fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self {
        self.expected_updates = expected_updates;
        self
    }

    fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self {
        self.time_unit = time_unit;
        self
    }

    fn local_speed(&mut self, local_speed: bool) -> &mut Self {
        self.local_speed = local_speed;
        self
    }

    fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self {
        self.log_target = target.as_ref().into();
        self
    }

    fn start(&mut self, msg: impl AsRef<str>) {
        let now = Instant::now();
        self.start_time = Some(now);
        self.stop_time = None;
        self.count = 0;
        self.last_count = 0;
        self.last_log_time = now;
        self.next_log_time = now + self.log_interval;
        if !msg.as_ref().is_empty() {
            info!(target: &self.log_target, "{}", msg.as_ref());
        }
    }

    fn refresh(&mut self) {
        if let Some(system) = &mut self.system {
            system.refresh_processes_specifics(
                ProcessesToUpdate::Some(&[self.pid]),
                false,
                ProcessRefreshKind::nothing().with_memory(),
            );
        }
    }

    fn update(&mut self) {
        self.count += 1;
        self.log_if();
    }

    fn update_with_count(&mut self, count: usize) {
        self.count += count;
        self.log_if();
    }

    /// Increases the count and, once every
    /// [`LIGHT_UPDATE_MASK`](#fields.LIGHT_UPDATE_MASK) + 1 calls, check
    /// whether it is time to log.
    #[inline(always)]
    fn light_update(&mut self) {
        self.count += 1;
        if (self.count & Self::LIGHT_UPDATE_MASK) == 0 {
            self.log_if();
        }
    }

    fn update_and_display(&mut self) {
        self.count += 1;
        self.log(Instant::now());
    }

    fn stop(&mut self) {
        self.stop_time = Some(Instant::now());
        self.expected_updates = None;
    }

    fn done(&mut self) {
        self.stop();
        info!(target: &self.log_target, "Completed.");
        // just to avoid wrong reuses
        self.expected_updates = None;
        self.refresh();
        info!(target: &self.log_target, "{}", self);
    }

    fn done_with_count(&mut self, count: usize) {
        self.count = count;
        self.done();
    }

    fn elapsed(&self) -> Option<Duration> {
        self.start_time?.elapsed().into()
    }

    fn info(&self, args: Arguments<'_>) {
        info!(target: &self.log_target, "{}", std::fmt::format(args));
    }

    fn concurrent(&self) -> Self::Concurrent {
        ConcurrentWrapper::wrap(self.clone())
    }
}

impl Display for ProgressLogger {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        if let Some(start_time) = self.start_time {
            let count_fmtd = if self.time_unit.is_none() {
                self.count.to_formatted_string(&Locale::en)
            } else {
                self.count.to_string()
            };

            if let Some(stop_time) = self.stop_time {
                let elapsed = stop_time - start_time;
                let seconds_per_item = elapsed.as_secs_f64() / self.count as f64;

                f.write_fmt(format_args!(
                    "Elapsed: {}",
                    TimeUnit::pretty_print(elapsed.as_millis())
                ))?;

                if self.count != 0 {
                    f.write_fmt(format_args!(
                        " [{} {}, ",
                        count_fmtd,
                        pluralize(&self.item_name, self.count as isize, false)
                    ))?;
                    self.fmt_timing_speed(f, seconds_per_item)?;
                    f.write_fmt(format_args!("]"))?
                }
            } else {
                let now = Instant::now();

                let elapsed = now - start_time;

                f.write_fmt(format_args!(
                    "{} {}, {}, ",
                    count_fmtd,
                    pluralize(&self.item_name, self.count as isize, false),
                    TimeUnit::pretty_print(elapsed.as_millis()),
                ))?;

                let seconds_per_item = elapsed.as_secs_f64() / self.count as f64;
                self.fmt_timing_speed(f, seconds_per_item)?;

                if let Some(expected_updates) = self.expected_updates {
                    let millis_to_end: u128 = (expected_updates.saturating_sub(self.count) as u128
                        * elapsed.as_millis())
                        / (self.count as u128 + 1);
                    f.write_fmt(format_args!(
                        "; {:.2}% done, {} to end",
                        100.0 * self.count as f64 / expected_updates as f64,
                        TimeUnit::pretty_print(millis_to_end)
                    ))?;
                }

                if self.local_speed && self.stop_time.is_none() {
                    f.write_fmt(format_args!(" ["))?;

                    let elapsed = now - self.last_log_time;
                    let seconds_per_item =
                        elapsed.as_secs_f64() / (self.count - self.last_count) as f64;
                    self.fmt_timing_speed(f, seconds_per_item)?;

                    f.write_fmt(format_args!("]"))?;
                }
            }

            // It would be ideal to refresh self.system here, but this operation
            // would require an &mut self reference.
            if let Some(system) = &self.system {
                f.write_fmt(format_args!(
                    "; res/vir/avail/free/total mem {}/{}/{}B/{}B/{}B",
                    system
                        .process(self.pid)
                        .map(|process| humanize(process.memory() as _) + "B")
                        .unwrap_or("N/A".to_string()),
                    system
                        .process(self.pid)
                        .map(|process| humanize(process.virtual_memory() as _) + "B")
                        .unwrap_or("N/A".to_string()),
                    humanize(system.available_memory() as _),
                    humanize(system.free_memory() as _),
                    humanize(system.total_memory() as _)
                ))?;
            }

            Ok(())
        } else {
            write!(f, "ProgressLogger not started")
        }
    }
}

/// A [`ConcurrentProgressLog`] implementation that wraps a [`ProgressLog`] in
/// an [`Arc`]/[`Mutex`].
///
/// The methods [`update`](ProgressLog::update) and
/// [`update_with_count`](ProgressLog::update_with_count) buffer the increment
/// and add it to the underlying logger only when the buffer reaches a
/// threshold; this prevents locking the underlying logger too often. The
/// threshold is set at creation using the methods
/// [`with_threshold`](Self::with_threshold) and
/// [`wrap_with_threshold`](Self::wrap_with_threshold), or by calling the method
/// [`threshold`](Self::threshold).
///
/// The method [`light_update`](ProgressLog::light_update), as in the case of
/// [`ProgressLogger`], further delays updates using an even faster check.
///
/// # Examples
///
/// In this example, we manually spawn processes:
///
/// ```rust
/// use dsi_progress_logger::prelude::*;
/// use std::thread;
///
/// let mut cpl = concurrent_progress_logger![item_name = "pumpkin"];
/// cpl.start("Smashing pumpkins (using many threads)...");
///
/// std::thread::scope(|s| {
///     for i in 0..100 {
///         let mut pl = cpl.clone();
///         s.spawn(move || {
///             for _ in 0..100000 {
///                 // do something on each pumpkin
///                 pl.update();
///             }
///         });
///     }
/// });
///
/// cpl.done();
/// ```
///
/// You can obtain the same behavior with [`rayon`](https://docs.rs/rayon) using
/// methods such as
/// [`for_each_with`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.for_each_with)
/// and
/// [`map_with`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.map_with):
///
/// ```rust
/// use dsi_progress_logger::prelude::*;
/// use std::thread;
/// use rayon::prelude::*;
///
/// let mut cpl = concurrent_progress_logger![item_name = "pumpkin"];
/// cpl.start("Smashing pumpkins (using many threads)...");
///
/// (0..1000000).into_par_iter().
///     with_min_len(1000). // optional, might reduce the amount of cloning
///     for_each_with(cpl.clone(), |pl, i| {
///         // do something on each pumpkin
///         pl.update();
///     }
/// );
///
/// cpl.done();
/// ```
///
/// Note that you have to pass `cpl.clone()` to avoid a move that would make the
/// call to [`done`](ProgressLog::done) impossible. Also, since
/// [`for_each_with`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.for_each_with)
/// might perform excessive cloning if jobs are too short, you can use
/// [`with_min_len`](https://docs.rs/rayon/latest/rayon/iter/trait.ParallelIterator.html#method.with_min_len)
/// to reduce the amount of cloning.
pub struct ConcurrentWrapper<P: ProgressLog = ProgressLogger> {
    /// Underlying logger
    inner: Arc<Mutex<P>>,
    /// The number of items processed by the current thread.
    local_count: u32,
    /// The threshold for updating the underlying logger.
    threshold: u32,
}

impl Default for ConcurrentWrapper {
    /// Create a new [`ConcurrentWrapper`] based on a default
    /// [`ProgressLogger`], with a threshold of
    /// [`DEFAULT_THRESHOLD`](Self::DEFAULT_THRESHOLD).
    fn default() -> Self {
        Self {
            inner: Arc::new(Mutex::new(ProgressLogger::default())),
            local_count: 0,
            threshold: Self::DEFAULT_THRESHOLD,
        }
    }
}

impl<P: ProgressLog + Clone> Clone for ConcurrentWrapper<P> {
    /// Clone the concurrent wrapper, obtaning a new one with the same
    /// threshold, with a local count of zero, and with the same inner
    /// [`ProgressLog`].
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            local_count: 0,
            threshold: self.threshold,
        }
    }
}

/// Macro to create a [`ConcurrentWrapper`] based on a
/// [`ProgressLogger`], with default log target set to [`std::module_path!`],
/// and key-value pairs instead of setters.
///
/// # Examples
///
/// ```rust
/// use dsi_progress_logger::prelude::*;
///
/// let mut pl = concurrent_progress_logger![item_name="pumpkin", display_memory=true];
/// ```
#[macro_export]
macro_rules! concurrent_progress_logger {
    ($($method:ident = $arg:expr),* $(,)?) => {
        {
            let mut cpl = ::dsi_progress_logger::ConcurrentWrapper::default();
            ::dsi_progress_logger::ProgressLog::log_target(&mut cpl, ::std::module_path!());
            $(
                ::dsi_progress_logger::ProgressLog::$method(&mut cpl, $arg);
            )*
            cpl
        }
    }
}

impl ConcurrentWrapper {
    /// Create a new [`ConcurrentWrapper`] based on a default
    /// [`ProgressLogger`], using the [default
    /// threshold](Self::DEFAULT_THRESHOLD).
    pub fn new() -> Self {
        Self::with_threshold(Self::DEFAULT_THRESHOLD)
    }

    /// Create a new [`ConcurrentWrapper`] wrapping a default
    /// [`ProgressLogger`], using the given threshold.
    pub fn with_threshold(threshold: u32) -> Self {
        Self {
            inner: Arc::new(Mutex::new(ProgressLogger::default())),
            local_count: 0,
            threshold,
        }
    }
}

impl<P: ProgressLog> ConcurrentWrapper<P> {
    /// The default threshold for updating the underlying logger.
    pub const DEFAULT_THRESHOLD: u32 = 1 << 15;

    /// Calls to [`light_update`](ProgressLog::light_update) will cause a call
    /// to [`update_with_count`](ProgressLog::update_with_count) only if the
    /// current local count is a multiple of this mask plus one.
    ///
    /// Note that this constant is significantly smaller than the one used in
    /// [`ProgressLogger`], as updates will be further delayed by the threshold
    /// mechanism.
    pub const LIGHT_UPDATE_MASK: u32 = (1 << 10) - 1;

    /// Set the threshold for updating the underlying logger.
    ///
    /// Note that concurrent loggers with the same underlying logger
    /// have independent thresholds.
    pub fn threshold(&mut self, threshold: u32) -> &mut Self {
        self.threshold = threshold;
        self
    }

    /// Wrap a given [`ProgressLog`] in a [`ConcurrentWrapper`]
    /// using the [default threshold](Self::DEFAULT_THRESHOLD).
    pub fn wrap(inner: P) -> Self {
        Self {
            inner: Arc::new(Mutex::new(inner)),
            local_count: 0,
            threshold: Self::DEFAULT_THRESHOLD,
        }
    }

    /// Wrap a given [`ProgressLog`] in a [`ConcurrentWrapper`] using a
    /// given threshold.
    pub fn wrap_with_threshold(inner: P, threshold: u32) -> Self {
        Self {
            inner: Arc::new(Mutex::new(inner)),
            local_count: 0,
            threshold,
        }
    }

    /// Force an update of the underlying logger with the current local count.
    pub fn flush(&mut self) {
        self.inner
            .lock()
            .unwrap()
            .add_to_count(self.local_count as _);
        self.local_count = 0;
    }
}

impl<P: ProgressLog + Clone> ConcurrentWrapper<P> {
    /// Duplicates the concurrent wrapper, obtaning a new one with the same
    /// threshold, with a local count of zero, and with an inner
    /// [`ProgressLog`] that is a clone of the original one.
    pub fn dup(&self) -> Self {
        Self {
            inner: Arc::new(Mutex::new(self.inner.lock().unwrap().clone())),
            local_count: 0,
            threshold: self.threshold,
        }
    }
}

impl<P: ProgressLog + Clone + Send> ConcurrentProgressLog for ConcurrentWrapper<P> {
    type Duplicated = ConcurrentWrapper<P>;
    fn dup(&self) -> Self {
        Self {
            inner: Arc::new(Mutex::new(self.inner.lock().unwrap().clone())),
            local_count: 0,
            threshold: self.threshold,
        }
    }
}

impl<P: ProgressLog + Clone + Send> ProgressLog for ConcurrentWrapper<P> {
    type Concurrent = Self;

    fn log(&mut self, now: Instant) {
        self.inner.lock().unwrap().log(now);
        self.local_count = 0;
    }

    fn log_if(&mut self) {
        self.inner.lock().unwrap().log_if();
        self.local_count = 0;
    }

    fn add_to_count(&mut self, count: usize) {
        self.inner.lock().unwrap().add_to_count(count);
    }

    fn display_memory(&mut self, display_memory: bool) -> &mut Self {
        self.inner.lock().unwrap().display_memory(display_memory);
        self
    }

    fn item_name(&mut self, item_name: impl AsRef<str>) -> &mut Self {
        self.inner.lock().unwrap().item_name(item_name);
        self
    }

    fn log_interval(&mut self, log_interval: Duration) -> &mut Self {
        self.inner.lock().unwrap().log_interval(log_interval);
        self
    }

    fn expected_updates(&mut self, expected_updates: Option<usize>) -> &mut Self {
        self.inner
            .lock()
            .unwrap()
            .expected_updates(expected_updates);
        self
    }

    fn time_unit(&mut self, time_unit: Option<TimeUnit>) -> &mut Self {
        self.inner.lock().unwrap().time_unit(time_unit);
        self
    }

    fn local_speed(&mut self, local_speed: bool) -> &mut Self {
        self.inner.lock().unwrap().local_speed(local_speed);
        self
    }

    fn log_target(&mut self, target: impl AsRef<str>) -> &mut Self {
        self.inner.lock().unwrap().log_target(target);
        self
    }

    fn start(&mut self, msg: impl AsRef<str>) {
        self.inner.lock().unwrap().start(msg);
        self.local_count = 0;
    }

    #[inline]
    fn update(&mut self) {
        self.update_with_count(1)
    }

    #[inline]
    fn update_with_count(&mut self, count: usize) {
        match (self.local_count as usize).checked_add(count) {
            None => {
                // Sum overflows, update in two steps
                {
                    let mut inner = self.inner.lock().unwrap();
                    inner.update_with_count(self.local_count as _);
                    inner.update_with_count(count);
                }
                self.local_count = 0;
            }
            Some(total_count) => {
                if total_count >= self.threshold as usize {
                    self.local_count = 0;
                    // Threshold reached, time to flush to the inner ProgressLog
                    self.inner.lock().unwrap().update_with_count(total_count);
                } else {
                    // total_count is lower than self.threshold, which is a u32;
                    // so total_count fits in u32.
                    self.local_count = total_count as u32;
                }
            }
        }
    }

    #[inline]
    fn light_update(&mut self) {
        self.local_count += 1;
        if (self.local_count & Self::LIGHT_UPDATE_MASK) == 0 {
            self.update_with_count(self.local_count as _);
        }
    }

    fn update_and_display(&mut self) {
        {
            let mut inner = self.inner.lock().unwrap();
            inner.add_to_count(self.local_count as _);
            inner.update_and_display();
        }
        self.local_count = 0;
    }

    fn stop(&mut self) {
        self.inner.lock().unwrap().stop();
        self.local_count = 0;
    }

    fn done(&mut self) {
        self.inner.lock().unwrap().done();
        self.local_count = 0;
    }

    fn done_with_count(&mut self, count: usize) {
        self.inner.lock().unwrap().done_with_count(count);
        self.local_count = 0;
    }

    fn elapsed(&self) -> Option<Duration> {
        self.inner.lock().unwrap().elapsed()
    }

    fn refresh(&mut self) {
        self.inner.lock().unwrap().refresh();
    }

    fn info(&self, args: Arguments<'_>) {
        self.inner.lock().unwrap().info(args);
    }

    fn concurrent(&self) -> Self::Concurrent {
        self.dup()
    }
}

/// This implementation just calls [`flush`](ConcurrentWrapper::flush),
///     type Concurrent = Option<P::Concurrent>;
///
/// to guarantee that all updates are correctly passed to the underlying logger.
impl<P: ProgressLog> Drop for ConcurrentWrapper<P> {
    fn drop(&mut self) {
        self.flush();
    }
}

impl Display for ConcurrentWrapper {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        self.inner.lock().unwrap().fmt(f)
    }
}

/// Convenience macro specifying that no (concurrent) logging should be
/// performed.
#[macro_export]
macro_rules! no_logging {
    () => {
        &mut Option::<dsi_progress_logger::ConcurrentWrapper::<dsi_progress_logger::ProgressLogger>>::None
    };
}

pub mod prelude {
    pub use super::{
        concurrent_progress_logger, no_logging, progress_logger, ConcurrentProgressLog,
        ConcurrentWrapper, ProgressLog, ProgressLogger,
    };
}