tauri-plugin-tracing 0.3.3

Use the tracing crate in your Tauri app
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
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
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
// Doc examples show complete Tauri programs with fn main() for clarity
#![allow(clippy::needless_doctest_main)]

//! # Tauri Plugin Tracing
//!
//! A Tauri plugin that integrates the [`tracing`] crate for structured logging
//! in Tauri applications. This plugin bridges logging between the Rust backend
//! and JavaScript frontend, providing call stack information.
//!
//! ## Features
//!
//! - **`colored`**: Enables colored terminal output using ANSI escape codes
//! - **`specta`**: Enables TypeScript type generation via the `specta` crate
//! - **`flamegraph`**: Enables flamegraph/flamechart profiling support (wall-clock span timing)
//! - **`profiling`**: Enables CPU profiling via [`tauri-plugin-profiling`](https://crates.io/crates/tauri-plugin-profiling)
//!
//! ## Usage
//!
//! By default, this plugin does **not** set up a global tracing subscriber,
//! following the convention that libraries should not set globals. You compose
//! your own subscriber using [`WebviewLayer`] to forward logs to the frontend:
//!
//! ```rust,no_run
//! # use tauri_plugin_tracing::{Builder, WebviewLayer, LevelFilter};
//! # use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt, fmt};
//! let tracing_builder = Builder::new()
//!     .with_max_level(LevelFilter::DEBUG)
//!     .with_target("hyper", LevelFilter::WARN);
//! let filter = tracing_builder.build_filter();
//!
//! tauri::Builder::default()
//!     .plugin(tracing_builder.build())
//!     .setup(move |app| {
//!         Registry::default()
//!             .with(fmt::layer())
//!             .with(WebviewLayer::new(app.handle().clone()))
//!             .with(filter)
//!             .init();
//!         Ok(())
//!     });
//!     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//! ```
//!
//! ## Quick Start
//!
//! For simple applications, use [`Builder::with_default_subscriber()`] to let
//! the plugin handle all tracing setup:
//!
//! ```rust,no_run
//! # use tauri_plugin_tracing::{Builder, LevelFilter};
//! tauri::Builder::default()
//!     .plugin(
//!         Builder::new()
//!             .with_max_level(LevelFilter::DEBUG)
//!             .with_default_subscriber()  // Let plugin set up tracing
//!             .build(),
//!     );
//!     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//! ```
//!
//! ## File Logging
//!
//! For simple file logging, use [`Builder::with_file_logging()`]:
//!
//! ```rust,no_run
//! # use tauri_plugin_tracing::{Builder, LevelFilter};
//! Builder::new()
//!     .with_max_level(LevelFilter::DEBUG)
//!     .with_file_logging()
//!     .with_default_subscriber()
//!     .build::<tauri::Wry>();
//! ```
//!
//! For custom subscribers, use [`tracing_appender`] directly (re-exported by this crate):
//!
//! ```rust,no_run
//! # use tauri::Manager;
//! # use tauri_plugin_tracing::{Builder, WebviewLayer, LevelFilter, tracing_appender};
//! # use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt, fmt};
//! let tracing_builder = Builder::new().with_max_level(LevelFilter::DEBUG);
//! let filter = tracing_builder.build_filter();
//!
//! tauri::Builder::default()
//!     .plugin(tracing_builder.build())
//!     .setup(move |app| {
//!         let log_dir = app.path().app_log_dir()?;
//!         let file_appender = tracing_appender::rolling::daily(&log_dir, "app");
//!         let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
//!         // Store _guard in Tauri state to keep file logging active
//!
//!         Registry::default()
//!             .with(fmt::layer())
//!             .with(fmt::layer().with_ansi(false).with_writer(non_blocking))
//!             .with(WebviewLayer::new(app.handle().clone()))
//!             .with(filter)
//!             .init();
//!         Ok(())
//!     });
//!     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//! ```
//!
//! Log files rotate daily and are written to:
//! - **macOS**: `~/Library/Logs/{bundle_identifier}/app.YYYY-MM-DD.log`
//! - **Linux**: `~/.local/share/{bundle_identifier}/logs/app.YYYY-MM-DD.log`
//! - **Windows**: `%LOCALAPPDATA%/{bundle_identifier}/logs/app.YYYY-MM-DD.log`
//!
//! ## Early Initialization
//!
//! For maximum control, initialize tracing before creating the Tauri app. This
//! pattern uses [`tracing_subscriber::registry()`] with [`init()`](tracing_subscriber::util::SubscriberInitExt::init)
//! and passes a minimal [`Builder`] to the plugin:
//!
//! ```rust,no_run
//! use tauri_plugin_tracing::{Builder, StripAnsiWriter, tracing_appender};
//! use tracing::Level;
//! use tracing_subscriber::filter::Targets;
//! use tracing_subscriber::layer::SubscriberExt;
//! use tracing_subscriber::util::SubscriberInitExt;
//! use tracing_subscriber::{fmt, registry};
//!
//! fn setup_logger() -> Builder {
//!     let log_dir = std::env::temp_dir().join("my-app");
//!     let _ = std::fs::create_dir_all(&log_dir);
//!
//!     let file_appender = tracing_appender::rolling::daily(&log_dir, "app");
//!     let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
//!     std::mem::forget(guard); // Keep file logging active for app lifetime
//!
//!     let targets = Targets::new()
//!         .with_default(Level::DEBUG)
//!         .with_target("hyper", Level::WARN)
//!         .with_target("reqwest", Level::WARN);
//!
//!     registry()
//!         .with(fmt::layer().with_ansi(true))
//!         .with(fmt::layer().with_writer(StripAnsiWriter::new(non_blocking)).with_ansi(false))
//!         .with(targets)
//!         .init();
//!
//!     // Return minimal builder - logging is already configured
//!     Builder::new()
//! }
//!
//! fn main() {
//!     let builder = setup_logger();
//!     tauri::Builder::default()
//!         .plugin(builder.build());
//!         // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//! }
//! ```
//!
//! This approach is useful when you need logging available before Tauri starts,
//! or when you want full control over the subscriber configuration.
//!
//! ## Flamegraph Profiling
//!
//! The `flamegraph` feature enables performance profiling with flamegraph/flamechart
//! visualizations.
//!
//! ### With Default Subscriber
//!
//! ```rust,no_run
//! # use tauri_plugin_tracing::{Builder, LevelFilter};
//! Builder::new()
//!     .with_max_level(LevelFilter::DEBUG)
//!     .with_flamegraph()
//!     .with_default_subscriber()
//!     .build::<tauri::Wry>();
//! ```
//!
//! ### With Custom Subscriber
//!
//! Use [`create_flame_layer()`] to add flamegraph profiling to a custom subscriber:
//!
//! ```no_run
//! use tauri_plugin_tracing::{Builder, WebviewLayer, LevelFilter, create_flame_layer};
//! use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt, fmt};
//!
//! fn main() {
//!     let tracing_builder = Builder::new().with_max_level(LevelFilter::DEBUG);
//!     let filter = tracing_builder.build_filter();
//!
//!     tauri::Builder::default()
//!         .plugin(tracing_builder.build())
//!         .setup(move |app| {
//!             let flame_layer = create_flame_layer(app.handle())?;
//!
//!             Registry::default()
//!                 .with(flame_layer) // Must be first - typed for Registry
//!                 .with(fmt::layer())
//!                 .with(WebviewLayer::new(app.handle().clone()))
//!                 .with(filter)
//!                 .init();
//!             Ok(())
//!         })
//!         .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//!         .expect("error while running tauri application");
//! }
//! ```
//!
//! ### Early Initialization with Flamegraph
//!
//! Use [`create_flame_layer_with_path()`] and [`FlameExt`] to initialize tracing
//! before Tauri starts while still enabling frontend flamegraph generation:
//!
//! ```no_run
//! use tauri_plugin_tracing::{Builder, create_flame_layer_with_path, FlameExt};
//! use tracing_subscriber::{registry, layer::SubscriberExt, util::SubscriberInitExt, fmt};
//!
//! fn main() {
//!     let log_dir = std::env::temp_dir().join("my-app");
//!     std::fs::create_dir_all(&log_dir).unwrap();
//!
//!     // Create flame layer before Tauri starts
//!     let (flame_layer, flame_guard) = create_flame_layer_with_path(
//!         &log_dir.join("profile.folded")
//!     ).unwrap();
//!
//!     // Initialize tracing early
//!     registry()
//!         .with(flame_layer) // Must be first - typed for Registry
//!         .with(fmt::layer())
//!         .init();
//!
//!     // Now start Tauri and register the guard
//!     tauri::Builder::default()
//!         .plugin(Builder::new().build())
//!         .setup(move |app| {
//!             // Register the guard so JS can generate flamegraphs
//!             app.handle().register_flamegraph(flame_guard)?;
//!             Ok(())
//!         })
//!         .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//!         .expect("error while running tauri application");
//! }
//! ```
//!
//! Then generate visualizations from JavaScript:
//!
//! ```javascript
//! import { generateFlamegraph, generateFlamechart } from '@fltsci/tauri-plugin-tracing';
//!
//! // Generate a flamegraph (collapses identical stack frames)
//! const flamegraphPath = await generateFlamegraph();
//!
//! // Generate a flamechart (preserves event ordering)
//! const flamechartPath = await generateFlamechart();
//! ```
//!
//! ## CPU Profiling
//!
//! The `profiling` feature enables sampling-based CPU profiling via
//! [`tauri-plugin-profiling`](https://crates.io/crates/tauri-plugin-profiling).
//! Unlike flamegraph profiling (which measures wall-clock time including I/O waits),
//! CPU profiling measures actual CPU cycles spent executing code.
//!
//! Use [`TracedProfilingExt`] for automatic span creation and logging:
//!
//! ```rust,no_run
//! use tauri::Manager;
//! use tauri_plugin_tracing::{Builder, LevelFilter, TracedProfilingExt, init_profiling};
//!
//! tauri::Builder::default()
//!     .plugin(Builder::new().with_max_level(LevelFilter::DEBUG).build())
//!     .plugin(init_profiling())
//!     .setup(|app| {
//!         // Start CPU profiling with automatic span + logging
//!         app.start_cpu_profile_traced()?;
//!
//!         // ... do CPU-intensive work ...
//!
//!         // Stop - automatically logs results (samples, duration, path)
//!         let result = app.stop_cpu_profile_traced()?;
//!         Ok(())
//!     })
//!     .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
//!     .expect("error while running tauri application");
//! ```
//!
//! Or use the base [`ProfilingExt`](ProfilingExtBase) trait directly without tracing integration.
//!
//! From JavaScript (import from the profiling package directly):
//!
//! ```javascript
//! import { startCpuProfile, stopCpuProfile } from '@fltsci/tauri-plugin-profiling';
//!
//! await startCpuProfile({ frequency: 100 });
//! // ... do work ...
//! const result = await stopCpuProfile();
//! console.log('CPU flamegraph:', result.flamegraphPath);
//! ```
//!
//! ## JavaScript API
//!
//! ```javascript
//! import { trace, debug, info, warn, error } from '@fltsci/tauri-plugin-tracing';
//!
//! info('Application started');
//! debug('Debug information', { key: 'value' });
//! error('Something went wrong');
//! ```

