syslog-rs 7.1.0

A native Rust implementation of the glibc/libc/windows syslog client and windows native log for logging.
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
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
/*-
 * syslog-rs - a syslog client translated from libc to rust
 * 
 * Copyright 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *
 *   2. The MIT License (MIT)
 *                     
 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

use std::{fmt, marker::PhantomData, sync::{Arc, Mutex}};

use crate::
{
    DefaultLocalSyslogDestination, LogFacility, LogStat, Priority, SyslogDestination, error::SyRes, 
    formatters::{DefaultSyslogFormatter, SyslogFormatter}, 
    sync::{LogItems, SyStream, SyStreamPri, SyStreamSyslogApi, syslog_sync_internal::SyslogSocketLockless}
};

/// A public trait which defines the public interface for the `dyn` syslog instance.
pub trait DynSyslog: Send + Sync
{
    /// Connects the current instance to the syslog server (destination).
    fn connectlog(&self) -> SyRes<()>;

    /// Sets the logmask to filter out the syslog calls.
    /// 
    /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
    ///
    /// # Example
    ///
    /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
    ///
    /// or
    ///
    /// ~(LOG_MASK!(Priority::LOG_INFO))
    /// LOG_UPTO!(Priority::LOG_ERROR)
    fn setlogmask(&self, logmask: i32) -> SyRes<i32>;

    /// Closes connection to the syslog server (destination).
    fn closelog(&self) -> SyRes<()>;

    /// Similar to libc, syslog() sends data to syslog server.
    /// 
    /// # Arguments
    ///
    /// * `pri` - a priority [Priority]
    ///
    /// * `fmt` - a formatter string which should be sent to syslog/file.
    /// 
    /// # Returns 
    /// 
    /// A [SyRes] is returned which may describe an error.
    fn syslog(&self, pri: Priority, fmt: String) -> SyRes<()>;

    /// This function can be used to update the facility name, for example
    /// after fork().
    /// 
    /// # Arguments
    /// 
    /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
    ///     If set to [Option::None] would request the program name from OS.
    fn change_identity(&self, ident: Option<&str>) -> SyRes<()>;

    /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
    /// the same variant (type) as current.
    fn reconnect(&self) -> SyRes<()>;
}

/// A public trait which defines the public interface for the `dyn` syslog instance.
pub trait MutDynSyslog
{
    /// Connects the current instance to the syslog server (destination).
    fn connectlog(&mut self) -> SyRes<()>;

    /// Sets the logmask to filter out the syslog calls.
    /// 
    /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
    ///
    /// # Example
    ///
    /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
    ///
    /// or
    ///
    /// ~(LOG_MASK!(Priority::LOG_INFO))
    /// LOG_UPTO!(Priority::LOG_ERROR)
    fn setlogmask(&mut self, logmask: i32) -> SyRes<i32>;

    /// Closes connection to the syslog server (destination).
    fn closelog(&mut self) -> SyRes<()>;

    /// Similar to libc, syslog() sends data to syslog server.
    /// 
    /// # Arguments
    ///
    /// * `pri` - a priority [Priority]
    ///
    /// * `fmt` - a formatter string which should be sent to syslog/file.
    /// 
    /// # Returns 
    /// 
    /// A [SyRes] is returned which may describe an error.
    fn syslog(&mut self, pri: Priority, fmt: String) -> SyRes<()>;

    /// This function can be used to update the facility name, for example
    /// after fork().
    /// 
    /// # Arguments
    /// 
    /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
    ///     If set to [Option::None] would request the program name from OS.
    fn change_identity(&mut self, ident: Option<&str>) -> SyRes<()>;

    /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
    /// the same variant (type) as current.
    fn reconnect(&mut self) -> SyRes<()>;
}

/// Exposed only to allow constraint on the generic type. Should not be implemented on the
/// user's side.
pub trait SyslogType: fmt::Debug
{
    type LockItemsBase;

    type LockItems;

    type LockStreamBase<D: SyslogDestination>;

    type LockStream<D: SyslogDestination>;

    type MoveLockType;

    fn wrap_items(items: Self::LockItemsBase) -> Self::LockItems;
    fn wrap_stream<D: SyslogDestination>(stream: Self::LockStreamBase<D>) -> Self::LockStream<D>;
}

/// A main sync syslog client instance.
/// 
/// # Generics
/// 
/// * `F` - a syslog message formatter. See dir `src/formatters` for available or define one 
///     yourself. A default is [DefaultSyslogFormatter].
/// 
/// * `D` - a destination server/file. See file `src/syslog_provider.rs` for available 
///     transports. A default is [DefaultLocalSyslogDestination].
/// 
/// * `SL` - a client type. Sets the mechanism how the resources are shared. There are 3 types
///     of syslog. See info below. Default is [SyShared].
/// 
/// # Client types
/// 
/// * [SyShared] - is a single FD descriptor which is protected by Mutex. This type can be 
///     clonned, moved between threads.
/// 
/// * [SyThreadLocal] - is a thread local/lockless type which should be used only in the 
///     single thread applications or with [std::thread_local] only! Can be enabled with
///     feature `build_with_thread_local`.
/// 
/// * [SyQueued] - is a buffering/fast type which uses a separate thread to access the 
///     socket's/file's FD. This type can be clonned, moved between threads. Can be
///     enabled with feature `build_with_queue`. Also, it can be used to use the same 
///     connection/file from async and sync code, see `examples/example_sync_async.rs`.
/// 
/// Feature `build_with_queue` without any async features uses a `crossbeam` MPSC. If
/// any feature which enables `async` i.e `build_async_tokio`, `build_async_smol`, 
/// `build_async_interface`, the MPSC provider will be switch to either Smoll's, Tokio's,
/// or which you will define.
/// 
/// # Examples
/// 
/// Full default:
/// 
/// ```ignore
/// pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(|| 
/// {
///     #[cfg(target_family = "unix")]
///     let syslog_provider = 
///         SyslogLocal::new();
/// 
///     #[cfg(target_family = "windows")]   
///     let syslog_provider = 
///         WindowsEvent::new();
/// 
///     Syslog::openlog(
///         Some("example"), 
///         LogStat::LOG_CONS | LogStat::LOG_PID, 
///         LogFacility::LOG_DAEMON,
///         syslog_provider
///     )
///     .unwrap()
/// }
/// );
/// ```
/// 
/// Custom i.e queued
/// 
/// ```ignore
/// pub static SYSLOG: LazyLock<Syslog<DefaultSyslogFormatter, DefaultLocalSyslogDestination, SyQueued<DefaultQueueAdapter, DefaultSyslogFormatter, DefaultLocalSyslogDestination>>> = 
/// LazyLock::new(|| 
///     {
///         #[cfg(target_family = "unix")]
///        let syslog_provider = 
///             SyslogLocal::new();
/// 
///         #[cfg(target_family = "windows")]   
///         let syslog_provider = 
///             WindowsEvent::new();
/// 
///         Syslog
///             ::<_, _, SyQueued<DefaultQueueAdapter, _, _>>
///             ::openlog_with(
///                 Some("example_sync_queue"), 
///                 LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
///                 LogFacility::LOG_DAEMON,
///                 syslog_provider
///             )
///             .unwrap()
///     }
///  );
/// ```
/// 
#[derive(Debug)]
pub struct Syslog<F = DefaultSyslogFormatter, D = DefaultLocalSyslogDestination, SL = SyShared>
where 
    F: SyslogFormatter, 
    D: SyslogDestination,
    SL: SyslogType
{   
    /// An identification i.e program name, thread name
    log_items: SL::LockItems,

    /// A stream (unixdatagram, udp, tcp)
    stream: SL::LockStream<D>,

    _p: PhantomData<F>,

    _p_not_ss: SL::MoveLockType
}

impl Syslog
{
    /// Opens a default connection to the local syslog server with default formatter.
    /// 
    /// In order to access the syslog API, use the [SyslogApi].
    /// 
    /// # Arguments
    /// 
    /// * `ident` - A program name which will appear on the logs. If none, will be determined
    ///     automatically.
    /// 
    /// * `logstat` - [LogStat] an instance config.
    /// 
    /// * `facility` - [LogFacility] a syslog facility.
    /// 
    /// * `net_tap_prov` - a [SyslogLocal] instance with configuration.
    /// 
    /// # Returns
    /// 
    /// A [SyRes] is returned ([Result]) with: 
    /// 
    /// * [Result::Ok] - with instance
    /// 
    /// * [Result::Err] - with error description.
    pub 
    fn openlog(ident: Option<&str>, logstat: LogStat, facility: LogFacility, 
        net_tap_prov: DefaultLocalSyslogDestination) -> SyRes<Self> 
    {
        return Ok( 
            Self
            {
                log_items: 
                    <SyShared as SyslogType>
                        ::wrap_items(
                            <SyShared as SyslogType>::LockItemsBase::new(ident, 0xff, logstat, facility)
                        ),
                stream: 
                    <SyShared as SyslogType>
                        ::wrap_stream(
                            <SyShared as SyslogType>::LockStreamBase::<DefaultLocalSyslogDestination>::new(logstat, net_tap_prov)?,
                        ),
                _p: 
                    PhantomData,
                _p_not_ss: 
                    PhantomData
            }
        );
    }
}

#[cfg(feature = "build_with_thread_local")]
pub mod build_with_thread_local
{
    use super::*;

    /// A threal local syslog which is completely lockless! It can be used in signle threaded
    /// applications or with the [thread_local] functionality.
    /// 
    /// ```ignore
    /// thread_local! 
    /// {
    ///     // Could add pub to make it public to whatever Foo already is public to.
    ///     static SYSLOG: RefCell<Syslog<DefaultSyslogFormatter, SyslogLocal, SyThreadLocal>> = 
    ///         RefCell::new(
    ///             Syslog
    ///                 ::<_, _, SyThreadLocal>
    ////                ::openlog_with(Some("test"), LogStat::LOG_PID, LogFacility::LOG_DAEMON, SyslogLocal::new())
    ///                     .unwrap()
    ///         );
    /// }
    /// ```
    /// 
    /// A stream is availble via [SyStreamApi].
    /// 
    /// ```ignore
    /// let _ = write!(SYSLOG.stream(Priority::LOG_DEBUG), "test {} 123 stream test ", d);
    /// ```
    /// 
    /// The instances will be completly separated and have own FD.
    /// 
    /// # Generics
    /// 
    /// * `D` - a [SyslogDestination] instance which is either:
    ///     [SyslogLocal], [crate::syslog_provider::SyslogFile], [crate::syslog_provider::SyslogNet], 
    ///     [crate::syslog_provider::SyslogTls]. By default a `SyslogLocal` is selected.
    /// 
    /// * `F` - a [SyslogFormatter] which sets the instance which would 
    ///     format the message.
    /// 
    #[derive(Debug)]
    pub struct SyThreadLocal;

    impl SyslogType for SyThreadLocal
    {
        type LockItemsBase = LogItems;
        type LockItems = Self::LockItemsBase;

        type LockStreamBase<D: SyslogDestination> = SyslogSocketLockless<D>;
        type LockStream<D: SyslogDestination> = Self::LockStreamBase<D>;
        
        type MoveLockType = PhantomData<*const ()>;

        fn wrap_items(items: Self::LockItemsBase) -> Self::LockItems 
        {
            items
        }

        fn wrap_stream<D: SyslogDestination>(stream: Self::LockStreamBase<D>) -> Self::LockStream<D> 
        {
            stream
        }
    }

    impl<F, D> MutDynSyslog for Syslog<F, D, SyThreadLocal>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
    {
        #[inline]
        fn connectlog(&mut self) -> SyRes<()> 
        {
            self.connectlog()
        }

        #[inline]
        fn setlogmask(&mut self, logmask: i32) -> SyRes<i32> 
        {
            self.setlogmask(logmask)
        }

        #[inline]
        fn closelog(&mut self) -> SyRes<()>
        {
            self.closelog()
        }
        
        fn syslog(&mut self, pri: Priority, fmt: String) -> SyRes<()> 
        {
            self.syslog(pri, fmt.into())
        }
        
        fn change_identity(&mut self, ident: Option<&str>) -> SyRes<()> 
        {
            self.change_identity(ident)
        }
        
        fn reconnect(&mut self) -> SyRes<()> 
        {
            self.reconnect()
        }
    }

    impl<F, D> Syslog<F, D, SyThreadLocal>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
    {
        pub 
        fn make_dyn(self) -> Box<dyn MutDynSyslog>
        {
            Box::new(self)
        }

        /// Opens a default connection to the local syslog server with default formatter.
        /// 
        /// # Arguments
        /// 
        /// * `ident` - A program name which will appear on the logs. If none, will be determined
        ///     automatically.
        /// 
        /// * `logstat` - [LogStat] an instance config.
        /// 
        /// * `facility` - [LogFacility] a syslog facility.
        /// 
        /// * `net_tap_prov` - a [SyslogLocal] instance with configuration.
        /// 
        /// # Returns
        /// 
        /// A [SyRes] is returned ([Result]) with: 
        /// 
        /// * [Result::Ok] - with instance
        /// 
        /// * [Result::Err] - with error description.
        pub 
        fn openlog_with(ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap_prov: D) -> SyRes<Self> 
        {
            return Ok(
                Self
                {
                    log_items:
                        <SyThreadLocal as SyslogType>
                            ::wrap_items(
                                <SyThreadLocal as SyslogType>::LockItems::new(ident, 0xff, logstat, facility)
                            ),
                    stream:
                        <SyThreadLocal as SyslogType>
                            ::wrap_stream(
                                <SyThreadLocal as SyslogType>::LockStream::new(logstat, net_tap_prov)?
                            ),
                    _p: 
                        PhantomData,
                    _p_not_ss: 
                        PhantomData
                }
            );
        }
    }

    impl<F, D> Syslog<F, D, SyThreadLocal>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
    {
        /// Connects the current instance to the syslog server (destination).
        #[inline]
        pub 
        fn connectlog(&mut self) -> SyRes<()>
        {
            return 
                self
                    .stream
                    .connectlog();
        }

        /// Sets the logmask to filter out the syslog calls.
        /// 
        /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
        ///
        /// # Example
        ///
        /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
        ///
        /// or
        ///
        /// ~(LOG_MASK!(Priority::LOG_INFO))
        /// LOG_UPTO!(Priority::LOG_ERROR)
        #[inline]
        pub 
        fn setlogmask(&mut self, logmask: i32) -> SyRes<i32>
        {
            return Ok(
                self
                    .log_items
                    .set_logmask(logmask)
            );
        }

        /// Closes connection to the syslog server (destination).
        #[inline]
        pub 
        fn closelog(&mut self) -> SyRes<()> 
        {
            return 
                self
                    .stream
                    .disconnectlog();
        }

        /// Similar to libc, syslog() sends data to syslog server.
        /// 
        /// # Arguments
        ///
        /// * `pri` - a priority [Priority]
        ///
        /// * `fmt` - a formatter [SyslogFormatter] message. In C exists a functions with
        ///     variable argumets amount. In Rust you should create your
        ///     own macros like format!() or use format!()]. The [String] and ref `'static` 
        ///     [str] can be passed directly.
        /// 
        /// # Returns 
        /// 
        /// A [SyRes] is returned which may describe an error.
        #[inline]
        pub 
        fn syslog(&mut self, pri: Priority, fmt: F) -> SyRes<()>
        {
            let Some((formatted_msg, logstat)) = 
                self.log_items.vsyslog1_msg::<F, D>(pri, &fmt)
                else { return Ok(()) };

            self.stream.vsyslog1(logstat, formatted_msg)
        }

        /// This function can be used to update the facility name, for example
        /// after fork().
        /// 
        /// # Arguments
        /// 
        /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
        ///     If set to [Option::None] would request the program name from OS.
        #[inline]
        pub 
        fn change_identity(&mut self, ident: Option<&str>) -> SyRes<()>
        {
            self.log_items.set_identity(ident);

            return Ok(());
        }

        /// Re-opens the connection to the syslog server. Can be used to 
        /// rotate logs(handle SIGHUP).
        /// 
        /// # Returns
        /// 
        /// A [Result] is retured as [SyRes].
        /// 
        /// * [Result::Ok] - with empty inner type.
        /// 
        /// * [Result::Err] - an error code and description 
        #[inline]
        pub 
        fn reconnect(&mut self) -> SyRes<()>
        {
            return
                self
                    .stream
                    .reconnectlog();
        }

        /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
        /// the same variant (type) as current.
        #[inline]
        pub 
        fn update_tap_data(&mut self, tap_data: D) -> SyRes<()>
        {
            return 
                self
                    .stream
                    .update_tap_data(tap_data.clone());
        }
    }

    impl<F, D> Syslog<F, D, SyThreadLocal>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
    {
        /// Returns the streamable [SyStream] instance which can be used with [write!].
        /// 
        /// It implements both [std::fmt::Write] and [std::io::Write].
        /// 
        /// # Example
        /// 
        /// ```ignore
        /// let log = 
        ///     SingleSyslog::openlog(
        ///         Some("test1"), 
        ///         LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
        ///         LogFacility::LOG_DAEMON,
        ///         SyslogLocal::new()
        ///     ).unwrap();
        /// 
        /// write!(log.get_stream::<SyStreamPriDebug>(), "test stream singlesyslog {}", i).unwrap();
        /// ```
        pub 
        fn get_stream<'t, PRI>(&'t mut self) -> SyStream<'t, PRI, D, F, &'t mut Self>
        where PRI: SyStreamPri
        {
            SyStream
            {
                s: Some(self),
                _p: PhantomData,
                _p1: PhantomData,
                _p2: PhantomData
            }
        }
    }

    impl<F, D> SyStreamSyslogApi<F, D>  
    for &mut Syslog<F, D, SyThreadLocal>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
    {
        type SYSLOG<'t> = &'t mut Syslog<F, D, SyThreadLocal>;

        fn syslog<'t>(syslog: Self::SYSLOG<'t>, pri: Priority, fmt: F) -> SyRes<()>
        {
            syslog.syslog(pri, fmt)
        }
    }
}

#[cfg(feature = "build_with_thread_local")]
pub use self::build_with_thread_local::SyThreadLocal;

// ---- SYSLOG SHARED

/// A `sync`, shared instance of the syslog client which is shared between many
/// threads. Previously a mutex was used, but since the v5.0.0 a CoW experimental
/// approach is used. The `CoW` creates clones of the instance without holding
/// a long mutex locks. When writing, the instance does not hold a long lock on
/// updated item. Only exclusive lock locks the readers, because the instance is
/// updated and not usable anyway.
/// 
/// If the program has fixed amount of threads, probably the `syslog_threadlocal`
/// will be better alternative. It has the same functionality, but avoids 
/// any sync locks by working in current thread.
/// 
/// # Traits
/// 
/// For this isntance a [SyslogApi] and [SyStreamApi] are implemented.
/// 
/// # Examples
/// 
/// ```ignore
/// let log = 
///     Syslog::openlog(
///         Some("test1"), 
///         LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
///         LogFacility::LOG_DAEMON,
///         SyslogLocal::new()
///     );
/// ```
/// 
/// ```ignore
/// let log = 
///     Syslog
///         ::<DefaultSyslogFormatter, SyslogLocal, SyShared>
///         ::openlog_with(
///             Some("test1"), 
///             LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
///             LogFacility::LOG_DAEMON,
///             SyslogLocal::new()
///         );
/// ```
/// 
/// ```ignore
/// pub static SYSLOG3: LazyLock<Syslog<DefaultSyslogFormatter, SyslogLocal, SyShared>> = 
///     LazyLock::new(|| 
///         {
///             Syslog
///                 ::<DefaultSyslogFormatter, SyslogLocal, SyShared>
///                 ::openlog_with(
///                     Some("test1"), 
///                     LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
///                     LogFacility::LOG_DAEMON,
///                     SyslogLocal::new()
///                 )
///                 .unwrap()
///         }
///     );
/// ```
/// # Streaming
/// 
/// A stream is availble via [SyStreamApi].
/// 
/// ```ignore
/// let _ = write!(SYSLOG.stream(Priority::LOG_DEBUG), "test {} 123 stream test ", d);
/// ```
/// 
/// # Generics
/// 
/// * `F` - a [SyslogFormatter] which sets the instance which would 
///     format the message.
/// 
/// * `D` - a [SyslogDestination] instance which is either:
///     [SyslogLocal], [SyslogFile], [SyslogNet], [SyslogTls]. By
///     default a `SyslogLocal` is selected.
#[derive(Debug)]
pub struct SyShared;

impl SyslogType for SyShared
{
    type LockItemsBase = LogItems;
    type LockItems = Arc<Mutex<Self::LockItemsBase>>;

    type LockStreamBase<D: SyslogDestination> = SyslogSocketLockless<D>;
    type LockStream<D: SyslogDestination> = Arc<Mutex<Self::LockStreamBase<D>>>;

    type MoveLockType = PhantomData<()>;

    fn wrap_items(items: Self::LockItemsBase) -> Self::LockItems 
    {
        Arc::new(Mutex::new(items))
    }

    fn wrap_stream<D: SyslogDestination>(stream: Self::LockStreamBase<D>) -> Self::LockStream<D> 
    {
        Arc::new(Mutex::new(stream))
    }
}

impl<F, D> Clone for Syslog<F, D, SyShared>
where 
    F: SyslogFormatter,
    D: SyslogDestination,
{
    fn clone(&self) -> Self 
    {
        Self 
        { 
            log_items: 
                self.log_items.clone(), 
            stream: 
                self.stream.clone(), 
            _p: 
                PhantomData, 
            _p_not_ss: 
                PhantomData,
        }
    }
}

impl<F, D> DynSyslog for Syslog<F, D, SyShared>
where 
    F: SyslogFormatter,
    D: SyslogDestination,
{
    #[inline]
    fn connectlog(&self) -> SyRes<()> 
    {
        self.connectlog()
    }

    #[inline]
    fn setlogmask(&self, logmask: i32) -> SyRes<i32> 
    {
        self.setlogmask(logmask)
    }

    #[inline]
    fn closelog(&self) -> SyRes<()>
    {
        self.closelog()
    }
    
    fn syslog(&self, pri: Priority, fmt: String) -> SyRes<()> 
    {
        self.syslog(pri, fmt.into())
    }
    
    fn change_identity(&self, ident: Option<&str>) -> SyRes<()> 
    {
        self.change_identity(ident)
    }
    
    fn reconnect(&self) -> SyRes<()> 
    {
        self.reconnect()
    }
}


impl<F, D> Syslog<F, D, SyShared>
where 
    F: SyslogFormatter,
    D: SyslogDestination,
{
    pub 
    fn make_dyn(self) -> Box<dyn DynSyslog + Send + Sync>
    {
        Box::new(self)
    }

    /// Opens a special connection to the destination syslog server with specific formatter.
    /// 
    /// All struct generic should be specified before calling this function.
    /// 
    /// In order to access the syslog API, use the [SyslogApi].
    /// 
    /// # Arguments
    /// 
    /// * `ident` - A program name which will appear on the logs. If none, will be determined
    ///     automatically.
    /// 
    /// * `logstat` - [LogStat] an instance config.
    /// 
    /// * `facility` - [LogFacility] a syslog facility.
    /// 
    /// * `net_tap_prov` - a destination server. A specific `D` instance which contains infomation 
    ///     about the destination server. See `syslog_provider.rs`.
    /// 
    /// # Returns
    /// 
    /// A [SyRes] is returned ([Result]) with: 
    /// 
    /// * [Result::Ok] - with instance
    /// 
    /// * [Result::Err] - with error description.
    pub 
    fn openlog_with(ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap_prov: D) -> SyRes<Self> 
    {
        return Ok( 
            Self
            {
                log_items: 
                    <SyShared as SyslogType>
                        ::wrap_items(
                            <SyShared as SyslogType>::LockItemsBase::new(ident, 0xff, logstat, facility)
                        ),
                stream: 
                    <SyShared as SyslogType>
                        ::wrap_stream(
                            <SyShared as SyslogType>::LockStreamBase::<D>::new(logstat, net_tap_prov)?,
                        ),
                _p: 
                    PhantomData,
                _p_not_ss:
                    PhantomData
            }
        );
    }

    /// Just initializes the instance from other instance. For example, in the crate which uses
    /// this type of logging. 
    /// 
    /// Will work only if `F` and `D` are same.
    pub 
    fn attach(other: &Syslog<F, D, SyShared>) -> SyRes<Self>
    {
        Ok(other.clone())
    }
}

impl<F, D> Syslog<F, D, SyShared>
where 
    F: SyslogFormatter,
    D: SyslogDestination,
{
    /// Connects the current instance to the syslog server (destination).
    pub 
    fn connectlog(&self) -> SyRes<()>
    {
        return 
            self
                .stream
                .lock()
                .unwrap()
                .connectlog();
    }

    /// Sets the logmask to filter out the syslog calls.
    /// 
    /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
    ///
    /// # Example
    ///
    /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
    ///
    /// or
    ///
    /// ~(LOG_MASK!(Priority::LOG_INFO))
    /// LOG_UPTO!(Priority::LOG_ERROR)
    pub 
    fn setlogmask(&self, logmask: i32) -> SyRes<i32> 
    {
        let pri = 
            self
                .log_items
                .lock()
                .unwrap()
                .set_logmask(logmask);

        return Ok(pri);
    }

    /// Closes connection to the syslog server (destination).
    pub 
    fn closelog(&self) -> SyRes<()> 
    {
        return 
            self
                .stream
                .lock()
                .unwrap()
                .disconnectlog();
    }

    /// Similar to libc, syslog() sends data to syslog server.
    /// 
    /// # Arguments
    ///
    /// * `pri` - a priority [Priority]
    ///
    /// * `fmt` - a formatter [SyslogFormatter] message. In C exists a functions with
    ///     variable argumets amount. In Rust you should create your
    ///     own macros like format!() or use format!()]. The [String] and ref `'static` 
    ///     [str] can be passed directly.
    /// 
    /// # Returns 
    /// 
    /// A [SyRes] is returned which may describe an error.
    #[inline]
    pub 
    fn syslog(&self, pri: Priority, fmt: F) -> SyRes<()>
    {
        let Some((formatted_msg, logstat)) = 
            self.log_items.lock().unwrap().vsyslog1_msg::<F, D>(pri, &fmt)
        else { return Ok(()) };

        self.stream.lock().unwrap().vsyslog1(logstat, formatted_msg)
    }

    /// This function can be used to update the facility name, for example
    /// after fork().
    /// 
    /// # Arguments
    /// 
    /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
    ///     If set to [Option::None] would request the program name from OS.
    pub 
    fn change_identity(&self, ident: Option<&str>) -> SyRes<()>
    {
        self
            .log_items
            .lock()
            .unwrap()
            .set_identity(ident);

        return Ok(());
    }

    /// Re-opens the connection to the syslog server. Can be used to 
    /// rotate logs(handle SIGHUP).
    /// 
    /// # Returns
    /// 
    /// A [Result] is retured as [SyRes].
    /// 
    /// * [Result::Ok] - with empty inner type.
    /// 
    /// * [Result::Err] - an error code and description 
    pub 
    fn reconnect(&self) -> SyRes<()>
    {
        return
            self
                .stream
                .lock()
                .unwrap()
                .reconnectlog();
    }

    /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
    /// the same variant (type) as current.
    pub 
    fn update_tap_data(&self, tap_data: D) -> SyRes<()>
    {
        return
            self
                .stream
                .lock()
                .unwrap()
                .update_tap_data(tap_data);
    }
}

impl<F, D> Syslog<F, D, SyShared>
where 
    F: SyslogFormatter,
    D: SyslogDestination,
{
    /// Returns the streamable [SyStream] instance which can be used with [write!].
    /// 
    /// It implements both [std::fmt::Write] and [std::io::Write].
    /// 
    /// # Example
    /// 
    /// ```ignore
    /// let log = 
    ///     SingleSyslog::openlog(
    ///         Some("test1"), 
    ///         LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
    ///         LogFacility::LOG_DAEMON,
    ///         SyslogLocal::new()
    ///     ).unwrap();
    /// 
    /// write!(log.get_stream::<SyStreamPriDebug>(), "test stream singlesyslog {}", i).unwrap();
    /// ```
    pub 
    fn get_stream<'t, PRI>(&'t self) -> SyStream<'t, PRI, D, F, &'t Self>
    where PRI: SyStreamPri
    {
        SyStream
        {
            s: Some(self),
            _p: PhantomData,
            _p1: PhantomData,
            _p2: PhantomData
        }
    }
}

impl<F: SyslogFormatter, D: SyslogDestination> SyStreamSyslogApi<F, D>  
for &Syslog<F, D, SyShared>
{
    type SYSLOG<'t> = &'t Syslog<F, D, SyShared>;

    fn syslog<'t>(syslog: Self::SYSLOG<'t>, pri: Priority, fmt: F) -> SyRes<()>
    {
        syslog.syslog(pri, fmt)
    }
}

// --- QUEUE


#[cfg(feature = "build_with_queue")]
pub mod build_with_queue
{
    use std::{sync::{Arc, Mutex, Weak, atomic::{AtomicBool, Ordering}}, thread};

    use crate::{DefaultLocalSyslogDestination, SyslogDestination, formatters::{DefaultSyslogFormatter, SyslogFormatter}, map_error, sy_sync_queue::{SyCmd, SyslogInternal, SyslogQueueChanSnd, SyslogQueueChannel, SyslogQueueOneChanRcv}, sync::DefaultQueueAdapter};

    use super::*;

    /// A queued syslog client which uses a separate thread to send messages to upstream.
    /// 
    /// # Generics
    /// 
    /// * `F` - a syslog message formatter. See dir `src/formatters` for available or define one 
    ///     yourself. A default is [DefaultSyslogFormatter].
    /// 
    /// * `D` - a destination server/file. See file `src/syslog_provider.rs` for available 
    ///     transports. A default is [DefaultLocalSyslogDestination].
    /// 
    /// * `S` - a queue mechanism. A default is [DefaultLocalSyslogDestination]. If you don't
    ///     need any special queue mechanism, use the default.
    #[derive(Debug, Clone)]
    pub struct SyQueued<S = DefaultQueueAdapter, F = DefaultSyslogFormatter, D = DefaultLocalSyslogDestination>
    where 
        F: SyslogFormatter, 
        D: SyslogDestination,
        S: SyslogQueueChannel<F, D>,

    {
        /// Control flag
        run_control: Weak<AtomicBool>,

        /// commands channel
        pub(crate) tasks: S::ChannelSnd,

        /// process thread
        thread: Arc<Mutex<Option<thread::JoinHandle<()>>>>,
    }

    impl<F, DD, S> SyslogType for SyQueued<S, F, DD>
    where F: SyslogFormatter, DD: SyslogDestination, S: SyslogQueueChannel<F, DD>
    {
        type LockItemsBase = ();
        type LockItems = Self::LockItemsBase;

        type LockStreamBase<D: SyslogDestination> = SyQueued<S, F, DD>;
        type LockStream<D: SyslogDestination> = Self::LockStreamBase<D>;

        type MoveLockType = PhantomData<()>;

        fn wrap_items(items: Self::LockItemsBase) -> Self::LockItems 
        {
            items
        }

        fn wrap_stream<D: SyslogDestination>(stream: Self::LockStreamBase<D>) -> Self::LockStream<D> 
        {
            stream
        }
    }

    unsafe impl<F, D, S> Send for SyQueued<S, F, D> 
    where F: SyslogFormatter, D: SyslogDestination, S: SyslogQueueChannel<F, D>
    {}

    impl<F, D, S> Drop for SyQueued<S, F, D>
    where F: SyslogFormatter, D: SyslogDestination, S: SyslogQueueChannel<F, D>
    {
        fn drop(&mut self) 
        {
            if let Some(ctrl) = self.run_control.upgrade()
            {   
                ctrl.store(false, Ordering::SeqCst);

                if let Err(_e) = self.tasks.q_send_blocking(SyCmd::form_stop())
                {

                }

                let join_handle = self.thread.lock().unwrap().take().unwrap();

                let _ = join_handle.join();
            }
        }
    }

    impl<F, D, S> Clone for Syslog<F, D, SyQueued<S, F, D>>
    where F: SyslogFormatter, D: SyslogDestination, S: SyslogQueueChannel<F, D>
    {
        fn clone(&self) -> Self 
        {
            Self 
            { 
                log_items: 
                    self.log_items.clone(), 
                stream: 
                    self.stream.clone(), 
                _p: 
                    PhantomData, 
                _p_not_ss: 
                    PhantomData
            }
        }
    }

    impl<F, D, S> DynSyslog for Syslog<F, D, SyQueued<S, F, D>>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
        S: SyslogQueueChannel<F, D>
    {
        #[inline]
        fn connectlog(&self) -> SyRes<()> 
        {
            self.connectlog()
        }

        #[inline]
        fn setlogmask(&self, logmask: i32) -> SyRes<i32> 
        {
            self.setlogmask(logmask)
        }

        #[inline]
        fn closelog(&self) -> SyRes<()>
        {
            self.closelog()
        }
        
        fn syslog(&self, pri: Priority, fmt: String) -> SyRes<()> 
        {
            self.syslog(pri, fmt.into())
        }
        
        fn change_identity(&self, ident: Option<&str>) -> SyRes<()> 
        {
            self.change_identity(ident)
        }
        
        fn reconnect(&self) -> SyRes<()> 
        {
            self.reconnect()
        }
    }

    impl<F, D, S> Syslog<F, D, SyQueued<S, F, D>>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
        S: SyslogQueueChannel<F, D>
    {
        /// Converts the current instance into dyncamic object. See [DynSyslog] for the interface.
        pub 
        fn make_dyn(self) -> Box<dyn DynSyslog + Send + Sync>
        {
            Box::new(self)
        }

        /// Opens a default connection to the local syslog server with default formatter with
        /// provided generics.
        /// 
        /// # Arguments
        /// 
        /// * `ident` - A program name which will appear on the logs. If none, will be determined
        ///     automatically.
        /// 
        /// * `logstat` - [LogStat] an instance config.
        /// 
        /// * `facility` - [LogFacility] a syslog facility.
        /// 
        /// * `net_tap_prov` - a [SyslogLocal] instance with configuration.
        /// 
        /// # Returns
        /// 
        /// A [SyRes] is returned ([Result]) with: 
        /// 
        /// * [Result::Ok] - with instance
        /// 
        /// * [Result::Err] - with error description.
        pub 
        fn openlog_with(ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap_prov: D) -> SyRes<Self>
        {
            // creating internal syslog struct

            let log_items = 
                LogItems::new(ident, 0xff, logstat, facility);

            let stream = 
                SyslogSocketLockless::<D>::new(logstat, net_tap_prov)?;

            let (inst, sender, run_ctrl) = 
                SyslogInternal::<F, D, S>::new(log_items, stream)?;
            
            
            let thr_name: String = "syslog_queue/0".into();

            // initiate a thread
            let thread_hnd = 
                thread::Builder::new()
                    .name(thr_name.clone())
                    .spawn(move || SyslogInternal::<F, D, S>::thread_worker(inst))
                    .map_err(|e| 
                        map_error!("{} thread spawn failed. {}", thr_name, e)
                    )?;

            let sy_q = 
                SyQueued
                {
                    run_control: 
                        run_ctrl,
                    tasks: 
                        sender,
                    thread: 
                        Arc::new(Mutex::new(Some(thread_hnd))),
                };
            
            return Ok( 
                Self
                {
                    log_items: 
                        (),
                    stream: 
                        sy_q,
                    _p: 
                        PhantomData,
                    _p_not_ss:
                        PhantomData
                }
            );
        }
    }

    impl<F, D, S> Syslog<F, D, SyQueued<S, F, D>>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
        S: SyslogQueueChannel<F, D>
    {
        /// Connects the current instance to the syslog server (destination).
        pub 
        fn connectlog(&self) -> SyRes<()>
        {
            let (sy_cmd, loopback) = 
                SyCmd::form_connectlog();

            self.stream.tasks.q_send_blocking(sy_cmd)?;

            return 
                loopback
                    .recv_once_blocking()?;
        }

        /// Sets the logmask to filter out the syslog calls.
        /// 
        /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
        ///
        /// # Example
        ///
        /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
        ///
        /// or
        ///
        /// ~(LOG_MASK!(Priority::LOG_INFO))
        /// LOG_UPTO!(Priority::LOG_ERROR)
        pub 
        fn setlogmask(&self, logmask: i32) -> SyRes<i32> 
        {
            let (sy_cmd, loopback) = 
                SyCmd::form_logmask(logmask);

            self.stream.tasks.q_send_blocking(sy_cmd)?;

            return 
                loopback
                    .recv_once_blocking();
        }

        /// Closes connection to the syslog server (destination).
        pub 
        fn closelog(&self) -> SyRes<()> 
        {
            let (sy_cmd, loopback) = 
                SyCmd::form_disconnectlog();

            // send stop
            self.stream.tasks.q_send_blocking(sy_cmd)?;

            return 
                loopback
                    .recv_once_blocking()?;
        }

        /// Similar to libc, syslog() sends data to syslog server.
        /// 
        /// # Arguments
        ///
        /// * `pri` - a priority [Priority]
        ///
        /// * `fmt` - a formatter [SyslogFormatter] message. In C exists a functions with
        ///     variable argumets amount. In Rust you should create your
        ///     own macros like format!() or use format!()]. The [String] and ref `'static` 
        ///     [str] can be passed directly.
        /// 
        /// # Returns 
        /// 
        /// A [SyRes] is returned which may describe an error.
        pub 
        fn syslog(&self, pri: Priority, fmt: F) -> SyRes<()>
        {
            // even if the thread is in a process of termination, there is
            // no need to sync access to the run_control field as even if
            // syslog thread will terminate before someone push something on the
            // queue, it will be left in the queue until the end of program's time.
            
            let sy_cmd = SyCmd::form_syslog(pri, fmt);

            return self.stream.tasks.q_send_blocking(sy_cmd);
        }

        /// This function can be used to update the facility name, for example
        /// after fork().
        /// 
        /// # Arguments
        /// 
        /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
        ///     If set to [Option::None] would request the program name from OS.
        pub 
        fn change_identity(&self, ident: Option<&str>) -> SyRes<()>
        {
            let sy_cmd = 
                SyCmd::form_change_ident(ident.map(|v| v.to_string()));

            return 
                self.stream.tasks.q_send_blocking(sy_cmd);
        }

        /// Re-opens the connection to the syslog server. Can be used to 
        /// rotate logs(handle SIGHUP).
        /// 
        /// # Returns
        /// 
        /// A [Result] is retured as [SyRes].
        /// 
        /// * [Result::Ok] - with empty inner type.
        /// 
        /// * [Result::Err] - an error code and description 
        pub 
        fn reconnect(&self) -> SyRes<()>
        {
            return 
                self.stream.tasks.q_send_blocking(SyCmd::form_reconnect());
        }

        /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
        /// the same variant (type) as current.
        pub 
        fn update_tap_data(&self, tap_data: D) -> SyRes<()>
        {
            let (tap_data_cmd, loopback) = SyCmd::form_update_tap(tap_data);

            self.stream.tasks.q_send_blocking(tap_data_cmd)?;

            return 
                loopback
                    .recv_once_blocking()?;
        }
    }

    impl<F, D, S> Syslog<F, D, SyQueued<S, F, D>>
    where 
        F: SyslogFormatter,
        D: SyslogDestination,
        S: SyslogQueueChannel<F, D>
    {
        /// Returns the streamable [SyStream] instance which can be used with [write!].
        /// 
        /// It implements both [std::fmt::Write] and [std::io::Write].
        /// 
        /// # Example
        /// 
        /// ```ignore
        /// let log = 
        ///     SingleSyslog::openlog(
        ///         Some("test1"), 
        ///         LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
        ///         LogFacility::LOG_DAEMON,
        ///         SyslogLocal::new()
        ///     ).unwrap();
        /// 
        /// write!(log.get_stream::<SyStreamPriDebug>(), "test stream singlesyslog {}", i).unwrap();
        /// ```
        pub 
        fn get_stream<'t, PRI>(&'t self) -> SyStream<'t, PRI, D, F, &'t Self>
        where PRI: SyStreamPri
        {
            SyStream
            {
                s: Some(self),
                _p: PhantomData,
                _p1: PhantomData,
                _p2: PhantomData
            }
        }
    }

    impl<F, D, S> SyStreamSyslogApi<F, D>  
    for &Syslog<F, D, SyQueued<S, F, D>>
    where F: SyslogFormatter, D: SyslogDestination, S: SyslogQueueChannel<F, D>
    {
        type SYSLOG<'t> = &'t Syslog<F, D, SyQueued<S, F, D>>;

        fn syslog<'t>(syslog: Self::SYSLOG<'t>, pri: Priority, fmt: F) -> SyRes<()>
        {
            syslog.syslog(pri, fmt)
        }
    }


    /// A queued implementation for ASYNC.
    #[cfg(feature = "async_enabled")]
    pub mod syslog_async_queue
    {
        use crate::error::SyRes;
        use crate::sy_sync_queue::{SyCmd, SyslogQueueChanSnd, SyslogQueueChannel, SyslogQueueOneChanRcv};
        use crate::{Priority, Syslog};
        use crate::{formatters::SyslogFormatter, SyslogDestination, SyQueued};
        use crate::a_sync::syslog_trait::AsyncSyslogQueueApi;

        impl<F: SyslogFormatter, D: SyslogDestination, S: SyslogQueueChannel<F, D>> AsyncSyslogQueueApi<F, D> 
        for Syslog<F, D, SyQueued<S, F, D>>
        {
            async 
            fn a_connectlog(&mut self) -> SyRes<()> 
            {
                let (sy_cmd, loopback) = 
                    SyCmd::form_connectlog();

                self.stream.tasks.q_send(sy_cmd).await?;

                return 
                    loopback
                        .recv_once()
                        .await?;
            }

            /// Sets the logmask to filter out the syslog calls. This function behaves 
            /// differently as it behaves in syslog_sync.rs or syslog_async.rs.
            /// It may return an error if: syslog thread had exit and some thread calls
            /// this function. Or something happened with channel. 
            /// This function blocks until the previous mask is received.
            /// 
            /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
            ///
            /// # Example
            ///
            /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
            ///
            /// or
            ///
            /// ~(LOG_MASK!(Priority::LOG_INFO))
            /// LOG_UPTO!(Priority::LOG_ERROR) 
            async 
            fn a_setlogmask(&self, logmask: i32) -> SyRes<i32>
            {
                let (sy_cmd, loopback) = 
                    SyCmd::form_logmask(logmask);

                self.stream.tasks.q_send(sy_cmd).await?;

                return 
                    loopback
                        .recv_once()
                        .await;
            }

            /// Closes connection to the syslog server
            async 
            fn a_closelog(&self) -> SyRes<()>
            {
                let (sy_cmd, loopback) = 
                    SyCmd::form_disconnectlog();

                // send stop
                self.stream.tasks.q_send(sy_cmd).await?;

                return 
                    loopback
                        .recv_once()
                        .await?;
            }

            /// Similar to libc, syslog() sends data to syslog server, but asynchroniously.
            /// 
            /// # Arguments
            ///
            /// * `pri` - a priority [Priority]
            ///
            /// * `fmt` - a program's message to be sent as payload. The message is encoded with the
            ///     [SyslogFormatter] and may be different for different formatters.
            #[inline]
            async 
            fn a_syslog(&self, pri: Priority, fmt: F) -> SyRes<()>
            {
                let sy_cmd = SyCmd::form_syslog(pri, fmt);

                return self.stream.tasks.q_send(sy_cmd).await;
            }


            /// Performs the reconnection to the syslog server or file re-open.
            /// 
            /// # Returns
            /// 
            /// A [Result] is retured as [SyRes].
            /// 
            /// * [Result::Ok] - with empty inner type.
            /// 
            /// * [Result::Err] - an error code and description
            async 
            fn a_reconnect(&self) -> SyRes<()>
            {
                return 
                    self.stream.tasks.q_send(SyCmd::form_reconnect()).await;
            }

            async 
            fn a_change_identity(&self, ident: &str) -> SyRes<()> 
            {
                let sy_cmd = 
                    SyCmd::form_change_ident(Some(ident.to_string()));

                return 
                    self.stream.tasks.q_send(sy_cmd).await;
            }

            /// Updates the inner instance destionation i.e path to file
            /// or server address. The type of destination can not be changed.
            /// 
            /// This function disconnects from syslog server if previously was 
            /// connected (and reconnects if was connected previously).
            /// 
            /// # Arguments 
            /// 
            /// * `new_tap` - a consumed instance of type `D` [SyslogDestination]
            /// 
            /// # Returns 
            /// 
            /// A [SyRes] is returned. An error may be returned if:
            /// 
            /// * connection to server was failed
            /// 
            /// * incorrect type
            /// 
            /// * disconnect frm server failed
            async 
            fn a_update_tap_data(&self, new_tap: D) -> SyRes<()>
            {
                let (tap_data_cmd, loopback) = SyCmd::form_update_tap(new_tap);

                self.stream.tasks.q_send(tap_data_cmd).await?;

                return 
                    loopback
                        .recv_once()
                        .await?;
            }
        }
    }
}

#[cfg(feature = "build_with_queue")]
pub use self::build_with_queue::SyQueued;