wpilog-parser 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,6 +21,20 @@ for (const record of decodeRecords(readRecords(bytes))) {
21
21
  }
22
22
  ```
23
23
 
24
+ ## Agent skill
25
+
26
+ This package ships an [Agent Skill](https://agentskills.io) at `skills/wpilog-parser/SKILL.md` that teaches AI coding agents how to use the library. Install it into your project with either:
27
+
28
+ ```bash
29
+ # https://skills.sh/
30
+ npx skills add wpilog-parser
31
+ ```
32
+
33
+ ```bash
34
+ # https://github.com/antfu/skills-npm
35
+ npx skills-npm
36
+ ```
37
+
24
38
  ## Development
25
39
 
26
40
  ```bash
package/dist/index.d.mts CHANGED
@@ -147,13 +147,25 @@ type DataLogInput = Uint8Array | ArrayBuffer;
147
147
  declare function readRecords(input: DataLogInput): Generator<ReadRecord>;
148
148
  //#endregion
149
149
  //#region src/decode-records.d.ts
150
+ /** Options for {@link decodeRecords}. */
151
+ type DecodeRecordsOptions = {
152
+ /**
153
+ * When `true`, throw if a data record references an entry ID with no preceding
154
+ * Start control record (i.e. a corrupt or truncated log). When `false` (default),
155
+ * such orphan records are silently skipped.
156
+ */
157
+ strict?: boolean;
158
+ };
150
159
  /**
151
160
  * Decode raw WPILOG records into typed values.
152
161
  *
153
162
  * Accepts the output of {@link readRecords} and yields fully decoded records,
154
163
  * including struct decoding with dependency resolution.
164
+ *
165
+ * By default, data records that reference an entry ID with no preceding Start
166
+ * control record are silently skipped. Pass `{ strict: true }` to throw instead.
155
167
  */
156
- declare function decodeRecords(records: Iterable<ReadRecord>): Generator<DecodedRecord>;
168
+ declare function decodeRecords(records: Iterable<ReadRecord>, options?: DecodeRecordsOptions): Generator<DecodedRecord>;
157
169
  //#endregion
158
170
  //#region src/struct-payload-to-json.d.ts
159
171
  /** Convert a {@link StructPayload} Map to a plain JSON-compatible object. */
