emtrace 0.1.0

Minimal overhead tracing/logging library for embedded, and resource constrained projects.
Documentation
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
#![doc = include_str!("../README.md")]
use core::convert::Infallible;
use core::mem::size_of;
use core::ops::DerefMut;
use core::result::Result;
use std::io::StdoutLock;
use std::io::Write;
use std::sync::MutexGuard;

cfg_if::cfg_if!(
    if #[cfg(feature="ptr_u128")] {
        pub type PointerT = u128;
    } else if #[cfg(feature="ptr_u64")] {
        pub type PointerT = u64;
    } else if #[cfg(feature="ptr_u32")] {
        pub type PointerT = u32;
    } else if #[cfg(feature="ptr_u16")] {
        pub type PointerT = u16;
    } else if #[cfg(feature="ptr_u8")] {
        pub type PointerT = u8;
    } else {
        pub type PointerT = u128;
    }
);

cfg_if::cfg_if!(
    if #[cfg(feature="size_u128")] {
        pub type SizeT = u128;
    } else if #[cfg(feature="size_u64")] {
        pub type SizeT = u64;
    } else if #[cfg(feature="size_u32")] {
        pub type SizeT = u32;
    } else if #[cfg(feature="size_u16")] {
        pub type SizeT = u16;
    } else if #[cfg(feature="size_u8")] {
        pub type SizeT = u8;
    } else {
        pub type SizeT = u128;
    }
);

cfg_if::cfg_if!(
    if #[cfg(feature="alignment_power_0")] {
        pub const ALIGNMENT_POWER: u8 = 0;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(1), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_1")] {
        pub const ALIGNMENT_POWER: u8 = 1;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(2), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_2")] {
        pub const ALIGNMENT_POWER: u8 = 2;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(4), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_3")] {
        pub const ALIGNMENT_POWER: u8 = 3;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(8), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_4")] {
        pub const ALIGNMENT_POWER: u8 = 4;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(16), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_5")] {
        pub const ALIGNMENT_POWER: u8 = 5;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(32), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_6")] {
        pub const ALIGNMENT_POWER: u8 = 6;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(64), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_7")] {
        pub const ALIGNMENT_POWER: u8 = 7;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(128), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_8")] {
        pub const ALIGNMENT_POWER: u8 = 8;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(256), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_9")] {
        pub const ALIGNMENT_POWER: u8 = 9;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(512), C)]
                $struct
            }
        }
    } else if #[cfg(feature="alignment_power_10")] {
        pub const ALIGNMENT_POWER: u8 = 10;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(1024), C)]
                $struct
            }
        }
    } else {
        pub const ALIGNMENT_POWER: u8 = 6;
        macro_rules! format_info_repr {
            ($struct:item) => {
                #[repr(align(64), C)]
                $struct
            }
        }
    }
);

format_info_repr! {
    pub struct FormatInfo<const LEN: usize> {
        pub bytes: [u8; LEN],
    }
}

#[derive(Copy, Clone)]
pub enum Size {
    Static(usize),
    NullTerminated,
    LengthPrefixed,
}

impl Size {
    pub const fn bytes(&self) -> [u8; size_of::<SizeT>()] {
        match self {
            Self::NullTerminated => NULL_TERMINATED.to_ne_bytes(),
            Self::LengthPrefixed => LENGTH_PREFIXED.to_ne_bytes(),
            Self::Static(size) => (*size as SizeT).to_ne_bytes(),
        }
    }
}

pub const NULL_TERMINATED: SizeT = 1 << (SizeT::BITS - 1);
pub const LENGTH_PREFIXED: SizeT = 1 << (SizeT::BITS - 2);
pub const PY_FORMAT: SizeT = 0;
pub const NO_FORMAT: SizeT = 1;
pub const C_STYLE_FORMAT: SizeT = 2;

/// Trait for emtrace sinks.
pub trait Sink {
    /// The error that can happen when calling `out`.
    /// Set this to `Infallible` if your implementation can't produce an error.
    type OutError;
    /// The error that can happen when calling `begin`.
    /// Set this to `Infallible` if your implementation can't produce an error.
    type BeginError;
    /// The bytes passed to this function are the contents of the emitted trace
    fn out(&mut self, b: &[u8]) -> Result<(), Self::OutError>;
    /// Begin emitting a trace.
    ///
    /// This function is called by the `trace` macro just before starting to emit a trace.
    /// It will provide the address of the format info bytes as `info_addr`,
    /// and the total number of bytes that the trace will contain as `total_size`.
    ///
    /// If the trace contains dynamically sized data, then `total_size` is provided as
    /// the minimal number of bytes that will be sent, bitwise ored with either `emtrace::NULL_TERMINATED` or
    /// `emtrace::LENGTH_PREFIXED`, depending on the kind of dynamic data.
    fn begin(&mut self, info_addr: PointerT, total_size: SizeT) -> Result<(), Self::BeginError>;
}