mod callstack;
mod commands;
mod error;
#[cfg(feature = "flamegraph")]
mod flamegraph;
mod layer;
#[cfg(feature = "profiling")]
mod profiling;
mod strip_ansi;
mod types;

use std::path::PathBuf;
use tauri::plugin::{self, TauriPlugin};
use tauri::{AppHandle, Manager, Runtime};
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::{
    Layer as _, Registry,
    filter::{Targets, filter_fn},
    fmt::{self, SubscriberBuilder},
    layer::SubscriberExt,
};

// Re-export public types from modules
pub use callstack::{CallStack, CallStackLine};
pub use commands::log;
pub use error::{Error, Result};
pub use layer::{LogLevel, LogMessage, RecordPayload, WebviewLayer};
pub use strip_ansi::{StripAnsiWriter, StripAnsiWriterGuard};
pub use types::{
    FormatOptions, LogFormat, MaxFileSize, Rotation, RotationStrategy, Target, TimezoneStrategy,
};

/// A boxed filter function for metadata-based log filtering.
///
/// This type alias represents a filter that examines event metadata to determine
/// whether a log should be emitted. The function receives a reference to the
/// metadata and returns `true` if the log should be included.
pub type FilterFn = Box<dyn Fn(&tracing::Metadata<'_>) -> bool + Send + Sync>;

/// A boxed tracing layer that can be added to the default subscriber.
///
/// Use this type with [`Builder::with_layer()`] to add custom tracing layers
/// (e.g., for OpenTelemetry, Sentry, or custom logging integrations) to the
/// plugin-managed subscriber.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_plugin_tracing::{Builder, BoxedLayer};
/// use tracing_subscriber::Layer;
///
/// // Create a custom layer (e.g., from another crate) and box it
/// let my_layer: BoxedLayer = tracing_subscriber::fmt::layer().boxed();
///
/// Builder::new()
///     .with_layer(my_layer)
///     .with_default_subscriber()
///     .build::<tauri::Wry>();
/// ```
pub type BoxedLayer = Box<dyn tracing_subscriber::Layer<Registry> + Send + Sync + 'static>;

#[cfg(feature = "flamegraph")]
pub use flamegraph::*;

#[cfg(feature = "profiling")]
pub use profiling::*;

/// Re-export of the [`tracing`] crate for convenience.
pub use tracing;
/// Re-export of the [`tracing_appender`] crate for file logging configuration.
pub use tracing_appender;
/// Re-export of the [`tracing_subscriber`] crate for subscriber configuration.
pub use tracing_subscriber;

/// Re-export of [`tracing_subscriber::filter::LevelFilter`] for configuring log levels.
pub use tracing_subscriber::filter::LevelFilter;

#[cfg(target_os = "ios")]
mod ios {
    swift_rs::swift!(pub fn tauri_log(
      level: u8, message: *const std::ffi::c_void
    ));
}

/// Stores the WorkerGuard to ensure logs are flushed on shutdown.
/// This must be kept alive for the lifetime of the application.
struct LogGuard(#[allow(dead_code)] Option<WorkerGuard>);

/// Builder for configuring and creating the tracing plugin.
///
/// Use this builder to customize logging behavior before registering the plugin
/// with your Tauri application.
///
/// # Example
///
/// ```rust,no_run
/// use tauri_plugin_tracing::{Builder, LevelFilter};
///
/// let plugin = Builder::new()
///     .with_max_level(LevelFilter::DEBUG)
///     .with_target("hyper", LevelFilter::WARN)  // Reduce noise from hyper
///     .with_target("my_app", LevelFilter::TRACE)  // Verbose logging for your app
///     .build::<tauri::Wry>();
/// ```
pub struct Builder {
    builder: SubscriberBuilder,
    log_level: LevelFilter,
    filter: Targets,
    custom_filter: Option<FilterFn>,
    custom_layer: Option<BoxedLayer>,
    targets: Vec<Target>,
    rotation: Rotation,
    rotation_strategy: RotationStrategy,
    max_file_size: Option<MaxFileSize>,
    timezone_strategy: TimezoneStrategy,
    log_format: LogFormat,
    show_file: bool,
    show_line_number: bool,
    show_thread_ids: bool,
    show_thread_names: bool,
    show_target: bool,
    show_level: bool,
    set_default_subscriber: bool,
    #[cfg(feature = "colored")]
    use_colors: bool,
    #[cfg(feature = "flamegraph")]
    enable_flamegraph: bool,
}

impl Default for Builder {
    fn default() -> Self {
        Self {
            builder: SubscriberBuilder::default(),
            log_level: LevelFilter::WARN,
            filter: Targets::default(),
            custom_filter: None,
            custom_layer: None,
            targets: vec![Target::Stdout, Target::Webview],
            rotation: Rotation::default(),
            rotation_strategy: RotationStrategy::default(),
            max_file_size: None,
            timezone_strategy: TimezoneStrategy::default(),
            log_format: LogFormat::default(),
            show_file: false,
            show_line_number: false,
            show_thread_ids: false,
            show_thread_names: false,
            show_target: true,
            show_level: true,
            set_default_subscriber: false,
            #[cfg(feature = "colored")]
            use_colors: false,
            #[cfg(feature = "flamegraph")]
            enable_flamegraph: false,
        }
    }
}

impl Builder {
    /// Creates a new builder with default settings.
    ///
    /// The default log level is [`LevelFilter::WARN`].
    pub fn new() -> Self {
        Default::default()
    }

    /// Sets the maximum log level.
    ///
    /// Events more verbose than this level will be filtered out.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::{Builder, LevelFilter};
    /// Builder::new().with_max_level(LevelFilter::DEBUG);
    /// ```
    pub fn with_max_level(mut self, max_level: LevelFilter) -> Self {
        self.log_level = max_level;
        self.builder = self.builder.with_max_level(max_level);
        self
    }

    /// Sets the log level for a specific target (module path).
    ///
    /// This allows fine-grained control over logging verbosity for different
    /// parts of your application or dependencies.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::{Builder, LevelFilter};
    /// Builder::new()
    ///     .with_max_level(LevelFilter::INFO)
    ///     .with_target("my_app::database", LevelFilter::DEBUG)
    ///     .with_target("hyper", LevelFilter::WARN);
    /// ```
    pub fn with_target(mut self, target: &str, level: LevelFilter) -> Self {
        self.filter = self.filter.with_target(target, level);
        self
    }

    /// Sets a custom filter function for metadata-based log filtering.
    ///
    /// The filter function receives the metadata for each log event and returns
    /// `true` if the event should be logged. This filter is applied in addition
    /// to the level and target filters configured via [`with_max_level()`](Self::with_max_level)
    /// and [`with_target()`](Self::with_target).
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    /// For custom subscribers, use [`tracing_subscriber::filter::filter_fn()`] directly.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::Builder;
    ///
    /// // Filter out logs from a specific module
    /// Builder::new()
    ///     .filter(|metadata| {
    ///         metadata.target() != "noisy_crate::spammy_module"
    ///     })
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    ///
    /// // Only log events (not spans)
    /// Builder::new()
    ///     .filter(|metadata| metadata.is_event())
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn filter<F>(mut self, filter: F) -> Self
    where
        F: Fn(&tracing::Metadata<'_>) -> bool + Send + Sync + 'static,
    {
        self.custom_filter = Some(Box::new(filter));
        self
    }

    /// Adds a custom tracing layer to the subscriber.
    ///
    /// Use this to integrate additional tracing functionality (e.g., OpenTelemetry,
    /// Sentry, custom metrics) with the plugin-managed subscriber.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// Note: Only one custom layer is supported. Calling this multiple times will
    /// replace the previous layer. To use multiple custom layers, compose them
    /// with [`tracing_subscriber::layer::Layered`] before passing to this method.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::Builder;
    /// use tracing_subscriber::Layer;
    ///
    /// // Add a custom layer (e.g., a secondary fmt layer or OpenTelemetry)
    /// let custom_layer = tracing_subscriber::fmt::layer().boxed();
    ///
    /// Builder::new()
    ///     .with_layer(custom_layer)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_layer(mut self, layer: BoxedLayer) -> Self {
        self.custom_layer = Some(layer);
        self
    }

    /// Enables flamegraph profiling.
    ///
    /// When enabled, tracing spans are recorded to a folded stack format file
    /// that can be converted to a flamegraph or flamechart visualization.
    ///
    /// The folded stack data is written to `{app_log_dir}/profile.folded`.
    /// Use the `generate_flamegraph` or `generate_flamechart` commands to
    /// convert this data to an SVG visualization.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```no_run
    /// use tauri_plugin_tracing::Builder;
    ///
    /// let _plugin = Builder::new()
    ///     .with_flamegraph()
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    #[cfg(feature = "flamegraph")]
    pub fn with_flamegraph(mut self) -> Self {
        self.enable_flamegraph = true;
        self
    }

    /// Enables colored output in the terminal.
    ///
    /// This adds ANSI color codes to log level indicators.
    /// Only available when the `colored` feature is enabled.
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    #[cfg(feature = "colored")]
    pub fn with_colors(mut self) -> Self {
        self.builder = self.builder.with_ansi(true);
        self.use_colors = true;
        self
    }

    /// Enables file logging to the platform-standard log directory.
    ///
    /// Log files rotate daily with the naming pattern `app.YYYY-MM-DD.log`.
    ///
    /// Platform log directories:
    /// - **macOS**: `~/Library/Logs/{bundle_identifier}`
    /// - **Linux**: `~/.local/share/{bundle_identifier}/logs`
    /// - **Windows**: `%LOCALAPPDATA%/{bundle_identifier}/logs`
    ///
    /// This is a convenience method equivalent to calling
    /// `.target(Target::LogDir { file_name: None })`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::{Builder, LevelFilter};
    /// Builder::new()
    ///     .with_max_level(LevelFilter::DEBUG)
    ///     .with_file_logging()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_file_logging(self) -> Self {
        self.target(Target::LogDir { file_name: None })
    }

    /// Sets the rotation period for log files.
    ///
    /// This controls how often new log files are created. Only applies when
    /// file logging is enabled.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, Rotation};
    ///
    /// Builder::new()
    ///     .with_file_logging()
    ///     .with_rotation(Rotation::Hourly)  // Rotate every hour
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_rotation(mut self, rotation: Rotation) -> Self {
        self.rotation = rotation;
        self
    }

    /// Sets the retention strategy for rotated log files.
    ///
    /// This controls how many old log files are kept. Cleanup happens when
    /// the application starts.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, RotationStrategy};
    ///
    /// Builder::new()
    ///     .with_file_logging()
    ///     .with_rotation_strategy(RotationStrategy::KeepSome(7))  // Keep 7 files
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_rotation_strategy(mut self, strategy: RotationStrategy) -> Self {
        self.rotation_strategy = strategy;
        self
    }

    /// Sets the maximum file size before rotating.
    ///
    /// When set, log files will rotate when they reach this size, in addition
    /// to any time-based rotation configured via [`with_rotation()`](Self::with_rotation).
    ///
    /// Use [`MaxFileSize`] for convenient size specification:
    /// - `MaxFileSize::kb(100)` - 100 kilobytes
    /// - `MaxFileSize::mb(10)` - 10 megabytes
    /// - `MaxFileSize::gb(1)` - 1 gigabyte
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, MaxFileSize};
    ///
    /// // Rotate when file reaches 10 MB
    /// Builder::new()
    ///     .with_file_logging()
    ///     .with_max_file_size(MaxFileSize::mb(10))
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_max_file_size(mut self, size: MaxFileSize) -> Self {
        self.max_file_size = Some(size);
        self
    }

    /// Sets the timezone strategy for log timestamps.
    ///
    /// Controls whether timestamps are displayed in UTC or local time.
    /// The default is [`TimezoneStrategy::Utc`].
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, TimezoneStrategy};
    ///
    /// // Use local time for timestamps
    /// Builder::new()
    ///     .with_timezone_strategy(TimezoneStrategy::Local)
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_timezone_strategy(mut self, strategy: TimezoneStrategy) -> Self {
        self.timezone_strategy = strategy;
        self
    }

    /// Sets the log output format style.
    ///
    /// Controls the overall structure of log output. The default is [`LogFormat::Full`].
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, LogFormat};
    ///
    /// // Use compact format for shorter lines
    /// Builder::new()
    ///     .with_format(LogFormat::Compact)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_format(mut self, format: LogFormat) -> Self {
        self.log_format = format;
        self
    }

    /// Sets whether to include the source file path in log output.
    ///
    /// When enabled, logs will show which file the log event originated from.
    /// Default is `false`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// Builder::new()
    ///     .with_file(true)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_file(mut self, show: bool) -> Self {
        self.show_file = show;
        self
    }

    /// Sets whether to include the source line number in log output.
    ///
    /// When enabled, logs will show which line number the log event originated from.
    /// Default is `false`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// Builder::new()
    ///     .with_line_number(true)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_line_number(mut self, show: bool) -> Self {
        self.show_line_number = show;
        self
    }

    /// Sets whether to include the current thread ID in log output.
    ///
    /// When enabled, logs will show the ID of the thread that emitted the event.
    /// Default is `false`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// Builder::new()
    ///     .with_thread_ids(true)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_thread_ids(mut self, show: bool) -> Self {
        self.show_thread_ids = show;
        self
    }

    /// Sets whether to include the current thread name in log output.
    ///
    /// When enabled, logs will show the name of the thread that emitted the event.
    /// Default is `false`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// Builder::new()
    ///     .with_thread_names(true)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_thread_names(mut self, show: bool) -> Self {
        self.show_thread_names = show;
        self
    }

    /// Sets whether to include the log target (module path) in log output.
    ///
    /// When enabled, logs will show which module/target emitted the event.
    /// Default is `true`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// // Disable target display for cleaner output
    /// Builder::new()
    ///     .with_target_display(false)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_target_display(mut self, show: bool) -> Self {
        self.show_target = show;
        self
    }

    /// Sets whether to include the log level in log output.
    ///
    /// When enabled, logs will show the severity level (TRACE, DEBUG, INFO, etc.).
    /// Default is `true`.
    ///
    /// Only applies when using [`with_default_subscriber()`](Self::with_default_subscriber).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// // Disable level display
    /// Builder::new()
    ///     .with_level(false)
    ///     .with_default_subscriber()
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn with_level(mut self, show: bool) -> Self {
        self.show_level = show;
        self
    }

    /// Adds a log output target.
    ///
    /// By default, logs are sent to [`Target::Stdout`] and [`Target::Webview`].
    /// Use this method to add additional targets.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, Target};
    ///
    /// // Add file logging to the default targets
    /// Builder::new()
    ///     .target(Target::LogDir { file_name: None })
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn target(mut self, target: Target) -> Self {
        self.targets.push(target);
        self
    }

    /// Sets the log output targets, replacing any previously configured targets.
    ///
    /// By default, logs are sent to [`Target::Stdout`] and [`Target::Webview`].
    /// Use this method to completely replace the default targets.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, Target};
    ///
    /// // Log only to file and webview (no stdout)
    /// Builder::new()
    ///     .targets([
    ///         Target::LogDir { file_name: None },
    ///         Target::Webview,
    ///     ])
    ///     .build::<tauri::Wry>();
    ///
    /// // Log only to stderr
    /// Builder::new()
    ///     .targets([Target::Stderr])
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn targets(mut self, targets: impl IntoIterator<Item = Target>) -> Self {
        self.targets = targets.into_iter().collect();
        self
    }

    /// Removes all configured log targets.
    ///
    /// Use this followed by [`target()`](Self::target) to build a custom set
    /// of targets from scratch.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, Target};
    ///
    /// // Start fresh and only log to webview
    /// Builder::new()
    ///     .clear_targets()
    ///     .target(Target::Webview)
    ///     .build::<tauri::Wry>();
    /// ```
    pub fn clear_targets(mut self) -> Self {
        self.targets.clear();
        self
    }

    /// Enables the plugin to set up and register the global tracing subscriber.
    ///
    /// By default, this plugin does **not** call [`tracing::subscriber::set_global_default()`],
    /// following the convention that libraries should not set globals. This allows your
    /// application to compose its own subscriber with layers from multiple crates.
    ///
    /// Call this method if you want the plugin to handle all tracing setup for you,
    /// using the configuration from this builder (log levels, targets, file logging, etc.).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::{Builder, LevelFilter};
    /// // Let the plugin set up everything
    /// tauri::Builder::default()
    ///     .plugin(
    ///         Builder::new()
    ///             .with_max_level(LevelFilter::DEBUG)
    ///             .with_file_logging()
    ///             .with_default_subscriber()  // Opt-in to global subscriber
    ///             .build()
    ///     );
    ///     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
    /// ```
    pub fn with_default_subscriber(mut self) -> Self {
        self.set_default_subscriber = true;
        self
    }

    /// Returns the configured log output targets.
    ///
    /// Use this when setting up your own subscriber to determine which
    /// layers to include based on the configured targets.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use tauri_plugin_tracing::{Builder, Target};
    ///
    /// let builder = Builder::new()
    ///     .target(Target::LogDir { file_name: None });
    ///
    /// for target in builder.configured_targets() {
    ///     match target {
    ///         Target::Stdout => { /* add stdout layer */ }
    ///         Target::Stderr => { /* add stderr layer */ }
    ///         Target::Webview => { /* add WebviewLayer */ }
    ///         Target::LogDir { .. } | Target::Folder { .. } => { /* add file layer */ }
    ///     }
    /// }
    /// ```
    pub fn configured_targets(&self) -> &[Target] {
        &self.targets
    }

    /// Returns the configured rotation period for file logging.
    pub fn configured_rotation(&self) -> Rotation {
        self.rotation
    }

    /// Returns the configured rotation strategy for file logging.
    pub fn configured_rotation_strategy(&self) -> RotationStrategy {
        self.rotation_strategy
    }

    /// Returns the configured maximum file size for rotation, if set.
    pub fn configured_max_file_size(&self) -> Option<MaxFileSize> {
        self.max_file_size
    }

    /// Returns the configured timezone strategy for timestamps.
    pub fn configured_timezone_strategy(&self) -> TimezoneStrategy {
        self.timezone_strategy
    }

    /// Returns the configured log format style.
    pub fn configured_format(&self) -> LogFormat {
        self.log_format
    }

    /// Returns the configured format options.
    pub fn configured_format_options(&self) -> FormatOptions {
        FormatOptions {
            format: self.log_format,
            file: self.show_file,
            line_number: self.show_line_number,
            thread_ids: self.show_thread_ids,
            thread_names: self.show_thread_names,
            target: self.show_target,
            level: self.show_level,
        }
    }

    /// Returns the configured filter based on log level and per-target settings.
    ///
    /// Use this when setting up your own subscriber to apply the same filtering
    /// configured via [`with_max_level()`](Self::with_max_level) and
    /// [`with_target()`](Self::with_target).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::{Builder, WebviewLayer, LevelFilter};
    /// # use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt, fmt};
    /// let builder = Builder::new()
    ///     .with_max_level(LevelFilter::DEBUG)
    ///     .with_target("hyper", LevelFilter::WARN);
    ///
    /// let filter = builder.build_filter();
    ///
    /// tauri::Builder::default()
    ///     .plugin(builder.build())
    ///     .setup(move |app| {
    ///         Registry::default()
    ///             .with(fmt::layer())
    ///             .with(WebviewLayer::new(app.handle().clone()))
    ///             .with(filter)
    ///             .init();
    ///         Ok(())
    ///     });
    ///     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
    /// ```
    pub fn build_filter(&self) -> Targets {
        self.filter.clone().with_default(self.log_level)
    }

    #[cfg(feature = "flamegraph")]
    fn plugin_builder<R: Runtime>() -> plugin::Builder<R> {
        plugin::Builder::new("tracing").invoke_handler(tauri::generate_handler![
            commands::log,
            commands::generate_flamegraph,
            commands::generate_flamechart
        ])
    }

    #[cfg(not(feature = "flamegraph"))]
    fn plugin_builder<R: Runtime>() -> plugin::Builder<R> {
        plugin::Builder::new("tracing").invoke_handler(tauri::generate_handler![commands::log,])
    }

    /// Builds and returns the configured Tauri plugin.
    ///
    /// This consumes the builder and returns a [`TauriPlugin`] that can be
    /// registered with your Tauri application.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use tauri_plugin_tracing::Builder;
    /// tauri::Builder::default()
    ///     .plugin(Builder::new().build());
    ///     // .run(tauri::generate_context!("examples/default-subscriber/src-tauri/tauri.conf.json"))
    /// ```
    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
        let log_level = self.log_level;
        let filter = self.filter;
        let custom_filter = self.custom_filter;
        let custom_layer = self.custom_layer;
        let targets = self.targets;
        let rotation = self.rotation;
        let rotation_strategy = self.rotation_strategy;
        let max_file_size = self.max_file_size;
        let timezone_strategy = self.timezone_strategy;
        let format_options = FormatOptions {
            format: self.log_format,
            file: self.show_file,
            line_number: self.show_line_number,
            thread_ids: self.show_thread_ids,
            thread_names: self.show_thread_names,
            target: self.show_target,
            level: self.show_level,
        };
        let set_default_subscriber = self.set_default_subscriber;

        #[cfg(feature = "colored")]
        let use_colors = self.use_colors;

        #[cfg(feature = "flamegraph")]
        let enable_flamegraph = self.enable_flamegraph;

        Self::plugin_builder()
            .setup(move |app, _api| {
                #[cfg(feature = "flamegraph")]
                setup_flamegraph(app);

                #[cfg(desktop)]
                if set_default_subscriber {
                    let guard = acquire_logger(
                        app,
                        log_level,
                        filter,
                        custom_filter,
                        custom_layer,
                        &targets,
                        rotation,
                        rotation_strategy,
                        max_file_size,
                        timezone_strategy,
                        format_options,
                        #[cfg(feature = "colored")]
                        use_colors,
                        #[cfg(feature = "flamegraph")]
                        enable_flamegraph,
                    )?;

                    // Store the guard in Tauri's state management to ensure logs flush on shutdown
                    if guard.is_some() {
                        app.manage(LogGuard(guard));
                    }
                }

                Ok(())
            })
            .build()
    }
}