@@ -210,4 +222,4 @@ type StructDeclaration = {
210
222
  /** Parse a WPILib struct specification string into declarations. */
211
223
  declare function parseStructSpecification(declaration: string): StructDeclaration[];
212
224
  //#endregion
213
- export { type CatalogEntry, type ControlRecord, type ControlRecordPayload, ControlRecordType, type DataLogHeader, type DataLogInput, type DataRecord, type DecodedRecord, type EnumSpecification, type FinishControlRecord, KnownStructTypeName, type RawRecord, type ReadRecord, RecordType, type SetMetadataControlRecord, type StartControlRecord, type StructDeclaration, type StructPayload, type StructSpecification, type StructTypeName, catalogEntries, decodeRecords, isDataRecord, parseStructSpecification, readRecords, structPayloadToJson };
225
+ export { type CatalogEntry, type ControlRecord, type ControlRecordPayload, ControlRecordType, type DataLogHeader, type DataLogInput, type DataRecord, type DecodeRecordsOptions, type DecodedRecord, type EnumSpecification, type FinishControlRecord, KnownStructTypeName, type RawRecord, type ReadRecord, RecordType, type SetMetadataControlRecord, type StartControlRecord, type StructDeclaration, type StructPayload, type StructSpecification, type StructTypeName, catalogEntries, decodeRecords, isDataRecord, parseStructSpecification, readRecords, structPayloadToJson };
package/dist/index.mjs CHANGED
@@ -53,7 +53,7 @@ let RecordType = /* @__PURE__ */ function(RecordType) {
53
53
  }({});
54
54
  /** Type guard that narrows a {@link DecodedRecord} to a {@link DataRecord}. */
55
55
  function isDataRecord(record) {
56
- return record.type !== RecordType.Control;
56
+ return record.type !== "control";
57
57
  }
58
58
  //#endregion
59
59
  //#region src/read-records.ts
@@ -66,7 +66,7 @@ function readControlRecordPayload(payload) {
66
66
  const type = view.getUint8(offset.get());
67
67
  offset.advance8();
68
68
  switch (type) {
69
- case ControlRecordType.Start: {
69
+ case 0: {
70
70
  const entryId = view.getUint32(offset.get(), true);
71
71
  offset.advance32();
72
72
  const entryNameLength = view.getUint32(offset.get(), true);
@@ -89,7 +89,7 @@ function readControlRecordPayload(payload) {
89
89
  entryMetadata
90
90
  };
91
91
  }
92
- case ControlRecordType.Finish: {
92
+ case 1: {
93
93
  const entryId = view.getUint32(offset.get(), true);
94
94
  offset.advance32();
95
95
  return {
@@ -97,7 +97,7 @@ function readControlRecordPayload(payload) {
97
97
  entryId
98
98
  };
99
99
  }
100
- case ControlRecordType.SetMetadata: {
100
+ case 2: {
101
101
  const entryId = view.getUint32(offset.get(), true);
102
102
  offset.advance32();
103
103
  const entryMetadataLength = view.getUint32(offset.get(), true);
@@ -475,14 +475,14 @@ var StructAstVisitor = class extends BaseStructVisitorWithDefaults {
475
475
  }
476
476
  enumSpecification(children, dataType) {
477
477
  switch (dataType) {
478
- case KnownStructTypeName.Int8:
479
- case KnownStructTypeName.Uint8:
480
- case KnownStructTypeName.Int16:
481
- case KnownStructTypeName.Uint16:
482
- case KnownStructTypeName.Int32:
483
- case KnownStructTypeName.Uint32:
484
- case KnownStructTypeName.Int64:
485
- case KnownStructTypeName.Uint64: break;
478
+ case "int8":
479
+ case "uint8":
480
+ case "int16":
481
+ case "uint16":
482
+ case "int32":
483
+ case "uint32":
484
+ case "int64":
485
+ case "uint64": break;
486
486
  default: throw new RangeError("Enums must be integers");
487
487
  }
488
488
  const entries = children.enumMember?.map((member) => this.enumMember(member.children));
@@ -497,23 +497,23 @@ var StructAstVisitor = class extends BaseStructVisitorWithDefaults {
497
497
  const [value] = children.Integer;
498
498
  const parsed = Number(value.image);
499
499
  switch (dataType) {
500
- case KnownStructTypeName.Boolean:
500
+ case "bool":
501
501
  if (parsed !== 1) throw new RangeError("Boolean bit-field members must be 1 bit");
502
502
  break;
503
- case KnownStructTypeName.Int8:
504
- case KnownStructTypeName.Uint8:
503
+ case "int8":
504
+ case "uint8":
505
505
  if (parsed > 8) throw new RangeError("8-bit bit-field members must be less than 8 bits");
506
506
  break;
507
- case KnownStructTypeName.Int16:
508
- case KnownStructTypeName.Uint16:
507
+ case "int16":
508
+ case "uint16":
509
509
  if (parsed > 16) throw new RangeError("16-bit bit-field members must be less than 16 bits");
510
510
  break;
511
- case KnownStructTypeName.Int32:
512
- case KnownStructTypeName.Uint32:
511
+ case "int32":
512
+ case "uint32":
513
513
  if (parsed > 32) throw new RangeError("32-bit bit-field members must be less than 32 bits");
514
514
  break;
515
- case KnownStructTypeName.Int64:
516
- case KnownStructTypeName.Uint64:
515
+ case "int64":
516
+ case "uint64":
517
517
  if (parsed > 64) throw new RangeError("64-bit bit-field members must be less than 64 bits");
518
518
  break;
519
519
  default: throw new RangeError("Bit-field members must be integers or booleans");
@@ -586,7 +586,7 @@ var StructRegistry = class StructRegistry {
586
586
  for (const member of specification) {
587
587
  if (member.bitWidth) throw new Error("Bit-field members are not implemented");
588
588
  switch (member.value) {
589
- case KnownStructTypeName.Boolean:
589
+ case "bool":
590
590
  if (member.arraySize !== void 0) {
591
591
  const array = [];
592
592
  for (let i = 0; i < member.arraySize; i++) {
@@ -599,7 +599,7 @@ var StructRegistry = class StructRegistry {
599
599
  offset.advance8();
600
600
  }
601
601
  break;
602
- case KnownStructTypeName.Int8:
602
+ case "int8":
603
603
  if (member.arraySize !== void 0) {
604
604
  const array = [];
605
605
  for (let i = 0; i < member.arraySize; i++) {
@@ -612,7 +612,7 @@ var StructRegistry = class StructRegistry {
612
612
  offset.advance8();
613
613
  }
614
614
  break;
615
- case KnownStructTypeName.Int16:
615
+ case "int16":
616
616
  if (member.arraySize !== void 0) {
617
617
  const array = [];
618
618
  for (let i = 0; i < member.arraySize; i++) {
@@ -625,7 +625,7 @@ var StructRegistry = class StructRegistry {
625
625
  offset.advance16();
626
626
  }
627
627
  break;
628
- case KnownStructTypeName.Int32:
628
+ case "int32":
629
629
  if (member.arraySize !== void 0) {
630
630
  const array = [];
631
631
  for (let i = 0; i < member.arraySize; i++) {
@@ -638,7 +638,7 @@ var StructRegistry = class StructRegistry {
638
638
  offset.advance32();
639
639
  }
640
640
  break;
641
- case KnownStructTypeName.Int64:
641
+ case "int64":
642
642
  if (member.arraySize !== void 0) {
643
643
  const array = [];
644
644
  for (let i = 0; i < member.arraySize; i++) {
@@ -651,7 +651,7 @@ var StructRegistry = class StructRegistry {
651
651
  offset.advance64();
652
652
  }
653
653
  break;
654
- case KnownStructTypeName.Uint8:
654
+ case "uint8":
655
655
  if (member.arraySize !== void 0) {
656
656
  const array = [];
657
657
  for (let i = 0; i < member.arraySize; i++) {
@@ -664,7 +664,7 @@ var StructRegistry = class StructRegistry {
664
664
  offset.advance8();
665
665
  }
666
666
  break;
667
- case KnownStructTypeName.Uint16:
667
+ case "uint16":
668
668
  if (member.arraySize !== void 0) {
669
669
  const array = [];
670
670
  for (let i = 0; i < member.arraySize; i++) {
@@ -677,7 +677,7 @@ var StructRegistry = class StructRegistry {
677
677
  offset.advance16();
678
678
  }
679
679
  break;
680
- case KnownStructTypeName.Uint32:
680
+ case "uint32":
681
681
  if (member.arraySize !== void 0) {
682
682
  const array = [];
683
683
  for (let i = 0; i < member.arraySize; i++) {
@@ -690,7 +690,7 @@ var StructRegistry = class StructRegistry {
690
690
  offset.advance32();
691
691
  }
692
692
  break;
693
- case KnownStructTypeName.Uint64:
693
+ case "uint64":
694
694
  if (member.arraySize !== void 0) {
695
695
  const array = [];
696
696
  for (let i = 0; i < member.arraySize; i++) {
@@ -703,8 +703,8 @@ var StructRegistry = class StructRegistry {
703
703
  offset.advance64();
704
704
  }
705
705
  break;
706
- case KnownStructTypeName.Float32:
707
- case KnownStructTypeName.Float:
706
+ case "float32":
707
+ case "float":
708
708
  if (member.arraySize !== void 0) {
709
709
  const array = [];
710
710
  for (let i = 0; i < member.arraySize; i++) {
@@ -717,8 +717,8 @@ var StructRegistry = class StructRegistry {
717
717
  offset.advance32();
718
718
  }
719
719
  break;
720
- case KnownStructTypeName.Float64:
721
- case KnownStructTypeName.Double:
720
+ case "float64":
721
+ case "double":
722
722
  if (member.arraySize !== void 0) {
723
723
  const array = [];
724
724
  for (let i = 0; i < member.arraySize; i++) {
@@ -731,7 +731,7 @@ var StructRegistry = class StructRegistry {
731
731
  offset.advance64();
732
732
  }
733
733
  break;
734
- case KnownStructTypeName.Character:
734
+ case "char":
735
735
  if (member.arraySize !== void 0) result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance(member.arraySize).get())));
736
736
  else result.set(member.name, StructRegistry.TEXT_DECODER.decode(payload.subarray(offset.get(), offset.advance8().get())));
737
737
  break;
@@ -769,30 +769,30 @@ var StructRegistry = class StructRegistry {
769
769
  if (member.bitWidth) throw new Error("Bit-field members are not implemented");
770
770
  let byteLengthForOne = 0;
771
771
  switch (member.value) {
772
- case KnownStructTypeName.Boolean:
773
- case KnownStructTypeName.Character:
774
- case KnownStructTypeName.Int8:
775
- case KnownStructTypeName.Uint8:
772
+ case "bool":
773
+ case "char":
774
+ case "int8":
775
+ case "uint8":
776
776
  byteLengthForOne = 1;
777
777
  break;
778
- case KnownStructTypeName.Int16:
779
- case KnownStructTypeName.Uint16:
778
+ case "int16":
779
+ case "uint16":
780
780
  byteLengthForOne = 2;
781
781
  break;
782
- case KnownStructTypeName.Int32:
783
- case KnownStructTypeName.Uint32:
782
+ case "int32":
783
+ case "uint32":
784
784
  byteLengthForOne = 4;
785
785
  break;
786
- case KnownStructTypeName.Int64:
787
- case KnownStructTypeName.Uint64:
786
+ case "int64":
787
+ case "uint64":
788
788
  byteLengthForOne = 8;
789
789
  break;
790
- case KnownStructTypeName.Float32:
791
- case KnownStructTypeName.Float:
790
+ case "float32":
791
+ case "float":
792
792
  byteLengthForOne = 4;
793
793
  break;
794
- case KnownStructTypeName.Float64:
795
- case KnownStructTypeName.Double:
794
+ case "float64":
795
+ case "double":
796
796
  byteLengthForOne = 8;
797
797
  break;
798
798
  default: {
@@ -827,8 +827,11 @@ function normalizeEntryName(rawName) {
827
827
  *
828
828
  * Accepts the output of {@link readRecords} and yields fully decoded records,
829
829
  * including struct decoding with dependency resolution.
830
+ *
831
+ * By default, data records that reference an entry ID with no preceding Start
832
+ * control record are silently skipped. Pass `{ strict: true }` to throw instead.
830
833
  */
831
- function* decodeRecords(records) {
834
+ function* decodeRecords(records, options = { strict: false }) {
832
835
  const asyncDecodedStructs = [];
833
836
  const structDecodeQueue = new StructDecodeQueue((structName, queuedRecords) => {
834
837
  for (const raw of queuedRecords) {
@@ -845,7 +848,7 @@ function* decodeRecords(records) {
845
848
  timestamp: raw.timestamp,
846
849
  name,
847
850
  metadata,
848
- type: RecordType.StructArray,
851
+ type: "struct[]",
849
852
  structName: normalizedName,
850
853
  payload: decoded
851
854
  });
@@ -857,7 +860,7 @@ function* decodeRecords(records) {
857
860
  timestamp: raw.timestamp,
858
861
  name,
859
862
  metadata,
860
- type: RecordType.Struct,
863
+ type: "struct",
861
864
  structName: normalizedName,
862
865
  payload: decoded
863
866
  });
@@ -871,24 +874,27 @@ function* decodeRecords(records) {
871
874
  if (readRecord.kind === "control") {
872
875
  const controlPayload = readRecord.payload;
873
876
  switch (controlPayload.controlRecordType) {
874
- case ControlRecordType.Start:
877
+ case 0:
875
878
  context.set(controlPayload.entryId, controlPayload);
876
879
  break;
877
- case ControlRecordType.Finish:
880
+ case 1:
878
881
  context.delete(controlPayload.entryId);
879
882
  break;
880
883
  }
881
884
  yield {
882
885
  entryId: readRecord.entryId,
883
886
  timestamp: readRecord.timestamp,
884
- type: RecordType.Control,
887
+ type: "control",
885
888
  payload: controlPayload
886
889
  };
887
890
  continue;
888
891
  }
889
892
  const raw = readRecord.record;
890
893
  const recordContext = context.get(raw.entryId);
891
- if (recordContext === void 0) throw new RangeError(`No type registered for entry ID ${raw.entryId}`);
894
+ if (recordContext === void 0) {
895
+ if (options.strict) throw new RangeError(`No type registered for entry ID ${raw.entryId}`);
896
+ continue;
897
+ }
892
898
  const name = normalizeEntryName(recordContext.entryName);
893
899
  const metadata = recordContext.entryMetadata;
894
900
  const view = new DataView(raw.payload.buffer, raw.payload.byteOffset, raw.payload.byteLength);
@@ -908,27 +914,27 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
908
914
  switch (entryType) {
909
915
  case "boolean": return {
910
916
  ...base,
911
- type: RecordType.Boolean,
917
+ type: "boolean",
912
918
  payload: byteToBoolean(view.getUint8(0))
913
919
  };
914
920
  case "int64": return {
915
921
  ...base,
916
- type: RecordType.Int64,
922
+ type: "int64",
917
923
  payload: view.getBigInt64(0, true)
918
924
  };
919
925
  case "float": return {
920
926
  ...base,
921
- type: RecordType.Float,
927
+ type: "float",
922
928
  payload: view.getFloat32(0, true)
923
929
  };
924
930
  case "double": return {
925
931
  ...base,
926
- type: RecordType.Double,
932
+ type: "double",
927
933
  payload: view.getFloat64(0, true)
928
934
  };
929
935
  case "string": return {
930
936
  ...base,
931
- type: RecordType.String,
937
+ type: "string",
932
938
  payload: TEXT_DECODER.decode(raw.payload)
933
939
  };
934
940
  case "boolean[]": {
@@ -936,7 +942,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
936
942
  for (let i = 0; i < raw.payload.byteLength; i++) payload.push(byteToBoolean(view.getUint8(i)));
937
943
  return {
938
944
  ...base,
939
- type: RecordType.BooleanArray,
945
+ type: "boolean[]",
940
946
  payload
941
947
  };
942
948
  }
@@ -945,7 +951,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
945
951
  for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getBigUint64(i, true));
946
952
  return {
947
953
  ...base,
948
- type: RecordType.Int64Array,
954
+ type: "int64[]",
949
955
  payload
950
956
  };
951
957
  }
@@ -954,7 +960,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
954
960
  for (let i = 0; i < raw.payload.byteLength; i += 4) payload.push(view.getFloat32(i, true));
955
961
  return {
956
962
  ...base,
957
- type: RecordType.FloatArray,
963
+ type: "float[]",
958
964
  payload
959
965
  };
960
966
  }
@@ -963,7 +969,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
963
969
  for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getFloat64(i, true));
964
970
  return {
965
971
  ...base,
966
- type: RecordType.DoubleArray,
972
+ type: "double[]",
967
973
  payload
968
974
  };
969
975
  }
@@ -981,7 +987,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
981
987
  }
982
988
  return {
983
989
  ...base,
984
- type: RecordType.StringArray,
990
+ type: "string[]",
985
991
  payload
986
992
  };
987
993
  }
@@ -991,7 +997,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
991
997
  structRegistry.register(structName, payload);
992
998
  return {
993
999
  ...base,
994
- type: RecordType.String,
1000
+ type: "string",
995
1001
  payload
996
1002
  };
997
1003
  }
@@ -1006,7 +1012,7 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
1006
1012
  }
1007
1013
  return {
1008
1014
  ...base,
1009
- type: RecordType.StructArray,
1015
+ type: "struct[]",
1010
1016
  structName: normalizedStructName,
1011
1017
  payload: decoded
1012
1018
  };
@@ -1019,14 +1025,14 @@ function decodePayload(raw, entryType, name, metadata, view, structRegistry, str
1019
1025
  }
1020
1026
  return {
1021
1027
  ...base,
1022
- type: RecordType.Struct,
1028
+ type: "struct",
1023
1029
  structName: normalizedStructName,
1024
1030
  payload: decoded
1025
1031
  };
1026
1032
  }
1027
1033
  return {
1028
1034
  ...base,
1029
- type: RecordType.Raw,
1035
+ type: "raw",
1030
1036
  payload: raw.payload
1031
1037
  };
1032
1038
  }
@@ -1054,17 +1060,17 @@ function* catalogEntries(records) {
1054
1060
  const entries = /* @__PURE__ */ new Map();
1055
1061
  for (const record of records) {
1056
1062
  if (record.kind !== "control") continue;
1057
- if (record.payload.controlRecordType === ControlRecordType.Start) {
1063
+ if (record.payload.controlRecordType === 0) {
1058
1064
  const entry = {
1059
1065
  entryId: record.payload.entryId,
1060
- name: record.payload.entryName,
1066
+ name: normalizeEntryName(record.payload.entryName),
1061
1067
  type: record.payload.entryType,
1062
1068
  metadata: record.payload.entryMetadata
1063
1069
  };
1064
1070
  entries.set(record.payload.entryId, entry);
1065
1071
  yield entry;
1066
1072
  }
1067
- if (record.payload.controlRecordType === ControlRecordType.SetMetadata) {
1073
+ if (record.payload.controlRecordType === 2) {
1068
1074
  const existing = entries.get(record.payload.entryId);
1069
1075
  if (existing) existing.metadata = record.payload.entryMetadata;
1070
1076
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wpilog-parser",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Read WPILib wpilog data log files in TypeScript & JavaScript",
5
5
  "homepage": "https://github.com/jonahsnider/wpilog-parser#readme",
6
6
  "license": "MIT",
@@ -14,7 +14,8 @@
14
14
  "url": "git+https://github.com/jonahsnider/wpilog-parser.git"
15
15
  },
16
16
  "files": [
17
- "dist"
17
+ "dist",
18
+ "skills"
18
19
  ],
19
20
  "type": "module",
20
21
  "exports": {
@@ -40,9 +41,9 @@
40
41
  "devDependencies": {
41
42
  "@codspeed/vitest-plugin": "^5.2.0",
42
43
  "@types/node": "24.12.2",
43
- "@typescript/native-preview": "7.0.0-dev.20260416.2",
44
+ "@typescript/native-preview": "7.0.0-dev.20260425.1",
44
45
  "typescript": "6.0.3",
45
- "vite-plus": "0.1.18"
46
+ "vite-plus": "0.1.19"
46
47
  },
47
48
  "overrides": {
48
49
  "vite": "npm:@voidzero-dev/vite-plus-core@latest",
@@ -0,0 +1,128 @@
1
+ ---
2
+ name: wpilog-parser
3
+ description: Analyze WPILOG log files from FRC robots using the wpilog-parser library with TypeScript.
4
+ ---
5
+
6
+ # wpilog-parser
7
+
8
+ WPILOG is the binary log format used by logging libraries in the FRC ecosystem. The wpilog-parser library makes it easy to read and analyze the log files using TS/JS.
9
+
10
+ ## Creating a project
11
+
12
+ 1. Init a project using `bun init` or similar
13
+ 2. Add `wpilog-parser` as a dependency
14
+ 3. Create a `.ts` file for catalogging the records in the WPILOG file
15
+
16
+ ```ts
17
+ import { readFile } from 'node:fs/promises';
18
+ import { readRecords, catalogEntries } from 'wpilog-parser';
19
+
20
+ const bytes = await readFile('./example.wpilog');
21
+
22
+ for (const catalogEntry of catalogEntries(readRecords(bytes))) {
23
+ console.log(catalogEntry);
24
+ // {
25
+ // entryId: 3,
26
+ // name: "/Robot/DogLog/Options",
27
+ // type: "string",
28
+ // metadata: "{\"source\":\"DogLog\"}",
29
+ // }
30
+ }
31
+ ```
32
+
33
+ ## Analyzing logs
34
+
35
+ Use `decodeRecords` to parse record contents. Filter by `name` and narrow on `type`:
36
+
37
+ ```ts
38
+ import { readFile } from 'node:fs/promises';
39
+ import { readRecords, decodeRecords, isDataRecord, RecordType } from 'wpilog-parser';
40
+
41
+ const bytes = await readFile('./example.wpilog');
42
+
43
+ for (const record of decodeRecords(readRecords(bytes))) {
44
+ if (!isDataRecord(record)) continue;
45
+
46
+ if (record.name === '/Robot/Intake/Voltage' && record.type === RecordType.Double) {
47
+ console.log(record.timestamp, record.payload);
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### Decoding structs
53
+
54
+ ```ts
55
+ import { structPayloadToJson, RecordType } from 'wpilog-parser';
56
+
57
+ for (const record of decodeRecords(readRecords(bytes))) {
58
+ if (record.type === RecordType.Struct && record.name === '/Robot/Localization/EstimatedPose') {
59
+ console.log(record.timestamp, structPayloadToJson(record.payload));
60
+ // 18688018n {
61
+ // translation: { x: 10.289, y: 0.47 },
62
+ // rotation: { value: 1.5707961072652017 },
63
+ // }
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### Strict mode
69
+
70
+ ```ts
71
+ for (const record of decodeRecords(readRecords(bytes), { strict: true })) {
72
+ // throws on orphan data records instead of skipping
73
+ }
74
+ ```
75
+
76
+ ## Robot state cheatsheet
77
+
78
+ When DriverStation logging is on, every WPILOG has these `boolean` entries:
79
+
80
+ | Entry | Meaning |
81
+ | ---------------- | ------------------------------------------------------------------- |
82
+ | `/DS:enabled` | `true` while the robot is enabled (any mode). |
83
+ | `/DS:autonomous` | `true` during autonomous. `false` means teleop (unless `/DS:test`). |
84
+ | `/DS:test` | `true` during test mode. |
85
+ | `/DS:estop` | `true` if e-stopped. |
86
+
87
+ The mode flags (`autonomous`, `test`) reflect what the DS _would_ run if enabled, so check `/DS:enabled` together with them. Timestamps are microseconds (`bigint`).
88
+
89
+ ### Tracking enable/mode durations
90
+
91
+ ```ts
92
+ import { readRecords, decodeRecords, isDataRecord, RecordType } from 'wpilog-parser';
93
+
94
+ type Mode = 'auto' | 'teleop' | 'test';
95
+ function modeOf(auto: boolean, test: boolean): Mode {
96
+ if (test) return 'test';
97
+ if (auto) return 'auto';
98
+ return 'teleop';
99
+ }
100
+
101
+ let enabled = false;
102
+ let auto = false;
103
+ let test = false;
104
+ let enabledSince: bigint | null = null;
105
+ const totals = { auto: 0n, teleop: 0n, test: 0n };
106
+
107
+ for (const r of decodeRecords(readRecords(bytes))) {
108
+ if (!isDataRecord(r) || r.type !== RecordType.Boolean) continue;
109
+
110
+ const wasEnabled = enabled;
111
+ const prevMode = modeOf(auto, test);
112
+
113
+ if (r.name === '/DS:enabled') enabled = r.payload;
114
+ else if (r.name === '/DS:autonomous') auto = r.payload;
115
+ else if (r.name === '/DS:test') test = r.payload;
116
+ else continue;
117
+
118
+ if (wasEnabled && (!enabled || modeOf(auto, test) !== prevMode)) {
119
+ totals[prevMode] += r.timestamp - (enabledSince ?? r.timestamp);
120
+ enabledSince = enabled ? r.timestamp : null;
121
+ } else if (!wasEnabled && enabled) {
122
+ enabledSince = r.timestamp;
123
+ }
124
+ }
125
+
126
+ // Durations in microseconds; divide by 1_000_000n for seconds.
127
+ console.log(totals);
128
+ ```