/// Initialize the trace, by serializing the virtual memory address of the
/// magic constant into the given sink.
pub fn init<T: Sink>(sink: &mut T) -> Result<(), T::OutError> {
    magic_address_bytes().serialize(sink)
}

impl<T: Sink> Sink for MutexGuard<'_, T> {
    type OutError = T::OutError;
    type BeginError = T::BeginError;
    fn out(&mut self, b: &[u8]) -> Result<(), Self::OutError> {
        self.deref_mut().out(b)
    }
    fn begin(&mut self, info_addr: PointerT, total_size: SizeT) -> Result<(), Self::BeginError> {
        self.deref_mut().begin(info_addr, total_size)
    }
}

impl Sink for StdoutLock<'_> {
    type OutError = std::io::Error;
    type BeginError = Infallible;
    fn out(&mut self, b: &[u8]) -> Result<(), Self::OutError> {
        self.write_all(b)?;
        Ok(())
    }
    fn begin(&mut self, _info_addr: PointerT, _total_size: SizeT) -> Result<(), Self::BeginError> {
        Ok(())
    }
}

impl Sink for Vec<u8> {
    type OutError = Infallible;
    type BeginError = Infallible;
    fn out(&mut self, b: &[u8]) -> Result<(), Self::OutError> {
        self.extend_from_slice(b);
        Ok(())
    }
    fn begin(&mut self, _info_addr: PointerT, total_size: SizeT) -> Result<(), Self::BeginError> {
        let total_size = total_size & !(NULL_TERMINATED | LENGTH_PREFIXED);
        self.reserve(total_size as usize);
        Ok(())
    }
}

pub const DESCENDANTS_LIMIT: usize = 0x1000;

pub trait Trace {
    const SIZE: Size;
    const ID: &'static str;
    const NUM_CHILDREN: usize;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)];
    /// How many SizeT fields the type requires in the offset table
    const NUM_OFFSET_TABLE_ENTRIES: usize = {
        let mut n = 0;
        // to store own size, and number of children
        n += 2;
        // to store offset to own id
        n += 1;

        let mut i = 0;
        let mut remaining = Self::NUM_CHILDREN;

        // iterate over all descendants
        while remaining > 0 {
            let descendant = Self::DESCENDANTS[i];

            // to store descendant's size and number of children
            n += 2;
            // to store offsets to descendant's name, and id
            n += 2;

            remaining += descendant.3;
            remaining -= 1;
            i += 1;
        }

        n
    };

    /// how many bytes this type needs to describe its type to the parser
    const INFO_SIZE: usize = {
        let mut size = 0;
        // to store own id as null-terminated string
        size += Self::ID.len() + size_of::<u8>();

        let mut i = 0;
        let mut remaining = Self::NUM_CHILDREN;

        // iterate over all descendants
        while remaining > 0 {
            let descendant = Self::DESCENDANTS[i];

            // to store descendant's name as null-terminated string
            size += descendant.0.len() + size_of::<u8>();
            // to store descendant's id as null-terminated string
            size += descendant.1.len() + size_of::<u8>();

            remaining += descendant.3;
            remaining -= 1;
            i += 1;
        }

        size
    };
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError>;
    fn size(&self) -> usize;
}

impl<T> Trace for &T
where
    T: Trace,
    T: ?Sized,
{
    const SIZE: Size = T::SIZE;
    const ID: &'static str = T::ID;
    const NUM_CHILDREN: usize = T::NUM_CHILDREN;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = T::DESCENDANTS;
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        (*self).serialize(f)
    }
    fn size(&self) -> usize {
        (*self).size()
    }
}

impl<T: Trace> Trace for Box<T> {
    const SIZE: Size = T::SIZE;
    const ID: &'static str = T::ID;
    const NUM_CHILDREN: usize = T::NUM_CHILDREN;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = T::DESCENDANTS;
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        self.as_ref().serialize(f)
    }
    fn size(&self) -> usize {
        self.as_ref().size()
    }
}

impl Trace for str {
    const SIZE: Size = Size::NullTerminated;
    const ID: &'static str = "string";
    const NUM_CHILDREN: usize = 0;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = &[];
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        f.out(self.as_bytes())
    }
    fn size(&self) -> usize {
        self.len()
    }
}

impl Trace for String {
    const SIZE: Size = str::SIZE;
    const ID: &'static str = str::ID;
    const NUM_CHILDREN: usize = str::NUM_CHILDREN;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = str::DESCENDANTS;
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        self.as_str().serialize(f)
    }
    fn size(&self) -> usize {
        self.as_str().size()
    }
}