/// Configuration for a file logging target.
struct FileTargetConfig {
    log_dir: PathBuf,
    file_name: String,
}

/// Resolves file target configuration from a Target.
fn resolve_file_target<R: Runtime>(
    app_handle: &AppHandle<R>,
    target: &Target,
) -> Result<Option<FileTargetConfig>> {
    match target {
        Target::LogDir { file_name } => {
            let log_dir = app_handle.path().app_log_dir()?;
            std::fs::create_dir_all(&log_dir)?;
            Ok(Some(FileTargetConfig {
                log_dir,
                file_name: file_name.clone().unwrap_or_else(|| "app".to_string()),
            }))
        }
        Target::Folder { path, file_name } => {
            std::fs::create_dir_all(path)?;
            Ok(Some(FileTargetConfig {
                log_dir: path.clone(),
                file_name: file_name.clone().unwrap_or_else(|| "app".to_string()),
            }))
        }
        _ => Ok(None),
    }
}

/// Cleans up old log files based on the retention strategy.
fn cleanup_old_logs(
    log_dir: &std::path::Path,
    file_prefix: &str,
    strategy: RotationStrategy,
) -> Result<()> {
    match strategy {
        RotationStrategy::KeepAll => Ok(()),
        RotationStrategy::KeepOne => cleanup_logs_keeping(log_dir, file_prefix, 1),
        RotationStrategy::KeepSome(n) => cleanup_logs_keeping(log_dir, file_prefix, n as usize),
    }
}