impl<T: Trace> Trace for [T] {
    const SIZE: Size = Size::LengthPrefixed;
    const ID: &'static str = "list";
    const NUM_CHILDREN: usize = 1;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = &{
        let mut children = [("", T::ID, T::SIZE, T::NUM_CHILDREN); DESCENDANTS_LIMIT];

        let mut remaining = T::NUM_CHILDREN;
        let mut i = 0;
        while remaining > 0 {
            children[i + Self::NUM_CHILDREN] = T::DESCENDANTS[remaining];
            remaining += T::DESCENDANTS[remaining].3;
            remaining -= 1;
            i += 1;
        }

        children
    };
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        for el in self {
            if matches!(T::SIZE, Size::LengthPrefixed) {
                (el.size() as SizeT).serialize(f)?;
            }
            el.serialize(f)?;
            if matches!(T::SIZE, Size::NullTerminated) {
                0u8.serialize(f)?;
            }
        }
        Ok(())
    }
    fn size(&self) -> usize {
        self.len()
    }
}

impl<T: Trace> Trace for Vec<T> {
    const SIZE: Size = <[T] as Trace>::SIZE;
    const ID: &'static str = <[T] as Trace>::ID;
    const NUM_CHILDREN: usize = <[T] as Trace>::NUM_CHILDREN;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] =
        <[T] as Trace>::DESCENDANTS;
    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        self[..].serialize(f)
    }
    fn size(&self) -> usize {
        self[..].size()
    }
}

impl<T: Trace, const N: usize> Trace for [T; N] {
    const SIZE: Size = Size::Static(N);
    const ID: &'static str = "list";
    const NUM_CHILDREN: usize = 1;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] =
        <[T] as Trace>::DESCENDANTS;

    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        for el in self {
            if matches!(T::SIZE, Size::LengthPrefixed) {
                (el.size() as SizeT).serialize(f)?;
            }
            el.serialize(f)?;
            if matches!(T::SIZE, Size::NullTerminated) {
                0u8.serialize(f)?;
            }
        }
        Ok(())
    }

    fn size(&self) -> usize {
        N
    }
}

macro_rules! impl_trace_for_primitive {
    ($type:ty, $id:expr) => {
        impl Trace for $type {
            const SIZE: Size = Size::Static(std::mem::size_of::<Self>());
            const ID: &'static str = $id;
            const NUM_CHILDREN: usize = 0;
            const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = &[];

            fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
                f.out(&self.to_ne_bytes())
            }

            fn size(&self) -> usize {
                std::mem::size_of::<Self>()
            }
        }
    };
}

impl_trace_for_primitive!(i8, "signed char");
impl_trace_for_primitive!(i16, "signed");
impl_trace_for_primitive!(i32, "signed");
impl_trace_for_primitive!(i64, "signed");
impl_trace_for_primitive!(i128, "signed");
impl_trace_for_primitive!(isize, "signed");

impl_trace_for_primitive!(u8, "char");
impl_trace_for_primitive!(u16, "unsigned");
impl_trace_for_primitive!(u32, "unsigned");
impl_trace_for_primitive!(u64, "unsigned");
impl_trace_for_primitive!(u128, "unsigned");
impl_trace_for_primitive!(usize, "unsigned");

impl_trace_for_primitive!(f32, "float");
impl_trace_for_primitive!(f64, "double");

impl Trace for bool {
    const SIZE: Size = Size::Static(std::mem::size_of::<u8>());
    const ID: &'static str = "bool";
    const NUM_CHILDREN: usize = 0;
    const DESCENDANTS: &'static [(&'static str, &'static str, Size, usize)] = &[];

    fn serialize<O: Sink>(&self, f: &mut O) -> Result<(), O::OutError> {
        f.out(&[*self as u8])
    }

    fn size(&self) -> usize {
        std::mem::size_of::<u8>()
    }
}

#[derive(Debug)]
pub enum Error<BeginError, OutError> {
    Begin(BeginError),
    Out(OutError),
}

#[macro_export]
macro_rules! count {
    () => (0usize);
    ( $x:tt $($xs:tt)* ) => (1usize + $crate::count!($($xs)*));
}

#[macro_export]
macro_rules! sink {
    () => {
        std::io::stdout().lock()
    };
    (.sink_guard=$sink_guard:expr,) => {
        $sink_guard
    };
    (.sink_guard=$sink_guard:expr, .sink=$sink:expr,) => {{
        const _: () = assert!(
            false,
            "Only one of '.sink', and '.sink_guard' may be specified"
        );
        ()
    }};
    (.sink=$sink:expr,) => {
        ()
    };
}

/// The core macro for emitting a trace.
///
/// `trace_impl!` is the main macro that `trace!` and `traceln!` are built on.
///
/// The format string is the concatenation of all string literals before the first comma.
/// After that come the values to be emitted, along with their types, e.g.:
///
/// ```rust
/// # use emtrace::{trace_impl, Sink, traceln};
/// # fn main() {
/// trace_impl!("{} + {}" " = {}", i32: 1, i32: 2, i32: 3);
/// # }
/// ```
///
/// A type can be made traceable by implementing the `Trace` trait for it.
///
/// ### Customization
///
/// `trace_impl!` supports several customization options, passed after the trace arguments:
///
/// - **`.sink = <variable>`**: Specifies a sink to write the trace output to. The macro will
///   mutably borrow the value for the duration of the call. It must implement the
///   [`Sink`] trait. For example one could capture trace output in a buffer like so:
///   ```rust
///   # use emtrace::{trace_impl, Sink};
///   # fn main() {
///   let mut buf = Vec::new();
///   trace_impl!("Hello", .sink = buf);
///   // `buf` is still owned and contains the trace output
///   assert!(!buf.is_empty());
///   # }
///   ```
///
/// - **`.sink_guard = <expr>`**: Specifies a sink that will be consumed by the macro. Like with
///   `.sink=` the expression has to  evaluate to a value that implements [`Sink`].
///   This value is moved into the macro and dropped at the end of the scope.
///   This is ideal for sinks that manage a resource, like a `MutexGuard`,
///   as it ensures the resource is released correctly.
///   ```rust
///   # use emtrace::{trace_impl, Sink};
///   # use std::sync::Mutex;
///   # fn main() {
///   let sink = Mutex::new(Vec::new());
///   trace_impl!("Hello", .sink_guard = sink.lock().unwrap());
///   // The mutex is unlocked here as the guard has been dropped.
///   # }
///   ```
///   By default, if neither `.sink` nor `.sink_guard` is provided, the trace is written to
///   `std::io::stdout().lock()`.
///
/// - **`.section = "<section_name>"`**: Specifies the ELF section where the format string and
///   metadata are stored. Defaults to `.emtrace`.
///
/// - **`.formatter = <expr>`**: Specifies how the format string should be interpreted by the consumer
///   of the trace data. It can be one of `PY_FORMAT`, `C_STYLE_FORMAT`, or `NO_FORMAT`.
///
/// - **`.error_handler = <closure>`**: Provides a closure to handle potential errors during tracing.
///   The closure receives a `Result<(), Error<BeginError, OutError>>`. If not provided, the `Result`
///   is returned from the macro expansion.
///   ```rust
///   # use emtrace::{trace_impl, Sink, Error};
///   # fn main() {
///   trace_impl!("Hello", .error_handler = |res: Result<(), Error<_, _>>| {
///       if res.is_err() {
///           // Handle error
///       }
///   });
///   # }
///   ```
///
/// The macro returns the output of the provided error handler.
///
/// [`Sink`]: trait.Sink.html
#[macro_export]
macro_rules! trace_impl {
    ($($fmt:literal)+ $(, $($types:ty : $args:expr),+)? $(, .section = $section:literal)? $(, .sink_guard = $sink_guard:expr)? $(, .sink = $sink:expr)? $(, .error_handler = $error_handler:expr)? $(, .formatter=$formatter:expr)?)
     =>
    {
        {
            let result = 'trace: {
                const FMT: &str = core::concat!($($fmt,)+);
                const NUM_PREFIX: usize = {
                    let mut size = 5;
                    $($(
                            size += <$types as $crate::Trace>::NUM_OFFSET_TABLE_ENTRIES;
                    )*)?
                        size
                };
                const INFO_SIZE: usize = {
                    let mut size: usize = 0;
                    $($(
                            size += <$types as $crate::Trace>::INFO_SIZE;
                    )*)?
                        size += NUM_PREFIX * size_of::<$crate::SizeT>() + FMT.as_bytes().len() + file!().as_bytes().len() + 2;
                    size
                };
                #[unsafe(link_section = ".emtrace")]
                $(
                    static _DUMMY: i32 = 0;
                    #[unsafe(link_section = $section)]
                )?
                    #[used]
                    static FMT_INFO: $crate::FormatInfo<INFO_SIZE> = {
                        let size_t_size = size_of::<$crate::SizeT>();
                        let num_args = ($crate::count!($($($types)*)?)) as $crate::SizeT;
                        let mut offset_idx: usize = 0;
                        let mut data_idx = (NUM_PREFIX * size_t_size) as $crate::SizeT;
                        let mut info  = [0; INFO_SIZE];

                        let num_args_arr = num_args.to_ne_bytes();
                        let mut i = 0;
                        while i < size_t_size {
                            info[offset_idx] = num_args_arr[i];
                            offset_idx += 1;
                            i += 1;
                        }

                        let fmt_offset_arr = data_idx.to_ne_bytes();
                        let mut i = 0;
                        while i < size_t_size {
                            info[offset_idx] = fmt_offset_arr[i];
                            offset_idx += 1;
                            i += 1;
                        }

                        let fmt_str_arr = FMT.as_bytes();
                        let mut i = 0;
                        while i < FMT.len() {
                            info[data_idx as usize] = fmt_str_arr[i];
                            data_idx += 1;
                            i += 1;
                        }
                        data_idx += 1;

                        ($($(
                                    {
                                        let id_offset_arr = data_idx.to_ne_bytes();
                                        let mut i = 0;
                                        while i < size_t_size {
                                            info[offset_idx] = id_offset_arr[i];
                                            offset_idx += 1;
                                            i += 1;
                                        }

                                        let size_arr = <$types as $crate::Trace>::SIZE.bytes();
                                        let mut i = 0;
                                        while i < size_t_size {
                                            info[offset_idx] = size_arr[i];
                                            offset_idx += 1;
                                            i += 1;
                                        }

                                        let num_children_arr = (<$types as $crate::Trace>::NUM_CHILDREN as $crate::SizeT).to_ne_bytes();
                                        let mut i = 0;
                                        while i < size_t_size {
                                            info[offset_idx] = num_children_arr[i];
                                            offset_idx += 1;
                                            i += 1;
                                        }

                                        let id_arr = <$types as $crate::Trace>::ID.as_bytes();
                                        let mut i = 0;
                                        while i < <$types as $crate::Trace>::ID.len() {
                                            info[data_idx as usize] = id_arr[i];
                                            data_idx += 1;
                                            i += 1;
                                        }
                                        data_idx += 1;

                                        let mut j = 0;
                                        let mut remaining = <$types as $crate::Trace>::NUM_CHILDREN;
                                        while remaining > 0 {
                                            let descendant = <$types as $crate::Trace>::DESCENDANTS[j];

                                            let name_offset_arr = data_idx.to_ne_bytes();
                                            let mut i = 0;
                                            while i < size_t_size {
                                                info[offset_idx] = name_offset_arr[i];
                                                offset_idx += 1;
                                                i += 1;
                                            }

                                            let size_arr = descendant.2.bytes();
                                            let mut i = 0;
                                            while i < size_t_size {
                                                info[offset_idx] = size_arr[i];
                                                offset_idx += 1;
                                                i += 1;
                                            }

                                            let num_children_arr = (descendant.3 as $crate::SizeT).to_ne_bytes();
                                            let mut i = 0;
                                            while i < size_t_size {
                                                info[offset_idx] = num_children_arr[i];
                                                offset_idx += 1;
                                                i += 1;
                                            }

                                            let n = descendant.0.len();
                                            let name_arr = descendant.0.as_bytes();
                                            let mut i = 0;
                                            while i < n {
                                                info[data_idx as usize] = name_arr[i];
                                                data_idx += 1;
                                                i += 1;
                                            }
                                            data_idx += 1;

                                            let id_offset_arr = data_idx.to_ne_bytes();
                                            let mut i = 0;
                                            while i < size_t_size {
                                                info[offset_idx] = id_offset_arr[i];
                                                offset_idx += 1;
                                                i += 1;
                                            }

                                            let n = descendant.1.len();
                                            let id_arr = descendant.1.as_bytes();
                                            let mut i = 0;
                                            while i < n {
                                                info[data_idx as usize] = id_arr[i];
                                                data_idx += 1;
                                                i += 1;
                                            }
                                            data_idx += 1;

                                            remaining += descendant.3;
                                            remaining -= 1;
                                            j += 1;
                                        }

                                    }
                        ),*)?);

                        let formatter = $crate::NO_FORMAT;
                        // if there is at least one `,type: arg` pair in the macro arguments,
                        // then this will overwrite the default NO_FORMAT formatter to PY_FORMAT
                        $($(
                                let _dummy = formatter;
                                let _dummy = <$types as $crate::Trace>::SIZE;
                                let formatter =  $crate::PY_FORMAT;
                        )*)?
                            $(
                                let _dummy = formatter;
                                let formatter = $formatter;
                            )?
                            let formatter_arr = (formatter as $crate::SizeT).to_ne_bytes();
                        let mut i = 0;
                        while i < size_t_size {
                            info[offset_idx] = formatter_arr[i];
                            offset_idx += 1;
                            i += 1;
                        }

                        let file_offset_arr = data_idx.to_ne_bytes();
                        let mut i = 0;
                        while i < size_t_size {
                            info[offset_idx] = file_offset_arr[i];
                            offset_idx += 1;
                            i += 1;
                        }

                        let file_arr = file!().as_bytes();
                        let mut i = 0;
                        while i < file!().len() {
                            info[data_idx as usize] = file_arr[i];
                            data_idx += 1;
                            i += 1;
                        }

                        let line_arr = (line!() as $crate::SizeT).to_ne_bytes();
                        let mut i = 0;
                        while i < size_t_size {
                            info[offset_idx] = line_arr[i];
                            offset_idx += 1;
                            i += 1;
                        }

                        $crate::FormatInfo{
                            bytes: info
                        }

                    };
                const TOTAL_SIZE: usize = {
                    let mut total_size = 0usize;
                    $($(
                            total_size += match <$types as $crate::Trace>::SIZE {
                                $crate::Size::NullTerminated |$crate::Size::LengthPrefixed => 0,
                                $crate::Size::Static(size) => size,

                            };
                    )*)?
                        total_size += size_of::<$crate::SizeT>();

                    total_size
                };

                use $crate::Sink;
                #[allow(unused_mut)]
                let mut sink = $crate::sink!(
                    $(.sink_guard=$sink_guard,)? $(.sink=$sink,)?
                );
                let sink_ref = &mut sink;
                $(
                    let _dummy = sink_ref;
                    #[allow(unused_mut)]
                    let sink_ref = &mut $sink;
                )?
                let addr = (FMT_INFO.bytes.as_ptr().addr() >> $crate::ALIGNMENT_POWER) as $crate::PointerT;
                let result = sink_ref.begin(addr, TOTAL_SIZE as $crate::SizeT);
                if let Err(err) = result {
                    break 'trace Err($crate::Error::Begin(err));
                }

                let result = $crate::Trace::serialize(&addr, sink_ref);
                if let Err(err) = result {
                    break 'trace Err($crate::Error::Out(err));
                }
                $($(
                        let x:&$types = &$args;
                        if matches!(<$types as $crate::Trace>::SIZE, $crate::Size::LengthPrefixed) {
                            let result = $crate::Trace::serialize(&($crate::Trace::size(x) as $crate::SizeT), sink_ref);
                            if let Err(err) = result {
                                break 'trace Err($crate::Error::Out(err));
                            }
                        }
                        let result = $crate::Trace::serialize(x, sink_ref);
                        if let Err(err) = result {
                                break 'trace Err($crate::Error::Out(err));
                        }
                        if matches!(<$types as $crate::Trace>::SIZE, $crate::Size::NullTerminated) {
                            let null = 0u8;
                            let result = $crate::Trace::serialize(&null, sink_ref);
                            if let Err(err) = result {
                                break 'trace Err($crate::Error::Out(err));
                            }
                        }
                )*)?
                    Ok(())
            };
            $(
                let result = $error_handler(result);
            )?
            result
        }
    }
}