/// Helper to delete old log files, keeping only the most recent `keep` files.
fn cleanup_logs_keeping(log_dir: &std::path::Path, file_prefix: &str, keep: usize) -> Result<()> {
    let prefix_with_dot = format!("{}.", file_prefix);
    let mut log_files: Vec<_> = std::fs::read_dir(log_dir)?
        .filter_map(|entry| entry.ok())
        .filter(|entry| {
            entry
                .file_name()
                .to_str()
                .is_some_and(|name| name.starts_with(&prefix_with_dot) && name.ends_with(".log"))
        })
        .collect();

    // Sort by filename (which includes date) in descending order (newest first)
    log_files.sort_by_key(|entry| std::cmp::Reverse(entry.file_name()));

    // Delete all but the most recent `keep` files
    for entry in log_files.into_iter().skip(keep) {
        if let Err(e) = std::fs::remove_file(entry.path()) {
            tracing::warn!("Failed to remove old log file {:?}: {}", entry.path(), e);
        }
    }

    Ok(())
}

/// Sets up the tracing subscriber based on configured targets.
#[cfg(desktop)]
#[allow(clippy::too_many_arguments)]
fn acquire_logger<R: Runtime>(
    app_handle: &AppHandle<R>,
    log_level: LevelFilter,
    filter: Targets,
    custom_filter: Option<FilterFn>,
    custom_layer: Option<BoxedLayer>,
    targets: &[Target],
    rotation: Rotation,
    rotation_strategy: RotationStrategy,
    max_file_size: Option<MaxFileSize>,
    timezone_strategy: TimezoneStrategy,
    format_options: FormatOptions,
    #[cfg(feature = "colored")] use_colors: bool,
    #[cfg(feature = "flamegraph")] enable_flamegraph: bool,
) -> Result<Option<WorkerGuard>> {
    use std::io;
    use tracing_subscriber::fmt::time::OffsetTime;

    let filter_with_default = filter.with_default(log_level);

    // Determine which targets are enabled
    let has_stdout = targets.iter().any(|t| matches!(t, Target::Stdout));
    let has_stderr = targets.iter().any(|t| matches!(t, Target::Stderr));
    let has_webview = targets.iter().any(|t| matches!(t, Target::Webview));

    // Find file target (only first one is used)
    let file_config = targets
        .iter()
        .find_map(|t| resolve_file_target(app_handle, t).transpose())
        .transpose()?;

    // Determine if ANSI should be enabled for stdout/stderr.
    // File output uses StripAnsiWriter to strip ANSI codes, so stdout can use colors.
    #[cfg(feature = "colored")]
    let use_ansi = use_colors;
    #[cfg(not(feature = "colored"))]
    let use_ansi = false;

    // Helper to create timer based on timezone strategy
    let make_timer = || match timezone_strategy {
        TimezoneStrategy::Utc => OffsetTime::new(
            time::UtcOffset::UTC,
            time::format_description::well_known::Rfc3339,
        ),
        TimezoneStrategy::Local => time::UtcOffset::current_local_offset()
            .map(|offset| OffsetTime::new(offset, time::format_description::well_known::Rfc3339))
            .unwrap_or_else(|_| {
                OffsetTime::new(
                    time::UtcOffset::UTC,
                    time::format_description::well_known::Rfc3339,
                )
            }),
    };

    // Macro to create a formatted layer with the appropriate format style.
    // This is needed because .compact() and .pretty() return different types.
    macro_rules! make_layer {
        ($layer:expr, $format:expr) => {
            match $format {
                LogFormat::Full => $layer.boxed(),
                LogFormat::Compact => $layer.compact().boxed(),
                LogFormat::Pretty => $layer.pretty().boxed(),
            }
        };
    }

    // Create optional layers based on targets
    let stdout_layer = if has_stdout {
        let layer = fmt::layer()
            .with_timer(make_timer())
            .with_ansi(use_ansi)
            .with_file(format_options.file)
            .with_line_number(format_options.line_number)
            .with_thread_ids(format_options.thread_ids)
            .with_thread_names(format_options.thread_names)
            .with_target(format_options.target)
            .with_level(format_options.level);
        Some(make_layer!(layer, format_options.format))
    } else {
        None
    };

    let stderr_layer = if has_stderr {
        let layer = fmt::layer()
            .with_timer(make_timer())
            .with_ansi(use_ansi)
            .with_file(format_options.file)
            .with_line_number(format_options.line_number)
            .with_thread_ids(format_options.thread_ids)
            .with_thread_names(format_options.thread_names)
            .with_target(format_options.target)
            .with_level(format_options.level)
            .with_writer(io::stderr);
        Some(make_layer!(layer, format_options.format))
    } else {
        None
    };

    let webview_layer = if has_webview {
        Some(WebviewLayer::new(app_handle.clone()))
    } else {
        None
    };

    // Set up file logging if configured
    let (file_layer, guard) = if let Some(config) = file_config {
        // Note: cleanup_old_logs only works reliably with time-based rotation
        // When using size-based rotation, files have numeric suffixes that may not sort correctly
        if max_file_size.is_none() {
            cleanup_old_logs(&config.log_dir, &config.file_name, rotation_strategy)?;
        }

        // Use rolling-file crate when max_file_size is set (supports both size and time-based rotation)
        // Otherwise use tracing-appender (time-based only)
        if let Some(max_size) = max_file_size {
            use rolling_file::{BasicRollingFileAppender, RollingConditionBasic};

            // Build rolling condition with both time and size triggers
            let mut condition = RollingConditionBasic::new();
            condition = match rotation {
                Rotation::Daily => condition.daily(),
                Rotation::Hourly => condition.hourly(),
                Rotation::Minutely => condition, // rolling-file doesn't have minutely, use size only
                Rotation::Never => condition,    // size-only rotation
            };
            condition = condition.max_size(max_size.0);

            // Determine max file count from rotation strategy
            let max_files = match rotation_strategy {
                RotationStrategy::KeepAll => u32::MAX as usize,
                RotationStrategy::KeepOne => 1,
                RotationStrategy::KeepSome(n) => n as usize,
            };

            let log_path = config.log_dir.join(format!("{}.log", config.file_name));
            let file_appender = BasicRollingFileAppender::new(log_path, condition, max_files)
                .map_err(std::io::Error::other)?;

            let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
            // Wrap with StripAnsiWriter to remove ANSI codes that leak from shared span formatting
            let strip_ansi_writer = StripAnsiWriter::new(non_blocking);

            let layer = fmt::layer()
                .with_timer(make_timer())
                .with_ansi(false)
                .with_file(format_options.file)
                .with_line_number(format_options.line_number)
                .with_thread_ids(format_options.thread_ids)
                .with_thread_names(format_options.thread_names)
                .with_target(format_options.target)
                .with_level(format_options.level)
                .with_writer(strip_ansi_writer);

            (Some(make_layer!(layer, format_options.format)), Some(guard))
        } else {
            // Time-based rotation only using tracing-appender with proper .log extension
            use tracing_appender::rolling::RollingFileAppender;

            let appender_rotation = match rotation {
                Rotation::Daily => tracing_appender::rolling::Rotation::DAILY,
                Rotation::Hourly => tracing_appender::rolling::Rotation::HOURLY,
                Rotation::Minutely => tracing_appender::rolling::Rotation::MINUTELY,
                Rotation::Never => tracing_appender::rolling::Rotation::NEVER,
            };

            let file_appender = RollingFileAppender::builder()
                .rotation(appender_rotation)
                .filename_prefix(&config.file_name)
                .filename_suffix("log")
                .build(&config.log_dir)
                .map_err(std::io::Error::other)?;

            let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
            // Wrap with StripAnsiWriter to remove ANSI codes that leak from shared span formatting
            let strip_ansi_writer = StripAnsiWriter::new(non_blocking);

            let layer = fmt::layer()
                .with_timer(make_timer())
                .with_ansi(false)
                .with_file(format_options.file)
                .with_line_number(format_options.line_number)
                .with_thread_ids(format_options.thread_ids)
                .with_thread_names(format_options.thread_names)
                .with_target(format_options.target)
                .with_level(format_options.level)
                .with_writer(strip_ansi_writer);

            (Some(make_layer!(layer, format_options.format)), Some(guard))
        }
    } else {
        (None, None)
    };

    // Create flame layer if flamegraph feature is enabled
    #[cfg(feature = "flamegraph")]
    let flame_layer = if enable_flamegraph {
        Some(create_flame_layer(app_handle)?)
    } else {
        None
    };

    // Create custom filter layer if configured
    let custom_filter_layer = custom_filter.map(|f| filter_fn(move |metadata| f(metadata)));

    // Compose the subscriber with all optional layers
    // Note: Boxed layers (custom_layer, flame_layer) must be combined and added first
    // because they're typed as Layer<Registry> and the subscriber type changes after each .with()
    #[cfg(feature = "flamegraph")]
    let combined_boxed_layer: Option<BoxedLayer> = match (custom_layer, flame_layer) {
        (Some(c), Some(f)) => {
            use tracing_subscriber::Layer;
            Some(c.and_then(f).boxed())
        }
        (Some(c), None) => Some(c),
        (None, Some(f)) => Some(f),
        (None, None) => None,
    };

    #[cfg(not(feature = "flamegraph"))]
    let combined_boxed_layer = custom_layer;

    let subscriber = Registry::default()
        .with(combined_boxed_layer)
        .with(stdout_layer)
        .with(stderr_layer)
        .with(file_layer)
        .with(webview_layer)
        .with(custom_filter_layer)
        .with(filter_with_default);

    tracing::subscriber::set_global_default(subscriber)?;
    tracing::info!("tracing initialized");
    Ok(guard)
}