/// Emit a trace.
///
/// The first argument is a format string, the rest are pairs of the form `type:expression`.
/// A trace is emitted - by default over stdout - containing the address of the format string, and
/// the data inside the other parameters.
///
/// ```rust
/// # use emtrace::trace;
/// # fn main() {
/// trace!("Hello, {}", str: "world");
/// # }
/// ```
///
/// ### Error Handling
///
/// By default, this macro will panic if an error occurs during tracing. To handle errors manually,
/// use the `.handle_errors` option. This will cause the macro to return a `Result` instead of
/// panicking, which you can then handle as needed.
///
/// ```rust
/// # use emtrace::trace;
/// # fn main() {
/// let result = trace!("Hello", .handle_errors);
/// if result.is_err() {
///     // Handle the error
/// }
/// # }
/// ```
///
/// For other customization options, see [`trace_impl!`] (this supports all the same options,
/// except for `.error_handler=`).
///
/// [`trace_impl!`]: macro.trace_impl.html
#[macro_export]
macro_rules! trace{
    ($fmt:literal $(, $($types:ty : $args:expr),+)? $(, .section = $section:literal)? $(, .sink_guard = $sink_guard:expr)? $(, .sink= $sink:expr)? $(, .formatter=$formatter:expr)?)
     => {
        $crate::trace_impl!($fmt $(, $($types: $args),+)? $(, .section = $section)? $(, .sink_guard = $sink_guard)? $(, .sink= $sink)?, .error_handler=core::result::Result::unwrap $(, .formatter=$formatter)?)
    };
    ($fmt:literal $(, $($types:ty : $args:expr),+)? $(, .section = $section:literal)? $(, .sink_guard = $sink_guard:expr)? $(, .sink= $sink:expr)?, .handle_errors $(, .formatter=$formatter:expr)?)
     => {
        $crate::trace_impl!($fmt $(, $($types: $args),+)? $(, .section = $section)? $(, .sink_guard = $sink_guard)? $(, .sink= $sink)? $(, .formatter=$formatter)?)
    };
}

/// Emit a trace with a trailing new line in the format string.
///
/// This macro is identical to [`trace!`], but it automatically appends a newline character (`\n`)
/// to the format string.
///
/// ```rust
/// # use emtrace::traceln;
/// # fn main() {
/// traceln!("This will be followed by a newline.");
/// # }
/// ```
///
/// ### Error Handling
///
/// Like `trace!`, this macro will panic on error by default. Use the `.handle_errors` option to
/// receive a `Result` and handle errors manually.
///
/// ```rust
/// # use emtrace::traceln;
/// # fn main() {
/// let result = traceln!("Hello", .handle_errors);
/// if result.is_err() {
///     // Handle the error
/// }
/// # }
/// ```
///
/// For other customization options, see [`trace_impl!`] (this supports all the same options,
/// except for `.error_handler=`).
///
/// [`trace!`]: macro.trace.html
/// [`trace_impl!`]: macro.trace_impl.html
#[macro_export]
macro_rules! traceln {
    ($fmt:literal $(, $($types:ty : $args:expr),+)? $(, .section = $section:literal)? $(, .sink_guard = $sink_guard:expr)? $(, .sink= $sink:expr)? $(, .formatter=$formatter:expr)?)
     => {
        $crate::trace_impl!($fmt "\n" $(, $($types: $args),+)? $(, .section = $section)? $(, .sink_guard = $sink_guard)? $(, .sink= $sink)?, .error_handler=core::result::Result::unwrap $(, .formatter=$formatter)?)
    };
    ($fmt:literal $(, $($types:ty : $args:expr),+)? $(, .section = $section:literal)? $(, .sink_guard = $sink_guard:expr)? $(, .sink= $sink:expr)?, .handle_errors $(, .formatter=$formatter:expr)?)
     => {
        $crate::trace_impl!($fmt "\n" $(, $($types: $args),+)? $(, .section = $section)? $(, .sink_guard = $sink_guard)? $(, .sink= $sink)? $(, .formatter=$formatter)?)
    };
}

#[macro_export]
macro_rules! expect {
    ($expected:literal, .section=$section:literal) => {
        {
            const LEN: usize = $expected.len();
            #[unsafe(link_section = $section)]
            #[unsafe(no_mangle)]
            static EXPECTED_OUTPUT: [u8; LEN] = {
                let mut bytes: [u8; LEN] = [0; LEN];

                let mut i = 0usize;
                while i < LEN {
                    bytes[i] = $expected.as_bytes()[i];
                    i += 1;
                }

                bytes
            };
            let _ = unsafe { std::ptr::read_volatile(&EXPECTED_OUTPUT) };
        }
    };
    ($expected:literal) => {
        expect!($expected, .section=".emtrace.test.expected")
    };
}
const MAGIC_SIZE: usize = 36 + 3 * size_of::<SizeT>();

type Magic = FormatInfo<MAGIC_SIZE>;

#[unsafe(link_section = ".emtrace")]
#[used]
pub static EMTRACE_MAGIC: Magic = {
    let id = [
        0xd1, 0x97, 0xf5, 0x22, 0xd9, 0x26, 0x9f, 0xd1, 0xad, 0x70, 0x33, 0x92, 0xf6, 0x59, 0xdf,
        0xd0, 0xfb, 0xec, 0xbd, 0x60, 0x97, 0x13, 0x25, 0xe8, 0x92, 0x01, 0xb2, 0x5a, 0x38, 0x5d,
        0x9e, 0xc7,
    ];

    let mut magic = [0; MAGIC_SIZE];
    let mut i = 0;
    while i < 32 {
        magic[i] = id[i];
        i += 1;
    }
    magic[32] = 36;
    magic[33] = size_of::<SizeT>() as u8;
    magic[34] = size_of::<PointerT>() as u8;
    magic[35] = ALIGNMENT_POWER;

    let mut idx = 36;

    #[allow(overflowing_literals)]
    let byte_order_id_arr = (0x0f0e0d0c0b0a09080706050403020100 as SizeT).to_ne_bytes();
    let mut i = 0;
    while i < size_of::<SizeT>() {
        magic[idx] = byte_order_id_arr[i];
        idx += 1;
        i += 1;
    }

    let null_terminated_arr = NULL_TERMINATED.to_ne_bytes();
    let mut i = 0;
    while i < size_of::<SizeT>() {
        magic[idx] = null_terminated_arr[i];
        idx += 1;
        i += 1;
    }

    let length_prefixed_arr = LENGTH_PREFIXED.to_ne_bytes();
    let mut i = 0;
    while i < size_of::<SizeT>() {
        magic[idx] = length_prefixed_arr[i];
        idx += 1;
        i += 1;
    }
    Magic { bytes: magic }
};

pub fn magic_address_bytes() -> [u8; size_of::<PointerT>()] {
    let raw_address = (&EMTRACE_MAGIC as *const Magic).addr();
    ((raw_address >> ALIGNMENT_POWER) as PointerT).to_ne_bytes()
}

#[cfg(test)]
mod tests {
    use super::{SizeT, trace};
    use std::str::FromStr;

    #[test]
    fn test() {
        let mut capture = Vec::new();
        trace!("{}", i32: 32, .sink=capture);
        assert_eq!(capture.len(), size_of::<SizeT>() + size_of::<i32>());
        let mut arg_bytes: [u8; size_of::<i32>()] = [0; size_of::<i32>()];
        arg_bytes
            .copy_from_slice(&capture[size_of::<SizeT>()..size_of::<SizeT>() + size_of::<i32>()]);
        let arg = i32::from_ne_bytes(arg_bytes);
        assert_eq!(arg, 32);
    }

    #[test]
    fn empty_vec() {
        let mut capture = Vec::new();
        let v = Vec::<i32>::new();
        trace!("{}", [i32]: v, .sink=capture);
        assert_eq!(capture.len(), size_of::<SizeT>() * 2);
        trace!("{}", Vec<i32>: v, .sink=capture);
        assert_eq!(capture.len(), size_of::<SizeT>() * 4);
    }

    #[test]
    fn flat_vec() {
        let mut capture1 = Vec::new();
        let mut capture2 = Vec::new();
        let v = vec![1, 3, 4, 6];

        let expected_len = 2 * size_of::<SizeT>() + v.len() * size_of::<i32>();
        trace!("{}", [i32]: v, .sink=capture1);
        assert_eq!(capture1.len(), expected_len);
        trace!("{}", Vec<i32>: v, .sink=capture2);
        assert_eq!(capture2.len(), expected_len);

        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture2[size_of::<SizeT>()..]
        );
        assert_ne!(
            capture1[..size_of::<SizeT>()],
            capture2[..size_of::<SizeT>()]
        );
    }

    #[test]
    fn nested_vecs() {
        let mut capture1 = Vec::new();
        let mut capture2 = Vec::new();
        let mut capture3 = Vec::new();
        let mut capture4 = Vec::new();
        let owning = vec![vec![1, 2, 3, 4], vec![10]];
        let referencing_vec = vec![&owning[0], &owning[1]];
        let referencing_slice = vec![&owning[0][..], &owning[1]];

        let expected_len = 2 * size_of::<SizeT>()
            + owning
                .iter()
                .map(|inner| inner.len() * size_of::<i32>() + size_of::<SizeT>())
                .sum::<usize>();
        trace!("{}", Vec<Vec<i32>>: owning, .sink=capture1);
        assert_eq!(capture1.len(), expected_len);
        trace!("{}", [Vec<i32>]: owning, .sink=capture2);
        assert_eq!(capture2.len(), expected_len);
        trace!("{}", [&Vec<i32>]: referencing_vec, .sink=capture3);
        assert_eq!(capture3.len(), expected_len);
        trace!("{}", [&[i32]]: referencing_slice, .sink=capture4);
        assert_eq!(capture4.len(), expected_len);

        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture2[size_of::<SizeT>()..]
        );
        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture3[size_of::<SizeT>()..]
        );
        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture4[size_of::<SizeT>()..]
        );
    }

    #[test]
    fn string() {
        let mut capture1 = Vec::new();
        let mut capture2 = Vec::new();
        let mut capture3 = Vec::new();
        let literal = "Hello, there!";
        let owning = String::from_str(literal).unwrap();

        let expected_len = size_of::<SizeT>() + literal.len() + size_of::<u8>();
        trace!("{}", str: literal, .sink=capture1);
        assert_eq!(capture1.len(), expected_len);
        trace!("{}", str: owning, .sink=capture2);
        assert_eq!(capture2.len(), expected_len);
        trace!("{}", String: owning, .sink=capture3);
        assert_eq!(capture3.len(), expected_len);

        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture2[size_of::<SizeT>()..]
        );
        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture3[size_of::<SizeT>()..]
        );
    }

    #[test]
    fn vec_of_string() {
        let mut capture1 = Vec::new();
        let mut capture2 = Vec::new();
        let slices = vec!["A", "vector", "of", "strings"];
        let owning = slices
            .iter()
            .map(|s| String::from_str(s).unwrap())
            .collect();

        let expected_len = 2 * size_of::<SizeT>() // for pointer, and length-prefix
            + slices
                .iter()
                .map(|inner| inner.len() + size_of::<u8>()) // for content and null-terminator
                .sum::<usize>();
        trace!("{}", Vec<&str>: slices, .sink=capture1);
        assert_eq!(capture1.len(), expected_len);
        trace!("{}", Vec<String>: owning, .sink=capture2);
        assert_eq!(capture2.len(), expected_len);

        assert_eq!(
            capture1[size_of::<SizeT>()..],
            capture2[size_of::<SizeT>()..]
        );
    }
}