mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
Compare commits
965 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb3220622e | ||
|
|
8f26e819ad | ||
|
|
b6b1c2f95d | ||
|
|
9ce9804a88 | ||
|
|
ddaa61ca9a | ||
|
|
4646bd082a | ||
|
|
53aca9c3d0 | ||
|
|
aa1e794213 | ||
|
|
45e3b678b0 | ||
|
|
bd99496423 | ||
|
|
e471ec884e | ||
|
|
b400705a1c | ||
|
|
cb35191fc1 | ||
|
|
1945a93b33 | ||
|
|
dfd12e6dac | ||
|
|
ba29e1d75d | ||
|
|
8f6d123586 | ||
|
|
d368ed586c | ||
|
|
87095a9f1f | ||
|
|
dd6d203488 | ||
|
|
f463ebf54a | ||
|
|
3547d7e24f | ||
|
|
a9c01aba78 | ||
|
|
f237947bdc | ||
|
|
890df3d90b | ||
|
|
14783585b6 | ||
|
|
243c4beac7 | ||
|
|
fe9cb54e0d | ||
|
|
dabec32748 | ||
|
|
6faa5fc95b | ||
|
|
dbbec6cdb4 | ||
|
|
43923cf038 | ||
|
|
2ccba49b01 | ||
|
|
362fdc6ceb | ||
|
|
7bb53541e4 | ||
|
|
c07b3aeef9 | ||
|
|
fb47935a7b | ||
|
|
ec3538c2ee | ||
|
|
84e15d1ee2 | ||
|
|
5b4c4f3f77 | ||
|
|
aecdfc60a0 | ||
|
|
816ede3a17 | ||
|
|
353c79ca71 | ||
|
|
e93115f436 | ||
|
|
197c9639bb | ||
|
|
a358a38b84 | ||
|
|
16d76e2293 | ||
|
|
536e583cbe | ||
|
|
9049f9aeb9 | ||
|
|
bee3e63e1b | ||
|
|
d8f13cbd5b | ||
|
|
0f39da5490 | ||
|
|
28fef35a12 | ||
|
|
1344d44a5a | ||
|
|
61ed2a670e | ||
|
|
db1bc035f7 | ||
|
|
8de6cdaa82 | ||
|
|
fe1a4f5fb6 | ||
|
|
d38f89cae8 | ||
|
|
9c90fe8264 | ||
|
|
b85a666f72 | ||
|
|
5ba95f6816 | ||
|
|
dc38b7c3c4 | ||
|
|
6484b03dd9 | ||
|
|
29235d9b4b | ||
|
|
4b3687f1a6 | ||
|
|
e7e8b75a4c | ||
|
|
e98265a49b | ||
|
|
e87f69bdb6 | ||
|
|
70d2832c0d | ||
|
|
7636f1f659 | ||
|
|
1523c83650 | ||
|
|
a6987efaec | ||
|
|
6491abb519 | ||
|
|
8faabb4e3a | ||
|
|
2838c2c8a5 | ||
|
|
3315bad009 | ||
|
|
3eeced78b5 | ||
|
|
c23430b438 | ||
|
|
0e49bfff51 | ||
|
|
3ed40d04a9 | ||
|
|
70b36aa55d | ||
|
|
0f83b33d4f | ||
|
|
b83106bed4 | ||
|
|
21413e599a | ||
|
|
5f4cc7b036 | ||
|
|
9aa26fb969 | ||
|
|
7f74012a0d | ||
|
|
96ebef093f | ||
|
|
a19f4bba0c | ||
|
|
c24b957e17 | ||
|
|
5ba2f77230 | ||
|
|
a09f490804 | ||
|
|
082d6fbea9 | ||
|
|
37372960a8 | ||
|
|
0035a0c98d | ||
|
|
036cc5d575 | ||
|
|
14950926ed | ||
|
|
baa3b1a07e | ||
|
|
2a09f66a44 | ||
|
|
e50b62c770 | ||
|
|
13d8b0f17f | ||
|
|
9e0c658b29 | ||
|
|
74fec56927 | ||
|
|
514f304a47 | ||
|
|
7f85a5c988 | ||
|
|
14d626d961 | ||
|
|
7560cacb3f | ||
|
|
3cd9bcdab9 | ||
|
|
32f1efdc99 | ||
|
|
bcc9f03457 | ||
|
|
4c845bf02b | ||
|
|
c727864393 | ||
|
|
4548573a75 | ||
|
|
385246730d | ||
|
|
e06d21a4c0 | ||
|
|
c132d2ae8c | ||
|
|
ece31100e0 | ||
|
|
ce4e1ac54b | ||
|
|
ef61fb11f0 | ||
|
|
42f2b11ec8 | ||
|
|
ac87cbb0d1 | ||
|
|
c65de3d689 | ||
|
|
1433fa4209 | ||
|
|
d51149e5ac | ||
|
|
ffd813435a | ||
|
|
d75fd2c7f9 | ||
|
|
cdad84aa46 | ||
|
|
0fdb545d8c | ||
|
|
a5f5ff70e0 | ||
|
|
4f0e320236 | ||
|
|
68aed6a5eb | ||
|
|
6811112208 | ||
|
|
9ebc4b24d9 | ||
|
|
b990080a52 | ||
|
|
efbe3e4d57 | ||
|
|
7b14a65b2b | ||
|
|
5887744d8b | ||
|
|
8bf718671a | ||
|
|
c858b14c03 | ||
|
|
12df172575 | ||
|
|
7fa751d36e | ||
|
|
7a7611e977 | ||
|
|
ec8763adf2 | ||
|
|
f2d1d573f5 | ||
|
|
a530b87fd0 | ||
|
|
6c21789aed | ||
|
|
616866fcf4 | ||
|
|
faf06bcfe5 | ||
|
|
cd376a5c43 | ||
|
|
6ba5ab6d67 | ||
|
|
1bee3218b4 | ||
|
|
802eaadd2d | ||
|
|
ee22eed23d | ||
|
|
ab72de5f7a | ||
|
|
a32cea24fd | ||
|
|
af0d805be4 | ||
|
|
181c22f798 | ||
|
|
87133ef6b7 | ||
|
|
1ef2f014ee | ||
|
|
0a92d1d684 | ||
|
|
ff5221b693 | ||
|
|
db484cc4b8 | ||
|
|
6442963f49 | ||
|
|
0f7b95ce47 | ||
|
|
632a2e0894 | ||
|
|
e9635c7b2d | ||
|
|
8e3b1338a5 | ||
|
|
9d3dde0900 | ||
|
|
c5abaeddca | ||
|
|
ca2cd6f3e7 | ||
|
|
7d07e0312a | ||
|
|
e1c73fd8f4 | ||
|
|
b83ab21283 | ||
|
|
8001156ca8 | ||
|
|
57e31f0a58 | ||
|
|
51fadf6b7e | ||
|
|
2a6a8aa0a0 | ||
|
|
aa264a7fb2 | ||
|
|
5e35c2b6ab | ||
|
|
0385372314 | ||
|
|
efbff95ec7 | ||
|
|
2a9edb2153 | ||
|
|
be14e60d9e | ||
|
|
ef4641cad7 | ||
|
|
100f30043f | ||
|
|
1574b5b0a2 | ||
|
|
012fe99ab1 | ||
|
|
8ff5a3e096 | ||
|
|
65317eb019 | ||
|
|
ac3e26b0ff | ||
|
|
e86f450428 | ||
|
|
7b2776fdc7 | ||
|
|
2a16d1d230 | ||
|
|
53e1c9ab11 | ||
|
|
410abc4626 | ||
|
|
a2e28443f0 | ||
|
|
c1af0a3f21 | ||
|
|
bb5e1ee2f9 | ||
|
|
3aee89c8fd | ||
|
|
44e1f9f682 | ||
|
|
37d76b961c | ||
|
|
1305663d99 | ||
|
|
8f4efe57a2 | ||
|
|
0613dbc4a2 | ||
|
|
0ed0d69368 | ||
|
|
2ffbbee1f6 | ||
|
|
b9d2f2537b | ||
|
|
69dc173979 | ||
|
|
ded8b5ebd4 | ||
|
|
ed58ae9f98 | ||
|
|
f7f790b4b3 | ||
|
|
fe74c80992 | ||
|
|
fa659bf7ad | ||
|
|
9b41649601 | ||
|
|
1b3438f5a5 | ||
|
|
3eed64e5c4 | ||
|
|
0fac33781d | ||
|
|
3135b6a33d | ||
|
|
2686ae2322 | ||
|
|
a709e29586 | ||
|
|
dd46579cb4 | ||
|
|
f4b7210e7b | ||
|
|
05a0b0d7b0 | ||
|
|
c1f4d7506a | ||
|
|
b6ba0be550 | ||
|
|
23dfb4e2f9 | ||
|
|
7a10e31982 | ||
|
|
de89c4fd01 | ||
|
|
5d4956d34b | ||
|
|
42c5eb59c9 | ||
|
|
09cc6e7754 | ||
|
|
4a5bc41e89 | ||
|
|
0ade18828d | ||
|
|
91046e6ca4 | ||
|
|
17e1ba8ae2 | ||
|
|
c47c854f15 | ||
|
|
e931866b35 | ||
|
|
7828a065bf | ||
|
|
3e689e700e | ||
|
|
a9964afcf7 | ||
|
|
95c19876c6 | ||
|
|
5efccfa5e2 | ||
|
|
89e737a258 | ||
|
|
8fbc853b0d | ||
|
|
2e008b319c | ||
|
|
ff6e3c95f2 | ||
|
|
7e9385405f | ||
|
|
592ea36a86 | ||
|
|
e059ebf99d | ||
|
|
609480ed78 | ||
|
|
4271185936 | ||
|
|
aacae62591 | ||
|
|
47cbf3828d | ||
|
|
46d418164d | ||
|
|
ede8d84884 | ||
|
|
53d223b45f | ||
|
|
ac35dd5a6f | ||
|
|
9e19012cb0 | ||
|
|
1234cda3b3 | ||
|
|
710a0e3a45 | ||
|
|
b7f24b2456 | ||
|
|
fc594b551a | ||
|
|
f39ccccc0c | ||
|
|
f0a4ddd78b | ||
|
|
c691769e46 | ||
|
|
19dc30567e | ||
|
|
a453bccff0 | ||
|
|
aa2053a575 | ||
|
|
3d8f71c4d2 | ||
|
|
6aaaabbc4d | ||
|
|
42c36f48ed | ||
|
|
a5f4139102 | ||
|
|
030d85a9b3 | ||
|
|
adcfb7fb55 | ||
|
|
cec365888a | ||
|
|
55bfa8dd11 | ||
|
|
e99759fe45 | ||
|
|
17c6e6ee3f | ||
|
|
7fff900a1a | ||
|
|
c67974e4c8 | ||
|
|
a36696e02e | ||
|
|
9b80ca6c41 | ||
|
|
22f514aabf | ||
|
|
211478e13e | ||
|
|
5e33a7e58b | ||
|
|
b2e31721e8 | ||
|
|
de0dbfa359 | ||
|
|
f93459579f | ||
|
|
2b81c40b90 | ||
|
|
233e97c5e4 | ||
|
|
fc1ce48dc7 | ||
|
|
fd5562eebe | ||
|
|
0695d9cb5f | ||
|
|
456b24134d | ||
|
|
f8ba24afee | ||
|
|
eebb921c9f | ||
|
|
e17ee87f38 | ||
|
|
18e3f07f7d | ||
|
|
9ce39a470f | ||
|
|
23572369fc | ||
|
|
01b350de96 | ||
|
|
365e470a32 | ||
|
|
a42b40656c | ||
|
|
40160f2a57 | ||
|
|
90b33b1552 | ||
|
|
5567ed01e5 | ||
|
|
cbe9448650 | ||
|
|
5b345534dc | ||
|
|
c8dc318fb3 | ||
|
|
23cb1a1080 | ||
|
|
21cf8d7d3c | ||
|
|
3cf4d34094 | ||
|
|
16d78ae5db | ||
|
|
62b4b7af83 | ||
|
|
9799ecac6a | ||
|
|
dccb766095 | ||
|
|
c97983a91c | ||
|
|
680fb07fd5 | ||
|
|
cfd0ea197c | ||
|
|
48d4ed9bc0 | ||
|
|
3bed78356e | ||
|
|
8923922f30 | ||
|
|
7542e42e4f | ||
|
|
7a9b23e4f4 | ||
|
|
47253ba2a1 | ||
|
|
69b54dd9e4 | ||
|
|
36138617fc | ||
|
|
231ca50700 | ||
|
|
c7613f3e91 | ||
|
|
05d5546eb1 | ||
|
|
cefe67726e | ||
|
|
1ac2dcc537 | ||
|
|
3a68eecb28 | ||
|
|
54a8259b42 | ||
|
|
32b6f1619f | ||
|
|
99b8c5d379 | ||
|
|
5deb7c55e1 | ||
|
|
9cd25dd216 | ||
|
|
4a9ccf7e38 | ||
|
|
2963b9f07f | ||
|
|
a4a9bc4d8e | ||
|
|
a16a029790 | ||
|
|
97fea81599 | ||
|
|
ccffb6ecd6 | ||
|
|
63b5a1a4d8 | ||
|
|
dfc777803a | ||
|
|
934a9bb23e | ||
|
|
7097f7a894 | ||
|
|
537fd7c4ba | ||
|
|
d5048b8b0c | ||
|
|
0348556aac | ||
|
|
d6329b9dce | ||
|
|
34f3d29d93 | ||
|
|
cd701761f9 | ||
|
|
23c2c00d69 | ||
|
|
fa501b46cf | ||
|
|
38dc0a5c5d | ||
|
|
685cc4edbc | ||
|
|
78369375e3 | ||
|
|
6587058f74 | ||
|
|
30f738e49a | ||
|
|
726ca01e5c | ||
|
|
83b40b8cda | ||
|
|
db0d0438ff | ||
|
|
3c527488e7 | ||
|
|
99abcf6ded | ||
|
|
7009727559 | ||
|
|
ae02fba141 | ||
|
|
76cdeb62e3 | ||
|
|
ae9627c64c | ||
|
|
58875bdcd7 | ||
|
|
616caa5d30 | ||
|
|
8236ee3ff6 | ||
|
|
19f2804661 | ||
|
|
c62ba5f48d | ||
|
|
22bee8128a | ||
|
|
39150eb8c7 | ||
|
|
1b14fa53ef | ||
|
|
cf55e5d4f8 | ||
|
|
1a1ea028f6 | ||
|
|
814c3445a3 | ||
|
|
075dcee042 | ||
|
|
fe97a03033 | ||
|
|
f593aad786 | ||
|
|
4a8c602a59 | ||
|
|
7143dbc46a | ||
|
|
e69699e12c | ||
|
|
d6dbdbf27a | ||
|
|
a0dae55a69 | ||
|
|
7f15fb2a21 | ||
|
|
d5aa8db36f | ||
|
|
357b6c9d8c | ||
|
|
b0c4794305 | ||
|
|
071206ef59 | ||
|
|
63ab8e6341 | ||
|
|
741b0d6e82 | ||
|
|
3041faffab | ||
|
|
30ee690401 | ||
|
|
22a169bc31 | ||
|
|
ac19803d03 | ||
|
|
95485ee89b | ||
|
|
bc61f69058 | ||
|
|
0b86d6a451 | ||
|
|
0317731dc9 | ||
|
|
3dedb52163 | ||
|
|
ad393b83a2 | ||
|
|
01dac453db | ||
|
|
03abdf49a0 | ||
|
|
83b9149930 | ||
|
|
5ca5cbd447 | ||
|
|
597e89efe3 | ||
|
|
683e44f5f8 | ||
|
|
0b36d4e360 | ||
|
|
67606e2460 | ||
|
|
b6c6b30c0d | ||
|
|
e5935f0ced | ||
|
|
75c15e8028 | ||
|
|
4831ae17d9 | ||
|
|
22655d7554 | ||
|
|
ff0e430e46 | ||
|
|
e86dc8c338 | ||
|
|
0814de6371 | ||
|
|
34bc6907d0 | ||
|
|
a6dd1a2b4b | ||
|
|
4fe5d3d5e3 | ||
|
|
937fe7e909 | ||
|
|
89ab1e679d | ||
|
|
559984b2fe | ||
|
|
3ac9540351 | ||
|
|
d5709c9d70 | ||
|
|
891cc95add | ||
|
|
0246b5657a | ||
|
|
26ca1fb9f3 | ||
|
|
6b4355b76f | ||
|
|
90bd9692f5 | ||
|
|
8878185628 | ||
|
|
15066d1d37 | ||
|
|
abaae6e28b | ||
|
|
2170de8819 | ||
|
|
300ec667f6 | ||
|
|
348c4380d6 | ||
|
|
98388d18de | ||
|
|
bbc5753b96 | ||
|
|
564eecaa3b | ||
|
|
0480920058 | ||
|
|
d977dd4395 | ||
|
|
ca402379a9 | ||
|
|
903bf2135d | ||
|
|
1f732585b2 | ||
|
|
224de0601e | ||
|
|
9b84337830 | ||
|
|
a4665c27df | ||
|
|
3337015346 | ||
|
|
42c466296a | ||
|
|
a9fcf9db47 | ||
|
|
f3b55fcab0 | ||
|
|
b56b6509b1 | ||
|
|
ac85e383a9 | ||
|
|
2b326e90b8 | ||
|
|
3e8be645d2 | ||
|
|
81444265f4 | ||
|
|
5716ab70ec | ||
|
|
faaef7686d | ||
|
|
9e6f5b6b2d | ||
|
|
d28465bf60 | ||
|
|
867fec260b | ||
|
|
f5309d902a | ||
|
|
82823e50dd | ||
|
|
394f79e9d3 | ||
|
|
595bbbd3e4 | ||
|
|
2127572c33 | ||
|
|
7d6c927684 | ||
|
|
9d2d4c82df | ||
|
|
515ce9bebb | ||
|
|
7698bb0ae1 | ||
|
|
c89a5148b2 | ||
|
|
c37adba77b | ||
|
|
95cc3dec3f | ||
|
|
42c4a91041 | ||
|
|
d253dad2ee | ||
|
|
7f0265e674 | ||
|
|
b9726ba66d | ||
|
|
b20ffa7369 | ||
|
|
854abdf5e6 | ||
|
|
d0fc8a572c | ||
|
|
8bc1ca0e44 | ||
|
|
d38bd138cd | ||
|
|
7766bc25d1 | ||
|
|
34244656a6 | ||
|
|
619849c793 | ||
|
|
927b2b3942 | ||
|
|
76389e057f | ||
|
|
0a14e491ab | ||
|
|
1f7f1c1ffb | ||
|
|
6440733002 | ||
|
|
02802af97f | ||
|
|
a8169a3d6b | ||
|
|
9ba7fc94a5 | ||
|
|
48b71a02d7 | ||
|
|
18ed04b990 | ||
|
|
d09e03606c | ||
|
|
b2017f5653 | ||
|
|
c16eb80d7f | ||
|
|
0c56f98a92 | ||
|
|
490940cd53 | ||
|
|
92d27b0aa3 | ||
|
|
ca9c83f824 | ||
|
|
fc900e2432 | ||
|
|
e3257e56ab | ||
|
|
7d2337c6eb | ||
|
|
58629f1fea | ||
|
|
132ec0a5fc | ||
|
|
c2b47430fb | ||
|
|
7906592230 | ||
|
|
f57378d8ba | ||
|
|
1ccdc225af | ||
|
|
3e4df86ac0 | ||
|
|
7054cf7a35 | ||
|
|
2a7fc9e30e | ||
|
|
f1b4f15dbb | ||
|
|
c98152e9d0 | ||
|
|
7c34859e0c | ||
|
|
dd38e096b2 | ||
|
|
ea89efbed7 | ||
|
|
61408a0f29 | ||
|
|
cca004efe4 | ||
|
|
da3f3da92c | ||
|
|
f0c35819bd | ||
|
|
ff616002cf | ||
|
|
e1c79869b6 | ||
|
|
bd43403f5a | ||
|
|
d3997cc4d1 | ||
|
|
5b0b8579b2 | ||
|
|
c927de137c | ||
|
|
eb23d505f8 | ||
|
|
2400cf16a4 | ||
|
|
bbe3ace533 | ||
|
|
3b87eb3d08 | ||
|
|
d43a17304e | ||
|
|
21d41b8e81 | ||
|
|
332eaaf916 | ||
|
|
0a5ada6411 | ||
|
|
963f8d3485 | ||
|
|
60a8c5f1c9 | ||
|
|
d1a1024465 | ||
|
|
0d7ff9ac47 | ||
|
|
752d5685dc | ||
|
|
c6c517431f | ||
|
|
ee54f54ced | ||
|
|
2c5c96e159 | ||
|
|
a10763138e | ||
|
|
208eb0ca07 | ||
|
|
f0403fa9e4 | ||
|
|
3f86b250e6 | ||
|
|
d1819f5f76 | ||
|
|
19c7e63858 | ||
|
|
7efdcc26fe | ||
|
|
3ab3970dd2 | ||
|
|
5ab487dbae | ||
|
|
55fbc2c78e | ||
|
|
643426e2b2 | ||
|
|
f31a834613 | ||
|
|
683080be53 | ||
|
|
d14b8a9ad6 | ||
|
|
0f87ba6c93 | ||
|
|
02bfa0898c | ||
|
|
f5313f92f1 | ||
|
|
e41a258b93 | ||
|
|
ffa85cda1a | ||
|
|
0123d41647 | ||
|
|
2b0481deed | ||
|
|
1389f86675 | ||
|
|
cf6bb88af2 | ||
|
|
8e19a267bd | ||
|
|
3b55709e7f | ||
|
|
f78bca4ad8 | ||
|
|
713feca582 | ||
|
|
26c20ed91d | ||
|
|
e399249f31 | ||
|
|
b4a1b4b59a | ||
|
|
b309a88bea | ||
|
|
e88bee49a6 | ||
|
|
ec12770693 | ||
|
|
5b3a18319e | ||
|
|
3b73278348 | ||
|
|
0ca2cb625e | ||
|
|
67561f97ec | ||
|
|
b667bae65d | ||
|
|
54be9bd8b9 | ||
|
|
06d0299639 | ||
|
|
84851e230f | ||
|
|
52aed9e0de | ||
|
|
ead9a550fd | ||
|
|
cf80b492a3 | ||
|
|
69f3d2678e | ||
|
|
8038bc2fc8 | ||
|
|
f20b12cf3f | ||
|
|
c8bd53509c | ||
|
|
006124d816 | ||
|
|
efd73ac956 | ||
|
|
b7d7334451 | ||
|
|
8284865f9a | ||
|
|
1f8b04cbd1 | ||
|
|
b3402a0b9f | ||
|
|
4037959945 | ||
|
|
d7313a3274 | ||
|
|
8302086942 | ||
|
|
6095db951b | ||
|
|
817d2764b6 | ||
|
|
62189602cb | ||
|
|
0120dcc787 | ||
|
|
6bfc0ec3a7 | ||
|
|
f999d879d5 | ||
|
|
c861e2d9cf | ||
|
|
e696978d11 | ||
|
|
fbf2e942a9 | ||
|
|
d18f282938 | ||
|
|
c10be7eaec | ||
|
|
05ecad4263 | ||
|
|
4cdb159ccb | ||
|
|
fccb25586f | ||
|
|
ab2f3307eb | ||
|
|
db26a103d6 | ||
|
|
32902f79ad | ||
|
|
fab33dd230 | ||
|
|
daaa025356 | ||
|
|
ffe272c165 | ||
|
|
6e763d2776 | ||
|
|
c71b433a35 | ||
|
|
0b91d55269 | ||
|
|
9f41903067 | ||
|
|
64de8807e2 | ||
|
|
3848cbe24a | ||
|
|
15ac7b08f7 | ||
|
|
e47ecc1828 | ||
|
|
c09641cf47 | ||
|
|
92467db591 | ||
|
|
ea5e7182ab | ||
|
|
d38d53d9dd | ||
|
|
c9e094d9fc | ||
|
|
af75985ec6 | ||
|
|
4b7c05903b | ||
|
|
695912c7cf | ||
|
|
5c06306ccc | ||
|
|
d4fd17f64f | ||
|
|
76d94e69ae | ||
|
|
0f42744f5c | ||
|
|
e8daf7c73b | ||
|
|
0cf1af5bbf | ||
|
|
a343328a21 | ||
|
|
53a56b82af | ||
|
|
64dd4dc219 | ||
|
|
9e9da42c64 | ||
|
|
5c410f4ca2 | ||
|
|
0778211116 | ||
|
|
574563d711 | ||
|
|
e9d0b424d5 | ||
|
|
eef981e05f | ||
|
|
9f24f4bc69 | ||
|
|
5da9818676 | ||
|
|
ff59b07986 | ||
|
|
1b6d4fd277 | ||
|
|
7b19890deb | ||
|
|
5370443ece | ||
|
|
ad4fb1cf84 | ||
|
|
7f8169f0da | ||
|
|
66e8652862 | ||
|
|
05cbdbc1ef | ||
|
|
38584a1fca | ||
|
|
d96d8c49ac | ||
|
|
4bb623a0a3 | ||
|
|
3aa94a0997 | ||
|
|
ccad4ae04f | ||
|
|
346b9ae5a1 | ||
|
|
12f36debae | ||
|
|
87acec6a91 | ||
|
|
58a5e654f9 | ||
|
|
e278953191 | ||
|
|
573ddf8aec | ||
|
|
4f32243214 | ||
|
|
601bdfb1b4 | ||
|
|
90454a93b2 | ||
|
|
640921cd3f | ||
|
|
fccee959b1 | ||
|
|
67a8ecf2bf | ||
|
|
d8701890b2 | ||
|
|
2435f46d06 | ||
|
|
4bece787c8 | ||
|
|
d4ce938679 | ||
|
|
033fe9f133 | ||
|
|
25b10dc264 | ||
|
|
a9c3630d1b | ||
|
|
f3d99f41d4 | ||
|
|
fdb46b857f | ||
|
|
8d06df9775 | ||
|
|
a8d6e60ec6 | ||
|
|
4e643fa42c | ||
|
|
eb234bbf91 | ||
|
|
db1a221427 | ||
|
|
5378f35239 | ||
|
|
622f5eb967 | ||
|
|
966d827d35 | ||
|
|
bed56d3e52 | ||
|
|
24173d5ebc | ||
|
|
60853b5e54 | ||
|
|
da2ff552c5 | ||
|
|
742df52236 | ||
|
|
1b4621962f | ||
|
|
0a36828ff3 | ||
|
|
85ea4297b9 | ||
|
|
34cc3419fa | ||
|
|
46fcd2e844 | ||
|
|
23f0cdf901 | ||
|
|
26bdf66659 | ||
|
|
cf6f1dd01e | ||
|
|
286eb59081 | ||
|
|
40bb28e9b6 | ||
|
|
aac085a9be | ||
|
|
58e68901c7 | ||
|
|
8e69c6e492 | ||
|
|
4d98a14cb1 | ||
|
|
5bf99dfd61 | ||
|
|
bc42415ceb | ||
|
|
284e6a80ac | ||
|
|
0243882238 | ||
|
|
877eee408e | ||
|
|
8dd54de326 | ||
|
|
09d729bfba | ||
|
|
9caaca742e | ||
|
|
ac95c3ffbf | ||
|
|
9715d80030 | ||
|
|
a0a1e5c078 | ||
|
|
d7ba1fdd3d | ||
|
|
2544fca519 | ||
|
|
0b55e2c332 | ||
|
|
b105046202 | ||
|
|
de20255c71 | ||
|
|
1a1c37db7c | ||
|
|
a87700a28c | ||
|
|
1f8e9ad0fc | ||
|
|
e13e978af4 | ||
|
|
28e334c728 | ||
|
|
15a9427112 | ||
|
|
010b0e1d75 | ||
|
|
cd5ddca00d | ||
|
|
f18e1fccfd | ||
|
|
773b8c5a54 | ||
|
|
fc3d18ed64 | ||
|
|
68ed281461 | ||
|
|
65ada37399 | ||
|
|
9f539d7028 | ||
|
|
c73a5ff918 | ||
|
|
9858d4e918 | ||
|
|
0dfb1d264e | ||
|
|
a056b9115b | ||
|
|
62ecc04212 | ||
|
|
4a0f4fc186 | ||
|
|
3a61dcd360 | ||
|
|
04d0240f8d | ||
|
|
13ebfc0779 | ||
|
|
d70d5aa9d8 | ||
|
|
70d3c2cd3e | ||
|
|
6fbe0dec2c | ||
|
|
9d3591dcd5 | ||
|
|
8992f36fbf | ||
|
|
3d203aa7c4 | ||
|
|
cd8d7e6de9 | ||
|
|
5d4e6f17ee | ||
|
|
49f707ec93 | ||
|
|
6a305df46d | ||
|
|
35e9482574 | ||
|
|
dac61d4e9c | ||
|
|
d52e825bbc | ||
|
|
4fa463dff6 | ||
|
|
ebaa16f403 | ||
|
|
175741ed1d | ||
|
|
8d9d9899b7 | ||
|
|
cff7448fb2 | ||
|
|
0f8f510ebb | ||
|
|
3812c22f86 | ||
|
|
2b3000dddc | ||
|
|
b278baf94e | ||
|
|
4119b72d50 | ||
|
|
da2c15ecb4 | ||
|
|
25a702fc22 | ||
|
|
ab178057db | ||
|
|
c44cf5a720 | ||
|
|
98ca01bf2d | ||
|
|
d0ed873ab6 | ||
|
|
0f24399887 | ||
|
|
abbbda6f74 | ||
|
|
1a5ee7ab83 | ||
|
|
3a258ee5c9 | ||
|
|
4d41fdf0fc | ||
|
|
1586c4b0c7 | ||
|
|
9198e97401 | ||
|
|
346267c82f | ||
|
|
529f72325f | ||
|
|
1cf1209586 | ||
|
|
36774529a4 | ||
|
|
27dcb1008c | ||
|
|
e8a9c7b13e | ||
|
|
7be59851d5 | ||
|
|
15cf9ec365 | ||
|
|
c44c904161 | ||
|
|
9e3d8d1650 | ||
|
|
7b9668fe01 | ||
|
|
2334c48e02 | ||
|
|
afb949a417 | ||
|
|
c9bb85c91d | ||
|
|
13e1667d61 | ||
|
|
3c106c9cec | ||
|
|
1988668d10 | ||
|
|
484d7f91e5 | ||
|
|
53d58f222f | ||
|
|
d5a72b1eaf | ||
|
|
6b5ebab6ae | ||
|
|
8107df08a8 | ||
|
|
dc29500931 | ||
|
|
31fc1aca53 | ||
|
|
0db4b04ad3 | ||
|
|
1aa9ea92e2 | ||
|
|
2698f54a9c | ||
|
|
6f977248bf | ||
|
|
046fd62dc4 | ||
|
|
da60dda2dd | ||
|
|
d25fb08a75 | ||
|
|
79e105243c | ||
|
|
2d4e531ac9 | ||
|
|
52403ad9ed | ||
|
|
2d264855cc | ||
|
|
c172c72be9 | ||
|
|
79259fdb3f | ||
|
|
cee35f7d24 | ||
|
|
1f5f17622e | ||
|
|
e8f7f80f2b | ||
|
|
6db8beeade | ||
|
|
4f66313440 | ||
|
|
89b5bcfdc7 | ||
|
|
26f706ebe3 | ||
|
|
0cb38085a1 | ||
|
|
cff6644b28 | ||
|
|
63837530ed | ||
|
|
62e09e73f7 | ||
|
|
daef0a2374 | ||
|
|
042045b998 | ||
|
|
bad7284465 | ||
|
|
6f0cb6365e | ||
|
|
653ec05c0e | ||
|
|
be2a751513 | ||
|
|
840adfbbcf | ||
|
|
acf32be842 | ||
|
|
3999613eca | ||
|
|
bff85725d2 | ||
|
|
93008b2369 | ||
|
|
be336e7514 | ||
|
|
255f7f2dee | ||
|
|
de2c07ac62 | ||
|
|
844d54d7e6 | ||
|
|
ff3e6c7248 | ||
|
|
408a2229d6 | ||
|
|
7cdd65075c | ||
|
|
436ce16e79 | ||
|
|
58320e2678 | ||
|
|
a6f7edf94b | ||
|
|
4a4f13be46 | ||
|
|
a13b0abb7d | ||
|
|
c081919320 | ||
|
|
21f7f78130 | ||
|
|
bb1b24c178 | ||
|
|
3f30000088 | ||
|
|
e6ce39f76e | ||
|
|
10116b7717 | ||
|
|
18edb8bd63 | ||
|
|
dae1aeb1f7 | ||
|
|
57085c892f | ||
|
|
d67efb2cab | ||
|
|
0e09ecbaa5 | ||
|
|
e3699070a4 | ||
|
|
bf40855825 | ||
|
|
3ee4f2810d | ||
|
|
79468cf676 | ||
|
|
4037942a26 | ||
|
|
cae6c9ab36 | ||
|
|
15b393193a | ||
|
|
53ab34928c | ||
|
|
eb4a169cfb | ||
|
|
6f6cadf31d | ||
|
|
17513a6dce | ||
|
|
a44560ddb6 | ||
|
|
2b8afb38b7 | ||
|
|
685ad74d53 | ||
|
|
288ea11534 | ||
|
|
b848ff8db9 | ||
|
|
5881fcb0d6 | ||
|
|
491a2e8732 | ||
|
|
4a620a2c5e | ||
|
|
05105155f8 | ||
|
|
9f96545fa7 | ||
|
|
0c60107e62 | ||
|
|
49eb9cbdd8 | ||
|
|
594d226056 | ||
|
|
aac7dccf45 | ||
|
|
c19e325b83 | ||
|
|
bd92c23add | ||
|
|
88335bd92e | ||
|
|
a4602021d8 | ||
|
|
dbe5c17a96 | ||
|
|
c40555c0ac | ||
|
|
bfc76278a9 | ||
|
|
a1f283946e | ||
|
|
066087b383 | ||
|
|
e9d42e059f | ||
|
|
d1dadc9814 | ||
|
|
2cc620ef33 | ||
|
|
cee705ccd3 | ||
|
|
a8f72424db | ||
|
|
31ed133932 | ||
|
|
d3c6974e99 | ||
|
|
1271081865 | ||
|
|
8a638a95a0 | ||
|
|
d9f726f2a5 | ||
|
|
5f3521b3d4 | ||
|
|
9a68bd8cc8 | ||
|
|
9b7812a0f2 | ||
|
|
4858d7e454 | ||
|
|
fbb3f41dff | ||
|
|
1472048b97 | ||
|
|
4aad51a352 | ||
|
|
9a0a0c2d8c | ||
|
|
fcc809f4f1 | ||
|
|
f3369677ef | ||
|
|
a03f9eb156 | ||
|
|
aa65dd8905 | ||
|
|
856b4f4654 | ||
|
|
9369fe8c27 | ||
|
|
1549ff12f1 | ||
|
|
70357ceff2 | ||
|
|
cfe7cac1c4 | ||
|
|
fb70eca0a3 | ||
|
|
cf2bf488a2 | ||
|
|
5c02fc47b9 | ||
|
|
4021e5eea9 | ||
|
|
5cd0b6272d | ||
|
|
bf49bebe7a | ||
|
|
1e8299e893 | ||
|
|
5381061d97 | ||
|
|
274558c430 | ||
|
|
188afe20f9 | ||
|
|
1add9c9a02 | ||
|
|
e7d4b99350 | ||
|
|
8627721533 | ||
|
|
6696416107 | ||
|
|
453be2e08a | ||
|
|
83497e4dc9 | ||
|
|
3806a9c320 | ||
|
|
4da95066a0 | ||
|
|
ab1105524f | ||
|
|
d70b743e03 | ||
|
|
920dd078f3 | ||
|
|
f8e780b9dd | ||
|
|
588910129c | ||
|
|
e42867f0a8 | ||
|
|
fe20afac17 |
@@ -104,5 +104,6 @@ SpacesInSquareBrackets: false
|
|||||||
Standard: Cpp11
|
Standard: Cpp11
|
||||||
TabWidth: 8
|
TabWidth: 8
|
||||||
UseTab: Never
|
UseTab: Never
|
||||||
|
IndentPPDirectives: AfterHash
|
||||||
...
|
...
|
||||||
|
|
||||||
@@ -1,6 +1,32 @@
|
|||||||
Checks: 'modernize-*,modernize-use-override,google-*,-google-runtime-references,misc-*,clang-analyzer-*,-misc-non-private-member-variables-in-classes'
|
Checks: 'cppcoreguidelines-*,
|
||||||
|
performance-*,
|
||||||
|
modernize-*,
|
||||||
|
google-*,
|
||||||
|
misc-*
|
||||||
|
cert-*,
|
||||||
|
readability-*,
|
||||||
|
clang-analyzer-*,
|
||||||
|
-performance-unnecessary-value-param,
|
||||||
|
-modernize-use-trailing-return-type,
|
||||||
|
-google-runtime-references,
|
||||||
|
-misc-non-private-member-variables-in-classes,
|
||||||
|
-readability-braces-around-statements,
|
||||||
|
-google-readability-braces-around-statements,
|
||||||
|
-cppcoreguidelines-avoid-magic-numbers,
|
||||||
|
-readability-magic-numbers,
|
||||||
|
-readability-magic-numbers,
|
||||||
|
-cppcoreguidelines-pro-type-vararg,
|
||||||
|
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
|
||||||
|
-cppcoreguidelines-avoid-c-arrays,
|
||||||
|
-modernize-avoid-c-arrays,
|
||||||
|
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
||||||
|
-readability-named-parameter,
|
||||||
|
-cert-env33-c
|
||||||
|
'
|
||||||
|
|
||||||
|
|
||||||
WarningsAsErrors: ''
|
WarningsAsErrors: ''
|
||||||
HeaderFilterRegex: 'async.h|async_logger.h|common.h|details|formatter.h|logger.h|sinks|spdlog.h|tweakme.h|version.h'
|
HeaderFilterRegex: '*spdlog/[^f].*'
|
||||||
AnalyzeTemporaryDtors: false
|
AnalyzeTemporaryDtors: false
|
||||||
FormatStyle: none
|
FormatStyle: none
|
||||||
|
|
||||||
107
.travis.yml
107
.travis.yml
@@ -5,7 +5,7 @@
|
|||||||
sudo: required
|
sudo: required
|
||||||
language: cpp
|
language: cpp
|
||||||
|
|
||||||
# gcc 4.8
|
# gcc t
|
||||||
addons: &gcc48
|
addons: &gcc48
|
||||||
apt:
|
apt:
|
||||||
packages:
|
packages:
|
||||||
@@ -21,6 +21,24 @@ addons: &gcc7
|
|||||||
sources:
|
sources:
|
||||||
- ubuntu-toolchain-r-test
|
- ubuntu-toolchain-r-test
|
||||||
|
|
||||||
|
|
||||||
|
# gcc 9.0
|
||||||
|
addons: &gcc9
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- g++-9
|
||||||
|
sources:
|
||||||
|
- ubuntu-toolchain-r-test
|
||||||
|
|
||||||
|
# gcc 11.0
|
||||||
|
addons: &gcc11
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- g++-11
|
||||||
|
sources:
|
||||||
|
- ubuntu-toolchain-r-test
|
||||||
|
|
||||||
|
|
||||||
# Clang 3.5
|
# Clang 3.5
|
||||||
addons: &clang35
|
addons: &clang35
|
||||||
apt:
|
apt:
|
||||||
@@ -30,54 +48,80 @@ addons: &clang35
|
|||||||
- ubuntu-toolchain-r-test
|
- ubuntu-toolchain-r-test
|
||||||
- llvm-toolchain-precise-3.5
|
- llvm-toolchain-precise-3.5
|
||||||
|
|
||||||
# Clang 7.0
|
|
||||||
addons: &clang70
|
addons: &clang10
|
||||||
apt:
|
apt:
|
||||||
packages:
|
packages:
|
||||||
- clang-7
|
- clang-10
|
||||||
|
- lldb-10
|
||||||
|
- lld-10
|
||||||
sources:
|
sources:
|
||||||
- ubuntu-toolchain-r-test
|
- sourceline: "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main"
|
||||||
- llvm-toolchain-trusty-7
|
key_url: "https://apt.llvm.org/llvm-snapshot.gpg.key"
|
||||||
|
|
||||||
|
addons: &clang12
|
||||||
|
apt:
|
||||||
|
packages:
|
||||||
|
- clang-12
|
||||||
|
- lldb-12
|
||||||
|
- lld-12
|
||||||
|
sources:
|
||||||
|
- sourceline: "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main"
|
||||||
|
key_url: "https://apt.llvm.org/llvm-snapshot.gpg.key"
|
||||||
|
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
include:
|
include:
|
||||||
# Test gcc-4.8: C++11, Build=Debug/Release
|
# Test gcc-4.8: C++11, Build=Release
|
||||||
- env: GCC_VERSION=4.8 BUILD_TYPE=Debug CPP=11
|
|
||||||
os: linux
|
|
||||||
addons: *gcc48
|
|
||||||
|
|
||||||
- env: GCC_VERSION=4.8 BUILD_TYPE=Release CPP=11
|
- env: GCC_VERSION=4.8 BUILD_TYPE=Release CPP=11
|
||||||
os: linux
|
os: linux
|
||||||
addons: *gcc48
|
addons: *gcc48
|
||||||
|
|
||||||
|
# Test gcc-7: C++11, Build=Release
|
||||||
- env: GCC_VERSION=7 BUILD_TYPE=Release CPP=11
|
- env: GCC_VERSION=7 BUILD_TYPE=Release CPP=11
|
||||||
os: linux
|
os: linux
|
||||||
addons: *gcc7
|
addons: *gcc7
|
||||||
|
|
||||||
# Test clang-3.5: C++11, Build=Debug/Release
|
# Test gcc-9: C++17, Build=Release
|
||||||
- env: CLANG_VERSION=3.5 BUILD_TYPE=Debug CPP=11
|
- env: GCC_VERSION=9 BUILD_TYPE=Release CPP=17
|
||||||
os: linux
|
os: linux
|
||||||
addons: *clang35
|
addons: *gcc9
|
||||||
|
|
||||||
|
# Test gcc-11.0: C++20, Build=Debug
|
||||||
|
- env: GCC_VERSION=11 BUILD_TYPE=Debug CPP=20 ASAN=Off
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
addons: *gcc11
|
||||||
|
|
||||||
|
# Test clang-3.5: C++11, Build=Release
|
||||||
- env: CLANG_VERSION=3.5 BUILD_TYPE=Release CPP=11
|
- env: CLANG_VERSION=3.5 BUILD_TYPE=Release CPP=11
|
||||||
os: linux
|
os: linux
|
||||||
addons: *clang35
|
addons: *clang35
|
||||||
|
|
||||||
# Test clang-7.0: C++11, Build=Debug, ASAN=On
|
# Text osx
|
||||||
- env: CLANG_VERSION=7 BUILD_TYPE=Debug CPP=11 ASAN=On TSAN=Off
|
|
||||||
dist: bionic
|
|
||||||
|
|
||||||
- env: CLANG_VERSION=7 BUILD_TYPE=Release CPP=11 ASAN=On TSAN=Off
|
|
||||||
dist: bionic
|
|
||||||
|
|
||||||
# osx
|
|
||||||
- env: BUILD_TYPE=Release CPP=11 ASAN=Off TSAN=Off
|
- env: BUILD_TYPE=Release CPP=11 ASAN=Off TSAN=Off
|
||||||
os: osx
|
os: osx
|
||||||
|
|
||||||
|
# Test clang-10.0: C++11, Build=Release
|
||||||
|
- env: CLANG_VERSION=10 BUILD_TYPE=Release CPP=11 ASAN=On
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
addons: *clang10
|
||||||
|
|
||||||
|
# Test clang-10.0: C++17, Build=Debug
|
||||||
|
- env: CLANG_VERSION=10 BUILD_TYPE=Debug CPP=17 ASAN=Off
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
addons: *clang10
|
||||||
|
|
||||||
|
|
||||||
|
# Test clang-12.0: C++17, Build=Debug
|
||||||
|
- env: CLANG_VERSION=12 BUILD_TYPE=Debug CPP=17 ASAN=Off
|
||||||
|
os: linux
|
||||||
|
dist: bionic
|
||||||
|
addons: *clang12
|
||||||
|
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- if [ -n "$GCC_VERSION" ]; then export CXX="g++-${GCC_VERSION}" CC="gcc-${GCC_VERSION}"; fi
|
- if [ -n "$GCC_VERSION" ]; then export CXX="g++-${GCC_VERSION}" CC="gcc-${GCC_VERSION}"; fi
|
||||||
- if [ -n "$CLANG_VERSION" ]; then export CXX="clang++-${CLANG_VERSION}" CC="clang-${CLANG_VERSION}"; fi
|
- if [ -n "$CLANG_VERSION" ]; then export CXX="clang++-${CLANG_VERSION}" CC="clang-${CLANG_VERSION}"; fi
|
||||||
@@ -97,15 +141,12 @@ script:
|
|||||||
-DCMAKE_CXX_STANDARD=$CPP \
|
-DCMAKE_CXX_STANDARD=$CPP \
|
||||||
-DSPDLOG_BUILD_EXAMPLE=ON \
|
-DSPDLOG_BUILD_EXAMPLE=ON \
|
||||||
-DSPDLOG_BUILD_EXAMPLE_HO=ON \
|
-DSPDLOG_BUILD_EXAMPLE_HO=ON \
|
||||||
|
-DSPDLOG_BUILD_WARNINGS=ON \
|
||||||
-DSPDLOG_BUILD_BENCH=OFF \
|
-DSPDLOG_BUILD_BENCH=OFF \
|
||||||
-DSPDLOG_BUILD_TESTS=ON \
|
-DSPDLOG_BUILD_TESTS=ON \
|
||||||
-DSPDLOG_BUILD_TESTS_HO=OFf \
|
-DSPDLOG_BUILD_TESTS_HO=OFF \
|
||||||
-DSPDLOG_SANITIZE_ADDRESS=$ASAN
|
-DSPDLOG_SANITIZE_ADDRESS=$ASAN
|
||||||
|
|
||||||
- make VERBOSE=1 -j2
|
- make VERBOSE=1 -j2
|
||||||
- ctest -j2 --output-on-failure
|
- ctest -j2 --output-on-failure
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
email: false
|
|
||||||
|
|||||||
320
CMakeLists.txt
320
CMakeLists.txt
@@ -1,57 +1,79 @@
|
|||||||
# Copyright(c) 2019 spdlog authors
|
# Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.2)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Start spdlog project
|
# Start spdlog project
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
include(GNUInstallDirs)
|
|
||||||
include(cmake/utils.cmake)
|
include(cmake/utils.cmake)
|
||||||
include(cmake/ide.cmake)
|
include(cmake/ide.cmake)
|
||||||
|
|
||||||
spdlog_extract_version()
|
spdlog_extract_version()
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
|
||||||
|
message(STATUS "Build spdlog: ${SPDLOG_VERSION}")
|
||||||
|
|
||||||
|
include(GNUInstallDirs)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
# Set CMake policies to support later version behaviour
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
if(POLICY CMP0077)
|
||||||
|
cmake_policy(SET CMP0077 NEW) # option() honors variables already set
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
# Set default build to release
|
# Set default build to release
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if(NOT CMAKE_BUILD_TYPE)
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
|
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
project(spdlog VERSION ${SPDLOG_VERSION} LANGUAGES CXX)
|
# ---------------------------------------------------------------------------------------
|
||||||
message(STATUS "Build spdlog: ${SPDLOG_VERSION}")
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
|
||||||
# Compiler config
|
# Compiler config
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
set(CMAKE_CXX_STANDARD 11)
|
if(NOT CMAKE_CXX_STANDARD)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# make sure __cplusplus is defined when using msvc and enable parallel build
|
||||||
|
if(MSVC)
|
||||||
|
string(APPEND CMAKE_CXX_FLAGS " /Zc:__cplusplus /MP")
|
||||||
|
endif()
|
||||||
|
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN" OR CMAKE_SYSTEM_NAME MATCHES "MSYS")
|
||||||
|
set(CMAKE_CXX_EXTENSIONS ON)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
# Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
|
# Set SPDLOG_MASTER_PROJECT to ON if we are building spdlog
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Check if spdlog is being used directly or via add_subdirectory, but allow overriding
|
# Check if spdlog is being used directly or via add_subdirectory, but allow overriding
|
||||||
if (NOT DEFINED SPDLOG_MASTER_PROJECT)
|
if(NOT DEFINED SPDLOG_MASTER_PROJECT)
|
||||||
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
|
||||||
set(SPDLOG_MASTER_PROJECT ON)
|
set(SPDLOG_MASTER_PROJECT ON)
|
||||||
else()
|
else()
|
||||||
set(SPDLOG_MASTER_PROJECT OFF)
|
set(SPDLOG_MASTER_PROJECT OFF)
|
||||||
endif()
|
endif()
|
||||||
endif ()
|
endif()
|
||||||
|
|
||||||
|
option(SPDLOG_BUILD_ALL "Build all artifacts" OFF)
|
||||||
|
|
||||||
# build shared option
|
# build shared option
|
||||||
if(NOT WIN32)
|
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
|
||||||
option(SPDLOG_BUILD_SHARED "Build shared library" OFF)
|
|
||||||
endif()
|
# precompiled headers option
|
||||||
|
option(SPDLOG_ENABLE_PCH "Build static or shared library using precompiled header to speed up compilation time" OFF)
|
||||||
|
|
||||||
# example options
|
# example options
|
||||||
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
|
option(SPDLOG_BUILD_EXAMPLE "Build example" ${SPDLOG_MASTER_PROJECT})
|
||||||
option(SPDLOG_BUILD_EXAMPLE_HO "Build header only example" OFF)
|
option(SPDLOG_BUILD_EXAMPLE_HO "Build header only example" OFF)
|
||||||
|
|
||||||
# testing options
|
# testing options
|
||||||
option(SPDLOG_BUILD_TESTS "Build tests" ${SPDLOG_MASTER_PROJECT})
|
option(SPDLOG_BUILD_TESTS "Build tests" OFF)
|
||||||
option(SPDLOG_BUILD_TESTS_HO "Build tests using the header only version" OFF)
|
option(SPDLOG_BUILD_TESTS_HO "Build tests using the header only version" OFF)
|
||||||
|
|
||||||
# bench options
|
# bench options
|
||||||
@@ -60,38 +82,79 @@ option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/
|
|||||||
# sanitizer options
|
# sanitizer options
|
||||||
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
|
option(SPDLOG_SANITIZE_ADDRESS "Enable address sanitizer in tests" OFF)
|
||||||
|
|
||||||
|
# warning options
|
||||||
|
option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" OFF)
|
||||||
|
|
||||||
# install options
|
# install options
|
||||||
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
|
option(SPDLOG_INSTALL "Generate the install target" ${SPDLOG_MASTER_PROJECT})
|
||||||
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
|
option(SPDLOG_FMT_EXTERNAL "Use external fmt library instead of bundled" OFF)
|
||||||
|
option(SPDLOG_FMT_EXTERNAL_HO "Use external fmt header-only library instead of bundled" OFF)
|
||||||
if(WIN32)
|
|
||||||
option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
|
option(SPDLOG_NO_EXCEPTIONS "Compile with -fno-exceptions. Call abort() on any spdlog exceptions" OFF)
|
||||||
|
|
||||||
|
if(SPDLOG_FMT_EXTERNAL AND SPDLOG_FMT_EXTERNAL_HO)
|
||||||
|
message(FATAL_ERROR "SPDLOG_FMT_EXTERNAL and SPDLOG_FMT_EXTERNAL_HO are mutually exclusive")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# misc tweakme options
|
||||||
|
if(WIN32)
|
||||||
|
option(SPDLOG_WCHAR_SUPPORT "Support wchar api" OFF)
|
||||||
|
option(SPDLOG_WCHAR_FILENAMES "Support wchar filenames" OFF)
|
||||||
|
else()
|
||||||
|
set(SPDLOG_WCHAR_SUPPORT OFF CACHE BOOL "non supported option" FORCE)
|
||||||
|
set(SPDLOG_WCHAR_FILENAMES OFF CACHE BOOL "non supported option" FORCE)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
|
||||||
|
option(SPDLOG_CLOCK_COARSE "Use CLOCK_REALTIME_COARSE instead of the regular clock," OFF)
|
||||||
|
else()
|
||||||
|
set(SPDLOG_CLOCK_COARSE OFF CACHE BOOL "non supported option" FORCE)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" OFF)
|
||||||
|
option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" OFF)
|
||||||
|
option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" OFF)
|
||||||
|
option(
|
||||||
|
SPDLOG_NO_ATOMIC_LEVELS
|
||||||
|
"prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently"
|
||||||
|
OFF)
|
||||||
|
option(SPDLOG_DISABLE_DEFAULT_LOGGER "Disable default logger creation" OFF)
|
||||||
|
|
||||||
|
# clang-tidy
|
||||||
|
if(${CMAKE_VERSION} VERSION_GREATER "3.5")
|
||||||
|
option(SPDLOG_TIDY "run clang-tidy" OFF)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(SPDLOG_TIDY)
|
||||||
|
set(CMAKE_CXX_CLANG_TIDY "clang-tidy")
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
message(STATUS "Enabled clang-tidy")
|
||||||
|
endif()
|
||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
|
message(STATUS "Build type: " ${CMAKE_BUILD_TYPE})
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
#---------------------------------------------------------------------------------------
|
|
||||||
# Static/Shared library (shared not supported in windows yet)
|
# Static/Shared library (shared not supported in windows yet)
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
set(SPDLOG_SRCS
|
set(SPDLOG_SRCS src/spdlog.cpp src/stdout_sinks.cpp src/color_sinks.cpp src/file_sinks.cpp src/async.cpp src/cfg.cpp)
|
||||||
src/spdlog.cpp
|
|
||||||
src/stdout_sinks.cpp
|
|
||||||
src/fmt.cpp
|
|
||||||
src/color_sinks.cpp
|
|
||||||
src/file_sinks.cpp
|
|
||||||
src/async.cpp)
|
|
||||||
|
|
||||||
|
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
||||||
|
list(APPEND SPDLOG_SRCS src/fmt.cpp)
|
||||||
|
endif()
|
||||||
|
|
||||||
if (SPDLOG_BUILD_SHARED)
|
if(SPDLOG_BUILD_SHARED OR BUILD_SHARED_LIBS)
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
message(FATAL_ERROR "spdlog shared lib is not yet supported under windows")
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
|
||||||
|
list(APPEND SPDLOG_SRCS ${CMAKE_CURRENT_BINARY_DIR}/version.rc)
|
||||||
endif()
|
endif()
|
||||||
add_library(spdlog SHARED ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
|
add_library(spdlog SHARED ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
|
||||||
|
target_compile_definitions(spdlog PUBLIC SPDLOG_SHARED_LIB)
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(spdlog PUBLIC $<$<AND:$<CXX_COMPILER_ID:MSVC>,$<NOT:$<COMPILE_LANGUAGE:CUDA>>>:/wd4251
|
||||||
|
/wd4275>)
|
||||||
|
endif()
|
||||||
|
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
||||||
|
target_compile_definitions(spdlog PRIVATE FMT_EXPORT PUBLIC FMT_SHARED)
|
||||||
|
endif()
|
||||||
else()
|
else()
|
||||||
add_library(spdlog STATIC ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
|
add_library(spdlog STATIC ${SPDLOG_SRCS} ${SPDLOG_ALL_HEADERS})
|
||||||
endif()
|
endif()
|
||||||
@@ -99,112 +162,159 @@ endif()
|
|||||||
add_library(spdlog::spdlog ALIAS spdlog)
|
add_library(spdlog::spdlog ALIAS spdlog)
|
||||||
|
|
||||||
target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
|
target_compile_definitions(spdlog PUBLIC SPDLOG_COMPILED_LIB)
|
||||||
target_include_directories(spdlog PUBLIC
|
target_include_directories(spdlog PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
||||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
|
||||||
target_link_libraries(spdlog PUBLIC Threads::Threads)
|
target_link_libraries(spdlog PUBLIC Threads::Threads)
|
||||||
spdlog_enable_warnings(spdlog)
|
spdlog_enable_warnings(spdlog)
|
||||||
|
|
||||||
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR})
|
set_target_properties(spdlog PROPERTIES VERSION ${SPDLOG_VERSION} SOVERSION ${SPDLOG_VERSION_MAJOR})
|
||||||
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)
|
set_target_properties(spdlog PROPERTIES DEBUG_POSTFIX d)
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
if(COMMAND target_precompile_headers AND SPDLOG_ENABLE_PCH)
|
||||||
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/pch.h.in ${PROJECT_BINARY_DIR}/spdlog_pch.h @ONLY)
|
||||||
|
target_precompile_headers(spdlog PRIVATE ${PROJECT_BINARY_DIR}/spdlog_pch.h)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
# Header only version
|
# Header only version
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
add_library(spdlog_header_only INTERFACE)
|
add_library(spdlog_header_only INTERFACE)
|
||||||
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
|
add_library(spdlog::spdlog_header_only ALIAS spdlog_header_only)
|
||||||
|
|
||||||
target_include_directories(spdlog_header_only INTERFACE
|
target_include_directories(spdlog_header_only INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
||||||
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
|
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
||||||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
|
|
||||||
target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)
|
target_link_libraries(spdlog_header_only INTERFACE Threads::Threads)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
#---------------------------------------------------------------------------------------
|
# Use fmt package if using external fmt
|
||||||
# Use fmt package if using exertnal fmt
|
# ---------------------------------------------------------------------------------------
|
||||||
#---------------------------------------------------------------------------------------
|
if(SPDLOG_FMT_EXTERNAL OR SPDLOG_FMT_EXTERNAL_HO)
|
||||||
if(SPDLOG_FMT_EXTERNAL)
|
if(NOT TARGET fmt::fmt)
|
||||||
if (NOT TARGET fmt::fmt)
|
find_package(fmt CONFIG REQUIRED)
|
||||||
find_package(fmt REQUIRED)
|
endif()
|
||||||
endif ()
|
|
||||||
|
|
||||||
target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL)
|
target_compile_definitions(spdlog PUBLIC SPDLOG_FMT_EXTERNAL)
|
||||||
target_link_libraries(spdlog PUBLIC fmt::fmt)
|
|
||||||
|
|
||||||
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL)
|
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_FMT_EXTERNAL)
|
||||||
target_link_libraries(spdlog_header_only INTERFACE fmt::fmt)
|
|
||||||
|
# use external fmt-header-nly
|
||||||
|
if(SPDLOG_FMT_EXTERNAL_HO)
|
||||||
|
target_link_libraries(spdlog PUBLIC fmt::fmt-header-only)
|
||||||
|
target_link_libraries(spdlog_header_only INTERFACE fmt::fmt-header-only)
|
||||||
|
else() # use external compile fmt
|
||||||
|
target_link_libraries(spdlog PUBLIC fmt::fmt)
|
||||||
|
target_link_libraries(spdlog_header_only INTERFACE fmt::fmt)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(PKG_CONFIG_REQUIRES fmt) # add dependency to pkg-config
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(SPDLOG_WCHAR_SUPPORT)
|
# ---------------------------------------------------------------------------------------
|
||||||
target_compile_definitions(spdlog PUBLIC SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
# Add required libraries for Android CMake build
|
||||||
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
# ---------------------------------------------------------------------------------------
|
||||||
endif()
|
if(ANDROID)
|
||||||
|
target_link_libraries(spdlog PUBLIC log)
|
||||||
|
target_link_libraries(spdlog_header_only INTERFACE log)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(SPDLOG_NO_EXCEPTIONS)
|
# ---------------------------------------------------------------------------------------
|
||||||
target_compile_definitions(spdlog PUBLIC SPDLOG_NO_EXCEPTIONS)
|
# Misc definitions according to tweak options
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
set(SPDLOG_WCHAR_TO_UTF8_SUPPORT ${SPDLOG_WCHAR_SUPPORT})
|
||||||
|
foreach(
|
||||||
|
SPDLOG_OPTION
|
||||||
|
SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
SPDLOG_WCHAR_FILENAMES
|
||||||
|
SPDLOG_NO_EXCEPTIONS
|
||||||
|
SPDLOG_CLOCK_COARSE
|
||||||
|
SPDLOG_PREVENT_CHILD_FD
|
||||||
|
SPDLOG_NO_THREAD_ID
|
||||||
|
SPDLOG_NO_TLS
|
||||||
|
SPDLOG_NO_ATOMIC_LEVELS
|
||||||
|
SPDLOG_DISABLE_DEFAULT_LOGGER)
|
||||||
|
if(${SPDLOG_OPTION})
|
||||||
|
target_compile_definitions(spdlog PUBLIC ${SPDLOG_OPTION})
|
||||||
|
target_compile_definitions(spdlog_header_only INTERFACE ${SPDLOG_OPTION})
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
target_compile_definitions(spdlog_header_only INTERFACE SPDLOG_NO_EXCEPTIONS)
|
if(SPDLOG_NO_EXCEPTIONS AND NOT MSVC)
|
||||||
|
target_compile_options(spdlog PRIVATE -fno-exceptions)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(NOT MSVC)
|
# ---------------------------------------------------------------------------------------
|
||||||
target_compile_options(spdlog PRIVATE -fno-exceptions)
|
# Build binaries
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO OR SPDLOG_BUILD_ALL)
|
||||||
|
message(STATUS "Generating example(s)")
|
||||||
|
add_subdirectory(example)
|
||||||
|
spdlog_enable_warnings(example)
|
||||||
|
if(SPDLOG_BUILD_EXAMPLE_HO)
|
||||||
|
spdlog_enable_warnings(example_header_only)
|
||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_TESTS_HO OR SPDLOG_BUILD_ALL)
|
||||||
#---------------------------------------------------------------------------------------
|
|
||||||
# Build binaries
|
|
||||||
#---------------------------------------------------------------------------------------
|
|
||||||
if(SPDLOG_BUILD_EXAMPLE OR SPDLOG_BUILD_EXAMPLE_HO)
|
|
||||||
message(STATUS "Generating examples")
|
|
||||||
add_subdirectory(example)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(SPDLOG_BUILD_TESTS OR SPDLOG_BUILD_TESTS_HO)
|
|
||||||
message(STATUS "Generating tests")
|
message(STATUS "Generating tests")
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_subdirectory(tests)
|
add_subdirectory(tests)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(SPDLOG_BUILD_BENCH)
|
if(SPDLOG_BUILD_BENCH OR SPDLOG_BUILD_ALL)
|
||||||
message(STATUS "Generating benchmarks")
|
message(STATUS "Generating benchmarks")
|
||||||
add_subdirectory(bench)
|
add_subdirectory(bench)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Install
|
# Install
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if (SPDLOG_INSTALL)
|
if(SPDLOG_INSTALL)
|
||||||
message(STATUS "Generating install")
|
message(STATUS "Generating install")
|
||||||
set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
|
set(project_config_in "${CMAKE_CURRENT_LIST_DIR}/cmake/spdlogConfig.cmake.in")
|
||||||
set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake")
|
set(project_config_out "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfig.cmake")
|
||||||
set(config_targets_file "spdlogConfigTargets.cmake")
|
set(config_targets_file "spdlogConfigTargets.cmake")
|
||||||
set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake")
|
set(version_config_file "${CMAKE_CURRENT_BINARY_DIR}/spdlogConfigVersion.cmake")
|
||||||
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/spdlog/cmake")
|
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/spdlog")
|
||||||
|
set(pkgconfig_install_dir "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||||
|
set(pkg_config "${CMAKE_BINARY_DIR}/${PROJECT_NAME}.pc")
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Include files
|
# Include files
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
|
install(DIRECTORY include/ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" PATTERN "fmt/bundled" EXCLUDE)
|
||||||
install(TARGETS spdlog spdlog_header_only EXPORT spdlog DESTINATION "${CMAKE_INSTALL_LIBDIR}/spdlog")
|
install(
|
||||||
|
TARGETS spdlog spdlog_header_only
|
||||||
|
EXPORT spdlog
|
||||||
|
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||||
|
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
if(NOT SPDLOG_FMT_EXTERNAL AND NOT SPDLOG_FMT_EXTERNAL_HO)
|
||||||
# Package and version files
|
install(DIRECTORY include/${PROJECT_NAME}/fmt/bundled/
|
||||||
#---------------------------------------------------------------------------------------
|
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/fmt/bundled/")
|
||||||
install(EXPORT spdlog
|
endif()
|
||||||
DESTINATION ${export_dest_dir}
|
|
||||||
NAMESPACE spdlog::
|
# ---------------------------------------------------------------------------------------
|
||||||
FILE ${config_targets_file})
|
# Install pkg-config file
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
get_target_property(PKG_CONFIG_DEFINES spdlog INTERFACE_COMPILE_DEFINITIONS)
|
||||||
|
string(REPLACE ";" " -D" PKG_CONFIG_DEFINES "${PKG_CONFIG_DEFINES}")
|
||||||
|
string(CONCAT PKG_CONFIG_DEFINES "-D" "${PKG_CONFIG_DEFINES}")
|
||||||
|
configure_file("cmake/${PROJECT_NAME}.pc.in" "${pkg_config}" @ONLY)
|
||||||
|
install(FILES "${pkg_config}" DESTINATION "${pkgconfig_install_dir}")
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
# Install CMake config files
|
||||||
|
# ---------------------------------------------------------------------------------------
|
||||||
|
install(EXPORT spdlog DESTINATION ${export_dest_dir} NAMESPACE spdlog:: FILE ${config_targets_file})
|
||||||
|
|
||||||
include(CMakePackageConfigHelpers)
|
include(CMakePackageConfigHelpers)
|
||||||
configure_file("${project_config_in}" "${project_config_out}" @ONLY)
|
configure_package_config_file("${project_config_in}" "${project_config_out}"
|
||||||
|
INSTALL_DESTINATION ${export_dest_dir})
|
||||||
|
|
||||||
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
|
write_basic_package_version_file("${version_config_file}" COMPATIBILITY SameMajorVersion)
|
||||||
install(FILES
|
install(FILES "${project_config_out}" "${version_config_file}" DESTINATION "${export_dest_dir}")
|
||||||
"${project_config_out}"
|
|
||||||
"${version_config_file}" DESTINATION "${export_dest_dir}")
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Support creation of installable packages
|
# Support creation of installable packages
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
include(cmake/spdlogCPack.cmake)
|
include(cmake/spdlogCPack.cmake)
|
||||||
|
endif()
|
||||||
endif ()
|
|
||||||
|
|||||||
4
LICENSE
4
LICENSE
@@ -20,3 +20,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-- NOTE: Third party dependency used by this software --
|
||||||
|
This software depends on the fmt lib (MIT License),
|
||||||
|
and users must comply to its license: https://github.com/fmtlib/fmt/blob/master/LICENSE.rst
|
||||||
|
|
||||||
|
|||||||
143
README.md
143
README.md
@@ -1,14 +1,12 @@
|
|||||||
# spdlog
|
# spdlog
|
||||||
|
|
||||||
Very fast, header-only/compiled, C++ logging library. [](https://travis-ci.org/gabime/spdlog) [](https://ci.appveyor.com/project/gabime/spdlog)
|
Very fast, header-only/compiled, C++ logging library. [](https://travis-ci.com/gabime/spdlog) [](https://ci.appveyor.com/project/gabime/spdlog) [](https://github.com/gabime/spdlog/releases/latest)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
#### Header only version
|
#### Header only version
|
||||||
Copy the source [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) to your build tree and use a C++11 compiler.
|
Copy the include [folder](https://github.com/gabime/spdlog/tree/v1.x/include/spdlog) to your build tree and use a C++11 compiler.
|
||||||
|
|
||||||
#### Static lib version (recommended - much faster compile times, v1.4.0)
|
#### Static lib version (recommended - much faster compile times)
|
||||||
```console
|
```console
|
||||||
$ git clone https://github.com/gabime/spdlog.git
|
$ git clone https://github.com/gabime/spdlog.git
|
||||||
$ cd spdlog && mkdir build && cd build
|
$ cd spdlog && mkdir build && cd build
|
||||||
@@ -25,18 +23,23 @@ $ cmake .. && make -j
|
|||||||
|
|
||||||
## Package managers:
|
## Package managers:
|
||||||
* Homebrew: `brew install spdlog`
|
* Homebrew: `brew install spdlog`
|
||||||
|
* MacPorts: `sudo port install spdlog`
|
||||||
* FreeBSD: `cd /usr/ports/devel/spdlog/ && make install clean`
|
* FreeBSD: `cd /usr/ports/devel/spdlog/ && make install clean`
|
||||||
* Fedora: `yum install spdlog`
|
* Fedora: `dnf install spdlog`
|
||||||
* Gentoo: `emerge dev-libs/spdlog`
|
* Gentoo: `emerge dev-libs/spdlog`
|
||||||
* Arch Linux: `yaourt -S spdlog-git`
|
* Arch Linux: `pacman -S spdlog`
|
||||||
* vcpkg: `vcpkg install spdlog`
|
* vcpkg: `vcpkg install spdlog`
|
||||||
|
* conan: `spdlog/[>=1.4.1]`
|
||||||
|
* conda: `conda install -c conda-forge spdlog`
|
||||||
|
* build2: ```depends: spdlog ^1.8.2```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
* Very fast (see [benchmarks](#benchmarks) below).
|
* Very fast (see [benchmarks](#benchmarks) below).
|
||||||
* Headers only, just copy and use. Or use as a compiled library.
|
* Headers only or compiled
|
||||||
* Feature rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
|
* Feature rich formatting, using the excellent [fmt](https://github.com/fmtlib/fmt) library.
|
||||||
* **New!** [Backtrace](#backtrace-support) support - store debug or other messages in a ring buffer and display later on demand.
|
* Asynchronous mode (optional)
|
||||||
* Fast asynchronous mode (optional)
|
|
||||||
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
|
||||||
* Multi/Single threaded loggers.
|
* Multi/Single threaded loggers.
|
||||||
* Various log targets:
|
* Various log targets:
|
||||||
@@ -44,17 +47,18 @@ $ cmake .. && make -j
|
|||||||
* Daily log files.
|
* Daily log files.
|
||||||
* Console logging (colors supported).
|
* Console logging (colors supported).
|
||||||
* syslog.
|
* syslog.
|
||||||
* Windows debugger (```OutputDebugString(..)```)
|
* Windows event log.
|
||||||
* Easily extendable with custom log targets (just implement a single function in the [sink](include/spdlog/sinks/sink.h) interface).
|
* Windows debugger (```OutputDebugString(..)```).
|
||||||
* Severity based filtering - threshold levels can be modified in runtime as well as in compile time.
|
* Easily [extendable](https://github.com/gabime/spdlog/wiki/4.-Sinks#implementing-your-own-sink) with custom log targets.
|
||||||
|
* Log filtering - log levels can be modified in runtime as well as in compile time.
|
||||||
|
* Support for loading log levels from argv or from environment var.
|
||||||
|
* [Backtrace](#backtrace-support) support - store debug messages in a ring buffer and display later on demand.
|
||||||
|
|
||||||
## Usage samples
|
## Usage samples
|
||||||
|
|
||||||
#### Basic usage
|
#### Basic usage
|
||||||
```c++
|
```c++
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -75,15 +79,13 @@ int main()
|
|||||||
|
|
||||||
// Compile time log levels
|
// Compile time log levels
|
||||||
// define SPDLOG_ACTIVE_LEVEL to desired level
|
// define SPDLOG_ACTIVE_LEVEL to desired level
|
||||||
SPDLOG_TRACE("Some trace message with param {}", {});
|
SPDLOG_TRACE("Some trace message with param {}", 42);
|
||||||
SPDLOG_DEBUG("Some debug message");
|
SPDLOG_DEBUG("Some debug message");
|
||||||
|
|
||||||
// Set the default logger to file logger
|
|
||||||
auto file_logger = spdlog::basic_logger_mt("basic_logger", "logs/basic.txt");
|
|
||||||
spdlog::set_default_logger(file_logger);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
#### create stdout/stderr logger object
|
---
|
||||||
|
#### Create stdout/stderr logger object
|
||||||
```c++
|
```c++
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||||
@@ -95,6 +97,7 @@ void stdout_example()
|
|||||||
spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
|
spdlog::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name)");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
#### Basic file logger
|
#### Basic file logger
|
||||||
```c++
|
```c++
|
||||||
@@ -103,7 +106,7 @@ void basic_logfile_example()
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto my_logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
|
auto logger = spdlog::basic_logger_mt("basic_logger", "logs/basic-log.txt");
|
||||||
}
|
}
|
||||||
catch (const spdlog::spdlog_ex &ex)
|
catch (const spdlog::spdlog_ex &ex)
|
||||||
{
|
{
|
||||||
@@ -118,7 +121,9 @@ void basic_logfile_example()
|
|||||||
void rotating_example()
|
void rotating_example()
|
||||||
{
|
{
|
||||||
// Create a file rotating logger with 5mb size max and 3 rotated files
|
// Create a file rotating logger with 5mb size max and 3 rotated files
|
||||||
auto rotating_logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
|
auto max_size = 1048576 * 5;
|
||||||
|
auto max_files = 3;
|
||||||
|
auto logger = spdlog::rotating_logger_mt("some_logger_name", "logs/rotating.txt", max_size, max_files);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -130,7 +135,7 @@ void rotating_example()
|
|||||||
void daily_example()
|
void daily_example()
|
||||||
{
|
{
|
||||||
// Create a daily logger - a new file is created every day on 2:30am
|
// Create a daily logger - a new file is created every day on 2:30am
|
||||||
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
auto logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -138,9 +143,11 @@ void daily_example()
|
|||||||
---
|
---
|
||||||
#### Backtrace support
|
#### Backtrace support
|
||||||
```c++
|
```c++
|
||||||
// Loggers can store in a ring buffer all messages (including debug/trace) and display later on demand.
|
// Debug messages can be stored in a ring buffer instead of being logged immediately.
|
||||||
// When needed, call dump_backtrace() to see them
|
// This is useful in order to display debug logs only when really needed (e.g. when error happens).
|
||||||
spdlog::enable_backtrace(32); // create ring buffer with capacity of 32 messages
|
// When needed, call dump_backtrace() to see them.
|
||||||
|
|
||||||
|
spdlog::enable_backtrace(32); // Store the latest 32 messages in a buffer. Older messages will be dropped.
|
||||||
// or my_logger->enable_backtrace(32)..
|
// or my_logger->enable_backtrace(32)..
|
||||||
for(int i = 0; i < 100; i++)
|
for(int i = 0; i < 100; i++)
|
||||||
{
|
{
|
||||||
@@ -156,11 +163,25 @@ spdlog::dump_backtrace(); // log them now! show the last 32 messages
|
|||||||
#### Periodic flush
|
#### Periodic flush
|
||||||
```c++
|
```c++
|
||||||
// periodically flush all *registered* loggers every 3 seconds:
|
// periodically flush all *registered* loggers every 3 seconds:
|
||||||
// warning: only use if all your loggers are thread safe!
|
// warning: only use if all your loggers are thread safe ("_mt" loggers)
|
||||||
spdlog::flush_every(std::chrono::seconds(3));
|
spdlog::flush_every(std::chrono::seconds(3));
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
#### Stopwatch
|
||||||
|
```c++
|
||||||
|
// Stopwatch support for spdlog
|
||||||
|
#include "spdlog/stopwatch.h"
|
||||||
|
void stopwatch_example()
|
||||||
|
{
|
||||||
|
spdlog::stopwatch sw;
|
||||||
|
spdlog::debug("Elapsed {}", sw);
|
||||||
|
spdlog::debug("Elapsed {:.3}", sw);
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
#### Log binary data in hex
|
#### Log binary data in hex
|
||||||
```c++
|
```c++
|
||||||
@@ -171,6 +192,7 @@ spdlog::flush_every(std::chrono::seconds(3));
|
|||||||
// {:s} - don't separate each byte with space.
|
// {:s} - don't separate each byte with space.
|
||||||
// {:p} - don't print the position on each line start.
|
// {:p} - don't print the position on each line start.
|
||||||
// {:n} - don't split the output to lines.
|
// {:n} - don't split the output to lines.
|
||||||
|
// {:a} - show ASCII if :n is not set.
|
||||||
|
|
||||||
#include "spdlog/fmt/bin_to_hex.h"
|
#include "spdlog/fmt/bin_to_hex.h"
|
||||||
|
|
||||||
@@ -264,6 +286,37 @@ void user_defined_example()
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
#### User defined flags in the log pattern
|
||||||
|
```c++
|
||||||
|
// Log patterns can contain custom flags.
|
||||||
|
// the following example will add new flag '%*' - which will be bound to a <my_formatter_flag> instance.
|
||||||
|
#include "spdlog/pattern_formatter.h"
|
||||||
|
class my_formatter_flag : public spdlog::custom_flag_formatter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
|
||||||
|
{
|
||||||
|
std::string some_txt = "custom-flag";
|
||||||
|
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<custom_flag_formatter> clone() const override
|
||||||
|
{
|
||||||
|
return spdlog::details::make_unique<my_formatter_flag>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void custom_flags_example()
|
||||||
|
{
|
||||||
|
auto formatter = std::make_unique<spdlog::pattern_formatter>();
|
||||||
|
formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
|
||||||
|
spdlog::set_formatter(std::move(formatter));
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
#### Custom error handler
|
#### Custom error handler
|
||||||
```c++
|
```c++
|
||||||
@@ -275,6 +328,7 @@ void err_handler_example()
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
#### syslog
|
#### syslog
|
||||||
```c++
|
```c++
|
||||||
@@ -298,6 +352,28 @@ void android_example()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
#### Load log levels from env variable or from argv
|
||||||
|
|
||||||
|
```c++
|
||||||
|
#include "spdlog/cfg/env.h"
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
spdlog::cfg::load_env_levels();
|
||||||
|
// or from command line:
|
||||||
|
// ./example SPDLOG_LEVEL=info,mylogger=trace
|
||||||
|
// #include "spdlog/cfg/argv.h" // for loading levels from argv
|
||||||
|
// spdlog::cfg::load_argv_levels(argc, argv);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
So then you can:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ export SPDLOG_LEVEL=info,mylogger=trace
|
||||||
|
$ ./example
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/bench.cpp) done in Ubuntu 64 bit, Intel i7-4770 CPU @ 3.40GHz
|
Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/bench.cpp) done in Ubuntu 64 bit, Intel i7-4770 CPU @ 3.40GHz
|
||||||
@@ -319,21 +395,20 @@ Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/ben
|
|||||||
[info] daily_st Elapsed: 0.42 secs 2,393,298/sec
|
[info] daily_st Elapsed: 0.42 secs 2,393,298/sec
|
||||||
[info] null_st Elapsed: 0.04 secs 27,446,957/sec
|
[info] null_st Elapsed: 0.04 secs 27,446,957/sec
|
||||||
[info] **************************************************************
|
[info] **************************************************************
|
||||||
[info] 10 threads sharing same logger, 1,000,000 iterations
|
[info] 10 threads, competing over the same logger object, 1,000,000 iterations
|
||||||
[info] **************************************************************
|
[info] **************************************************************
|
||||||
[info] basic_mt Elapsed: 0.60 secs 1,659,613/sec
|
[info] basic_mt Elapsed: 0.60 secs 1,659,613/sec
|
||||||
[info] rotating_mt Elapsed: 0.62 secs 1,612,493/sec
|
[info] rotating_mt Elapsed: 0.62 secs 1,612,493/sec
|
||||||
[info] daily_mt Elapsed: 0.61 secs 1,638,305/sec
|
[info] daily_mt Elapsed: 0.61 secs 1,638,305/sec
|
||||||
[info] null_mt Elapsed: 0.16 secs 6,272,758/sec
|
[info] null_mt Elapsed: 0.16 secs 6,272,758/sec
|
||||||
```
|
```
|
||||||
#### ASynchronous mode
|
#### Asynchronous mode
|
||||||
```
|
```
|
||||||
[info] -------------------------------------------------
|
[info] -------------------------------------------------
|
||||||
[info] Messages : 1,000,000
|
[info] Messages : 1,000,000
|
||||||
[info] Threads : 10
|
[info] Threads : 10
|
||||||
[info] Queue : 8,192 slots
|
[info] Queue : 8,192 slots
|
||||||
[info] Queue memory : 8,192 x 272 = 2,176 KB
|
[info] Queue memory : 8,192 x 272 = 2,176 KB
|
||||||
[info] Total iters : 3
|
|
||||||
[info] -------------------------------------------------
|
[info] -------------------------------------------------
|
||||||
[info]
|
[info]
|
||||||
[info] *********************************
|
[info] *********************************
|
||||||
@@ -354,3 +429,9 @@ Below are some [benchmarks](https://github.com/gabime/spdlog/blob/v1.x/bench/ben
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
Documentation can be found in the [wiki](https://github.com/gabime/spdlog/wiki/1.-QuickStart) pages.
|
Documentation can be found in the [wiki](https://github.com/gabime/spdlog/wiki/1.-QuickStart) pages.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Thanks to [JetBrains](https://www.jetbrains.com/?from=spdlog) for donating product licenses to help develop **spdlog** <a href="https://www.jetbrains.com/?from=spdlog"><img src="logos/jetbrains-variant-4.svg" width="94" align="center" /></a>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
90
appveyor.yml
90
appveyor.yml
@@ -1,34 +1,78 @@
|
|||||||
version: 1.0.{build}
|
version: 1.0.{build}
|
||||||
image: Visual Studio 2015
|
image: Visual Studio 2017
|
||||||
environment:
|
environment:
|
||||||
matrix:
|
matrix:
|
||||||
- GENERATOR: '"MinGW Makefiles"'
|
- GENERATOR: '"Visual Studio 14 2015"'
|
||||||
BUILD_TYPE: Debug
|
BUILD_TYPE: Debug
|
||||||
- GENERATOR: '"MinGW Makefiles"'
|
BUILD_SHARED: 'OFF'
|
||||||
BUILD_TYPE: Release
|
WCHAR: 'OFF'
|
||||||
- GENERATOR: '"Visual Studio 14 2015"'
|
WCHAR_FILES: 'OFF'
|
||||||
BUILD_TYPE: Debug
|
BUILD_EXAMPLE: 'ON'
|
||||||
- GENERATOR: '"Visual Studio 14 2015"'
|
- GENERATOR: '"Visual Studio 14 2015"'
|
||||||
BUILD_TYPE: Release
|
BUILD_TYPE: Release
|
||||||
- GENERATOR: '"Visual Studio 14 2015 Win64"'
|
BUILD_SHARED: 'OFF'
|
||||||
BUILD_TYPE: Debug
|
WCHAR: 'ON'
|
||||||
- GENERATOR: '"Visual Studio 14 2015 Win64"'
|
WCHAR_FILES: 'OFF'
|
||||||
BUILD_TYPE: Release
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 14 2015 Win64"'
|
||||||
|
BUILD_TYPE: Debug
|
||||||
|
BUILD_SHARED: 'OFF'
|
||||||
|
WCHAR: 'ON'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 14 2015 Win64"'
|
||||||
|
BUILD_TYPE: Release
|
||||||
|
BUILD_SHARED: 'OFF'
|
||||||
|
WCHAR: 'ON'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 15 2017 Win64"'
|
||||||
|
BUILD_TYPE: Debug
|
||||||
|
BUILD_SHARED: 'OFF'
|
||||||
|
WCHAR: 'ON'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 15 2017 Win64"'
|
||||||
|
BUILD_TYPE: Release
|
||||||
|
BUILD_SHARED: 'OFF'
|
||||||
|
WCHAR: 'OFF'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 15 2017 Win64"'
|
||||||
|
BUILD_TYPE: Release
|
||||||
|
BUILD_SHARED: 'ON'
|
||||||
|
WCHAR: 'OFF'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'ON'
|
||||||
|
- GENERATOR: '"Visual Studio 15 2017 Win64"'
|
||||||
|
BUILD_TYPE: Release
|
||||||
|
BUILD_SHARED: 'ON'
|
||||||
|
WCHAR: 'ON'
|
||||||
|
WCHAR_FILES: 'ON'
|
||||||
|
BUILD_EXAMPLE: 'OFF'
|
||||||
|
- GENERATOR: '"Visual Studio 16 2019" -A x64'
|
||||||
|
BUILD_TYPE: Release
|
||||||
|
BUILD_SHARED: 'ON'
|
||||||
|
WCHAR: 'OFF'
|
||||||
|
WCHAR_FILES: 'OFF'
|
||||||
|
BUILD_EXAMPLE: 'OFF'
|
||||||
|
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
|
||||||
build_script:
|
build_script:
|
||||||
- cmd: >-
|
- cmd: >-
|
||||||
set
|
set
|
||||||
|
|
||||||
mkdir build
|
mkdir build
|
||||||
|
|
||||||
cd build
|
cd build
|
||||||
|
|
||||||
set PATH=%PATH:C:\Program Files\Git\usr\bin;=%
|
set PATH=%PATH%;C:\Program Files\Git\usr\bin
|
||||||
|
|
||||||
set PATH=C:\mingw-w64\i686-5.3.0-posix-dwarf-rt_v4-rev0\mingw32\bin;%PATH%
|
cmake -G %GENERATOR% -D CMAKE_BUILD_TYPE=%BUILD_TYPE% -D BUILD_SHARED_LIBS=%BUILD_SHARED% -D SPDLOG_WCHAR_SUPPORT=%WCHAR% -D SPDLOG_WCHAR_FILENAMES=%WCHAR_FILES% -D SPDLOG_BUILD_EXAMPLE=%BUILD_EXAMPLE% -D SPDLOG_BUILD_EXAMPLE_HO=%BUILD_EXAMPLE% -D SPDLOG_BUILD_TESTS=ON -D SPDLOG_BUILD_TESTS_HO=OFF -D SPDLOG_BUILD_WARNINGS=ON ..
|
||||||
|
|
||||||
cmake .. -G %GENERATOR% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DSPDLOG_BUILD_EXAMPLE=ON -DSPDLOG_BUILD_EXAMPLE_HO=ON -DSPDLOG_BUILD_TESTS=ON -DSPDLOG_BUILD_TESTS_HO=OFF
|
cmake --build . --config %BUILD_TYPE%
|
||||||
|
|
||||||
cmake --build . --config %BUILD_TYPE%
|
|
||||||
|
|
||||||
|
before_test:
|
||||||
|
- set PATH=%PATH%;C:\projects\spdlog\build\%BUILD_TYPE%
|
||||||
|
|
||||||
test_script:
|
test_script:
|
||||||
- ctest -VV -C "%BUILD_TYPE%"
|
- C:\projects\spdlog\build\tests\%BUILD_TYPE%\spdlog-utests.exe
|
||||||
|
|||||||
@@ -1,16 +1,30 @@
|
|||||||
# Copyright(c) 2019 spdlog authors
|
# Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.1)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(spdlog_bench CXX)
|
project(spdlog_bench CXX)
|
||||||
|
|
||||||
if(NOT TARGET spdlog)
|
if(NOT TARGET spdlog)
|
||||||
# Stand-alone build
|
# Stand-alone build
|
||||||
find_package(spdlog CONFIG REQUIRED)
|
find_package(spdlog CONFIG REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
find_package(benchmark CONFIG REQUIRED)
|
find_package(benchmark CONFIG)
|
||||||
|
if(NOT benchmark_FOUND)
|
||||||
|
message(STATUS "Using CMake Version ${CMAKE_VERSION}")
|
||||||
|
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.11.0")
|
||||||
|
# User can fetch googlebenchmark
|
||||||
|
message(STATUS "Downloading GoogleBenchmark")
|
||||||
|
include(FetchContent)
|
||||||
|
set(BENCHMARK_ENABLE_GTEST_TESTS OFF CACHE INTERNAL "")
|
||||||
|
# Do not build and run googlebenchmark tests
|
||||||
|
FetchContent_Declare(googlebenchmark GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG v1.5.2)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(googlebenchmark)
|
||||||
|
else()
|
||||||
|
message(FATAL_ERROR "GoogleBenchmark is missing. Use CMake >= 3.11 or download it")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
add_executable(bench bench.cpp)
|
add_executable(bench bench.cpp)
|
||||||
spdlog_enable_warnings(bench)
|
spdlog_enable_warnings(bench)
|
||||||
@@ -24,5 +38,3 @@ target_link_libraries(latency PRIVATE benchmark::benchmark spdlog::spdlog)
|
|||||||
|
|
||||||
add_executable(formatter-bench formatter-bench.cpp)
|
add_executable(formatter-bench formatter-bench.cpp)
|
||||||
target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog)
|
target_link_libraries(formatter-bench PRIVATE benchmark::benchmark spdlog::spdlog)
|
||||||
|
|
||||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")
|
|
||||||
|
|||||||
@@ -9,7 +9,12 @@
|
|||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
#include "spdlog/sinks/basic_file_sink.h"
|
||||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
|
||||||
|
#ifdef SPDLOG_FMT_EXTERNAL
|
||||||
|
# include <fmt/format.h>
|
||||||
|
#else
|
||||||
|
# include "spdlog/fmt/bundled/format.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
@@ -27,9 +32,9 @@ using namespace utils;
|
|||||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(push)
|
# pragma warning(push)
|
||||||
#pragma warning(disable : 4996) // disable fopen warning under msvc
|
# pragma warning(disable : 4996) // disable fopen warning under msvc
|
||||||
#endif // _MSC_VER
|
#endif // _MSC_VER
|
||||||
|
|
||||||
int count_lines(const char *filename)
|
int count_lines(const char *filename)
|
||||||
{
|
{
|
||||||
@@ -48,18 +53,18 @@ int count_lines(const char *filename)
|
|||||||
|
|
||||||
void verify_file(const char *filename, int expected_count)
|
void verify_file(const char *filename, int expected_count)
|
||||||
{
|
{
|
||||||
spdlog::info("Verifying {} to contain {:n} line..", filename, expected_count);
|
spdlog::info("Verifying {} to contain {} line..", filename, expected_count);
|
||||||
auto count = count_lines(filename);
|
auto count = count_lines(filename);
|
||||||
if (count != expected_count)
|
if (count != expected_count)
|
||||||
{
|
{
|
||||||
spdlog::error("Test failed. {} has {:n} lines instead of {:n}", filename, count, expected_count);
|
spdlog::error("Test failed. {} has {} lines instead of {}", filename, count, expected_count);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
spdlog::info("Line count OK ({:n})\n", count);
|
spdlog::info("Line count OK ({})\n", count);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(pop)
|
# pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@@ -98,11 +103,11 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
auto slot_size = sizeof(spdlog::details::async_msg);
|
auto slot_size = sizeof(spdlog::details::async_msg);
|
||||||
spdlog::info("-------------------------------------------------");
|
spdlog::info("-------------------------------------------------");
|
||||||
spdlog::info("Messages : {:n}", howmany);
|
spdlog::info("Messages : {:L}", howmany);
|
||||||
spdlog::info("Threads : {:n}", threads);
|
spdlog::info("Threads : {:L}", threads);
|
||||||
spdlog::info("Queue : {:n} slots", queue_size);
|
spdlog::info("Queue : {:L} slots", queue_size);
|
||||||
spdlog::info("Queue memory : {:n} x {} = {:n} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024);
|
spdlog::info("Queue memory : {:L} x {:L} = {:L} KB ", queue_size, slot_size, (queue_size * slot_size) / 1024);
|
||||||
spdlog::info("Total iters : {:n}", iters);
|
spdlog::info("Total iters : {:L}", iters);
|
||||||
spdlog::info("-------------------------------------------------");
|
spdlog::info("-------------------------------------------------");
|
||||||
|
|
||||||
const char *filename = "logs/basic_async.log";
|
const char *filename = "logs/basic_async.log";
|
||||||
@@ -175,5 +180,5 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> logger, int thread_co
|
|||||||
|
|
||||||
auto delta = high_resolution_clock::now() - start;
|
auto delta = high_resolution_clock::now() - start;
|
||||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||||
spdlog::info("Elapsed: {} secs\t {:n}/sec", delta_d, int(howmany / delta_d));
|
spdlog::info("Elapsed: {} secs\t {:L}/sec", delta_d, int(howmany / delta_d));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,12 @@
|
|||||||
#include "spdlog/sinks/null_sink.h"
|
#include "spdlog/sinks/null_sink.h"
|
||||||
#include "spdlog/sinks/rotating_file_sink.h"
|
#include "spdlog/sinks/rotating_file_sink.h"
|
||||||
|
|
||||||
|
#ifdef SPDLOG_FMT_EXTERNAL
|
||||||
|
# include <fmt/locale.h>
|
||||||
|
#else
|
||||||
|
# include "spdlog/fmt/bundled/format.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstdlib> // EXIT_FAILURE
|
#include <cstdlib> // EXIT_FAILURE
|
||||||
@@ -19,24 +25,20 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::chrono;
|
|
||||||
using namespace spdlog;
|
|
||||||
using namespace spdlog::sinks;
|
|
||||||
using namespace utils;
|
|
||||||
|
|
||||||
void bench(int howmany, std::shared_ptr<spdlog::logger> log);
|
void bench(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count);
|
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count);
|
||||||
void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log);
|
|
||||||
void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log);
|
|
||||||
|
|
||||||
static size_t file_size = 30 * 1024 * 1024;
|
// void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||||
static size_t rotating_files = 5;
|
// void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log);
|
||||||
|
|
||||||
void bench_threaded_logging(int threads, int iters)
|
static const size_t file_size = 30 * 1024 * 1024;
|
||||||
|
static const size_t rotating_files = 5;
|
||||||
|
static const int max_threads = 1000;
|
||||||
|
|
||||||
|
void bench_threaded_logging(size_t threads, int iters)
|
||||||
{
|
{
|
||||||
spdlog::info("**************************************************************");
|
spdlog::info("**************************************************************");
|
||||||
spdlog::info("Multi threaded: {:n} threads, {:n} messages", threads, iters);
|
spdlog::info(fmt::format(std::locale("en_US.UTF-8"), "Multi threaded: {:L} threads, {:L} messages", threads, iters));
|
||||||
spdlog::info("**************************************************************");
|
spdlog::info("**************************************************************");
|
||||||
|
|
||||||
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
|
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "logs/basic_mt.log", true);
|
||||||
@@ -72,7 +74,7 @@ void bench_threaded_logging(int threads, int iters)
|
|||||||
void bench_single_threaded(int iters)
|
void bench_single_threaded(int iters)
|
||||||
{
|
{
|
||||||
spdlog::info("**************************************************************");
|
spdlog::info("**************************************************************");
|
||||||
spdlog::info("Single threaded: {:n} messages", iters);
|
spdlog::info(fmt::format(std::locale("en_US.UTF-8"), "Single threaded: {} messages", iters));
|
||||||
spdlog::info("**************************************************************");
|
spdlog::info("**************************************************************");
|
||||||
|
|
||||||
auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
|
auto basic_st = spdlog::basic_logger_st("basic_st", "logs/basic_st.log", true);
|
||||||
@@ -111,14 +113,23 @@ int main(int argc, char *argv[])
|
|||||||
spdlog::set_automatic_registration(false);
|
spdlog::set_automatic_registration(false);
|
||||||
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
|
spdlog::default_logger()->set_pattern("[%^%l%$] %v");
|
||||||
int iters = 250000;
|
int iters = 250000;
|
||||||
int threads = 4;
|
size_t threads = 4;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
iters = atoi(argv[1]);
|
{
|
||||||
|
iters = std::stoi(argv[1]);
|
||||||
|
}
|
||||||
if (argc > 2)
|
if (argc > 2)
|
||||||
threads = atoi(argv[2]);
|
{
|
||||||
|
threads = std::stoul(argv[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threads > max_threads)
|
||||||
|
{
|
||||||
|
throw std::runtime_error(fmt::format("Number of threads exceeds maximum({})", max_threads));
|
||||||
|
}
|
||||||
|
|
||||||
bench_single_threaded(iters);
|
bench_single_threaded(iters);
|
||||||
bench_threaded_logging(1, iters);
|
bench_threaded_logging(1, iters);
|
||||||
@@ -134,7 +145,10 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||||
{
|
{
|
||||||
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
using std::chrono::high_resolution_clock;
|
using std::chrono::high_resolution_clock;
|
||||||
|
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
for (auto i = 0; i < howmany; ++i)
|
for (auto i = 0; i < howmany; ++i)
|
||||||
{
|
{
|
||||||
@@ -144,23 +158,28 @@ void bench(int howmany, std::shared_ptr<spdlog::logger> log)
|
|||||||
auto delta = high_resolution_clock::now() - start;
|
auto delta = high_resolution_clock::now() - start;
|
||||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||||
|
|
||||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
|
spdlog::info(
|
||||||
|
fmt::format(std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(), delta_d, int(howmany / delta_d)));
|
||||||
spdlog::drop(log->name());
|
spdlog::drop(log->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count)
|
void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, size_t thread_count)
|
||||||
{
|
{
|
||||||
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
using std::chrono::high_resolution_clock;
|
using std::chrono::high_resolution_clock;
|
||||||
vector<thread> threads;
|
|
||||||
|
std::vector<std::thread> threads;
|
||||||
|
threads.reserve(thread_count);
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
for (int t = 0; t < thread_count; ++t)
|
for (size_t t = 0; t < thread_count; ++t)
|
||||||
{
|
{
|
||||||
threads.push_back(std::thread([&]() {
|
threads.emplace_back([&]() {
|
||||||
for (int j = 0; j < howmany / thread_count; j++)
|
for (int j = 0; j < howmany / static_cast<int>(thread_count); j++)
|
||||||
{
|
{
|
||||||
log->info("Hello logger: msg number {}", j);
|
log->info("Hello logger: msg number {}", j);
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &t : threads)
|
for (auto &t : threads)
|
||||||
@@ -170,13 +189,18 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
|
|||||||
|
|
||||||
auto delta = high_resolution_clock::now() - start;
|
auto delta = high_resolution_clock::now() - start;
|
||||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
|
spdlog::info(
|
||||||
|
fmt::format(std::locale("en_US.UTF-8"), "{:<30} Elapsed: {:0.2f} secs {:>16L}/sec", log->name(), delta_d, int(howmany / delta_d)));
|
||||||
spdlog::drop(log->name());
|
spdlog::drop(log->name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
|
void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||||
{
|
{
|
||||||
using std::chrono::high_resolution_clock;
|
using std::chrono::high_resolution_clock;
|
||||||
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
|
||||||
auto orig_default = spdlog::default_logger();
|
auto orig_default = spdlog::default_logger();
|
||||||
spdlog::set_default_logger(log);
|
spdlog::set_default_logger(log);
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
@@ -189,28 +213,34 @@ void bench_default_api(int howmany, std::shared_ptr<spdlog::logger> log)
|
|||||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||||
spdlog::drop(log->name());
|
spdlog::drop(log->name());
|
||||||
spdlog::set_default_logger(std::move(orig_default));
|
spdlog::set_default_logger(std::move(orig_default));
|
||||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
|
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany / delta_d));
|
||||||
}
|
}
|
||||||
|
|
||||||
void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log)
|
void bench_c_string(int howmany, std::shared_ptr<spdlog::logger> log)
|
||||||
{
|
{
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
|
||||||
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
||||||
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, eu consequat sem "
|
"lacus placerat congue. Nulla egestas, mauris a tincidunt tempus, enim lectus volutpat mi, eu consequat sem "
|
||||||
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec fringilla dui sed "
|
"libero nec massa. In dapibus ipsum a diam rhoncus gravida. Etiam non dapibus eros. Donec fringilla dui sed "
|
||||||
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, nisi turpis ornare "
|
"augue pretium, nec scelerisque est maximus. Nullam convallis, sem nec blandit maximus, nisi turpis ornare "
|
||||||
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
|
"nisl, sit amet volutpat neque massa eu odio. Maecenas malesuada quam ex, posuere congue nibh turpis duis.";
|
||||||
using std::chrono::high_resolution_clock;
|
|
||||||
auto orig_default = spdlog::default_logger();
|
auto orig_default = spdlog::default_logger();
|
||||||
spdlog::set_default_logger(log);
|
spdlog::set_default_logger(log);
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
for (auto i = 0; i < howmany; ++i)
|
for (auto i = 0; i < howmany; ++i)
|
||||||
{
|
{
|
||||||
spdlog::log(level::info, msg);
|
spdlog::log(spdlog::level::info, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto delta = high_resolution_clock::now() - start;
|
auto delta = high_resolution_clock::now() - start;
|
||||||
auto delta_d = duration_cast<duration<double>>(delta).count();
|
auto delta_d = duration_cast<duration<double>>(delta).count();
|
||||||
spdlog::drop(log->name());
|
spdlog::drop(log->name());
|
||||||
spdlog::set_default_logger(std::move(orig_default));
|
spdlog::set_default_logger(std::move(orig_default));
|
||||||
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16n}/sec", log->name(), delta_d, int(howmany / delta_d));
|
spdlog::info("{:<30} Elapsed: {:0.2f} secs {:>16}/sec", log->name(), delta_d, int(howmany / delta_d));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
#include "benchmark/benchmark.h"
|
#include "benchmark/benchmark.h"
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include "spdlog/pattern_formatter.h"
|
||||||
|
|
||||||
void bench_formatter(benchmark::State &state, std::string pattern)
|
void bench_formatter(benchmark::State &state, std::string pattern)
|
||||||
{
|
{
|
||||||
@@ -34,14 +34,14 @@ void bench_formatters()
|
|||||||
for (auto &flag : all_flags)
|
for (auto &flag : all_flags)
|
||||||
{
|
{
|
||||||
auto pattern = std::string("%") + flag;
|
auto pattern = std::string("%") + flag;
|
||||||
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
|
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||||
|
|
||||||
// pattern = std::string("%16") + flag;
|
// pattern = std::string("%16") + flag;
|
||||||
// benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
|
// benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||||
//
|
//
|
||||||
// // bench center padding
|
// // bench center padding
|
||||||
// pattern = std::string("%=16") + flag;
|
// pattern = std::string("%=16") + flag;
|
||||||
// benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
|
// benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
// complex patterns
|
// complex patterns
|
||||||
@@ -52,7 +52,7 @@ void bench_formatters()
|
|||||||
};
|
};
|
||||||
for (auto &pattern : patterns)
|
for (auto &pattern : patterns)
|
||||||
{
|
{
|
||||||
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern)->Iterations(2500000);
|
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern)->Iterations(2500000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +73,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
benchmark::RegisterBenchmark(pattern.c_str(), bench_formatter, pattern);
|
benchmark::RegisterBenchmark(pattern.c_str(), &bench_formatter, pattern);
|
||||||
}
|
}
|
||||||
benchmark::Initialize(&argc, argv);
|
benchmark::Initialize(&argc, argv);
|
||||||
benchmark::RunSpecifiedBenchmarks();
|
benchmark::RunSpecifiedBenchmarks();
|
||||||
|
|||||||
@@ -16,26 +16,6 @@
|
|||||||
#include "spdlog/sinks/null_sink.h"
|
#include "spdlog/sinks/null_sink.h"
|
||||||
#include "spdlog/sinks/rotating_file_sink.h"
|
#include "spdlog/sinks/rotating_file_sink.h"
|
||||||
|
|
||||||
void prepare_logdir()
|
|
||||||
{
|
|
||||||
spdlog::info("Preparing latency_logs directory..");
|
|
||||||
#ifdef _WIN32
|
|
||||||
system("if not exist logs mkdir latency_logs");
|
|
||||||
system("del /F /Q logs\\*");
|
|
||||||
#else
|
|
||||||
auto rv = system("mkdir -p latency_logs");
|
|
||||||
if (rv != 0)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Failed to mkdir -p latency_logs");
|
|
||||||
}
|
|
||||||
rv = system("rm -f latency_logs/*");
|
|
||||||
if (rv != 0)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Failed to rm -f latency_logs/*");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
void bench_c_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||||
{
|
{
|
||||||
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
const char *msg = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pharetra metus cursus "
|
||||||
@@ -59,6 +39,16 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void bench_logger_fmt_string(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for (auto _ : state)
|
||||||
|
{
|
||||||
|
logger->info(FMT_STRING("Hello logger: msg number {}..............."), ++i);
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -71,11 +61,21 @@ void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logge
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
void bench_dev_null()
|
||||||
|
{
|
||||||
|
auto dev_null_st = spdlog::basic_logger_st("/dev/null_st", "/dev/null");
|
||||||
|
benchmark::RegisterBenchmark("/dev/null_st", bench_logger, std::move(dev_null_st))->UseRealTime();
|
||||||
|
spdlog::drop("/dev/null_st");
|
||||||
|
|
||||||
|
auto dev_null_mt = spdlog::basic_logger_mt("/dev/null_mt", "/dev/null");
|
||||||
|
benchmark::RegisterBenchmark("/dev/null_mt", bench_logger, std::move(dev_null_mt))->UseRealTime();
|
||||||
|
spdlog::drop("/dev/null_mt");
|
||||||
|
}
|
||||||
|
#endif // __linux__
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
using spdlog::sinks::basic_file_sink_mt;
|
|
||||||
using spdlog::sinks::basic_file_sink_st;
|
|
||||||
using spdlog::sinks::null_sink_mt;
|
using spdlog::sinks::null_sink_mt;
|
||||||
using spdlog::sinks::null_sink_st;
|
using spdlog::sinks::null_sink_st;
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ int main(int argc, char *argv[])
|
|||||||
size_t rotating_files = 5;
|
size_t rotating_files = 5;
|
||||||
int n_threads = benchmark::CPUInfo::Get().num_cpus;
|
int n_threads = benchmark::CPUInfo::Get().num_cpus;
|
||||||
|
|
||||||
prepare_logdir();
|
auto full_bench = argc > 1 && std::string(argv[1]) == "full";
|
||||||
|
|
||||||
// disabled loggers
|
// disabled loggers
|
||||||
auto disabled_logger = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
auto disabled_logger = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||||
@@ -98,59 +98,67 @@ int main(int argc, char *argv[])
|
|||||||
auto null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
auto null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
||||||
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string, std::move(null_logger_st));
|
benchmark::RegisterBenchmark("null_sink_st (500_bytes c_str)", bench_c_string, std::move(null_logger_st));
|
||||||
benchmark::RegisterBenchmark("null_sink_st", bench_logger, null_logger_st);
|
benchmark::RegisterBenchmark("null_sink_st", bench_logger, null_logger_st);
|
||||||
|
benchmark::RegisterBenchmark("null_sink_fmt_string", bench_logger_fmt_string, null_logger_st);
|
||||||
// with backtrace of 64
|
// with backtrace of 64
|
||||||
auto tracing_null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
auto tracing_null_logger_st = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_st>());
|
||||||
tracing_null_logger_st->enable_backtrace(64);
|
tracing_null_logger_st->enable_backtrace(64);
|
||||||
benchmark::RegisterBenchmark("null_sink_st/backtrace", bench_logger, tracing_null_logger_st);
|
benchmark::RegisterBenchmark("null_sink_st/backtrace", bench_logger, tracing_null_logger_st);
|
||||||
|
|
||||||
// basic_st
|
#ifdef __linux
|
||||||
auto basic_st = spdlog::basic_logger_st("basic_st", "latency_logs/basic_st.log", true);
|
bench_dev_null();
|
||||||
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
|
#endif // __linux__
|
||||||
spdlog::drop("basic_st");
|
|
||||||
// with backtrace of 64
|
|
||||||
auto tracing_basic_st = spdlog::basic_logger_st("tracing_basic_st", "latency_logs/tracing_basic_st.log", true);
|
|
||||||
tracing_basic_st->enable_backtrace(64);
|
|
||||||
benchmark::RegisterBenchmark("basic_st/backtrace", bench_logger, std::move(tracing_basic_st))->UseRealTime();
|
|
||||||
spdlog::drop("tracing_basic_st");
|
|
||||||
|
|
||||||
// rotating st
|
if (full_bench)
|
||||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
|
{
|
||||||
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
|
// basic_st
|
||||||
spdlog::drop("rotating_st");
|
auto basic_st = spdlog::basic_logger_st("basic_st", "latency_logs/basic_st.log", true);
|
||||||
// with backtrace of 64
|
benchmark::RegisterBenchmark("basic_st", bench_logger, std::move(basic_st))->UseRealTime();
|
||||||
auto tracing_rotating_st =
|
spdlog::drop("basic_st");
|
||||||
spdlog::rotating_logger_st("tracing_rotating_st", "latency_logs/tracing_rotating_st.log", file_size, rotating_files);
|
// with backtrace of 64
|
||||||
benchmark::RegisterBenchmark("rotating_st/backtrace", bench_logger, std::move(tracing_rotating_st))->UseRealTime();
|
auto tracing_basic_st = spdlog::basic_logger_st("tracing_basic_st", "latency_logs/tracing_basic_st.log", true);
|
||||||
spdlog::drop("tracing_rotating_st");
|
tracing_basic_st->enable_backtrace(64);
|
||||||
|
benchmark::RegisterBenchmark("basic_st/backtrace", bench_logger, std::move(tracing_basic_st))->UseRealTime();
|
||||||
|
spdlog::drop("tracing_basic_st");
|
||||||
|
|
||||||
// daily st
|
// rotating st
|
||||||
auto daily_st = spdlog::daily_logger_mt("daily_st", "latency_logs/daily_st.log");
|
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "latency_logs/rotating_st.log", file_size, rotating_files);
|
||||||
benchmark::RegisterBenchmark("daily_st", bench_logger, std::move(daily_st))->UseRealTime();
|
benchmark::RegisterBenchmark("rotating_st", bench_logger, std::move(rotating_st))->UseRealTime();
|
||||||
spdlog::drop("daily_st");
|
spdlog::drop("rotating_st");
|
||||||
auto tracing_daily_st = spdlog::daily_logger_mt("tracing_daily_st", "latency_logs/daily_st.log");
|
// with backtrace of 64
|
||||||
benchmark::RegisterBenchmark("daily_st/backtrace", bench_logger, std::move(tracing_daily_st))->UseRealTime();
|
auto tracing_rotating_st =
|
||||||
spdlog::drop("tracing_daily_st");
|
spdlog::rotating_logger_st("tracing_rotating_st", "latency_logs/tracing_rotating_st.log", file_size, rotating_files);
|
||||||
|
benchmark::RegisterBenchmark("rotating_st/backtrace", bench_logger, std::move(tracing_rotating_st))->UseRealTime();
|
||||||
|
spdlog::drop("tracing_rotating_st");
|
||||||
|
|
||||||
//
|
// daily st
|
||||||
// Multi threaded bench, 10 loggers using same logger concurrently
|
auto daily_st = spdlog::daily_logger_mt("daily_st", "latency_logs/daily_st.log");
|
||||||
//
|
benchmark::RegisterBenchmark("daily_st", bench_logger, std::move(daily_st))->UseRealTime();
|
||||||
auto null_logger_mt = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
spdlog::drop("daily_st");
|
||||||
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)->Threads(n_threads)->UseRealTime();
|
auto tracing_daily_st = spdlog::daily_logger_mt("tracing_daily_st", "latency_logs/daily_st.log");
|
||||||
|
benchmark::RegisterBenchmark("daily_st/backtrace", bench_logger, std::move(tracing_daily_st))->UseRealTime();
|
||||||
|
spdlog::drop("tracing_daily_st");
|
||||||
|
|
||||||
// basic_mt
|
//
|
||||||
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "latency_logs/basic_mt.log", true);
|
// Multi threaded bench, 10 loggers using same logger concurrently
|
||||||
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
|
//
|
||||||
spdlog::drop("basic_mt");
|
auto null_logger_mt = std::make_shared<spdlog::logger>("bench", std::make_shared<null_sink_mt>());
|
||||||
|
benchmark::RegisterBenchmark("null_sink_mt", bench_logger, null_logger_mt)->Threads(n_threads)->UseRealTime();
|
||||||
|
|
||||||
// rotating mt
|
// basic_mt
|
||||||
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
|
auto basic_mt = spdlog::basic_logger_mt("basic_mt", "latency_logs/basic_mt.log", true);
|
||||||
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
|
benchmark::RegisterBenchmark("basic_mt", bench_logger, std::move(basic_mt))->Threads(n_threads)->UseRealTime();
|
||||||
spdlog::drop("rotating_mt");
|
spdlog::drop("basic_mt");
|
||||||
|
|
||||||
// daily mt
|
// rotating mt
|
||||||
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "latency_logs/daily_mt.log");
|
auto rotating_mt = spdlog::rotating_logger_mt("rotating_mt", "latency_logs/rotating_mt.log", file_size, rotating_files);
|
||||||
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))->Threads(n_threads)->UseRealTime();
|
benchmark::RegisterBenchmark("rotating_mt", bench_logger, std::move(rotating_mt))->Threads(n_threads)->UseRealTime();
|
||||||
spdlog::drop("daily_mt");
|
spdlog::drop("rotating_mt");
|
||||||
|
|
||||||
|
// daily mt
|
||||||
|
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "latency_logs/daily_mt.log");
|
||||||
|
benchmark::RegisterBenchmark("daily_mt", bench_logger, std::move(daily_mt))->Threads(n_threads)->UseRealTime();
|
||||||
|
spdlog::drop("daily_mt");
|
||||||
|
}
|
||||||
|
|
||||||
// async
|
// async
|
||||||
auto queue_size = 1024 * 1024 * 3;
|
auto queue_size = 1024 * 1024 * 3;
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
benchmark = dependency('benchmark')
|
|
||||||
|
|
||||||
bench_matrix = [
|
|
||||||
['bench', [spdlog_dep], []],
|
|
||||||
['async_bench', [spdlog_dep], []],
|
|
||||||
['formatter-bench', [spdlog_dep, benchmark], ['all']],
|
|
||||||
['latency', [spdlog_dep, benchmark], []],
|
|
||||||
]
|
|
||||||
|
|
||||||
foreach i : bench_matrix
|
|
||||||
bench_exe = executable(i[0], i[0] + '.cpp', dependencies: i[1])
|
|
||||||
benchmark('bench_' + i[0], bench_exe, args: i[2])
|
|
||||||
endforeach
|
|
||||||
|
|
||||||
run_command(find_program('mkdir'), meson.current_build_dir() + '/logs')
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# IDE support for headers
|
# IDE support for headers
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
set(SPDLOG_HEADERS_DIR "${CMAKE_CURRENT_LIST_DIR}/../include")
|
set(SPDLOG_HEADERS_DIR "${CMAKE_CURRENT_LIST_DIR}/../include")
|
||||||
|
|
||||||
file(GLOB SPDLOG_TOP_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/*.h")
|
file(GLOB SPDLOG_TOP_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/*.h")
|
||||||
@@ -8,11 +8,11 @@ file(GLOB SPDLOG_DETAILS_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/details/*.h")
|
|||||||
file(GLOB SPDLOG_SINKS_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/sinks/*.h")
|
file(GLOB SPDLOG_SINKS_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/sinks/*.h")
|
||||||
file(GLOB SPDLOG_FMT_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/*.h")
|
file(GLOB SPDLOG_FMT_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/*.h")
|
||||||
file(GLOB SPDLOG_FMT_BUNDELED_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/bundled/*.h")
|
file(GLOB SPDLOG_FMT_BUNDELED_HEADERS "${SPDLOG_HEADERS_DIR}/spdlog/fmt/bundled/*.h")
|
||||||
set(SPDLOG_ALL_HEADERS ${SPDLOG_TOP_HEADERS} ${SPDLOG_DETAILS_HEADERS} ${SPDLOG_SINKS_HEADERS} ${SPDLOG_FMT_HEADERS} ${SPDLOG_FMT_BUNDELED_HEADERS})
|
set(SPDLOG_ALL_HEADERS ${SPDLOG_TOP_HEADERS} ${SPDLOG_DETAILS_HEADERS} ${SPDLOG_SINKS_HEADERS} ${SPDLOG_FMT_HEADERS}
|
||||||
|
${SPDLOG_FMT_BUNDELED_HEADERS})
|
||||||
|
|
||||||
source_group("Header Files\\spdlog" FILES ${SPDLOG_TOP_HEADERS})
|
source_group("Header Files\\spdlog" FILES ${SPDLOG_TOP_HEADERS})
|
||||||
source_group("Header Files\\spdlog\\details" FILES ${SPDLOG_DETAILS_HEADERS})
|
source_group("Header Files\\spdlog\\details" FILES ${SPDLOG_DETAILS_HEADERS})
|
||||||
source_group("Header Files\\spdlog\\sinks" FILES ${SPDLOG_SINKS_HEADERS})
|
source_group("Header Files\\spdlog\\sinks" FILES ${SPDLOG_SINKS_HEADERS})
|
||||||
source_group("Header Files\\spdlog\\fmt" FILES ${SPDLOG_FMT_HEADERS})
|
source_group("Header Files\\spdlog\\fmt" FILES ${SPDLOG_FMT_HEADERS})
|
||||||
source_group("Header Files\\spdlog\\fmt\\bundled\\" FILES ${SPDLOG_FMT_BUNDELED_HEADERS})
|
source_group("Header Files\\spdlog\\fmt\\bundled\\" FILES ${SPDLOG_FMT_BUNDELED_HEADERS})
|
||||||
|
|
||||||
|
|||||||
258
cmake/pch.h.in
Normal file
258
cmake/pch.h.in
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// fmt/bin_to_hex.h
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// details/os-inl.h
|
||||||
|
// fmt/bundled/core.h
|
||||||
|
// fmt/bundled/posix.h
|
||||||
|
// logger-inl.h
|
||||||
|
// sinks/daily_file_sink.h
|
||||||
|
// sinks/stdout_sinks.h
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// fmt/bundled/posix.h
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// fmt/bundled/core.h
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/os.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/pattern_formatter.h
|
||||||
|
// fmt/bundled/chrono.h
|
||||||
|
// sinks/daily_file_sink.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <climits>
|
||||||
|
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <cwchar>
|
||||||
|
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
// fmt/bundled/format.h
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <cstdarg>
|
||||||
|
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// fmt/bundled/format.h
|
||||||
|
// fmt/bundled/posix.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
#include <cerrno>
|
||||||
|
|
||||||
|
// details/circular_q.h
|
||||||
|
// details/thread_pool-inl.h
|
||||||
|
// fmt/bundled/format-inl.h
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
// async_logger-inl.h
|
||||||
|
// cfg/helpers-inl.h
|
||||||
|
// log_levels.h
|
||||||
|
// common.h
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// details/log_msg.h
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/pattern_formatter.h
|
||||||
|
// details/registry-inl.h
|
||||||
|
// details/registry.h
|
||||||
|
// details/tcp_client-windows.h
|
||||||
|
// details/tcp_client.h
|
||||||
|
// fmt/bundled/core.h
|
||||||
|
// sinks/android_sink.h
|
||||||
|
// sinks/ansicolor_sink.h
|
||||||
|
// sinks/basic_file_sink.h
|
||||||
|
// sinks/daily_file_sink.h
|
||||||
|
// sinks/dup_filter_sink.h
|
||||||
|
// sinks/msvc_sink.h
|
||||||
|
// sinks/ringbuffer_sink.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
// sinks/rotating_file_sink.h
|
||||||
|
// sinks/syslog_sink.h
|
||||||
|
// sinks/tcp_sink.h
|
||||||
|
// sinks/win_eventlog_sink.h
|
||||||
|
// sinks/wincolor_sink.h
|
||||||
|
// spdlog.h:
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
// cfg/helpers-inl.h
|
||||||
|
// fmt/bundled/chrono.h
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// fmt/bundled/ostream.h
|
||||||
|
// sinks/ostream_sink.h
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
// cfg/log_levels.h
|
||||||
|
// details/registry-inl.h
|
||||||
|
// details/registry.h
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
// details/circular_q.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/pattern_formatter.h
|
||||||
|
// details/thread_pool.h
|
||||||
|
// fmt/bundled/compile.h
|
||||||
|
// logger.h
|
||||||
|
// sinks/dist_sink.h
|
||||||
|
// sinks/ringbuffer_sink.h
|
||||||
|
// sinks/win_eventlog_sink.h
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// sinks/ansicolor_sink.h
|
||||||
|
// sinks/syslog_sink.h
|
||||||
|
// sinks/systemd_sink.h
|
||||||
|
// sinks/wincolor_sink.h
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// details/file_helper.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// fmt/bundled/format.h
|
||||||
|
// fmt/bundled/printf.h
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
// details/backtracer.h
|
||||||
|
// details/null_mutex.h
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
// details/backtracer.h
|
||||||
|
// details/null_mutex.h
|
||||||
|
#include <locale>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
#include <initializer_list>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
// details/fmt_helper.h
|
||||||
|
// fmt/bundled/core.h
|
||||||
|
// fmt/bundled/ranges.h
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
// cfg/helpers-inl.h
|
||||||
|
// details/null_mutex.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
// async.h
|
||||||
|
// async_logger-inl.h
|
||||||
|
// common.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/pattern_formatter.h
|
||||||
|
// details/registry-inl.h
|
||||||
|
// details/registry.h
|
||||||
|
// details/thread_pool.h
|
||||||
|
// fmt/bundled/format.h
|
||||||
|
// sinks/ansicolor_sink.h
|
||||||
|
// sinks/base_sink-inl.h
|
||||||
|
// sinks/dist_sink.h
|
||||||
|
// sinks/stdout_sinks-inl.h
|
||||||
|
// sinks/wincolor_sink.h
|
||||||
|
// spdlog.h
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
// async.h
|
||||||
|
// common.h
|
||||||
|
// details/backtracer.h
|
||||||
|
// details/periodic_worker.h
|
||||||
|
// details/registry-inl.h
|
||||||
|
// details/registry.h
|
||||||
|
// details/thread_pool.h
|
||||||
|
// sinks/tcp_sink.h
|
||||||
|
// spdlog.h
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
// details/mpmc_blocking_q.h
|
||||||
|
// details/periodic_worker.h
|
||||||
|
#include <condition_variable>
|
||||||
|
|
||||||
|
// details/os-inl.h
|
||||||
|
// fmt/bundled/format.h
|
||||||
|
// fmt/bundled/printf.h
|
||||||
|
// sinks/dist_sink.h
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
// common.h
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// details/fmt_helper.h
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/pattern_formatter.h
|
||||||
|
// details/periodic_worker.h
|
||||||
|
// details/registry-inl.h
|
||||||
|
// details/registry.h
|
||||||
|
// details/thread_pool.h
|
||||||
|
// fmt/bundled/chrono.h
|
||||||
|
// sinks/android_sink.h
|
||||||
|
// sinks/daily_file_sink.h
|
||||||
|
// sinks/dup_filter_sink.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
// sinks/rotating_file_sink.h
|
||||||
|
// sinks/tcp_sink.h
|
||||||
|
// spdlog.h
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
// details/file_helper-inl.h
|
||||||
|
// details/os-inl.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/periodic_worker.h
|
||||||
|
// details/thread_pool.h
|
||||||
|
// sinks/android_sink.h
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
// async.h
|
||||||
|
// details/backtracer.h
|
||||||
|
// details/console_globals.h
|
||||||
|
// details/mpmc_blocking_q.h
|
||||||
|
// details/pattern_formatter-inl.h
|
||||||
|
// details/periodic_worker.h
|
||||||
|
// details/registry.h
|
||||||
|
// sinks/android_sink.h
|
||||||
|
// sinks/ansicolor_sink.h
|
||||||
|
// sinks/basic_file_sink.h
|
||||||
|
// sinks/daily_file_sink.h
|
||||||
|
// sinks/dist_sink.h
|
||||||
|
// sinks/dup_filter_sink.h
|
||||||
|
// sinks/msvc_sink.h
|
||||||
|
// sinks/null_sink.h
|
||||||
|
// sinks/ostream_sink.h
|
||||||
|
// sinks/ringbuffer_sink.h
|
||||||
|
// sinks/rotating_file_sink-inl.h
|
||||||
|
// sinks/rotating_file_sink.h
|
||||||
|
// sinks/tcp_sink.h
|
||||||
|
// sinks/win_eventlog_sink.h
|
||||||
|
// sinks/wincolor_sink.h
|
||||||
|
//
|
||||||
|
// color_sinks.cpp
|
||||||
|
// file_sinks.cpp
|
||||||
|
// spdlog.cpp
|
||||||
|
// stdout_sinks.cpp
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
// spdlog
|
||||||
|
#include <spdlog/common.h>
|
||||||
13
cmake/spdlog.pc.in
Normal file
13
cmake/spdlog.pc.in
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
prefix=@CMAKE_INSTALL_PREFIX@
|
||||||
|
exec_prefix=${prefix}
|
||||||
|
includedir=${prefix}/include
|
||||||
|
libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
|
||||||
|
|
||||||
|
Name: lib@PROJECT_NAME@
|
||||||
|
Description: Fast C++ logging library.
|
||||||
|
URL: https://github.com/gabime/@PROJECT_NAME@
|
||||||
|
Version: @SPDLOG_VERSION@
|
||||||
|
CFlags: -I${includedir} @PKG_CONFIG_DEFINES@
|
||||||
|
Libs: -L${libdir} -lspdlog -pthread
|
||||||
|
Requires: @PKG_CONFIG_REQUIRES@
|
||||||
|
|
||||||
@@ -1,26 +1,60 @@
|
|||||||
set(CPACK_GENERATOR
|
set(CPACK_GENERATOR "TGZ;ZIP" CACHE STRING "Semicolon separated list of generators")
|
||||||
TGZ
|
|
||||||
ZIP
|
|
||||||
)
|
|
||||||
|
|
||||||
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
|
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY 0)
|
||||||
set(CPACK_INSTALL_CMAKE_PROJECTS
|
set(CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_BINARY_DIR}" "${PROJECT_NAME}" ALL .)
|
||||||
"${CMAKE_BINARY_DIR}"
|
|
||||||
"${PROJECT_NAME}"
|
|
||||||
ALL
|
|
||||||
.
|
|
||||||
)
|
|
||||||
|
|
||||||
set(CPACK_PROJECT_URL "https://github.com/gabime/spdlog")
|
set(CPACK_PROJECT_URL "https://github.com/gabime/spdlog")
|
||||||
set(CPACK_PACKAGE_VENDOR "Gabi Melman")
|
set(CPACK_PACKAGE_VENDOR "Gabi Melman")
|
||||||
set(CPACK_PACKAGE_CONTACT "Gabi Melman <gmelman1@gmail.com>")
|
set(CPACK_PACKAGE_CONTACT "Gabi Melman <gmelman1@gmail.com>")
|
||||||
|
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fast C++ logging library")
|
||||||
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
|
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
|
||||||
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
|
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
|
||||||
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
|
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
|
||||||
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
|
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
|
||||||
if (PROJECT_VERSION_TWEAK)
|
if(PROJECT_VERSION_TWEAK)
|
||||||
set(CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION}.${PROJECT_VERSION_TWEAK})
|
set(CPACK_PACKAGE_VERSION ${CPACK_PACKAGE_VERSION}.${PROJECT_VERSION_TWEAK})
|
||||||
endif ()
|
endif()
|
||||||
set(CPACK_PACKAGE_RELOCATABLE ON)
|
set(CPACK_PACKAGE_RELOCATABLE ON CACHE BOOL "Build relocatable package")
|
||||||
|
|
||||||
|
set(CPACK_RPM_PACKAGE_LICENSE "MIT")
|
||||||
|
set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
|
||||||
|
set(CPACK_DEBIAN_PACKAGE_SECTION "libs")
|
||||||
|
set(CPACK_RPM_PACKAGE_URL ${CPACK_PROJECT_URL})
|
||||||
|
set(CPACK_DEBIAN_PACKAGE_HOMEPAGE ${CPACK_PROJECT_URL})
|
||||||
|
set(CPACK_RPM_PACKAGE_DESCRIPTION "Very fast, header-only/compiled, C++ logging library.")
|
||||||
|
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Very fast, header-only/compiled, C++ logging library.")
|
||||||
|
|
||||||
|
if(CPACK_PACKAGE_NAME)
|
||||||
|
set(CPACK_RPM_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||||
|
set(CPACK_DEBIAN_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||||
|
else()
|
||||||
|
set(CPACK_RPM_FILE_NAME "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||||
|
set(CPACK_DEBIAN_FILE_NAME "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||||
|
set(CPACK_RPM_PACKAGE_NAME "${PROJECT_NAME}")
|
||||||
|
set(CPACK_DEBIAN_PACKAGE_NAME "${PROJECT_NAME}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(CPACK_RPM_PACKAGE_RELEASE)
|
||||||
|
set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}-${CPACK_RPM_PACKAGE_RELEASE}")
|
||||||
|
endif()
|
||||||
|
if(CPACK_DEBIAN_PACKAGE_RELEASE)
|
||||||
|
set(CPACK_DEBIAN_FILE_NAME "${CPACK_DEBIAN_FILE_NAME}-${CPACK_DEBIAN_PACKAGE_RELEASE}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(CPACK_RPM_PACKAGE_ARCHITECTURE)
|
||||||
|
set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}.${CPACK_RPM_PACKAGE_ARCHITECTURE}")
|
||||||
|
endif()
|
||||||
|
if(CPACK_DEBIAN_PACKAGE_ARCHITECTURE)
|
||||||
|
set(CPACK_DEBIAN_FILE_NAME "${CPACK_DEBIAN_FILE_NAME}.${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}")
|
||||||
|
endif()
|
||||||
|
set(CPACK_RPM_FILE_NAME "${CPACK_RPM_FILE_NAME}.rpm")
|
||||||
|
set(CPACK_DEBIAN_FILE_NAME "${CPACK_DEBIAN_FILE_NAME}.deb")
|
||||||
|
|
||||||
|
if(NOT CPACK_PACKAGE_RELOCATABLE)
|
||||||
|
# Depend on pkgconfig rpm to create the system pkgconfig folder
|
||||||
|
set(CPACK_RPM_PACKAGE_REQUIRES pkgconfig)
|
||||||
|
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION
|
||||||
|
"${CPACK_PACKAGING_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig")
|
||||||
|
endif()
|
||||||
|
|
||||||
include(CPack)
|
include(CPack)
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
# Copyright(c) 2019 spdlog authors
|
# Copyright(c) 2019 spdlog authors
|
||||||
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
@PACKAGE_INIT@
|
||||||
|
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
set(SPDLOG_FMT_EXTERNAL @SPDLOG_FMT_EXTERNAL@)
|
set(SPDLOG_FMT_EXTERNAL @SPDLOG_FMT_EXTERNAL@)
|
||||||
@@ -13,3 +15,5 @@ endif()
|
|||||||
|
|
||||||
|
|
||||||
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
|
include("${CMAKE_CURRENT_LIST_DIR}/${config_targets_file}")
|
||||||
|
|
||||||
|
check_required_components(spdlog)
|
||||||
@@ -1,47 +1,62 @@
|
|||||||
# Get spdlog version from include/spdlog/version.h and put it in SPDLOG_VERSION
|
# Get spdlog version from include/spdlog/version.h and put it in SPDLOG_VERSION
|
||||||
function(spdlog_extract_version)
|
function(spdlog_extract_version)
|
||||||
file(READ "${CMAKE_CURRENT_LIST_DIR}/include/spdlog/version.h" file_contents)
|
file(READ "${CMAKE_CURRENT_LIST_DIR}/include/spdlog/version.h" file_contents)
|
||||||
string(REGEX MATCH "SPDLOG_VER_MAJOR ([0-9]+)" _ "${file_contents}")
|
string(REGEX MATCH "SPDLOG_VER_MAJOR ([0-9]+)" _ "${file_contents}")
|
||||||
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
||||||
message(FATAL_ERROR "Could not extract major version number from spdlog/version.h")
|
message(FATAL_ERROR "Could not extract major version number from spdlog/version.h")
|
||||||
endif()
|
endif()
|
||||||
set(ver_major ${CMAKE_MATCH_1})
|
set(ver_major ${CMAKE_MATCH_1})
|
||||||
|
|
||||||
string(REGEX MATCH "SPDLOG_VER_MINOR ([0-9]+)" _ "${file_contents}")
|
string(REGEX MATCH "SPDLOG_VER_MINOR ([0-9]+)" _ "${file_contents}")
|
||||||
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
||||||
message(FATAL_ERROR "Could not extract minor version number from spdlog/version.h")
|
message(FATAL_ERROR "Could not extract minor version number from spdlog/version.h")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(ver_minor ${CMAKE_MATCH_1})
|
set(ver_minor ${CMAKE_MATCH_1})
|
||||||
string(REGEX MATCH "SPDLOG_VER_PATCH ([0-9]+)" _ "${file_contents}")
|
string(REGEX MATCH "SPDLOG_VER_PATCH ([0-9]+)" _ "${file_contents}")
|
||||||
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
|
||||||
message(FATAL_ERROR "Could not extract patch version number from spdlog/version.h")
|
message(FATAL_ERROR "Could not extract patch version number from spdlog/version.h")
|
||||||
endif()
|
endif()
|
||||||
set(ver_patch ${CMAKE_MATCH_1})
|
set(ver_patch ${CMAKE_MATCH_1})
|
||||||
|
|
||||||
set(SPDLOG_VERSION_MAJOR ${ver_major} PARENT_SCOPE)
|
set(SPDLOG_VERSION_MAJOR ${ver_major} PARENT_SCOPE)
|
||||||
set (SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE)
|
set(SPDLOG_VERSION_MINOR ${ver_minor} PARENT_SCOPE)
|
||||||
endfunction()
|
set(SPDLOG_VERSION_PATCH ${ver_patch} PARENT_SCOPE)
|
||||||
|
set(SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE)
|
||||||
|
endfunction()
|
||||||
# Turn on warnings on the given target
|
|
||||||
function(spdlog_enable_warnings target_name)
|
# Turn on warnings on the given target
|
||||||
target_compile_options(${target_name} PRIVATE
|
function(spdlog_enable_warnings target_name)
|
||||||
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
|
if(SPDLOG_BUILD_WARNINGS)
|
||||||
-Wall -Wextra -Wconversion -pedantic -Wfatal-errors>
|
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
|
||||||
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>)
|
list(APPEND MSVC_OPTIONS "/W3")
|
||||||
endfunction()
|
if(MSVC_VERSION GREATER 1900) # Allow non fatal security warnings for msvc 2015
|
||||||
|
list(APPEND MSVC_OPTIONS "/WX")
|
||||||
|
endif()
|
||||||
# Enable address sanitizer (gcc/clang only)
|
endif()
|
||||||
function(spdlog_enable_sanitizer target_name)
|
|
||||||
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
target_compile_options(
|
||||||
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
|
${target_name}
|
||||||
endif()
|
PRIVATE $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
|
||||||
message(STATUS "Address sanitizer enabled")
|
-Wall
|
||||||
target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
|
-Wextra
|
||||||
target_compile_options(${target_name} PRIVATE -fno-sanitize=signed-integer-overflow)
|
-Wconversion
|
||||||
target_compile_options(${target_name} PRIVATE -fno-sanitize-recover=all)
|
-pedantic
|
||||||
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
|
-Werror
|
||||||
target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined -fuse-ld=gold)
|
-Wfatal-errors>
|
||||||
endfunction()
|
$<$<CXX_COMPILER_ID:MSVC>:${MSVC_OPTIONS}>)
|
||||||
|
endif()
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
# Enable address sanitizer (gcc/clang only)
|
||||||
|
function(spdlog_enable_sanitizer target_name)
|
||||||
|
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
|
||||||
|
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
|
||||||
|
endif()
|
||||||
|
message(STATUS "Address sanitizer enabled")
|
||||||
|
target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
|
||||||
|
target_compile_options(${target_name} PRIVATE -fno-sanitize=signed-integer-overflow)
|
||||||
|
target_compile_options(${target_name} PRIVATE -fno-sanitize-recover=all)
|
||||||
|
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
|
||||||
|
target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined -fuse-ld=gold)
|
||||||
|
endfunction()
|
||||||
|
|||||||
42
cmake/version.rc.in
Normal file
42
cmake/version.rc.in
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#define APSTUDIO_READONLY_SYMBOLS
|
||||||
|
#include <windows.h>
|
||||||
|
#undef APSTUDIO_READONLY_SYMBOLS
|
||||||
|
|
||||||
|
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
|
||||||
|
|
||||||
|
|
||||||
|
VS_VERSION_INFO VERSIONINFO
|
||||||
|
FILEVERSION @SPDLOG_VERSION_MAJOR@,@SPDLOG_VERSION_MINOR@,@SPDLOG_VERSION_PATCH@,0
|
||||||
|
PRODUCTVERSION @SPDLOG_VERSION_MAJOR@,@SPDLOG_VERSION_MINOR@,@SPDLOG_VERSION_PATCH@,0
|
||||||
|
FILEFLAGSMASK 0x3fL
|
||||||
|
#ifdef _DEBUG
|
||||||
|
FILEFLAGS 0x1L
|
||||||
|
#else
|
||||||
|
FILEFLAGS 0x0L
|
||||||
|
#endif
|
||||||
|
FILEOS 0x40004L
|
||||||
|
FILETYPE 0x2L
|
||||||
|
FILESUBTYPE 0x0L
|
||||||
|
BEGIN
|
||||||
|
BLOCK "StringFileInfo"
|
||||||
|
BEGIN
|
||||||
|
BLOCK "040904b0"
|
||||||
|
BEGIN
|
||||||
|
VALUE "FileDescription", "spdlog dll\0"
|
||||||
|
VALUE "FileVersion", "@SPDLOG_VERSION@.0\0"
|
||||||
|
VALUE "InternalName", "spdlog.dll\0"
|
||||||
|
VALUE "LegalCopyright", "Copyright (C) spdlog\0"
|
||||||
|
VALUE "ProductName", "spdlog\0"
|
||||||
|
VALUE "ProductVersion", "@SPDLOG_VERSION@.0\0"
|
||||||
|
END
|
||||||
|
END
|
||||||
|
BLOCK "VarFileInfo"
|
||||||
|
BEGIN
|
||||||
|
VALUE "Translation", 0x409, 1200
|
||||||
|
END
|
||||||
|
END
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
# Copyright(c) 2019 spdlog authors
|
# Copyright(c) 2019 spdlog authors Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
# Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.1)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(spdlog_examples CXX)
|
project(spdlog_examples CXX)
|
||||||
|
|
||||||
if(NOT TARGET spdlog)
|
if(NOT TARGET spdlog)
|
||||||
@@ -9,21 +8,16 @@ if(NOT TARGET spdlog)
|
|||||||
find_package(spdlog REQUIRED)
|
find_package(spdlog REQUIRED)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Example of using pre-compiled library
|
# Example of using pre-compiled library
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
add_executable(example example.cpp)
|
add_executable(example example.cpp)
|
||||||
spdlog_enable_warnings(example)
|
|
||||||
target_link_libraries(example PRIVATE spdlog::spdlog)
|
target_link_libraries(example PRIVATE spdlog::spdlog)
|
||||||
|
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
# Example of using header-only library
|
# Example of using header-only library
|
||||||
#---------------------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------------------
|
||||||
if(SPDLOG_BUILD_EXAMPLE_HO)
|
if(SPDLOG_BUILD_EXAMPLE_HO)
|
||||||
add_executable(example_header_only example.cpp)
|
add_executable(example_header_only example.cpp)
|
||||||
spdlog_enable_warnings(example_header_only)
|
|
||||||
target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only)
|
target_link_libraries(example_header_only PRIVATE spdlog::spdlog_header_only)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Create logs directory
|
|
||||||
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs")
|
|
||||||
|
|||||||
@@ -6,23 +6,32 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
|
void load_levels_example();
|
||||||
void stdout_logger_example();
|
void stdout_logger_example();
|
||||||
void basic_example();
|
void basic_example();
|
||||||
void rotating_example();
|
void rotating_example();
|
||||||
void daily_example();
|
void daily_example();
|
||||||
void async_example();
|
void async_example();
|
||||||
void binary_example();
|
void binary_example();
|
||||||
|
void stopwatch_example();
|
||||||
void trace_example();
|
void trace_example();
|
||||||
void multi_sink_example();
|
void multi_sink_example();
|
||||||
void user_defined_example();
|
void user_defined_example();
|
||||||
void err_handler_example();
|
void err_handler_example();
|
||||||
void syslog_example();
|
void syslog_example();
|
||||||
|
void custom_flags_example();
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
#include "spdlog/cfg/env.h" // support for loading levels from the environment variable
|
||||||
|
#include "spdlog/fmt/ostr.h" // support for user defined types
|
||||||
|
|
||||||
int main(int, char *[])
|
int main(int, char *[])
|
||||||
{
|
{
|
||||||
|
// Log levels can be loaded from argv/env using "SPDLOG_LEVEL"
|
||||||
|
load_levels_example();
|
||||||
|
|
||||||
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
|
spdlog::info("Welcome to spdlog version {}.{}.{} !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
|
||||||
|
|
||||||
spdlog::warn("Easy padding in numbers like {:08d}", 12);
|
spdlog::warn("Easy padding in numbers like {:08d}", 12);
|
||||||
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
spdlog::critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
|
||||||
spdlog::info("Support for floats {:03.2f}", 1.23456);
|
spdlog::info("Support for floats {:03.2f}", 1.23456);
|
||||||
@@ -64,6 +73,8 @@ int main(int, char *[])
|
|||||||
user_defined_example();
|
user_defined_example();
|
||||||
err_handler_example();
|
err_handler_example();
|
||||||
trace_example();
|
trace_example();
|
||||||
|
stopwatch_example();
|
||||||
|
custom_flags_example();
|
||||||
|
|
||||||
// Flush all *registered* loggers using a worker thread every 3 seconds.
|
// Flush all *registered* loggers using a worker thread every 3 seconds.
|
||||||
// note: registered loggers *must* be thread safe for this to work correctly!
|
// note: registered loggers *must* be thread safe for this to work correctly!
|
||||||
@@ -116,6 +127,18 @@ void daily_example()
|
|||||||
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
auto daily_logger = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "spdlog/cfg/env.h"
|
||||||
|
void load_levels_example()
|
||||||
|
{
|
||||||
|
// Set the log level to "info" and mylogger to "trace":
|
||||||
|
// SPDLOG_LEVEL=info,mylogger=trace && ./example
|
||||||
|
spdlog::cfg::load_env_levels();
|
||||||
|
// or from command line:
|
||||||
|
// ./example SPDLOG_LEVEL=info,mylogger=trace
|
||||||
|
// #include "spdlog/cfg/argv.h" // for loading levels from argv
|
||||||
|
// spdlog::cfg::load_argv_levels(args, argv);
|
||||||
|
}
|
||||||
|
|
||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
void async_example()
|
void async_example()
|
||||||
{
|
{
|
||||||
@@ -143,7 +166,7 @@ void async_example()
|
|||||||
#include "spdlog/fmt/bin_to_hex.h"
|
#include "spdlog/fmt/bin_to_hex.h"
|
||||||
void binary_example()
|
void binary_example()
|
||||||
{
|
{
|
||||||
std::vector<char> buf;
|
std::vector<char> buf(80);
|
||||||
for (int i = 0; i < 80; i++)
|
for (int i = 0; i < 80; i++)
|
||||||
{
|
{
|
||||||
buf.push_back(static_cast<char>(i & 0xff));
|
buf.push_back(static_cast<char>(i & 0xff));
|
||||||
@@ -154,6 +177,8 @@ void binary_example()
|
|||||||
// logger->info("uppercase: {:X}", spdlog::to_hex(buf));
|
// logger->info("uppercase: {:X}", spdlog::to_hex(buf));
|
||||||
// logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
|
// logger->info("uppercase, no delimiters: {:Xs}", spdlog::to_hex(buf));
|
||||||
// logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf));
|
// logger->info("uppercase, no delimiters, no position info: {:Xsp}", spdlog::to_hex(buf));
|
||||||
|
// logger->info("hexdump style: {:a}", spdlog::to_hex(buf));
|
||||||
|
// logger->info("hexdump style, 20 chars per line {:a}", spdlog::to_hex(buf, 20));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compile time log levels.
|
// Compile time log levels.
|
||||||
@@ -170,6 +195,16 @@ void trace_example()
|
|||||||
SPDLOG_LOGGER_TRACE(logger, "another trace message");
|
SPDLOG_LOGGER_TRACE(logger, "another trace message");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// stopwatch example
|
||||||
|
#include "spdlog/stopwatch.h"
|
||||||
|
#include <thread>
|
||||||
|
void stopwatch_example()
|
||||||
|
{
|
||||||
|
spdlog::stopwatch sw;
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(123));
|
||||||
|
spdlog::info("Stopwatch: {} seconds", sw);
|
||||||
|
}
|
||||||
|
|
||||||
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
|
// A logger with multiple sinks (stdout and file) - each with a different format and log level.
|
||||||
void multi_sink_example()
|
void multi_sink_example()
|
||||||
{
|
{
|
||||||
@@ -187,7 +222,6 @@ void multi_sink_example()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// User defined types logging by implementing operator<<
|
// User defined types logging by implementing operator<<
|
||||||
#include "spdlog/fmt/ostr.h" // must be included
|
|
||||||
struct my_type
|
struct my_type
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
@@ -212,7 +246,7 @@ void err_handler_example()
|
|||||||
|
|
||||||
// syslog example (linux/osx/freebsd)
|
// syslog example (linux/osx/freebsd)
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include "spdlog/sinks/syslog_sink.h"
|
# include "spdlog/sinks/syslog_sink.h"
|
||||||
void syslog_example()
|
void syslog_example()
|
||||||
{
|
{
|
||||||
std::string ident = "spdlog-example";
|
std::string ident = "spdlog-example";
|
||||||
@@ -223,12 +257,38 @@ void syslog_example()
|
|||||||
|
|
||||||
// Android example.
|
// Android example.
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
#include "spdlog/sinks/android_sink.h"
|
# include "spdlog/sinks/android_sink.h"
|
||||||
void android_example()
|
void android_example()
|
||||||
{
|
{
|
||||||
std::string tag = "spdlog-android";
|
std::string tag = "spdlog-android";
|
||||||
auto android_logger = spdlog::android_logger_mt("android", tag);
|
auto android_logger = spdlog::android_logger_mt("android", tag);
|
||||||
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
android_logger->critical("Use \"adb shell logcat\" to view this message.");
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Log patterns can contain custom flags.
|
||||||
|
// this will add custom flag '%*' which will be bound to a <my_formatter_flag> instance
|
||||||
|
#include "spdlog/pattern_formatter.h"
|
||||||
|
class my_formatter_flag : public spdlog::custom_flag_formatter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
|
||||||
|
{
|
||||||
|
std::string some_txt = "custom-flag";
|
||||||
|
dest.append(some_txt.data(), some_txt.data() + some_txt.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<custom_flag_formatter> clone() const override
|
||||||
|
{
|
||||||
|
return spdlog::details::make_unique<my_formatter_flag>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void custom_flags_example()
|
||||||
|
{
|
||||||
|
|
||||||
|
using spdlog::details::make_unique; // for pre c++14
|
||||||
|
auto formatter = make_unique<spdlog::pattern_formatter>();
|
||||||
|
formatter->add_flag<my_formatter_flag>('*').set_pattern("[%n] [%*] [%^%l%$] %v");
|
||||||
|
spdlog::set_formatter(std::move(formatter));
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
executable('example', 'example.cpp', dependencies: spdlog_dep)
|
|
||||||
executable('example_header_only', 'example.cpp', dependencies: spdlog_headeronly_dep)
|
|
||||||
run_command(find_program('mkdir'), meson.current_build_dir() + '/logs')
|
|
||||||
|
|
||||||
|
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
//
|
//
|
||||||
// Async logging using global thread pool
|
// Async logging using global thread pool
|
||||||
// All loggers created here share same global thread pool.
|
// All loggers created here share same global thread pool.
|
||||||
// Each log message is pushed to a queue along withe a shared pointer to the
|
// Each log message is pushed to a queue along with a shared pointer to the
|
||||||
// logger.
|
// logger.
|
||||||
// If a logger deleted while having pending messages in the queue, it's actual
|
// If a logger deleted while having pending messages in the queue, it's actual
|
||||||
// destruction will defer
|
// destruction will defer
|
||||||
@@ -14,9 +14,9 @@
|
|||||||
// This is because each message in the queue holds a shared_ptr to the
|
// This is because each message in the queue holds a shared_ptr to the
|
||||||
// originating logger.
|
// originating logger.
|
||||||
|
|
||||||
#include "spdlog/async_logger.h"
|
#include <spdlog/async_logger.h>
|
||||||
#include "spdlog/details/registry.h"
|
#include <spdlog/details/registry.h>
|
||||||
#include "spdlog/details/thread_pool.h"
|
#include <spdlog/details/thread_pool.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -35,7 +35,7 @@ template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
|
|||||||
struct async_factory_impl
|
struct async_factory_impl
|
||||||
{
|
{
|
||||||
template<typename Sink, typename... SinkArgs>
|
template<typename Sink, typename... SinkArgs>
|
||||||
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
|
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&...args)
|
||||||
{
|
{
|
||||||
auto ®istry_inst = details::registry::instance();
|
auto ®istry_inst = details::registry::instance();
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ struct async_factory_impl
|
|||||||
auto tp = registry_inst.get_tp();
|
auto tp = registry_inst.get_tp();
|
||||||
if (tp == nullptr)
|
if (tp == nullptr)
|
||||||
{
|
{
|
||||||
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
|
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1U);
|
||||||
registry_inst.set_tp(tp);
|
registry_inst.set_tp(tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,13 +61,13 @@ using async_factory = async_factory_impl<async_overflow_policy::block>;
|
|||||||
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
|
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;
|
||||||
|
|
||||||
template<typename Sink, typename... SinkArgs>
|
template<typename Sink, typename... SinkArgs>
|
||||||
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
|
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&...sink_args)
|
||||||
{
|
{
|
||||||
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Sink, typename... SinkArgs>
|
template<typename Sink, typename... SinkArgs>
|
||||||
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
|
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&...sink_args)
|
||||||
{
|
{
|
||||||
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/async_logger.h"
|
# include <spdlog/async_logger.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/sinks/sink.h"
|
#include <spdlog/sinks/sink.h>
|
||||||
#include "spdlog/details/thread_pool.h"
|
#include <spdlog/details/thread_pool.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -32,7 +32,7 @@ SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("async log: thread pool doesn't exist anymore"));
|
throw_spdlog_ex("async log: thread pool doesn't exist anymore");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ SPDLOG_INLINE void spdlog::async_logger::flush_()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("async flush: thread pool doesn't exist anymore"));
|
throw_spdlog_ex("async flush: thread pool doesn't exist anymore");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
// Upon destruction, logs all remaining messages in the queue before
|
// Upon destruction, logs all remaining messages in the queue before
|
||||||
// destructing..
|
// destructing..
|
||||||
|
|
||||||
#include "spdlog/logger.h"
|
#include <spdlog/logger.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ namespace details {
|
|||||||
class thread_pool;
|
class thread_pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
class async_logger final : public std::enable_shared_from_this<async_logger>, public logger
|
class SPDLOG_API async_logger final : public std::enable_shared_from_this<async_logger>, public logger
|
||||||
{
|
{
|
||||||
friend class details::thread_pool;
|
friend class details::thread_pool;
|
||||||
|
|
||||||
@@ -64,5 +64,5 @@ private:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "async_logger-inl.h"
|
# include "async_logger-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
44
include/spdlog/cfg/argv.h
Normal file
44
include/spdlog/cfg/argv.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <spdlog/cfg/helpers.h>
|
||||||
|
#include <spdlog/details/registry.h>
|
||||||
|
|
||||||
|
//
|
||||||
|
// Init log levels using each argv entry that starts with "SPDLOG_LEVEL="
|
||||||
|
//
|
||||||
|
// set all loggers to debug level:
|
||||||
|
// example.exe "SPDLOG_LEVEL=debug"
|
||||||
|
|
||||||
|
// set logger1 to trace level
|
||||||
|
// example.exe "SPDLOG_LEVEL=logger1=trace"
|
||||||
|
|
||||||
|
// turn off all logging except for logger1 and logger2:
|
||||||
|
// example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info"
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace cfg {
|
||||||
|
|
||||||
|
// search for SPDLOG_LEVEL= in the args and use it to init the levels
|
||||||
|
inline void load_argv_levels(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
const std::string spdlog_level_prefix = "SPDLOG_LEVEL=";
|
||||||
|
for (int i = 1; i < argc; i++)
|
||||||
|
{
|
||||||
|
std::string arg = argv[i];
|
||||||
|
if (arg.find(spdlog_level_prefix) == 0)
|
||||||
|
{
|
||||||
|
auto levels_string = arg.substr(spdlog_level_prefix.size());
|
||||||
|
helpers::load_levels(levels_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void load_argv_levels(int argc, char **argv)
|
||||||
|
{
|
||||||
|
load_argv_levels(argc, const_cast<const char **>(argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace cfg
|
||||||
|
} // namespace spdlog
|
||||||
38
include/spdlog/cfg/env.h
Normal file
38
include/spdlog/cfg/env.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <spdlog/cfg/helpers.h>
|
||||||
|
#include <spdlog/details/registry.h>
|
||||||
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
|
//
|
||||||
|
// Init levels and patterns from env variables SPDLOG_LEVEL
|
||||||
|
// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger).
|
||||||
|
// Note - fallback to "info" level on unrecognized levels
|
||||||
|
//
|
||||||
|
// Examples:
|
||||||
|
//
|
||||||
|
// set global level to debug:
|
||||||
|
// export SPDLOG_LEVEL=debug
|
||||||
|
//
|
||||||
|
// turn off all logging except for logger1:
|
||||||
|
// export SPDLOG_LEVEL="*=off,logger1=debug"
|
||||||
|
//
|
||||||
|
|
||||||
|
// turn off all logging except for logger1 and logger2:
|
||||||
|
// export SPDLOG_LEVEL="off,logger1=debug,logger2=info"
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace cfg {
|
||||||
|
inline void load_env_levels()
|
||||||
|
{
|
||||||
|
auto env_val = details::os::getenv("SPDLOG_LEVEL");
|
||||||
|
if (!env_val.empty())
|
||||||
|
{
|
||||||
|
helpers::load_levels(env_val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace cfg
|
||||||
|
} // namespace spdlog
|
||||||
120
include/spdlog/cfg/helpers-inl.h
Normal file
120
include/spdlog/cfg/helpers-inl.h
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
|
# include <spdlog/cfg/helpers.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <spdlog/details/os.h>
|
||||||
|
#include <spdlog/details/registry.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace cfg {
|
||||||
|
namespace helpers {
|
||||||
|
|
||||||
|
// inplace convert to lowercase
|
||||||
|
inline std::string &to_lower_(std::string &str)
|
||||||
|
{
|
||||||
|
std::transform(
|
||||||
|
str.begin(), str.end(), str.begin(), [](char ch) { return static_cast<char>((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); });
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// inplace trim spaces
|
||||||
|
inline std::string &trim_(std::string &str)
|
||||||
|
{
|
||||||
|
const char *spaces = " \n\r\t";
|
||||||
|
str.erase(str.find_last_not_of(spaces) + 1);
|
||||||
|
str.erase(0, str.find_first_not_of(spaces));
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return (name,value) trimmed pair from given "name=value" string.
|
||||||
|
// return empty string on missing parts
|
||||||
|
// "key=val" => ("key", "val")
|
||||||
|
// " key = val " => ("key", "val")
|
||||||
|
// "key=" => ("key", "")
|
||||||
|
// "val" => ("", "val")
|
||||||
|
|
||||||
|
inline std::pair<std::string, std::string> extract_kv_(char sep, const std::string &str)
|
||||||
|
{
|
||||||
|
auto n = str.find(sep);
|
||||||
|
std::string k, v;
|
||||||
|
if (n == std::string::npos)
|
||||||
|
{
|
||||||
|
v = str;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
k = str.substr(0, n);
|
||||||
|
v = str.substr(n + 1);
|
||||||
|
}
|
||||||
|
return std::make_pair(trim_(k), trim_(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
// return vector of key/value pairs from sequence of "K1=V1,K2=V2,.."
|
||||||
|
// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...}
|
||||||
|
inline std::unordered_map<std::string, std::string> extract_key_vals_(const std::string &str)
|
||||||
|
{
|
||||||
|
std::string token;
|
||||||
|
std::istringstream token_stream(str);
|
||||||
|
std::unordered_map<std::string, std::string> rv{};
|
||||||
|
while (std::getline(token_stream, token, ','))
|
||||||
|
{
|
||||||
|
if (token.empty())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto kv = extract_kv_('=', token);
|
||||||
|
rv[kv.first] = kv.second;
|
||||||
|
}
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void load_levels(const std::string &input)
|
||||||
|
{
|
||||||
|
if (input.empty() || input.size() > 512)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto key_vals = extract_key_vals_(input);
|
||||||
|
std::unordered_map<std::string, level::level_enum> levels;
|
||||||
|
level::level_enum global_level = level::info;
|
||||||
|
bool global_level_found = false;
|
||||||
|
|
||||||
|
for (auto &name_level : key_vals)
|
||||||
|
{
|
||||||
|
auto &logger_name = name_level.first;
|
||||||
|
auto level_name = to_lower_(name_level.second);
|
||||||
|
auto level = level::from_str(level_name);
|
||||||
|
// ignore unrecognized level names
|
||||||
|
if (level == level::off && level_name != "off")
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (logger_name.empty()) // no logger name indicate global level
|
||||||
|
{
|
||||||
|
global_level_found = true;
|
||||||
|
global_level = level;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
levels[logger_name] = level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
details::registry::instance().set_levels(std::move(levels), global_level_found ? &global_level : nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace helpers
|
||||||
|
} // namespace cfg
|
||||||
|
} // namespace spdlog
|
||||||
29
include/spdlog/cfg/helpers.h
Normal file
29
include/spdlog/cfg/helpers.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace cfg {
|
||||||
|
namespace helpers {
|
||||||
|
//
|
||||||
|
// Init levels from given string
|
||||||
|
//
|
||||||
|
// Examples:
|
||||||
|
//
|
||||||
|
// set global level to debug: "debug"
|
||||||
|
// turn off all logging except for logger1: "off,logger1=debug"
|
||||||
|
// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info"
|
||||||
|
//
|
||||||
|
SPDLOG_API void load_levels(const std::string &txt);
|
||||||
|
} // namespace helpers
|
||||||
|
|
||||||
|
} // namespace cfg
|
||||||
|
} // namespace spdlog
|
||||||
|
|
||||||
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
|
# include "helpers-inl.h"
|
||||||
|
#endif // SPDLOG_HEADER_ONLY
|
||||||
@@ -4,16 +4,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/common.h"
|
# include <spdlog/common.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace level {
|
namespace level {
|
||||||
static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
constexpr
|
||||||
|
#endif
|
||||||
|
static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES;
|
||||||
|
|
||||||
static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES;
|
static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES;
|
||||||
|
|
||||||
SPDLOG_INLINE string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
return level_string_views[l];
|
return level_string_views[l];
|
||||||
}
|
}
|
||||||
@@ -25,14 +32,18 @@ SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOE
|
|||||||
|
|
||||||
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
int level = 0;
|
auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name);
|
||||||
for (const auto &level_str : level_string_views)
|
if (it != std::end(level_string_views))
|
||||||
|
return static_cast<level::level_enum>(it - std::begin(level_string_views));
|
||||||
|
|
||||||
|
// check also for "warn" and "err" before giving up..
|
||||||
|
if (name == "warn")
|
||||||
{
|
{
|
||||||
if (level_str == name)
|
return level::warn;
|
||||||
{
|
}
|
||||||
return static_cast<level::level_enum>(level);
|
if (name == "err")
|
||||||
}
|
{
|
||||||
level++;
|
return level::err;
|
||||||
}
|
}
|
||||||
return level::off;
|
return level::off;
|
||||||
}
|
}
|
||||||
@@ -45,7 +56,7 @@ SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg)
|
|||||||
SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno)
|
SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno)
|
||||||
{
|
{
|
||||||
memory_buf_t outbuf;
|
memory_buf_t outbuf;
|
||||||
fmt::format_system_error(outbuf, last_errno, msg);
|
fmt::format_system_error(outbuf, last_errno, msg.c_str());
|
||||||
msg_ = fmt::to_string(outbuf);
|
msg_ = fmt::to_string(outbuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,4 +65,14 @@ SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT
|
|||||||
return msg_.c_str();
|
return msg_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
|
||||||
|
{
|
||||||
|
SPDLOG_THROW(spdlog_ex(msg, last_errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void throw_spdlog_ex(std::string msg)
|
||||||
|
{
|
||||||
|
SPDLOG_THROW(spdlog_ex(std::move(msg)));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/tweakme.h"
|
#include <spdlog/tweakme.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -15,69 +15,78 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#ifndef NOMINMAX
|
|
||||||
#define NOMINMAX // prevent windows redefining min/max
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
#endif //_WIN32
|
|
||||||
|
|
||||||
#ifdef SPDLOG_COMPILED_LIB
|
#ifdef SPDLOG_COMPILED_LIB
|
||||||
#undef SPDLOG_HEADER_ONLY
|
# undef SPDLOG_HEADER_ONLY
|
||||||
#define SPDLOG_INLINE
|
# if defined(_WIN32) && defined(SPDLOG_SHARED_LIB)
|
||||||
#else
|
# ifdef spdlog_EXPORTS
|
||||||
#define SPDLOG_HEADER_ONLY
|
# define SPDLOG_API __declspec(dllexport)
|
||||||
#define SPDLOG_INLINE inline
|
# else
|
||||||
#endif
|
# define SPDLOG_API __declspec(dllimport)
|
||||||
|
# endif
|
||||||
|
# else // !defined(_WIN32) || !defined(SPDLOG_SHARED_LIB)
|
||||||
|
# define SPDLOG_API
|
||||||
|
# endif
|
||||||
|
# define SPDLOG_INLINE
|
||||||
|
#else // !defined(SPDLOG_COMPILED_LIB)
|
||||||
|
# define SPDLOG_API
|
||||||
|
# define SPDLOG_HEADER_ONLY
|
||||||
|
# define SPDLOG_INLINE inline
|
||||||
|
#endif // #ifdef SPDLOG_COMPILED_LIB
|
||||||
|
|
||||||
#include "spdlog/fmt/fmt.h"
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
|
||||||
|
// backward compatibility with fmt versions older than 8
|
||||||
|
#if FMT_VERSION >= 80000
|
||||||
|
# define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string)
|
||||||
|
# if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT)
|
||||||
|
# include <spdlog/fmt/xchar.h>
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
# define SPDLOG_FMT_RUNTIME(format_string) format_string
|
||||||
|
#endif
|
||||||
|
|
||||||
// visual studio upto 2013 does not support noexcept nor constexpr
|
// visual studio upto 2013 does not support noexcept nor constexpr
|
||||||
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
||||||
#define SPDLOG_NOEXCEPT _NOEXCEPT
|
# define SPDLOG_NOEXCEPT _NOEXCEPT
|
||||||
#define SPDLOG_CONSTEXPR
|
# define SPDLOG_CONSTEXPR
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_NOEXCEPT noexcept
|
# define SPDLOG_NOEXCEPT noexcept
|
||||||
#define SPDLOG_CONSTEXPR constexpr
|
# define SPDLOG_CONSTEXPR constexpr
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__GNUC__) || defined(__clang__)
|
#if defined(__GNUC__) || defined(__clang__)
|
||||||
#define SPDLOG_DEPRECATED __attribute__((deprecated))
|
# define SPDLOG_DEPRECATED __attribute__((deprecated))
|
||||||
#elif defined(_MSC_VER)
|
#elif defined(_MSC_VER)
|
||||||
#define SPDLOG_DEPRECATED __declspec(deprecated)
|
# define SPDLOG_DEPRECATED __declspec(deprecated)
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_DEPRECATED
|
# define SPDLOG_DEPRECATED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// disable thread local on msvc 2013
|
// disable thread local on msvc 2013
|
||||||
#ifndef SPDLOG_NO_TLS
|
#ifndef SPDLOG_NO_TLS
|
||||||
#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
|
# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt)
|
||||||
#define SPDLOG_NO_TLS 1
|
# define SPDLOG_NO_TLS 1
|
||||||
#endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef SPDLOG_FUNCTION
|
#ifndef SPDLOG_FUNCTION
|
||||||
#define SPDLOG_FUNCTION __FUNCTION__
|
# define SPDLOG_FUNCTION static_cast<const char *>(__FUNCTION__)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef SPDLOG_NO_EXCEPTIONS
|
#ifdef SPDLOG_NO_EXCEPTIONS
|
||||||
#define SPDLOG_TRY
|
# define SPDLOG_TRY
|
||||||
#define SPDLOG_THROW(ex) \
|
# define SPDLOG_THROW(ex) \
|
||||||
do \
|
do \
|
||||||
{ \
|
{ \
|
||||||
printf("spdlog fatal error: %s\n", ex.what()); \
|
printf("spdlog fatal error: %s\n", ex.what()); \
|
||||||
std::abort(); \
|
std::abort(); \
|
||||||
} while (0)
|
} while (0)
|
||||||
#define SPDLOG_CATCH_ALL()
|
# define SPDLOG_CATCH_STD
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_TRY try
|
# define SPDLOG_TRY try
|
||||||
#define SPDLOG_THROW(ex) throw(ex)
|
# define SPDLOG_THROW(ex) throw(ex)
|
||||||
#define SPDLOG_CATCH_ALL() catch (...)
|
# define SPDLOG_CATCH_STD \
|
||||||
|
catch (const std::exception &) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@@ -90,10 +99,12 @@ class sink;
|
|||||||
|
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
using filename_t = std::wstring;
|
using filename_t = std::wstring;
|
||||||
#define SPDLOG_FILENAME_T(s) L##s
|
// allow macro expansion to occur in SPDLOG_FILENAME_T
|
||||||
|
# define SPDLOG_FILENAME_T_INNER(s) L##s
|
||||||
|
# define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s)
|
||||||
#else
|
#else
|
||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
#define SPDLOG_FILENAME_T(s) s
|
# define SPDLOG_FILENAME_T(s) s
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using log_clock = std::chrono::system_clock;
|
using log_clock = std::chrono::system_clock;
|
||||||
@@ -101,23 +112,31 @@ using sink_ptr = std::shared_ptr<sinks::sink>;
|
|||||||
using sinks_init_list = std::initializer_list<sink_ptr>;
|
using sinks_init_list = std::initializer_list<sink_ptr>;
|
||||||
using err_handler = std::function<void(const std::string &err_msg)>;
|
using err_handler = std::function<void(const std::string &err_msg)>;
|
||||||
using string_view_t = fmt::basic_string_view<char>;
|
using string_view_t = fmt::basic_string_view<char>;
|
||||||
|
using wstring_view_t = fmt::basic_string_view<wchar_t>;
|
||||||
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
|
using memory_buf_t = fmt::basic_memory_buffer<char, 250>;
|
||||||
|
using wmemory_buf_t = fmt::basic_memory_buffer<wchar_t, 250>;
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
using remove_cvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
|
||||||
|
|
||||||
|
// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the condition from basic_format_string here,
|
||||||
|
// in addition, fmt::basic_runtime<Char> is only convertible to basic_format_string<Char> but not basic_string_view<Char>
|
||||||
|
template<class T, class Char = char>
|
||||||
|
struct is_convertible_to_basic_format_string
|
||||||
|
: std::integral_constant<bool,
|
||||||
|
std::is_convertible<T, fmt::basic_string_view<Char>>::value || std::is_same<remove_cvref_t<T>, fmt::basic_runtime<Char>>::value>
|
||||||
|
{};
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
#ifndef _WIN32
|
# ifndef _WIN32
|
||||||
#error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
|
# error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
|
||||||
#else
|
# endif // _WIN32
|
||||||
using wstring_view_t = basic_string_view_t<wchar_t>;
|
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
|
||||||
template<typename T>
|
template<class T>
|
||||||
struct is_convertible_to_wstring_view : std::is_convertible<T, wstring_view_t>
|
struct is_convertible_to_any_format_string : std::integral_constant<bool, is_convertible_to_basic_format_string<T, char>::value ||
|
||||||
|
is_convertible_to_basic_format_string<T, wchar_t>::value>
|
||||||
{};
|
{};
|
||||||
#endif // _WIN32
|
|
||||||
#else
|
|
||||||
template<typename>
|
|
||||||
struct is_convertible_to_wstring_view : std::false_type
|
|
||||||
{};
|
|
||||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
|
||||||
|
|
||||||
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
#if defined(SPDLOG_NO_ATOMIC_LEVELS)
|
||||||
using level_t = details::null_atomic_int;
|
using level_t = details::null_atomic_int;
|
||||||
@@ -134,7 +153,7 @@ using level_t = std::atomic<int>;
|
|||||||
#define SPDLOG_LEVEL_OFF 6
|
#define SPDLOG_LEVEL_OFF 6
|
||||||
|
|
||||||
#if !defined(SPDLOG_ACTIVE_LEVEL)
|
#if !defined(SPDLOG_ACTIVE_LEVEL)
|
||||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
|
# define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Log level enum
|
// Log level enum
|
||||||
@@ -148,28 +167,37 @@ enum level_enum
|
|||||||
err = SPDLOG_LEVEL_ERROR,
|
err = SPDLOG_LEVEL_ERROR,
|
||||||
critical = SPDLOG_LEVEL_CRITICAL,
|
critical = SPDLOG_LEVEL_CRITICAL,
|
||||||
off = SPDLOG_LEVEL_OFF,
|
off = SPDLOG_LEVEL_OFF,
|
||||||
|
n_levels
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5)
|
||||||
|
#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5)
|
||||||
|
#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4)
|
||||||
|
#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7)
|
||||||
|
#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5)
|
||||||
|
#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8)
|
||||||
|
#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3)
|
||||||
|
|
||||||
#if !defined(SPDLOG_LEVEL_NAMES)
|
#if !defined(SPDLOG_LEVEL_NAMES)
|
||||||
#define SPDLOG_LEVEL_NAMES \
|
# define SPDLOG_LEVEL_NAMES \
|
||||||
{ \
|
{ \
|
||||||
"trace", "debug", "info", "warning", "error", "critical", "off" \
|
SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, \
|
||||||
}
|
SPDLOG_LEVEL_NAME_CRITICAL, SPDLOG_LEVEL_NAME_OFF \
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
|
#if !defined(SPDLOG_SHORT_LEVEL_NAMES)
|
||||||
|
|
||||||
#define SPDLOG_SHORT_LEVEL_NAMES \
|
# define SPDLOG_SHORT_LEVEL_NAMES \
|
||||||
{ \
|
{ \
|
||||||
"T", "D", "I", "W", "E", "C", "O" \
|
"T", "D", "I", "W", "E", "C", "O" \
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
|
SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
|
||||||
const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
|
SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT;
|
||||||
spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
|
SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
using level_hasher = std::hash<int>;
|
|
||||||
} // namespace level
|
} // namespace level
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -195,7 +223,7 @@ enum class pattern_time_type
|
|||||||
//
|
//
|
||||||
// Log exception
|
// Log exception
|
||||||
//
|
//
|
||||||
class spdlog_ex : public std::exception
|
class SPDLOG_API spdlog_ex : public std::exception
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit spdlog_ex(std::string msg);
|
explicit spdlog_ex(std::string msg);
|
||||||
@@ -206,6 +234,9 @@ private:
|
|||||||
std::string msg_;
|
std::string msg_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno);
|
||||||
|
[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg);
|
||||||
|
|
||||||
struct source_loc
|
struct source_loc
|
||||||
{
|
{
|
||||||
SPDLOG_CONSTEXPR source_loc() = default;
|
SPDLOG_CONSTEXPR source_loc() = default;
|
||||||
@@ -231,16 +262,15 @@ namespace details {
|
|||||||
using std::make_unique;
|
using std::make_unique;
|
||||||
#else
|
#else
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
std::unique_ptr<T> make_unique(Args &&... args)
|
std::unique_ptr<T> make_unique(Args &&...args)
|
||||||
{
|
{
|
||||||
static_assert(!std::is_array<T>::value, "arrays not supported");
|
static_assert(!std::is_array<T>::value, "arrays not supported");
|
||||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "common-inl.h"
|
# include "common-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/backtracer.h"
|
# include <spdlog/details/backtracer.h>
|
||||||
#endif
|
#endif
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
@@ -26,7 +26,7 @@ SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other)
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
enabled_ = other.enabled();
|
enabled_ = other.enabled();
|
||||||
messages_ = other.messages_;
|
messages_ = std::move(other.messages_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,11 +48,6 @@ SPDLOG_INLINE bool backtracer::enabled() const
|
|||||||
return enabled_.load(std::memory_order_relaxed);
|
return enabled_.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE backtracer::operator bool() const
|
|
||||||
{
|
|
||||||
return enabled();
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
|
SPDLOG_INLINE void backtracer::push_back(const log_msg &msg)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock{mutex_};
|
std::lock_guard<std::mutex> lock{mutex_};
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/log_msg_buffer.h"
|
#include <spdlog/details/log_msg_buffer.h>
|
||||||
#include "spdlog/details/circular_q.h"
|
#include <spdlog/details/circular_q.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
class backtracer
|
class SPDLOG_API backtracer
|
||||||
{
|
{
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
std::atomic<bool> enabled_{false};
|
std::atomic<bool> enabled_{false};
|
||||||
@@ -31,7 +31,6 @@ public:
|
|||||||
void enable(size_t size);
|
void enable(size_t size);
|
||||||
void disable();
|
void disable();
|
||||||
bool enabled() const;
|
bool enabled() const;
|
||||||
explicit operator bool() const;
|
|
||||||
void push_back(const log_msg &msg);
|
void push_back(const log_msg &msg);
|
||||||
|
|
||||||
// pop all items in the q and apply the given fun on each of them.
|
// pop all items in the q and apply the given fun on each of them.
|
||||||
@@ -42,5 +41,5 @@ public:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "backtracer-inl.h"
|
# include "backtracer-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
// cirucal q view of std::vector.
|
// circular q view of std::vector.
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
@@ -62,16 +63,37 @@ public:
|
|||||||
|
|
||||||
// Return reference to the front item.
|
// Return reference to the front item.
|
||||||
// If there are no elements in the container, the behavior is undefined.
|
// If there are no elements in the container, the behavior is undefined.
|
||||||
const T& front() const
|
const T &front() const
|
||||||
{
|
{
|
||||||
return v_[head_];
|
return v_[head_];
|
||||||
}
|
}
|
||||||
|
|
||||||
T& front()
|
T &front()
|
||||||
{
|
{
|
||||||
return v_[head_];
|
return v_[head_];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return number of elements actually stored
|
||||||
|
size_t size() const
|
||||||
|
{
|
||||||
|
if (tail_ >= head_)
|
||||||
|
{
|
||||||
|
return tail_ - head_;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return max_items_ - (head_ - tail_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return const reference to item by index.
|
||||||
|
// If index is out of range 0…size()-1, the behavior is undefined.
|
||||||
|
const T &at(size_t i) const
|
||||||
|
{
|
||||||
|
assert(i < size());
|
||||||
|
return v_[(head_ + i) % max_items_];
|
||||||
|
}
|
||||||
|
|
||||||
// Pop item from front.
|
// Pop item from front.
|
||||||
// If there are no elements in the container, the behavior is undefined.
|
// If there are no elements in the container, the behavior is undefined.
|
||||||
void pop_front()
|
void pop_front()
|
||||||
@@ -87,7 +109,7 @@ public:
|
|||||||
bool full() const
|
bool full() const
|
||||||
{
|
{
|
||||||
// head is ahead of the tail by 1
|
// head is ahead of the tail by 1
|
||||||
if(max_items_ > 0)
|
if (max_items_ > 0)
|
||||||
{
|
{
|
||||||
return ((tail_ + 1) % max_items_) == head_;
|
return ((tail_ + 1) % max_items_) == head_;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/file_helper.h"
|
# include <spdlog/details/file_helper.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -28,28 +28,46 @@ SPDLOG_INLINE file_helper::~file_helper()
|
|||||||
SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
|
SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
|
filename_ = fname;
|
||||||
_filename = fname;
|
|
||||||
for (int tries = 0; tries < open_tries; ++tries)
|
auto *mode = SPDLOG_FILENAME_T("ab");
|
||||||
|
auto *trunc_mode = SPDLOG_FILENAME_T("wb");
|
||||||
|
|
||||||
|
for (int tries = 0; tries < open_tries_; ++tries)
|
||||||
{
|
{
|
||||||
|
// create containing folder if not exists already.
|
||||||
|
os::create_dir(os::dir_name(fname));
|
||||||
|
if (truncate)
|
||||||
|
{
|
||||||
|
// Truncate by opening-and-closing a tmp file in "wb" mode, always
|
||||||
|
// opening the actual log-we-write-to in "ab" mode, since that
|
||||||
|
// interacts more politely with eternal processes that might
|
||||||
|
// rotate/truncate the file underneath us.
|
||||||
|
std::FILE *tmp;
|
||||||
|
if (os::fopen_s(&tmp, fname, trunc_mode))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
std::fclose(tmp);
|
||||||
|
}
|
||||||
if (!os::fopen_s(&fd_, fname, mode))
|
if (!os::fopen_s(&fd_, fname, mode))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
details::os::sleep_for_millis(open_interval);
|
details::os::sleep_for_millis(open_interval_);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_THROW(spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno));
|
throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void file_helper::reopen(bool truncate)
|
SPDLOG_INLINE void file_helper::reopen(bool truncate)
|
||||||
{
|
{
|
||||||
if (_filename.empty())
|
if (filename_.empty())
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("Failed re opening file - was not opened before"));
|
throw_spdlog_ex("Failed re opening file - was not opened before");
|
||||||
}
|
}
|
||||||
open(_filename, truncate);
|
this->open(filename_, truncate);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void file_helper::flush()
|
SPDLOG_INLINE void file_helper::flush()
|
||||||
@@ -72,7 +90,7 @@ SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf)
|
|||||||
auto data = buf.data();
|
auto data = buf.data();
|
||||||
if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
|
if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno));
|
throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,19 +98,14 @@ SPDLOG_INLINE size_t file_helper::size() const
|
|||||||
{
|
{
|
||||||
if (fd_ == nullptr)
|
if (fd_ == nullptr)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename)));
|
throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_));
|
||||||
}
|
}
|
||||||
return os::filesize(fd_);
|
return os::filesize(fd_);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE const filename_t &file_helper::filename() const
|
SPDLOG_INLINE const filename_t &file_helper::filename() const
|
||||||
{
|
{
|
||||||
return _filename;
|
return filename_;
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE bool file_helper::file_exists(const filename_t &fname)
|
|
||||||
{
|
|
||||||
return os::file_exists(fname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -119,8 +132,8 @@ SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension
|
|||||||
return std::make_tuple(fname, filename_t());
|
return std::make_tuple(fname, filename_t());
|
||||||
}
|
}
|
||||||
|
|
||||||
// treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
|
// treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
|
||||||
auto folder_index = fname.rfind(details::os::folder_sep);
|
auto folder_index = fname.find_last_of(details::os::folder_seps_filename);
|
||||||
if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
|
if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
|
||||||
{
|
{
|
||||||
return std::make_tuple(fname, filename_t());
|
return std::make_tuple(fname, filename_t());
|
||||||
@@ -129,5 +142,6 @@ SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension
|
|||||||
// finally - return a valid base and extension tuple
|
// finally - return a valid base and extension tuple
|
||||||
return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
|
return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@@ -13,7 +13,7 @@ namespace details {
|
|||||||
// When failing to open a file, retry several times(5) with a delay interval(10 ms).
|
// When failing to open a file, retry several times(5) with a delay interval(10 ms).
|
||||||
// Throw spdlog_ex exception on errors.
|
// Throw spdlog_ex exception on errors.
|
||||||
|
|
||||||
class file_helper
|
class SPDLOG_API file_helper
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit file_helper() = default;
|
explicit file_helper() = default;
|
||||||
@@ -29,7 +29,6 @@ public:
|
|||||||
void write(const memory_buf_t &buf);
|
void write(const memory_buf_t &buf);
|
||||||
size_t size() const;
|
size_t size() const;
|
||||||
const filename_t &filename() const;
|
const filename_t &filename() const;
|
||||||
static bool file_exists(const filename_t &fname);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// return file path and its extension:
|
// return file path and its extension:
|
||||||
@@ -47,14 +46,14 @@ public:
|
|||||||
static std::tuple<filename_t, filename_t> split_by_extension(const filename_t &fname);
|
static std::tuple<filename_t, filename_t> split_by_extension(const filename_t &fname);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int open_tries = 5;
|
const int open_tries_ = 5;
|
||||||
const int open_interval = 10;
|
const unsigned int open_interval_ = 10;
|
||||||
std::FILE *fd_{nullptr};
|
std::FILE *fd_{nullptr};
|
||||||
filename_t _filename;
|
filename_t filename_;
|
||||||
};
|
};
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "file_helper-inl.h"
|
# include "file_helper-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "spdlog/fmt/fmt.h"
|
#include <iterator>
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
// Some fmt helpers to efficiently format and pad ints and strings
|
// Some fmt helpers to efficiently format and pad ints and strings
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@@ -20,10 +21,7 @@ inline spdlog::string_view_t to_string_view(const memory_buf_t &buf) SPDLOG_NOEX
|
|||||||
inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest)
|
inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest)
|
||||||
{
|
{
|
||||||
auto *buf_ptr = view.data();
|
auto *buf_ptr = view.data();
|
||||||
if (buf_ptr != nullptr)
|
dest.append(buf_ptr, buf_ptr + view.size());
|
||||||
{
|
|
||||||
dest.append(buf_ptr, buf_ptr + view.size());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -34,31 +32,30 @@ inline void append_int(T n, memory_buf_t &dest)
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline unsigned count_digits(T n)
|
inline unsigned int count_digits(T n)
|
||||||
{
|
{
|
||||||
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
using count_type = typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type;
|
||||||
return static_cast<unsigned>(fmt::internal::count_digits(static_cast<count_type>(n)));
|
return static_cast<unsigned int>(fmt::
|
||||||
|
// fmt 7.0.0 renamed the internal namespace to detail.
|
||||||
|
// See: https://github.com/fmtlib/fmt/issues/1538
|
||||||
|
#if FMT_VERSION < 70000
|
||||||
|
internal
|
||||||
|
#else
|
||||||
|
detail
|
||||||
|
#endif
|
||||||
|
::count_digits(static_cast<count_type>(n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void pad2(int n, memory_buf_t &dest)
|
inline void pad2(int n, memory_buf_t &dest)
|
||||||
{
|
{
|
||||||
if (n > 99)
|
if (n >= 0 && n < 100) // 0-99
|
||||||
{
|
|
||||||
append_int(n, dest);
|
|
||||||
}
|
|
||||||
else if (n > 9) // 10-99
|
|
||||||
{
|
{
|
||||||
dest.push_back(static_cast<char>('0' + n / 10));
|
dest.push_back(static_cast<char>('0' + n / 10));
|
||||||
dest.push_back(static_cast<char>('0' + n % 10));
|
dest.push_back(static_cast<char>('0' + n % 10));
|
||||||
}
|
}
|
||||||
else if (n >= 0) // 0-9
|
else // unlikely, but just in case, let fmt deal with it
|
||||||
{
|
{
|
||||||
dest.push_back('0');
|
fmt::format_to(std::back_inserter(dest), SPDLOG_FMT_RUNTIME("{:02}"), n);
|
||||||
dest.push_back(static_cast<char>('0' + n));
|
|
||||||
}
|
|
||||||
else // negatives (unlikely, but just in case, let fmt deal with it)
|
|
||||||
{
|
|
||||||
fmt::format_to(dest, "{:02}", n);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,11 +63,9 @@ template<typename T>
|
|||||||
inline void pad_uint(T n, unsigned int width, memory_buf_t &dest)
|
inline void pad_uint(T n, unsigned int width, memory_buf_t &dest)
|
||||||
{
|
{
|
||||||
static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
|
static_assert(std::is_unsigned<T>::value, "pad_uint must get unsigned T");
|
||||||
auto digits = count_digits(n);
|
for (auto digits = count_digits(n); digits < width; digits++)
|
||||||
if (width > digits)
|
|
||||||
{
|
{
|
||||||
const char *zeroes = "0000000000000000000";
|
dest.push_back('0');
|
||||||
dest.append(zeroes, zeroes + width - digits);
|
|
||||||
}
|
}
|
||||||
append_int(n, dest);
|
append_int(n, dest);
|
||||||
}
|
}
|
||||||
@@ -78,7 +73,18 @@ inline void pad_uint(T n, unsigned int width, memory_buf_t &dest)
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
inline void pad3(T n, memory_buf_t &dest)
|
inline void pad3(T n, memory_buf_t &dest)
|
||||||
{
|
{
|
||||||
pad_uint(n, 3, dest);
|
static_assert(std::is_unsigned<T>::value, "pad3 must get unsigned T");
|
||||||
|
if (n < 1000)
|
||||||
|
{
|
||||||
|
dest.push_back(static_cast<char>(n / 100 + '0'));
|
||||||
|
n = n % 100;
|
||||||
|
dest.push_back(static_cast<char>((n / 10) + '0'));
|
||||||
|
dest.push_back(static_cast<char>((n % 10) + '0'));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
append_int(n, dest);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
@@ -4,21 +4,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/log_msg.h"
|
# include <spdlog/details/log_msg.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
SPDLOG_INLINE log_msg::log_msg(spdlog::log_clock::time_point log_time, spdlog::source_loc loc, string_view_t a_logger_name,
|
||||||
: logger_name(logger_name)
|
spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
||||||
|
: logger_name(a_logger_name)
|
||||||
, level(lvl)
|
, level(lvl)
|
||||||
#ifndef SPDLOG_NO_DATETIME
|
, time(log_time)
|
||||||
, time(os::now())
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_THREAD_ID
|
#ifndef SPDLOG_NO_THREAD_ID
|
||||||
, thread_id(os::thread_id())
|
, thread_id(os::thread_id())
|
||||||
#endif
|
#endif
|
||||||
@@ -26,8 +24,13 @@ SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, string_view_t logger_name
|
|||||||
, payload(msg)
|
, payload(msg)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg::log_msg(string_view_t logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
SPDLOG_INLINE log_msg::log_msg(
|
||||||
: log_msg(source_loc{}, logger_name, lvl, msg)
|
spdlog::source_loc loc, string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
||||||
|
: log_msg(os::now(), loc, a_logger_name, lvl, msg)
|
||||||
|
{}
|
||||||
|
|
||||||
|
SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name, spdlog::level::level_enum lvl, spdlog::string_view_t msg)
|
||||||
|
: log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|||||||
@@ -3,17 +3,19 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
struct log_msg
|
struct SPDLOG_API log_msg
|
||||||
{
|
{
|
||||||
log_msg() = default;
|
log_msg() = default;
|
||||||
|
log_msg(log_clock::time_point log_time, source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg);
|
||||||
log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg);
|
log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg);
|
||||||
log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg);
|
log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg);
|
||||||
log_msg(const log_msg &other) = default;
|
log_msg(const log_msg &other) = default;
|
||||||
|
log_msg &operator=(const log_msg &other) = default;
|
||||||
|
|
||||||
string_view_t logger_name;
|
string_view_t logger_name;
|
||||||
level::level_enum level{level::off};
|
level::level_enum level{level::off};
|
||||||
@@ -31,5 +33,5 @@ struct log_msg
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "log_msg-inl.h"
|
# include "log_msg-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,57 +4,55 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/log_msg_buffer.h"
|
# include <spdlog/details/log_msg_buffer.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg &orig_msg)
|
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg &orig_msg)
|
||||||
: log_msg{orig_msg}
|
: log_msg{orig_msg}
|
||||||
{
|
{
|
||||||
buffer.append(logger_name.begin(), logger_name.end());
|
buffer.append(logger_name.begin(), logger_name.end());
|
||||||
buffer.append(payload.begin(), payload.end());
|
buffer.append(payload.begin(), payload.end());
|
||||||
update_string_views();
|
update_string_views();
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
|
|
||||||
: log_msg{other}
|
|
||||||
{
|
|
||||||
buffer.append(logger_name.begin(), logger_name.end());
|
|
||||||
buffer.append(payload.begin(), payload.end());
|
|
||||||
update_string_views();
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer:: log_msg_buffer(log_msg_buffer &&other)
|
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other)
|
||||||
: log_msg{std::move(other)}
|
: log_msg{other}
|
||||||
, buffer{std::move(other.buffer)}
|
{
|
||||||
{
|
buffer.append(logger_name.begin(), logger_name.end());
|
||||||
update_string_views();
|
buffer.append(payload.begin(), payload.end());
|
||||||
}
|
update_string_views();
|
||||||
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other)
|
SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT : log_msg{other}, buffer{std::move(other.buffer)}
|
||||||
{
|
{
|
||||||
log_msg::operator=(other);
|
update_string_views();
|
||||||
buffer.clear();
|
}
|
||||||
buffer.append(other.buffer.data(), other.buffer.data() + other.buffer.size());
|
|
||||||
update_string_views();
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other)
|
SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other)
|
||||||
{
|
{
|
||||||
log_msg::operator=(std::move(other));
|
log_msg::operator=(other);
|
||||||
buffer = std::move(other.buffer);
|
buffer.clear();
|
||||||
update_string_views();
|
buffer.append(other.buffer.data(), other.buffer.data() + other.buffer.size());
|
||||||
return *this;
|
update_string_views();
|
||||||
}
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void log_msg_buffer::update_string_views()
|
SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
logger_name = string_view_t{buffer.data(), logger_name.size()};
|
log_msg::operator=(other);
|
||||||
payload = string_view_t{buffer.data()+logger_name.size(), payload.size()};
|
buffer = std::move(other.buffer);
|
||||||
}
|
update_string_views();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void log_msg_buffer::update_string_views()
|
||||||
|
{
|
||||||
|
logger_name = string_view_t{buffer.data(), logger_name.size()};
|
||||||
|
payload = string_view_t{buffer.data() + logger_name.size(), payload.size()};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -3,32 +3,31 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
// Extend log_msg with internal buffer to store its payload.
|
// Extend log_msg with internal buffer to store its payload.
|
||||||
// THis is needed since log_msg holds string_views that points to stack data.
|
// This is needed since log_msg holds string_views that points to stack data.
|
||||||
|
|
||||||
class log_msg_buffer : public log_msg
|
class SPDLOG_API log_msg_buffer : public log_msg
|
||||||
{
|
{
|
||||||
memory_buf_t buffer;
|
memory_buf_t buffer;
|
||||||
void update_string_views();
|
void update_string_views();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
log_msg_buffer() = default;
|
log_msg_buffer() = default;
|
||||||
explicit log_msg_buffer(const log_msg &orig_msg);
|
explicit log_msg_buffer(const log_msg &orig_msg);
|
||||||
log_msg_buffer(const log_msg_buffer &other);
|
log_msg_buffer(const log_msg_buffer &other);
|
||||||
log_msg_buffer(log_msg_buffer &&other);
|
log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT;
|
||||||
log_msg_buffer &operator=(const log_msg_buffer &other);
|
log_msg_buffer &operator=(const log_msg_buffer &other);
|
||||||
log_msg_buffer &operator=(log_msg_buffer &&other);
|
log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "log_msg_buffer-inl.h"
|
# include "log_msg_buffer-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
// dequeue_for(..) - will block until the queue is not empty or timeout have
|
// dequeue_for(..) - will block until the queue is not empty or timeout have
|
||||||
// passed.
|
// passed.
|
||||||
|
|
||||||
#include "spdlog/details/circular_q.h"
|
#include <spdlog/details/circular_q.h>
|
||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -70,7 +70,7 @@ public:
|
|||||||
// apparently mingw deadlocks if the mutex is released before cv.notify_one(),
|
// apparently mingw deadlocks if the mutex is released before cv.notify_one(),
|
||||||
// so release the mutex at the very end each function.
|
// so release the mutex at the very end each function.
|
||||||
|
|
||||||
// try to enqueue and block if no room left
|
// try to enqueue and block if no room left
|
||||||
void enqueue(T &&item)
|
void enqueue(T &&item)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(queue_mutex_);
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
||||||
@@ -85,7 +85,6 @@ public:
|
|||||||
std::unique_lock<std::mutex> lock(queue_mutex_);
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
||||||
q_.push_back(std::move(item));
|
q_.push_back(std::move(item));
|
||||||
push_cv_.notify_one();
|
push_cv_.notify_one();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to dequeue item. if no item found. wait upto timeout and try again
|
// try to dequeue item. if no item found. wait upto timeout and try again
|
||||||
@@ -111,6 +110,12 @@ public:
|
|||||||
return q_.overrun_counter();
|
return q_.overrun_counter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t size()
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(queue_mutex_);
|
||||||
|
return q_.size();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex queue_mutex_;
|
std::mutex queue_mutex_;
|
||||||
std::condition_variable push_cv_;
|
std::condition_variable push_cv_;
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/os.h"
|
# include <spdlog/details/os.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -23,50 +23,45 @@
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
#ifndef NOMINMAX
|
# include <io.h> // _get_osfhandle and _isatty support
|
||||||
#define NOMINMAX // prevent windows redefining min/max
|
# include <process.h> // _get_pid support
|
||||||
#endif
|
# include <spdlog/details/windows_include.h>
|
||||||
|
|
||||||
#ifndef WIN32_LEAN_AND_MEAN
|
# ifdef __MINGW32__
|
||||||
#define WIN32_LEAN_AND_MEAN
|
# include <share.h>
|
||||||
#endif
|
# endif
|
||||||
#include <io.h> // _get_osfhandle and _isatty support
|
|
||||||
#include <process.h> // _get_pid support
|
|
||||||
#include <windows.h>
|
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
# if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
#include <share.h>
|
# include <limits>
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
#if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)
|
# include <direct.h> // for _mkdir/_wmkdir
|
||||||
#include <limits>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#else // unix
|
#else // unix
|
||||||
|
|
||||||
#include <fcntl.h>
|
# include <fcntl.h>
|
||||||
#include <unistd.h>
|
# include <unistd.h>
|
||||||
|
|
||||||
#ifdef __linux__
|
# ifdef __linux__
|
||||||
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
# include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
||||||
|
|
||||||
#elif defined(_AIX)
|
# elif defined(_AIX)
|
||||||
#include <pthread.h> // for pthread_getthreadid_np
|
# include <pthread.h> // for pthread_getthreadid_np
|
||||||
|
|
||||||
#elif defined(__DragonFly__) || defined(__FreeBSD__)
|
# elif defined(__DragonFly__) || defined(__FreeBSD__)
|
||||||
#include <pthread_np.h> // for pthread_getthreadid_np
|
# include <pthread_np.h> // for pthread_getthreadid_np
|
||||||
|
|
||||||
#elif defined(__NetBSD__)
|
# elif defined(__NetBSD__)
|
||||||
#include <lwp.h> // for _lwp_self
|
# include <lwp.h> // for _lwp_self
|
||||||
|
|
||||||
#elif defined(__sun)
|
# elif defined(__sun)
|
||||||
#include <thread.h> // for thr_self
|
# include <thread.h> // for thr_self
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
#endif // unix
|
#endif // unix
|
||||||
|
|
||||||
#ifndef __has_feature // Clang - feature checking macros.
|
#ifndef __has_feature // Clang - feature checking macros.
|
||||||
#define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
# define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
@@ -91,17 +86,17 @@ SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::tm tm;
|
std::tm tm;
|
||||||
localtime_s(&tm, &time_tt);
|
::localtime_s(&tm, &time_tt);
|
||||||
#else
|
#else
|
||||||
std::tm tm;
|
std::tm tm;
|
||||||
localtime_r(&time_tt, &tm);
|
::localtime_r(&time_tt, &tm);
|
||||||
#endif
|
#endif
|
||||||
return tm;
|
return tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT
|
SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::time_t now_t = time(nullptr);
|
std::time_t now_t = ::time(nullptr);
|
||||||
return localtime(now_t);
|
return localtime(now_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,64 +105,65 @@ SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT
|
|||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
std::tm tm;
|
std::tm tm;
|
||||||
gmtime_s(&tm, &time_tt);
|
::gmtime_s(&tm, &time_tt);
|
||||||
#else
|
#else
|
||||||
std::tm tm;
|
std::tm tm;
|
||||||
gmtime_r(&time_tt, &tm);
|
::gmtime_r(&time_tt, &tm);
|
||||||
#endif
|
#endif
|
||||||
return tm;
|
return tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT
|
SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
std::time_t now_t = time(nullptr);
|
std::time_t now_t = ::time(nullptr);
|
||||||
return gmtime(now_t);
|
return gmtime(now_t);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void prevent_child_fd(FILE *f)
|
|
||||||
{
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#if !defined(__cplusplus_winrt)
|
|
||||||
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(f)));
|
|
||||||
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
|
|
||||||
SPDLOG_THROW(spdlog_ex("SetHandleInformation failed", errno));
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
auto fd = fileno(f);
|
|
||||||
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
||||||
{
|
|
||||||
SPDLOG_THROW(spdlog_ex("fcntl with FD_CLOEXEC failed", errno));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// fopen_s on non windows for writing
|
// fopen_s on non windows for writing
|
||||||
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
|
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
# ifdef SPDLOG_WCHAR_FILENAMES
|
||||||
*fp = _wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
*fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
||||||
#else
|
# else
|
||||||
*fp = _fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
*fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO);
|
||||||
#endif
|
# endif
|
||||||
#else // unix
|
# if defined(SPDLOG_PREVENT_CHILD_FD)
|
||||||
*fp = fopen((filename.c_str()), mode.c_str());
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SPDLOG_PREVENT_CHILD_FD
|
|
||||||
if (*fp != nullptr)
|
if (*fp != nullptr)
|
||||||
{
|
{
|
||||||
prevent_child_fd(*fp);
|
auto file_handle = reinterpret_cast<HANDLE>(_get_osfhandle(::_fileno(*fp)));
|
||||||
|
if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0))
|
||||||
|
{
|
||||||
|
::fclose(*fp);
|
||||||
|
*fp = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
# endif
|
||||||
|
#else // unix
|
||||||
|
# if defined(SPDLOG_PREVENT_CHILD_FD)
|
||||||
|
const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC;
|
||||||
|
const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644));
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
*fp = ::fdopen(fd, mode.c_str());
|
||||||
|
if (*fp == nullptr)
|
||||||
|
{
|
||||||
|
::close(fd);
|
||||||
|
}
|
||||||
|
# else
|
||||||
|
*fp = ::fopen((filename.c_str()), mode.c_str());
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return *fp == nullptr;
|
return *fp == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
return _wremove(filename.c_str());
|
return ::_wremove(filename.c_str());
|
||||||
#else
|
#else
|
||||||
return std::remove(filename.c_str());
|
return std::remove(filename.c_str());
|
||||||
#endif
|
#endif
|
||||||
@@ -175,93 +171,108 @@ SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT
|
|||||||
|
|
||||||
SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
return file_exists(filename) ? remove(filename) : 0;
|
return path_exists(filename) ? remove(filename) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
return _wrename(filename1.c_str(), filename2.c_str());
|
return ::_wrename(filename1.c_str(), filename2.c_str());
|
||||||
#else
|
#else
|
||||||
return std::rename(filename1.c_str(), filename2.c_str());
|
return std::rename(filename1.c_str(), filename2.c_str());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true if file exists
|
// Return true if path exists (file or directory)
|
||||||
SPDLOG_INLINE bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifdef SPDLOG_WCHAR_FILENAMES
|
# ifdef SPDLOG_WCHAR_FILENAMES
|
||||||
auto attribs = GetFileAttributesW(filename.c_str());
|
auto attribs = ::GetFileAttributesW(filename.c_str());
|
||||||
#else
|
# else
|
||||||
auto attribs = GetFileAttributesA(filename.c_str());
|
auto attribs = ::GetFileAttributesA(filename.c_str());
|
||||||
#endif
|
# endif
|
||||||
return (attribs != INVALID_FILE_ATTRIBUTES && !(attribs & FILE_ATTRIBUTE_DIRECTORY));
|
return attribs != INVALID_FILE_ATTRIBUTES;
|
||||||
#else // common linux/unix all have the stat system call
|
#else // common linux/unix all have the stat system call
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
return (::stat(filename.c_str(), &buffer) == 0);
|
return (::stat(filename.c_str(), &buffer) == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
// avoid warning about unreachable statement at the end of filesize()
|
||||||
|
# pragma warning(push)
|
||||||
|
# pragma warning(disable : 4702)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Return file size according to open FILE* object
|
// Return file size according to open FILE* object
|
||||||
SPDLOG_INLINE size_t filesize(FILE *f)
|
SPDLOG_INLINE size_t filesize(FILE *f)
|
||||||
{
|
{
|
||||||
if (f == nullptr)
|
if (f == nullptr)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("Failed getting file size. fd is null"));
|
throw_spdlog_ex("Failed getting file size. fd is null");
|
||||||
}
|
}
|
||||||
#if defined(_WIN32) && !defined(__CYGWIN__)
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
||||||
int fd = _fileno(f);
|
int fd = ::_fileno(f);
|
||||||
#if _WIN64 // 64 bits
|
# if defined(_WIN64) // 64 bits
|
||||||
__int64 ret = _filelengthi64(fd);
|
__int64 ret = ::_filelengthi64(fd);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
return static_cast<size_t>(ret);
|
return static_cast<size_t>(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // windows 32 bits
|
# else // windows 32 bits
|
||||||
long ret = _filelength(fd);
|
long ret = ::_filelength(fd);
|
||||||
if (ret >= 0)
|
if (ret >= 0)
|
||||||
{
|
{
|
||||||
return static_cast<size_t>(ret);
|
return static_cast<size_t>(ret);
|
||||||
}
|
}
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
#else // unix
|
#else // unix
|
||||||
|
// OpenBSD doesn't compile with :: before the fileno(..)
|
||||||
|
# if defined(__OpenBSD__)
|
||||||
int fd = fileno(f);
|
int fd = fileno(f);
|
||||||
|
# else
|
||||||
|
int fd = ::fileno(f);
|
||||||
|
# endif
|
||||||
// 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
|
// 64 bits(but not in osx or cygwin, where fstat64 is deprecated)
|
||||||
#if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
|
# if (defined(__linux__) || defined(__sun) || defined(_AIX)) && (defined(__LP64__) || defined(_LP64))
|
||||||
struct stat64 st;
|
struct stat64 st;
|
||||||
if (::fstat64(fd, &st) == 0)
|
if (::fstat64(fd, &st) == 0)
|
||||||
{
|
{
|
||||||
return static_cast<size_t>(st.st_size);
|
return static_cast<size_t>(st.st_size);
|
||||||
}
|
}
|
||||||
#else // unix 32 bits or cygwin
|
# else // other unix or linux 32 bits or cygwin
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if (::fstat(fd, &st) == 0)
|
if (::fstat(fd, &st) == 0)
|
||||||
{
|
{
|
||||||
return static_cast<size_t>(st.st_size);
|
return static_cast<size_t>(st.st_size);
|
||||||
}
|
}
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
#endif
|
throw_spdlog_ex("Failed getting file size from fd", errno);
|
||||||
SPDLOG_THROW(spdlog_ex("Failed getting file size from fd", errno));
|
return 0; // will not be reached.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# pragma warning(pop)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Return utc offset in minutes or throw spdlog_ex on failure
|
// Return utc offset in minutes or throw spdlog_ex on failure
|
||||||
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
|
SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#if _WIN32_WINNT < _WIN32_WINNT_WS08
|
# if _WIN32_WINNT < _WIN32_WINNT_WS08
|
||||||
TIME_ZONE_INFORMATION tzinfo;
|
TIME_ZONE_INFORMATION tzinfo;
|
||||||
auto rv = GetTimeZoneInformation(&tzinfo);
|
auto rv = ::GetTimeZoneInformation(&tzinfo);
|
||||||
#else
|
# else
|
||||||
DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
|
DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
|
||||||
auto rv = GetDynamicTimeZoneInformation(&tzinfo);
|
auto rv = ::GetDynamicTimeZoneInformation(&tzinfo);
|
||||||
#endif
|
# endif
|
||||||
if (rv == TIME_ZONE_ID_INVALID)
|
if (rv == TIME_ZONE_ID_INVALID)
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex("Failed getting timezone info. ", errno));
|
throw_spdlog_ex("Failed getting timezone info. ", errno);
|
||||||
|
|
||||||
int offset = -tzinfo.Bias;
|
int offset = -tzinfo.Bias;
|
||||||
if (tm.tm_isdst)
|
if (tm.tm_isdst)
|
||||||
@@ -275,7 +286,7 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
|
|||||||
return offset;
|
return offset;
|
||||||
#else
|
#else
|
||||||
|
|
||||||
#if defined(sun) || defined(__sun) || defined(_AIX)
|
# if defined(sun) || defined(__sun) || defined(_AIX) || (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
|
||||||
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
|
||||||
struct helper
|
struct helper
|
||||||
{
|
{
|
||||||
@@ -305,9 +316,9 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm)
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto offset_seconds = helper::calculate_gmt_offset(tm);
|
auto offset_seconds = helper::calculate_gmt_offset(tm);
|
||||||
#else
|
# else
|
||||||
auto offset_seconds = tm.tm_gmtoff;
|
auto offset_seconds = tm.tm_gmtoff;
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
return static_cast<int>(offset_seconds / 60);
|
return static_cast<int>(offset_seconds / 60);
|
||||||
#endif
|
#endif
|
||||||
@@ -321,18 +332,18 @@ SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return static_cast<size_t>(::GetCurrentThreadId());
|
return static_cast<size_t>(::GetCurrentThreadId());
|
||||||
#elif defined(__linux__)
|
#elif defined(__linux__)
|
||||||
#if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
|
# if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21)
|
||||||
#define SYS_gettid __NR_gettid
|
# define SYS_gettid __NR_gettid
|
||||||
#endif
|
# endif
|
||||||
return static_cast<size_t>(syscall(SYS_gettid));
|
return static_cast<size_t>(::syscall(SYS_gettid));
|
||||||
#elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__)
|
#elif defined(_AIX) || defined(__DragonFly__) || defined(__FreeBSD__)
|
||||||
return static_cast<size_t>(pthread_getthreadid_np());
|
return static_cast<size_t>(::pthread_getthreadid_np());
|
||||||
#elif defined(__NetBSD__)
|
#elif defined(__NetBSD__)
|
||||||
return static_cast<size_t>(_lwp_self());
|
return static_cast<size_t>(::_lwp_self());
|
||||||
#elif defined(__OpenBSD__)
|
#elif defined(__OpenBSD__)
|
||||||
return static_cast<size_t>(getthrid());
|
return static_cast<size_t>(::getthrid());
|
||||||
#elif defined(__sun)
|
#elif defined(__sun)
|
||||||
return static_cast<size_t>(thr_self());
|
return static_cast<size_t>(::thr_self());
|
||||||
#elif __APPLE__
|
#elif __APPLE__
|
||||||
uint64_t tid;
|
uint64_t tid;
|
||||||
pthread_threadid_np(nullptr, &tid);
|
pthread_threadid_np(nullptr, &tid);
|
||||||
@@ -355,7 +366,7 @@ SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT
|
|||||||
|
|
||||||
// This is avoid msvc issue in sleep_for that happens if the clock changes.
|
// This is avoid msvc issue in sleep_for that happens if the clock changes.
|
||||||
// See https://github.com/gabime/spdlog/issues/609
|
// See https://github.com/gabime/spdlog/issues/609
|
||||||
SPDLOG_INLINE void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
::Sleep(milliseconds);
|
::Sleep(milliseconds);
|
||||||
@@ -390,45 +401,54 @@ SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Determine if the terminal supports colors
|
// Determine if the terminal supports colors
|
||||||
// Source: https://github.com/agauniyal/rang/
|
// Based on: https://github.com/agauniyal/rang/
|
||||||
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
|
SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
static constexpr std::array<const char *, 14> Terms = {{
|
|
||||||
"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys", "putty", "rxvt", "screen", "vt100", "xterm"}};
|
|
||||||
|
|
||||||
const char *env_p = std::getenv("TERM");
|
static const bool result = []() {
|
||||||
if (env_p == nullptr)
|
const char *env_colorterm_p = std::getenv("COLORTERM");
|
||||||
{
|
if (env_colorterm_p != nullptr)
|
||||||
return false;
|
{
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr std::array<const char *, 16> terms = {{"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux",
|
||||||
|
"msys", "putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}};
|
||||||
|
|
||||||
|
const char *env_term_p = std::getenv("TERM");
|
||||||
|
if (env_term_p == nullptr)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::any_of(terms.begin(), terms.end(), [&](const char *term) { return std::strstr(env_term_p, term) != nullptr; });
|
||||||
|
}();
|
||||||
|
|
||||||
static const bool result =
|
|
||||||
std::any_of(std::begin(Terms), std::end(Terms), [&](const char *term) { return std::strstr(env_p, term) != nullptr; });
|
|
||||||
return result;
|
return result;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detrmine if the terminal attached
|
// Determine if the terminal attached
|
||||||
// Source: https://github.com/agauniyal/rang/
|
// Source: https://github.com/agauniyal/rang/
|
||||||
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
|
SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT
|
||||||
{
|
{
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return _isatty(_fileno(file)) != 0;
|
return ::_isatty(_fileno(file)) != 0;
|
||||||
#else
|
#else
|
||||||
return isatty(fileno(file)) != 0;
|
return ::isatty(fileno(file)) != 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, memory_buf_t &target)
|
SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target)
|
||||||
{
|
{
|
||||||
if (wstr.size() > static_cast<size_t>(std::numeric_limits<int>::max()))
|
if (wstr.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) / 2 - 1)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex("UTF-16 string is too big to be converted to UTF-8"));
|
throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8");
|
||||||
}
|
}
|
||||||
|
|
||||||
int wstr_size = static_cast<int>(wstr.size());
|
int wstr_size = static_cast<int>(wstr.size());
|
||||||
@@ -456,10 +476,124 @@ SPDLOG_INLINE void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, memory_buf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_THROW(spdlog::spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())));
|
throw_spdlog_ex(fmt::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError()));
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target)
|
||||||
|
{
|
||||||
|
if (str.size() > static_cast<size_t>((std::numeric_limits<int>::max)()) - 1)
|
||||||
|
{
|
||||||
|
throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16");
|
||||||
|
}
|
||||||
|
|
||||||
|
int str_size = static_cast<int>(str.size());
|
||||||
|
if (str_size == 0)
|
||||||
|
{
|
||||||
|
target.resize(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int result_size = static_cast<int>(target.capacity());
|
||||||
|
if (str_size + 1 > result_size)
|
||||||
|
{
|
||||||
|
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result_size > 0)
|
||||||
|
{
|
||||||
|
target.resize(result_size);
|
||||||
|
result_size = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.data(), str_size, target.data(), result_size);
|
||||||
|
|
||||||
|
if (result_size > 0)
|
||||||
|
{
|
||||||
|
target.resize(result_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw_spdlog_ex(fmt::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError()));
|
||||||
}
|
}
|
||||||
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
|
|
||||||
|
// return true on success
|
||||||
|
static SPDLOG_INLINE bool mkdir_(const filename_t &path)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
# ifdef SPDLOG_WCHAR_FILENAMES
|
||||||
|
return ::_wmkdir(path.c_str()) == 0;
|
||||||
|
# else
|
||||||
|
return ::_mkdir(path.c_str()) == 0;
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
return ::mkdir(path.c_str(), mode_t(0755)) == 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the given directory - and all directories leading to it
|
||||||
|
// return true on success or if the directory already exists
|
||||||
|
SPDLOG_INLINE bool create_dir(filename_t path)
|
||||||
|
{
|
||||||
|
if (path_exists(path))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path.empty())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t search_offset = 0;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
auto token_pos = path.find_first_of(folder_seps_filename, search_offset);
|
||||||
|
// treat the entire path as a folder if no folder separator not found
|
||||||
|
if (token_pos == filename_t::npos)
|
||||||
|
{
|
||||||
|
token_pos = path.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto subdir = path.substr(0, token_pos);
|
||||||
|
|
||||||
|
if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir))
|
||||||
|
{
|
||||||
|
return false; // return error if failed creating dir
|
||||||
|
}
|
||||||
|
search_offset = token_pos + 1;
|
||||||
|
} while (search_offset < path.size());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return directory name from given path or empty string
|
||||||
|
// "abc/file" => "abc"
|
||||||
|
// "abc/" => "abc"
|
||||||
|
// "abc" => ""
|
||||||
|
// "abc///" => "abc//"
|
||||||
|
SPDLOG_INLINE filename_t dir_name(filename_t path)
|
||||||
|
{
|
||||||
|
auto pos = path.find_last_of(folder_seps_filename);
|
||||||
|
return pos != filename_t::npos ? path.substr(0, pos) : filename_t{};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string SPDLOG_INLINE getenv(const char *field)
|
||||||
|
{
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# if defined(__cplusplus_winrt)
|
||||||
|
return std::string{}; // not supported under uwp
|
||||||
|
# else
|
||||||
|
size_t len = 0;
|
||||||
|
char buf[128];
|
||||||
|
bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0;
|
||||||
|
return ok ? buf : std::string{};
|
||||||
|
# endif
|
||||||
|
#else // revert to getenv
|
||||||
|
char *buf = ::getenv(field);
|
||||||
|
return buf ? buf : std::string{};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace os
|
} // namespace os
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -3,96 +3,116 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include <ctime> // std::time_t
|
#include <ctime> // std::time_t
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
namespace os {
|
namespace os {
|
||||||
|
|
||||||
spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT;
|
SPDLOG_API spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
|
SPDLOG_API std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::tm localtime() SPDLOG_NOEXCEPT;
|
SPDLOG_API std::tm localtime() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
|
SPDLOG_API std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::tm gmtime() SPDLOG_NOEXCEPT;
|
SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// eol definition
|
// eol definition
|
||||||
#if !defined(SPDLOG_EOL)
|
#if !defined(SPDLOG_EOL)
|
||||||
#ifdef _WIN32
|
# ifdef _WIN32
|
||||||
#define SPDLOG_EOL "\r\n"
|
# define SPDLOG_EOL "\r\n"
|
||||||
#else
|
# else
|
||||||
#define SPDLOG_EOL "\n"
|
# define SPDLOG_EOL "\n"
|
||||||
#endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
|
SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL;
|
||||||
|
|
||||||
// folder separator
|
// folder separator
|
||||||
#ifdef _WIN32
|
#if !defined(SPDLOG_FOLDER_SEPS)
|
||||||
const char folder_sep = '\\';
|
# ifdef _WIN32
|
||||||
#else
|
# define SPDLOG_FOLDER_SEPS "\\/"
|
||||||
SPDLOG_CONSTEXPR static const char folder_sep = '/';
|
# else
|
||||||
|
# define SPDLOG_FOLDER_SEPS "/"
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void prevent_child_fd(FILE *f);
|
SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS;
|
||||||
|
SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS);
|
||||||
|
|
||||||
// fopen_s on non windows for writing
|
// fopen_s on non windows for writing
|
||||||
bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
|
SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode);
|
||||||
|
|
||||||
// Remove filename. return 0 on success
|
// Remove filename. return 0 on success
|
||||||
int remove(const filename_t &filename) SPDLOG_NOEXCEPT;
|
SPDLOG_API int remove(const filename_t &filename) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Remove file if exists. return 0 on success
|
// Remove file if exists. return 0 on success
|
||||||
// Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread)
|
// Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread)
|
||||||
int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
|
SPDLOG_API int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT;
|
SPDLOG_API int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Return if file exists.
|
// Return if file exists.
|
||||||
bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
|
SPDLOG_API bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Return file size according to open FILE* object
|
// Return file size according to open FILE* object
|
||||||
size_t filesize(FILE *f);
|
SPDLOG_API size_t filesize(FILE *f);
|
||||||
|
|
||||||
// Return utc offset in minutes or throw spdlog_ex on failure
|
// Return utc offset in minutes or throw spdlog_ex on failure
|
||||||
int utc_minutes_offset(const std::tm &tm = details::os::localtime());
|
SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime());
|
||||||
|
|
||||||
// Return current thread id as size_t
|
// Return current thread id as size_t
|
||||||
// It exists because the std::this_thread::get_id() is much slower(especially
|
// It exists because the std::this_thread::get_id() is much slower(especially
|
||||||
// under VS 2013)
|
// under VS 2013)
|
||||||
size_t _thread_id() SPDLOG_NOEXCEPT;
|
SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Return current thread id as size_t (from thread local storage)
|
// Return current thread id as size_t (from thread local storage)
|
||||||
size_t thread_id() SPDLOG_NOEXCEPT;
|
SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// This is avoid msvc issue in sleep_for that happens if the clock changes.
|
// This is avoid msvc issue in sleep_for that happens if the clock changes.
|
||||||
// See https://github.com/gabime/spdlog/issues/609
|
// See https://github.com/gabime/spdlog/issues/609
|
||||||
void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT;
|
SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
std::string filename_to_str(const filename_t &filename);
|
SPDLOG_API std::string filename_to_str(const filename_t &filename);
|
||||||
|
|
||||||
int pid() SPDLOG_NOEXCEPT;
|
SPDLOG_API int pid() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Determine if the terminal supports colors
|
// Determine if the terminal supports colors
|
||||||
// Source: https://github.com/agauniyal/rang/
|
// Source: https://github.com/agauniyal/rang/
|
||||||
bool is_color_terminal() SPDLOG_NOEXCEPT;
|
SPDLOG_API bool is_color_terminal() SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
// Detrmine if the terminal attached
|
// Determine if the terminal attached
|
||||||
// Source: https://github.com/agauniyal/rang/
|
// Source: https://github.com/agauniyal/rang/
|
||||||
bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
|
SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32)
|
||||||
void wstr_to_utf8buf(basic_string_view_t<wchar_t> wstr, memory_buf_t &target);
|
SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target);
|
||||||
|
|
||||||
|
SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Return directory name from given path or empty string
|
||||||
|
// "abc/file" => "abc"
|
||||||
|
// "abc/" => "abc"
|
||||||
|
// "abc" => ""
|
||||||
|
// "abc///" => "abc//"
|
||||||
|
SPDLOG_API filename_t dir_name(filename_t path);
|
||||||
|
|
||||||
|
// Create a dir from the given path.
|
||||||
|
// Return true if succeeded or if this dir already exists.
|
||||||
|
SPDLOG_API bool create_dir(filename_t path);
|
||||||
|
|
||||||
|
// non thread safe, cross platform getenv/getenv_s
|
||||||
|
// return empty string if field not found
|
||||||
|
SPDLOG_API std::string getenv(const char *field);
|
||||||
|
|
||||||
} // namespace os
|
} // namespace os
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "os-inl.h"
|
# include "os-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/periodic_worker.h"
|
# include <spdlog/details/periodic_worker.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
class periodic_worker
|
class SPDLOG_API periodic_worker
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval);
|
periodic_worker(const std::function<void()> &callback_fun, std::chrono::seconds interval);
|
||||||
@@ -36,5 +36,5 @@ private:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "periodic_worker-inl.h"
|
# include "periodic_worker-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,21 +4,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/registry.h"
|
# include <spdlog/details/registry.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/periodic_worker.h"
|
#include <spdlog/details/periodic_worker.h>
|
||||||
#include "spdlog/logger.h"
|
#include <spdlog/logger.h>
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include <spdlog/pattern_formatter.h>
|
||||||
|
|
||||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
// support for the default stdout color logger
|
// support for the default stdout color logger
|
||||||
#ifdef _WIN32
|
# ifdef _WIN32
|
||||||
#include "spdlog/sinks/wincolor_sink.h"
|
# include <spdlog/sinks/wincolor_sink.h>
|
||||||
#else
|
# else
|
||||||
#include "spdlog/sinks/ansicolor_sink.h"
|
# include <spdlog/sinks/ansicolor_sink.h>
|
||||||
#endif
|
# endif
|
||||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -36,11 +36,11 @@ SPDLOG_INLINE registry::registry()
|
|||||||
|
|
||||||
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
|
// create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows).
|
||||||
#ifdef _WIN32
|
# ifdef _WIN32
|
||||||
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
|
auto color_sink = std::make_shared<sinks::wincolor_stdout_sink_mt>();
|
||||||
#else
|
# else
|
||||||
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
|
auto color_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
const char *default_logger_name = "";
|
const char *default_logger_name = "";
|
||||||
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
|
default_logger_ = std::make_shared<spdlog::logger>(default_logger_name, std::move(color_sink));
|
||||||
@@ -48,6 +48,9 @@ SPDLOG_INLINE registry::registry()
|
|||||||
|
|
||||||
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
#endif // SPDLOG_DISABLE_DEFAULT_LOGGER
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE registry::~registry() = default;
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger)
|
SPDLOG_INLINE void registry::register_logger(std::shared_ptr<logger> new_logger)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
@@ -64,7 +67,11 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr<logger> new_logge
|
|||||||
new_logger->set_error_handler(err_handler_);
|
new_logger->set_error_handler(err_handler_);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_logger->set_level(level_);
|
// set new level according to previously configured level or default level
|
||||||
|
auto it = log_levels_.find(new_logger->name());
|
||||||
|
auto new_level = it != log_levels_.end() ? it->second : global_log_level_;
|
||||||
|
new_logger->set_level(new_level);
|
||||||
|
|
||||||
new_logger->flush_on(flush_level_);
|
new_logger->flush_on(flush_level_);
|
||||||
|
|
||||||
if (backtrace_n_messages_ > 0)
|
if (backtrace_n_messages_ > 0)
|
||||||
@@ -168,7 +175,7 @@ SPDLOG_INLINE void registry::set_level(level::level_enum log_level)
|
|||||||
{
|
{
|
||||||
l.second->set_level(log_level);
|
l.second->set_level(log_level);
|
||||||
}
|
}
|
||||||
level_ = log_level;
|
global_log_level_ = log_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
||||||
@@ -184,18 +191,18 @@ SPDLOG_INLINE void registry::flush_on(level::level_enum log_level)
|
|||||||
SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
|
SPDLOG_INLINE void registry::flush_every(std::chrono::seconds interval)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
std::lock_guard<std::mutex> lock(flusher_mutex_);
|
||||||
std::function<void()> clbk = std::bind(®istry::flush_all, this);
|
auto clbk = [this]() { this->flush_all(); };
|
||||||
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
|
periodic_flusher_ = details::make_unique<periodic_worker>(clbk, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::set_error_handler(void (*handler)(const std::string &msg))
|
SPDLOG_INLINE void registry::set_error_handler(err_handler handler)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
for (auto &l : loggers_)
|
for (auto &l : loggers_)
|
||||||
{
|
{
|
||||||
l.second->set_error_handler(handler);
|
l.second->set_error_handler(handler);
|
||||||
}
|
}
|
||||||
err_handler_ = handler;
|
err_handler_ = std::move(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun)
|
SPDLOG_INLINE void registry::apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun)
|
||||||
@@ -254,10 +261,31 @@ SPDLOG_INLINE std::recursive_mutex ®istry::tp_mutex()
|
|||||||
return tp_mutex_;
|
return tp_mutex_;
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_regsistration)
|
SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registration)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
automatic_registration_ = automatic_regsistration;
|
automatic_registration_ = automatic_registration;
|
||||||
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *global_level)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(logger_map_mutex_);
|
||||||
|
log_levels_ = std::move(levels);
|
||||||
|
auto global_level_requested = global_level != nullptr;
|
||||||
|
global_log_level_ = global_level_requested ? *global_level : global_log_level_;
|
||||||
|
|
||||||
|
for (auto &logger : loggers_)
|
||||||
|
{
|
||||||
|
auto logger_entry = log_levels_.find(logger.first);
|
||||||
|
if (logger_entry != log_levels_.end())
|
||||||
|
{
|
||||||
|
logger.second->set_level(logger_entry->second);
|
||||||
|
}
|
||||||
|
else if (global_level_requested)
|
||||||
|
{
|
||||||
|
logger.second->set_level(*global_level);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE registry ®istry::instance()
|
SPDLOG_INLINE registry ®istry::instance()
|
||||||
@@ -270,7 +298,7 @@ SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name)
|
|||||||
{
|
{
|
||||||
if (loggers_.find(logger_name) != loggers_.end())
|
if (loggers_.find(logger_name) != loggers_.end())
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("logger with name '" + logger_name + "' already exists"));
|
throw_spdlog_ex("logger with name '" + logger_name + "' already exists");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,5 +308,6 @@ SPDLOG_INLINE void registry::register_logger_(std::shared_ptr<logger> new_logger
|
|||||||
throw_if_exists_(logger_name);
|
throw_if_exists_(logger_name);
|
||||||
loggers_[logger_name] = std::move(new_logger);
|
loggers_[logger_name] = std::move(new_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
// If user requests a non existing logger, nullptr will be returned
|
// If user requests a non existing logger, nullptr will be returned
|
||||||
// This class is thread safe
|
// This class is thread safe
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@@ -24,9 +24,10 @@ namespace details {
|
|||||||
class thread_pool;
|
class thread_pool;
|
||||||
class periodic_worker;
|
class periodic_worker;
|
||||||
|
|
||||||
class registry
|
class SPDLOG_API registry
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using log_levels = std::unordered_map<std::string, level::level_enum>;
|
||||||
registry(const registry &) = delete;
|
registry(const registry &) = delete;
|
||||||
registry &operator=(const registry &) = delete;
|
registry &operator=(const registry &) = delete;
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ public:
|
|||||||
|
|
||||||
void flush_every(std::chrono::seconds interval);
|
void flush_every(std::chrono::seconds interval);
|
||||||
|
|
||||||
void set_error_handler(void (*handler)(const std::string &msg));
|
void set_error_handler(err_handler handler);
|
||||||
|
|
||||||
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
|
void apply_all(const std::function<void(const std::shared_ptr<logger>)> &fun);
|
||||||
|
|
||||||
@@ -77,23 +78,28 @@ public:
|
|||||||
|
|
||||||
std::recursive_mutex &tp_mutex();
|
std::recursive_mutex &tp_mutex();
|
||||||
|
|
||||||
void set_automatic_registration(bool automatic_regsistration);
|
void set_automatic_registration(bool automatic_registration);
|
||||||
|
|
||||||
|
// set levels for all existing/future loggers. global_level can be null if should not set.
|
||||||
|
void set_levels(log_levels levels, level::level_enum *global_level);
|
||||||
|
|
||||||
static registry &instance();
|
static registry &instance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
registry();
|
registry();
|
||||||
~registry() = default;
|
~registry();
|
||||||
|
|
||||||
void throw_if_exists_(const std::string &logger_name);
|
void throw_if_exists_(const std::string &logger_name);
|
||||||
void register_logger_(std::shared_ptr<logger> new_logger);
|
void register_logger_(std::shared_ptr<logger> new_logger);
|
||||||
|
bool set_level_from_cfg_(logger *logger);
|
||||||
std::mutex logger_map_mutex_, flusher_mutex_;
|
std::mutex logger_map_mutex_, flusher_mutex_;
|
||||||
std::recursive_mutex tp_mutex_;
|
std::recursive_mutex tp_mutex_;
|
||||||
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
|
||||||
|
log_levels log_levels_;
|
||||||
std::unique_ptr<formatter> formatter_;
|
std::unique_ptr<formatter> formatter_;
|
||||||
level::level_enum level_ = level::info;
|
spdlog::level::level_enum global_log_level_ = level::info;
|
||||||
level::level_enum flush_level_ = level::off;
|
level::level_enum flush_level_ = level::off;
|
||||||
void (*err_handler_)(const std::string &msg);
|
err_handler err_handler_;
|
||||||
std::shared_ptr<thread_pool> tp_;
|
std::shared_ptr<thread_pool> tp_;
|
||||||
std::unique_ptr<periodic_worker> periodic_flusher_;
|
std::unique_ptr<periodic_worker> periodic_flusher_;
|
||||||
std::shared_ptr<logger> default_logger_;
|
std::shared_ptr<logger> default_logger_;
|
||||||
@@ -105,5 +111,5 @@ private:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "registry-inl.h"
|
# include "registry-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class logger;
|
|||||||
struct synchronous_factory
|
struct synchronous_factory
|
||||||
{
|
{
|
||||||
template<typename Sink, typename... SinkArgs>
|
template<typename Sink, typename... SinkArgs>
|
||||||
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&... args)
|
static std::shared_ptr<spdlog::logger> create(std::string logger_name, SinkArgs &&...args)
|
||||||
{
|
{
|
||||||
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
|
||||||
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
auto new_logger = std::make_shared<spdlog::logger>(std::move(logger_name), std::move(sink));
|
||||||
@@ -21,4 +21,4 @@ struct synchronous_factory
|
|||||||
return new_logger;
|
return new_logger;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
175
include/spdlog/details/tcp_client-windows.h
Normal file
175
include/spdlog/details/tcp_client-windows.h
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
// tcp client helper
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#pragma comment(lib, "Ws2_32.lib")
|
||||||
|
#pragma comment(lib, "Mswsock.lib")
|
||||||
|
#pragma comment(lib, "AdvApi32.lib")
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace details {
|
||||||
|
class tcp_client
|
||||||
|
{
|
||||||
|
SOCKET socket_ = INVALID_SOCKET;
|
||||||
|
|
||||||
|
static bool winsock_initialized_()
|
||||||
|
{
|
||||||
|
SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
|
if (s == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
closesocket(s);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_winsock_()
|
||||||
|
{
|
||||||
|
WSADATA wsaData;
|
||||||
|
auto rv = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
if (rv != 0)
|
||||||
|
{
|
||||||
|
throw_winsock_error_("WSAStartup failed", ::WSAGetLastError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void throw_winsock_error_(const std::string &msg, int last_error)
|
||||||
|
{
|
||||||
|
char buf[512];
|
||||||
|
::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, last_error,
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, (sizeof(buf) / sizeof(char)), NULL);
|
||||||
|
|
||||||
|
throw_spdlog_ex(fmt::format("tcp_sink - {}: {}", msg, buf));
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool is_connected() const
|
||||||
|
{
|
||||||
|
return socket_ != INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
::closesocket(socket_);
|
||||||
|
socket_ = INVALID_SOCKET;
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
SOCKET fd() const
|
||||||
|
{
|
||||||
|
return socket_;
|
||||||
|
}
|
||||||
|
|
||||||
|
~tcp_client()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to connect or throw on failure
|
||||||
|
void connect(const std::string &host, int port)
|
||||||
|
{
|
||||||
|
// initialize winsock if needed
|
||||||
|
if (!winsock_initialized_())
|
||||||
|
{
|
||||||
|
init_winsock_();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_connected())
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
struct addrinfo hints
|
||||||
|
{};
|
||||||
|
ZeroMemory(&hints, sizeof(hints));
|
||||||
|
|
||||||
|
hints.ai_family = AF_INET; // IPv4
|
||||||
|
hints.ai_socktype = SOCK_STREAM; // TCP
|
||||||
|
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||||
|
hints.ai_protocol = 0;
|
||||||
|
|
||||||
|
auto port_str = std::to_string(port);
|
||||||
|
struct addrinfo *addrinfo_result;
|
||||||
|
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||||
|
int last_error = 0;
|
||||||
|
if (rv != 0)
|
||||||
|
{
|
||||||
|
last_error = ::WSAGetLastError();
|
||||||
|
WSACleanup();
|
||||||
|
throw_winsock_error_("getaddrinfo failed", last_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try each address until we successfully connect(2).
|
||||||
|
|
||||||
|
for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next)
|
||||||
|
{
|
||||||
|
socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
|
if (socket_ == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
last_error = ::WSAGetLastError();
|
||||||
|
WSACleanup();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (::connect(socket_, rp->ai_addr, (int)rp->ai_addrlen) == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
last_error = ::WSAGetLastError();
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
::freeaddrinfo(addrinfo_result);
|
||||||
|
if (socket_ == INVALID_SOCKET)
|
||||||
|
{
|
||||||
|
WSACleanup();
|
||||||
|
throw_winsock_error_("connect failed", last_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set TCP_NODELAY
|
||||||
|
int enable_flag = 1;
|
||||||
|
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send exactly n_bytes of the given data.
|
||||||
|
// On error close the connection and throw.
|
||||||
|
void send(const char *data, size_t n_bytes)
|
||||||
|
{
|
||||||
|
size_t bytes_sent = 0;
|
||||||
|
while (bytes_sent < n_bytes)
|
||||||
|
{
|
||||||
|
const int send_flags = 0;
|
||||||
|
auto write_result = ::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags);
|
||||||
|
if (write_result == SOCKET_ERROR)
|
||||||
|
{
|
||||||
|
int last_error = ::WSAGetLastError();
|
||||||
|
close();
|
||||||
|
throw_winsock_error_("send failed", last_error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_result == 0) // (probably should not happen but in any case..)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bytes_sent += static_cast<size_t>(write_result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
||||||
146
include/spdlog/details/tcp_client.h
Normal file
146
include/spdlog/details/tcp_client.h
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
# error include tcp_client-windows.h instead
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// tcp client helper
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace details {
|
||||||
|
class tcp_client
|
||||||
|
{
|
||||||
|
int socket_ = -1;
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool is_connected() const
|
||||||
|
{
|
||||||
|
return socket_ != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close()
|
||||||
|
{
|
||||||
|
if (is_connected())
|
||||||
|
{
|
||||||
|
::close(socket_);
|
||||||
|
socket_ = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd() const
|
||||||
|
{
|
||||||
|
return socket_;
|
||||||
|
}
|
||||||
|
|
||||||
|
~tcp_client()
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to connect or throw on failure
|
||||||
|
void connect(const std::string &host, int port)
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
struct addrinfo hints
|
||||||
|
{};
|
||||||
|
memset(&hints, 0, sizeof(struct addrinfo));
|
||||||
|
hints.ai_family = AF_INET; // IPv4
|
||||||
|
hints.ai_socktype = SOCK_STREAM; // TCP
|
||||||
|
hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value
|
||||||
|
hints.ai_protocol = 0;
|
||||||
|
|
||||||
|
auto port_str = std::to_string(port);
|
||||||
|
struct addrinfo *addrinfo_result;
|
||||||
|
auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
|
||||||
|
if (rv != 0)
|
||||||
|
{
|
||||||
|
auto msg = fmt::format("::getaddrinfo failed: {}", gai_strerror(rv));
|
||||||
|
throw_spdlog_ex(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try each address until we successfully connect(2).
|
||||||
|
int last_errno = 0;
|
||||||
|
for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next)
|
||||||
|
{
|
||||||
|
#if defined(SOCK_CLOEXEC)
|
||||||
|
const int flags = SOCK_CLOEXEC;
|
||||||
|
#else
|
||||||
|
const int flags = 0;
|
||||||
|
#endif
|
||||||
|
socket_ = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol);
|
||||||
|
if (socket_ == -1)
|
||||||
|
{
|
||||||
|
last_errno = errno;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen);
|
||||||
|
if (rv == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
last_errno = errno;
|
||||||
|
::close(socket_);
|
||||||
|
socket_ = -1;
|
||||||
|
}
|
||||||
|
::freeaddrinfo(addrinfo_result);
|
||||||
|
if (socket_ == -1)
|
||||||
|
{
|
||||||
|
throw_spdlog_ex("::connect failed", last_errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
// set TCP_NODELAY
|
||||||
|
int enable_flag = 1;
|
||||||
|
::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||||
|
|
||||||
|
// prevent sigpipe on systems where MSG_NOSIGNAL is not available
|
||||||
|
#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||||
|
::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<char *>(&enable_flag), sizeof(enable_flag));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
|
||||||
|
# error "tcp_sink would raise SIGPIPE since niether SO_NOSIGPIPE nor MSG_NOSIGNAL are available"
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send exactly n_bytes of the given data.
|
||||||
|
// On error close the connection and throw.
|
||||||
|
void send(const char *data, size_t n_bytes)
|
||||||
|
{
|
||||||
|
size_t bytes_sent = 0;
|
||||||
|
while (bytes_sent < n_bytes)
|
||||||
|
{
|
||||||
|
#if defined(MSG_NOSIGNAL)
|
||||||
|
const int send_flags = MSG_NOSIGNAL;
|
||||||
|
#else
|
||||||
|
const int send_flags = 0;
|
||||||
|
#endif
|
||||||
|
auto write_result = ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags);
|
||||||
|
if (write_result < 0)
|
||||||
|
{
|
||||||
|
close();
|
||||||
|
throw_spdlog_ex("write(2) failed", errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_result == 0) // (probably should not happen but in any case..)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
bytes_sent += static_cast<size_t>(write_result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace details
|
||||||
|
} // namespace spdlog
|
||||||
@@ -4,10 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/thread_pool.h"
|
# include <spdlog/details/thread_pool.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
@@ -17,8 +18,8 @@ SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n, std
|
|||||||
{
|
{
|
||||||
if (threads_n == 0 || threads_n > 1000)
|
if (threads_n == 0 || threads_n > 1000)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
|
throw_spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid "
|
||||||
"range is 1-1000)"));
|
"range is 1-1000)");
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < threads_n; i++)
|
for (size_t i = 0; i < threads_n; i++)
|
||||||
{
|
{
|
||||||
@@ -48,7 +49,7 @@ SPDLOG_INLINE thread_pool::~thread_pool()
|
|||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_ALL() {}
|
SPDLOG_CATCH_STD
|
||||||
}
|
}
|
||||||
|
|
||||||
void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
|
void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy)
|
||||||
@@ -67,6 +68,11 @@ size_t SPDLOG_INLINE thread_pool::overrun_counter()
|
|||||||
return q_.overrun_counter();
|
return q_.overrun_counter();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t SPDLOG_INLINE thread_pool::queue_size()
|
||||||
|
{
|
||||||
|
return q_.size();
|
||||||
|
}
|
||||||
|
|
||||||
void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
if (overflow_policy == async_overflow_policy::block)
|
if (overflow_policy == async_overflow_policy::block)
|
||||||
@@ -81,7 +87,7 @@ void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, async_overf
|
|||||||
|
|
||||||
void SPDLOG_INLINE thread_pool::worker_loop_()
|
void SPDLOG_INLINE thread_pool::worker_loop_()
|
||||||
{
|
{
|
||||||
while (process_next_msg_()) {};
|
while (process_next_msg_()) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// process next message in the queue
|
// process next message in the queue
|
||||||
@@ -98,25 +104,21 @@ bool SPDLOG_INLINE thread_pool::process_next_msg_()
|
|||||||
|
|
||||||
switch (incoming_async_msg.msg_type)
|
switch (incoming_async_msg.msg_type)
|
||||||
{
|
{
|
||||||
case async_msg_type::log:
|
case async_msg_type::log: {
|
||||||
{
|
|
||||||
incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
|
incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case async_msg_type::flush:
|
case async_msg_type::flush: {
|
||||||
{
|
|
||||||
incoming_async_msg.worker_ptr->backend_flush_();
|
incoming_async_msg.worker_ptr->backend_flush_();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case async_msg_type::terminate:
|
case async_msg_type::terminate: {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default: {
|
||||||
{
|
assert(false);
|
||||||
assert(false && "Unexpected async_msg_type");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/log_msg_buffer.h"
|
#include <spdlog/details/log_msg_buffer.h>
|
||||||
#include "spdlog/details/mpmc_blocking_q.h"
|
#include <spdlog/details/mpmc_blocking_q.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -27,7 +27,6 @@ enum class async_msg_type
|
|||||||
terminate
|
terminate
|
||||||
};
|
};
|
||||||
|
|
||||||
#include "spdlog/details/log_msg_buffer.h"
|
|
||||||
// Async msg to move to/from the queue
|
// Async msg to move to/from the queue
|
||||||
// Movable only. should never be copied
|
// Movable only. should never be copied
|
||||||
struct async_msg : log_msg_buffer
|
struct async_msg : log_msg_buffer
|
||||||
@@ -79,7 +78,7 @@ struct async_msg : log_msg_buffer
|
|||||||
{}
|
{}
|
||||||
};
|
};
|
||||||
|
|
||||||
class thread_pool
|
class SPDLOG_API thread_pool
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using item_type = async_msg;
|
using item_type = async_msg;
|
||||||
@@ -97,6 +96,7 @@ public:
|
|||||||
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
|
void post_log(async_logger_ptr &&worker_ptr, const details::log_msg &msg, async_overflow_policy overflow_policy);
|
||||||
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy);
|
||||||
size_t overrun_counter();
|
size_t overrun_counter();
|
||||||
|
size_t queue_size();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
q_type q_;
|
q_type q_;
|
||||||
@@ -116,5 +116,5 @@ private:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "thread_pool-inl.h"
|
# include "thread_pool-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
11
include/spdlog/details/windows_include.h
Normal file
11
include/spdlog/details/windows_include.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NOMINMAX
|
||||||
|
# define NOMINMAX // prevent windows redefining min/max
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef WIN32_LEAN_AND_MEAN
|
||||||
|
# define WIN32_LEAN_AND_MEAN
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
@@ -5,13 +5,17 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cctype>
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
|
||||||
//
|
//
|
||||||
// Support for logging binary data as hex
|
// Support for logging binary data as hex
|
||||||
// format flags:
|
// format flags, any combination of the followng:
|
||||||
// {:X} - print in uppercase.
|
// {:X} - print in uppercase.
|
||||||
// {:s} - don't separate each byte with space.
|
// {:s} - don't separate each byte with space.
|
||||||
// {:p} - don't print the position on each line start.
|
// {:p} - don't print the position on each line start.
|
||||||
// {:n} - don't split the output to lines.
|
// {:n} - don't split the output to lines.
|
||||||
|
// {:a} - show ASCII if :n is not set
|
||||||
|
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
@@ -20,17 +24,19 @@
|
|||||||
// logger->info("Some buffer {}", spdlog::to_hex(v));
|
// logger->info("Some buffer {}", spdlog::to_hex(v));
|
||||||
// char buf[128];
|
// char buf[128];
|
||||||
// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf)));
|
// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf)));
|
||||||
|
// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf), 16));
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
class bytes_range
|
class dump_info
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
bytes_range(It range_begin, It range_end)
|
dump_info(It range_begin, It range_end, size_t size_per_line)
|
||||||
: begin_(range_begin)
|
: begin_(range_begin)
|
||||||
, end_(range_end)
|
, end_(range_end)
|
||||||
|
, size_per_line_(size_per_line)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
It begin() const
|
It begin() const
|
||||||
@@ -41,26 +47,31 @@ public:
|
|||||||
{
|
{
|
||||||
return end_;
|
return end_;
|
||||||
}
|
}
|
||||||
|
size_t size_per_line() const
|
||||||
|
{
|
||||||
|
return size_per_line_;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
It begin_, end_;
|
It begin_, end_;
|
||||||
|
size_t size_per_line_;
|
||||||
};
|
};
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
// create a bytes_range that wraps the given container
|
// create a dump_info that wraps the given container
|
||||||
template<typename Container>
|
template<typename Container>
|
||||||
inline details::bytes_range<typename Container::const_iterator> to_hex(const Container &container)
|
inline details::dump_info<typename Container::const_iterator> to_hex(const Container &container, size_t size_per_line = 32)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
|
static_assert(sizeof(typename Container::value_type) == 1, "sizeof(Container::value_type) != 1");
|
||||||
using Iter = typename Container::const_iterator;
|
using Iter = typename Container::const_iterator;
|
||||||
return details::bytes_range<Iter>(std::begin(container), std::end(container));
|
return details::dump_info<Iter>(std::begin(container), std::end(container), size_per_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create bytes_range from ranges
|
// create dump_info from ranges
|
||||||
template<typename It>
|
template<typename It>
|
||||||
inline details::bytes_range<It> to_hex(const It range_begin, const It range_end)
|
inline details::dump_info<It> to_hex(const It range_begin, const It range_end, size_t size_per_line = 32)
|
||||||
{
|
{
|
||||||
return details::bytes_range<It>(range_begin, range_end);
|
return details::dump_info<It>(range_begin, range_end, size_per_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
@@ -68,22 +79,21 @@ inline details::bytes_range<It> to_hex(const It range_begin, const It range_end)
|
|||||||
namespace fmt {
|
namespace fmt {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct formatter<spdlog::details::bytes_range<T>>
|
struct formatter<spdlog::details::dump_info<T>>
|
||||||
{
|
{
|
||||||
const std::size_t line_size = 100;
|
|
||||||
const char delimiter = ' ';
|
const char delimiter = ' ';
|
||||||
|
|
||||||
bool put_newlines = true;
|
bool put_newlines = true;
|
||||||
bool put_delimiters = true;
|
bool put_delimiters = true;
|
||||||
bool use_uppercase = false;
|
bool use_uppercase = false;
|
||||||
bool put_positions = true; // position on start of each line
|
bool put_positions = true; // position on start of each line
|
||||||
|
bool show_ascii = false;
|
||||||
|
|
||||||
// parse the format string flags
|
// parse the format string flags
|
||||||
template<typename ParseContext>
|
template<typename ParseContext>
|
||||||
auto parse(ParseContext &ctx) -> decltype(ctx.begin())
|
FMT_CONSTEXPR auto parse(ParseContext &ctx) -> decltype(ctx.begin())
|
||||||
{
|
{
|
||||||
auto it = ctx.begin();
|
auto it = ctx.begin();
|
||||||
while (*it && *it != '}')
|
while (it != ctx.end() && *it != '}')
|
||||||
{
|
{
|
||||||
switch (*it)
|
switch (*it)
|
||||||
{
|
{
|
||||||
@@ -98,6 +108,13 @@ struct formatter<spdlog::details::bytes_range<T>>
|
|||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
put_newlines = false;
|
put_newlines = false;
|
||||||
|
show_ascii = false;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
if (put_newlines)
|
||||||
|
{
|
||||||
|
show_ascii = true;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,53 +125,83 @@ struct formatter<spdlog::details::bytes_range<T>>
|
|||||||
|
|
||||||
// format the given bytes range as hex
|
// format the given bytes range as hex
|
||||||
template<typename FormatContext, typename Container>
|
template<typename FormatContext, typename Container>
|
||||||
auto format(const spdlog::details::bytes_range<Container> &the_range, FormatContext &ctx) -> decltype(ctx.out())
|
auto format(const spdlog::details::dump_info<Container> &the_range, FormatContext &ctx) -> decltype(ctx.out())
|
||||||
{
|
{
|
||||||
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
|
SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF";
|
||||||
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
|
SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef";
|
||||||
const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
|
const char *hex_chars = use_uppercase ? hex_upper : hex_lower;
|
||||||
|
|
||||||
std::size_t pos = 0;
|
|
||||||
std::size_t column = line_size;
|
|
||||||
#if FMT_VERSION < 60000
|
#if FMT_VERSION < 60000
|
||||||
auto inserter = ctx.begin();
|
auto inserter = ctx.begin();
|
||||||
#else
|
#else
|
||||||
auto inserter = ctx.out();
|
auto inserter = ctx.out();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (auto &item : the_range)
|
int size_per_line = static_cast<int>(the_range.size_per_line());
|
||||||
|
auto start_of_line = the_range.begin();
|
||||||
|
for (auto i = the_range.begin(); i != the_range.end(); i++)
|
||||||
{
|
{
|
||||||
auto ch = static_cast<unsigned char>(item);
|
auto ch = static_cast<unsigned char>(*i);
|
||||||
pos++;
|
|
||||||
|
|
||||||
if (put_newlines && column >= line_size)
|
if (put_newlines && (i == the_range.begin() || i - start_of_line >= size_per_line))
|
||||||
{
|
{
|
||||||
column = put_newline(inserter, pos);
|
if (show_ascii && i != the_range.begin())
|
||||||
|
{
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
for (auto j = start_of_line; j < i; j++)
|
||||||
|
{
|
||||||
|
auto pc = static_cast<unsigned char>(*j);
|
||||||
|
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
put_newline(inserter, static_cast<size_t>(i - the_range.begin()));
|
||||||
|
|
||||||
// put first byte without delimiter in front of it
|
// put first byte without delimiter in front of it
|
||||||
*inserter++ = hex_chars[(ch >> 4) & 0x0f];
|
*inserter++ = hex_chars[(ch >> 4) & 0x0f];
|
||||||
*inserter++ = hex_chars[ch & 0x0f];
|
*inserter++ = hex_chars[ch & 0x0f];
|
||||||
column += 2;
|
start_of_line = i;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (put_delimiters)
|
if (put_delimiters)
|
||||||
{
|
{
|
||||||
*inserter++ = delimiter;
|
*inserter++ = delimiter;
|
||||||
++column;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*inserter++ = hex_chars[(ch >> 4) & 0x0f];
|
*inserter++ = hex_chars[(ch >> 4) & 0x0f];
|
||||||
*inserter++ = hex_chars[ch & 0x0f];
|
*inserter++ = hex_chars[ch & 0x0f];
|
||||||
column += 2;
|
}
|
||||||
|
if (show_ascii) // add ascii to last line
|
||||||
|
{
|
||||||
|
if (the_range.end() - the_range.begin() > size_per_line)
|
||||||
|
{
|
||||||
|
auto blank_num = size_per_line - (the_range.end() - start_of_line);
|
||||||
|
while (blank_num-- > 0)
|
||||||
|
{
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
if (put_delimiters)
|
||||||
|
{
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
*inserter++ = delimiter;
|
||||||
|
for (auto j = start_of_line; j != the_range.end(); j++)
|
||||||
|
{
|
||||||
|
auto pc = static_cast<unsigned char>(*j);
|
||||||
|
*inserter++ = std::isprint(pc) ? static_cast<char>(*j) : '.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return inserter;
|
return inserter;
|
||||||
}
|
}
|
||||||
|
|
||||||
// put newline(and position header)
|
// put newline(and position header)
|
||||||
// return the next column
|
|
||||||
template<typename It>
|
template<typename It>
|
||||||
std::size_t put_newline(It inserter, std::size_t pos)
|
void put_newline(It inserter, std::size_t pos)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
*inserter++ = '\r';
|
*inserter++ = '\r';
|
||||||
@@ -163,12 +210,7 @@ struct formatter<spdlog::details::bytes_range<T>>
|
|||||||
|
|
||||||
if (put_positions)
|
if (put_positions)
|
||||||
{
|
{
|
||||||
fmt::format_to(inserter, "{:<04X}: ", pos - 1);
|
fmt::format_to(inserter, "{:04X}: ", pos);
|
||||||
return 7;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
Copyright (c) 2012 - present, Victor Zverovich
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining
|
|
||||||
a copy of this software and associated documentation files (the
|
|
||||||
"Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish,
|
|
||||||
distribute, sublicense, and/or sell copies of the Software, and to
|
|
||||||
permit persons to whom the Software is furnished to do so, subject to
|
|
||||||
the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be
|
|
||||||
included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
||||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
||||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
||||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
||||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
||||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
||||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
--- Optional exception to the license ---
|
|
||||||
|
|
||||||
As an exception, if, as a result of your compiling your source code, portions
|
|
||||||
of this Software are embedded into a machine-executable object form of such
|
|
||||||
source code, you may redistribute such embedded portions in such object form
|
|
||||||
without including the above copyright and permission notices.
|
|
||||||
232
include/spdlog/fmt/bundled/args.h
Normal file
232
include/spdlog/fmt/bundled/args.h
Normal file
@@ -0,0 +1,232 @@
|
|||||||
|
// Formatting library for C++ - dynamic format arguments
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012 - present, Victor Zverovich
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// For the license information refer to format.h.
|
||||||
|
|
||||||
|
#ifndef FMT_ARGS_H_
|
||||||
|
#define FMT_ARGS_H_
|
||||||
|
|
||||||
|
#include <functional> // std::reference_wrapper
|
||||||
|
#include <memory> // std::unique_ptr
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "core.h"
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
template <typename T> struct is_reference_wrapper : std::false_type {};
|
||||||
|
template <typename T>
|
||||||
|
struct is_reference_wrapper<std::reference_wrapper<T>> : std::true_type {};
|
||||||
|
|
||||||
|
template <typename T> const T& unwrap(const T& v) { return v; }
|
||||||
|
template <typename T> const T& unwrap(const std::reference_wrapper<T>& v) {
|
||||||
|
return static_cast<const T&>(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
class dynamic_arg_list {
|
||||||
|
// Workaround for clang's -Wweak-vtables. Unlike for regular classes, for
|
||||||
|
// templates it doesn't complain about inability to deduce single translation
|
||||||
|
// unit for placing vtable. So storage_node_base is made a fake template.
|
||||||
|
template <typename = void> struct node {
|
||||||
|
virtual ~node() = default;
|
||||||
|
std::unique_ptr<node<>> next;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T> struct typed_node : node<> {
|
||||||
|
T value;
|
||||||
|
|
||||||
|
template <typename Arg>
|
||||||
|
FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {}
|
||||||
|
|
||||||
|
template <typename Char>
|
||||||
|
FMT_CONSTEXPR typed_node(const basic_string_view<Char>& arg)
|
||||||
|
: value(arg.data(), arg.size()) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<node<>> head_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <typename T, typename Arg> const T& push(const Arg& arg) {
|
||||||
|
auto new_node = std::unique_ptr<typed_node<T>>(new typed_node<T>(arg));
|
||||||
|
auto& value = new_node->value;
|
||||||
|
new_node->next = std::move(head_);
|
||||||
|
head_ = std::move(new_node);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
A dynamic version of `fmt::format_arg_store`.
|
||||||
|
It's equipped with a storage to potentially temporary objects which lifetimes
|
||||||
|
could be shorter than the format arguments object.
|
||||||
|
|
||||||
|
It can be implicitly converted into `~fmt::basic_format_args` for passing
|
||||||
|
into type-erased formatting functions such as `~fmt::vformat`.
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename Context>
|
||||||
|
class dynamic_format_arg_store
|
||||||
|
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
|
||||||
|
// Workaround a GCC template argument substitution bug.
|
||||||
|
: public basic_format_args<Context>
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
using char_type = typename Context::char_type;
|
||||||
|
|
||||||
|
template <typename T> struct need_copy {
|
||||||
|
static constexpr detail::type mapped_type =
|
||||||
|
detail::mapped_type_constant<T, Context>::value;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
value = !(detail::is_reference_wrapper<T>::value ||
|
||||||
|
std::is_same<T, basic_string_view<char_type>>::value ||
|
||||||
|
std::is_same<T, detail::std_string_view<char_type>>::value ||
|
||||||
|
(mapped_type != detail::type::cstring_type &&
|
||||||
|
mapped_type != detail::type::string_type &&
|
||||||
|
mapped_type != detail::type::custom_type))
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
using stored_type = conditional_t<detail::is_string<T>::value &&
|
||||||
|
!has_formatter<T, Context>::value &&
|
||||||
|
!detail::is_reference_wrapper<T>::value,
|
||||||
|
std::basic_string<char_type>, T>;
|
||||||
|
|
||||||
|
// Storage of basic_format_arg must be contiguous.
|
||||||
|
std::vector<basic_format_arg<Context>> data_;
|
||||||
|
std::vector<detail::named_arg_info<char_type>> named_info_;
|
||||||
|
|
||||||
|
// Storage of arguments not fitting into basic_format_arg must grow
|
||||||
|
// without relocation because items in data_ refer to it.
|
||||||
|
detail::dynamic_arg_list dynamic_args_;
|
||||||
|
|
||||||
|
friend class basic_format_args<Context>;
|
||||||
|
|
||||||
|
unsigned long long get_types() const {
|
||||||
|
return detail::is_unpacked_bit | data_.size() |
|
||||||
|
(named_info_.empty()
|
||||||
|
? 0ULL
|
||||||
|
: static_cast<unsigned long long>(detail::has_named_args_bit));
|
||||||
|
}
|
||||||
|
|
||||||
|
const basic_format_arg<Context>* data() const {
|
||||||
|
return named_info_.empty() ? data_.data() : data_.data() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> void emplace_arg(const T& arg) {
|
||||||
|
data_.emplace_back(detail::make_arg<Context>(arg));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void emplace_arg(const detail::named_arg<char_type, T>& arg) {
|
||||||
|
if (named_info_.empty()) {
|
||||||
|
constexpr const detail::named_arg_info<char_type>* zero_ptr{nullptr};
|
||||||
|
data_.insert(data_.begin(), {zero_ptr, 0});
|
||||||
|
}
|
||||||
|
data_.emplace_back(detail::make_arg<Context>(detail::unwrap(arg.value)));
|
||||||
|
auto pop_one = [](std::vector<basic_format_arg<Context>>* data) {
|
||||||
|
data->pop_back();
|
||||||
|
};
|
||||||
|
std::unique_ptr<std::vector<basic_format_arg<Context>>, decltype(pop_one)>
|
||||||
|
guard{&data_, pop_one};
|
||||||
|
named_info_.push_back({arg.name, static_cast<int>(data_.size() - 2u)});
|
||||||
|
data_[0].value_.named_args = {named_info_.data(), named_info_.size()};
|
||||||
|
guard.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Adds an argument into the dynamic store for later passing to a formatting
|
||||||
|
function.
|
||||||
|
|
||||||
|
Note that custom types and string types (but not string views) are copied
|
||||||
|
into the store dynamically allocating memory if necessary.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
||||||
|
store.push_back(42);
|
||||||
|
store.push_back("abc");
|
||||||
|
store.push_back(1.5f);
|
||||||
|
std::string result = fmt::vformat("{} and {} and {}", store);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename T> void push_back(const T& arg) {
|
||||||
|
if (detail::const_check(need_copy<T>::value))
|
||||||
|
emplace_arg(dynamic_args_.push<stored_type<T>>(arg));
|
||||||
|
else
|
||||||
|
emplace_arg(detail::unwrap(arg));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Adds a reference to the argument into the dynamic store for later passing to
|
||||||
|
a formatting function.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::dynamic_format_arg_store<fmt::format_context> store;
|
||||||
|
char band[] = "Rolling Stones";
|
||||||
|
store.push_back(std::cref(band));
|
||||||
|
band[9] = 'c'; // Changing str affects the output.
|
||||||
|
std::string result = fmt::vformat("{}", store);
|
||||||
|
// result == "Rolling Scones"
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename T> void push_back(std::reference_wrapper<T> arg) {
|
||||||
|
static_assert(
|
||||||
|
need_copy<T>::value,
|
||||||
|
"objects of built-in types and string views are always copied");
|
||||||
|
emplace_arg(arg.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Adds named argument into the dynamic store for later passing to a formatting
|
||||||
|
function. ``std::reference_wrapper`` is supported to avoid copying of the
|
||||||
|
argument. The name is always copied into the store.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
void push_back(const detail::named_arg<char_type, T>& arg) {
|
||||||
|
const char_type* arg_name =
|
||||||
|
dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str();
|
||||||
|
if (detail::const_check(need_copy<T>::value)) {
|
||||||
|
emplace_arg(
|
||||||
|
fmt::arg(arg_name, dynamic_args_.push<stored_type<T>>(arg.value)));
|
||||||
|
} else {
|
||||||
|
emplace_arg(fmt::arg(arg_name, arg.value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Erase all elements from the store */
|
||||||
|
void clear() {
|
||||||
|
data_.clear();
|
||||||
|
named_info_.clear();
|
||||||
|
dynamic_args_ = detail::dynamic_arg_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Reserves space to store at least *new_cap* arguments including
|
||||||
|
*new_cap_named* named arguments.
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
void reserve(size_t new_cap, size_t new_cap_named) {
|
||||||
|
FMT_ASSERT(new_cap >= new_cap_named,
|
||||||
|
"Set of arguments includes set of named arguments");
|
||||||
|
data_.reserve(new_cap);
|
||||||
|
named_info_.reserve(new_cap_named);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // FMT_ARGS_H_
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -10,7 +10,15 @@
|
|||||||
|
|
||||||
#include "format.h"
|
#include "format.h"
|
||||||
|
|
||||||
|
// __declspec(deprecated) is broken in some MSVC versions.
|
||||||
|
#if FMT_MSC_VER
|
||||||
|
# define FMT_DEPRECATED_NONMSVC
|
||||||
|
#else
|
||||||
|
# define FMT_DEPRECATED_NONMSVC FMT_DEPRECATED
|
||||||
|
#endif
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
|
FMT_MODULE_EXPORT_BEGIN
|
||||||
|
|
||||||
enum class color : uint32_t {
|
enum class color : uint32_t {
|
||||||
alice_blue = 0xF0F8FF, // rgb(240,248,255)
|
alice_blue = 0xF0F8FF, // rgb(240,248,255)
|
||||||
@@ -198,7 +206,7 @@ struct rgb {
|
|||||||
uint8_t b;
|
uint8_t b;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace internal {
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
// color is a struct of either a rgb color or a terminal color.
|
// color is a struct of either a rgb color or a terminal color.
|
||||||
struct color_type {
|
struct color_type {
|
||||||
@@ -221,9 +229,10 @@ struct color_type {
|
|||||||
uint32_t rgb_color;
|
uint32_t rgb_color;
|
||||||
} value;
|
} value;
|
||||||
};
|
};
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
// Experimental text formatting support.
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
/** A text style consisting of foreground and background colors and emphasis. */
|
||||||
class text_style {
|
class text_style {
|
||||||
public:
|
public:
|
||||||
FMT_CONSTEXPR text_style(emphasis em = emphasis()) FMT_NOEXCEPT
|
FMT_CONSTEXPR text_style(emphasis em = emphasis()) FMT_NOEXCEPT
|
||||||
@@ -260,7 +269,55 @@ class text_style {
|
|||||||
return lhs |= rhs;
|
return lhs |= rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR text_style& operator&=(const text_style& rhs) {
|
FMT_DEPRECATED_NONMSVC FMT_CONSTEXPR text_style& operator&=(
|
||||||
|
const text_style& rhs) {
|
||||||
|
return and_assign(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_DEPRECATED_NONMSVC friend FMT_CONSTEXPR text_style
|
||||||
|
operator&(text_style lhs, const text_style& rhs) {
|
||||||
|
return lhs.and_assign(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_CONSTEXPR bool has_foreground() const FMT_NOEXCEPT {
|
||||||
|
return set_foreground_color;
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR bool has_background() const FMT_NOEXCEPT {
|
||||||
|
return set_background_color;
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR bool has_emphasis() const FMT_NOEXCEPT {
|
||||||
|
return static_cast<uint8_t>(ems) != 0;
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR detail::color_type get_foreground() const FMT_NOEXCEPT {
|
||||||
|
FMT_ASSERT(has_foreground(), "no foreground specified for this style");
|
||||||
|
return foreground_color;
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR detail::color_type get_background() const FMT_NOEXCEPT {
|
||||||
|
FMT_ASSERT(has_background(), "no background specified for this style");
|
||||||
|
return background_color;
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR emphasis get_emphasis() const FMT_NOEXCEPT {
|
||||||
|
FMT_ASSERT(has_emphasis(), "no emphasis specified for this style");
|
||||||
|
return ems;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
FMT_CONSTEXPR text_style(bool is_foreground,
|
||||||
|
detail::color_type text_color) FMT_NOEXCEPT
|
||||||
|
: set_foreground_color(),
|
||||||
|
set_background_color(),
|
||||||
|
ems() {
|
||||||
|
if (is_foreground) {
|
||||||
|
foreground_color = text_color;
|
||||||
|
set_foreground_color = true;
|
||||||
|
} else {
|
||||||
|
background_color = text_color;
|
||||||
|
set_background_color = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEPRECATED!
|
||||||
|
FMT_CONSTEXPR text_style& and_assign(const text_style& rhs) {
|
||||||
if (!set_foreground_color) {
|
if (!set_foreground_color) {
|
||||||
set_foreground_color = rhs.set_foreground_color;
|
set_foreground_color = rhs.set_foreground_color;
|
||||||
foreground_color = rhs.foreground_color;
|
foreground_color = rhs.foreground_color;
|
||||||
@@ -284,87 +341,49 @@ class text_style {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend FMT_CONSTEXPR text_style operator&(text_style lhs,
|
friend FMT_CONSTEXPR_DECL text_style fg(detail::color_type foreground)
|
||||||
const text_style& rhs) {
|
|
||||||
return lhs &= rhs;
|
|
||||||
}
|
|
||||||
|
|
||||||
FMT_CONSTEXPR bool has_foreground() const FMT_NOEXCEPT {
|
|
||||||
return set_foreground_color;
|
|
||||||
}
|
|
||||||
FMT_CONSTEXPR bool has_background() const FMT_NOEXCEPT {
|
|
||||||
return set_background_color;
|
|
||||||
}
|
|
||||||
FMT_CONSTEXPR bool has_emphasis() const FMT_NOEXCEPT {
|
|
||||||
return static_cast<uint8_t>(ems) != 0;
|
|
||||||
}
|
|
||||||
FMT_CONSTEXPR internal::color_type get_foreground() const FMT_NOEXCEPT {
|
|
||||||
assert(has_foreground() && "no foreground specified for this style");
|
|
||||||
return foreground_color;
|
|
||||||
}
|
|
||||||
FMT_CONSTEXPR internal::color_type get_background() const FMT_NOEXCEPT {
|
|
||||||
assert(has_background() && "no background specified for this style");
|
|
||||||
return background_color;
|
|
||||||
}
|
|
||||||
FMT_CONSTEXPR emphasis get_emphasis() const FMT_NOEXCEPT {
|
|
||||||
assert(has_emphasis() && "no emphasis specified for this style");
|
|
||||||
return ems;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
FMT_CONSTEXPR text_style(bool is_foreground,
|
|
||||||
internal::color_type text_color) FMT_NOEXCEPT
|
|
||||||
: set_foreground_color(),
|
|
||||||
set_background_color(),
|
|
||||||
ems() {
|
|
||||||
if (is_foreground) {
|
|
||||||
foreground_color = text_color;
|
|
||||||
set_foreground_color = true;
|
|
||||||
} else {
|
|
||||||
background_color = text_color;
|
|
||||||
set_background_color = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
friend FMT_CONSTEXPR_DECL text_style fg(internal::color_type foreground)
|
|
||||||
FMT_NOEXCEPT;
|
|
||||||
friend FMT_CONSTEXPR_DECL text_style bg(internal::color_type background)
|
|
||||||
FMT_NOEXCEPT;
|
FMT_NOEXCEPT;
|
||||||
|
|
||||||
internal::color_type foreground_color;
|
friend FMT_CONSTEXPR_DECL text_style bg(detail::color_type background)
|
||||||
internal::color_type background_color;
|
FMT_NOEXCEPT;
|
||||||
|
|
||||||
|
detail::color_type foreground_color;
|
||||||
|
detail::color_type background_color;
|
||||||
bool set_foreground_color;
|
bool set_foreground_color;
|
||||||
bool set_background_color;
|
bool set_background_color;
|
||||||
emphasis ems;
|
emphasis ems;
|
||||||
};
|
};
|
||||||
|
|
||||||
FMT_CONSTEXPR text_style fg(internal::color_type foreground) FMT_NOEXCEPT {
|
/** Creates a text style from the foreground (text) color. */
|
||||||
return text_style(/*is_foreground=*/true, foreground);
|
FMT_CONSTEXPR inline text_style fg(detail::color_type foreground) FMT_NOEXCEPT {
|
||||||
|
return text_style(true, foreground);
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR text_style bg(internal::color_type background) FMT_NOEXCEPT {
|
/** Creates a text style from the background color. */
|
||||||
return text_style(/*is_foreground=*/false, background);
|
FMT_CONSTEXPR inline text_style bg(detail::color_type background) FMT_NOEXCEPT {
|
||||||
|
return text_style(false, background);
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR text_style operator|(emphasis lhs, emphasis rhs) FMT_NOEXCEPT {
|
FMT_CONSTEXPR inline text_style operator|(emphasis lhs,
|
||||||
|
emphasis rhs) FMT_NOEXCEPT {
|
||||||
return text_style(lhs) | rhs;
|
return text_style(lhs) | rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace internal {
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
template <typename Char> struct ansi_color_escape {
|
template <typename Char> struct ansi_color_escape {
|
||||||
FMT_CONSTEXPR ansi_color_escape(internal::color_type text_color,
|
FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color,
|
||||||
const char* esc) FMT_NOEXCEPT {
|
const char* esc) FMT_NOEXCEPT {
|
||||||
// If we have a terminal color, we need to output another escape code
|
// If we have a terminal color, we need to output another escape code
|
||||||
// sequence.
|
// sequence.
|
||||||
if (!text_color.is_rgb) {
|
if (!text_color.is_rgb) {
|
||||||
bool is_background = esc == internal::data::background_color;
|
bool is_background = esc == string_view("\x1b[48;2;");
|
||||||
uint32_t value = text_color.value.term_color;
|
uint32_t value = text_color.value.term_color;
|
||||||
// Background ASCII codes are the same as the foreground ones but with
|
// Background ASCII codes are the same as the foreground ones but with
|
||||||
// 10 more.
|
// 10 more.
|
||||||
if (is_background) value += 10u;
|
if (is_background) value += 10u;
|
||||||
|
|
||||||
std::size_t index = 0;
|
size_t index = 0;
|
||||||
buffer[index++] = static_cast<Char>('\x1b');
|
buffer[index++] = static_cast<Char>('\x1b');
|
||||||
buffer[index++] = static_cast<Char>('[');
|
buffer[index++] = static_cast<Char>('[');
|
||||||
|
|
||||||
@@ -398,7 +417,7 @@ template <typename Char> struct ansi_color_escape {
|
|||||||
if (em_bits & static_cast<uint8_t>(emphasis::strikethrough))
|
if (em_bits & static_cast<uint8_t>(emphasis::strikethrough))
|
||||||
em_codes[3] = 9;
|
em_codes[3] = 9;
|
||||||
|
|
||||||
std::size_t index = 0;
|
size_t index = 0;
|
||||||
for (int i = 0; i < 4; ++i) {
|
for (int i = 0; i < 4; ++i) {
|
||||||
if (!em_codes[i]) continue;
|
if (!em_codes[i]) continue;
|
||||||
buffer[index++] = static_cast<Char>('\x1b');
|
buffer[index++] = static_cast<Char>('\x1b');
|
||||||
@@ -411,8 +430,8 @@ template <typename Char> struct ansi_color_escape {
|
|||||||
FMT_CONSTEXPR operator const Char*() const FMT_NOEXCEPT { return buffer; }
|
FMT_CONSTEXPR operator const Char*() const FMT_NOEXCEPT { return buffer; }
|
||||||
|
|
||||||
FMT_CONSTEXPR const Char* begin() const FMT_NOEXCEPT { return buffer; }
|
FMT_CONSTEXPR const Char* begin() const FMT_NOEXCEPT { return buffer; }
|
||||||
FMT_CONSTEXPR const Char* end() const FMT_NOEXCEPT {
|
FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const FMT_NOEXCEPT {
|
||||||
return buffer + std::strlen(buffer);
|
return buffer + std::char_traits<Char>::length(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -429,14 +448,14 @@ template <typename Char> struct ansi_color_escape {
|
|||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color(
|
FMT_CONSTEXPR ansi_color_escape<Char> make_foreground_color(
|
||||||
internal::color_type foreground) FMT_NOEXCEPT {
|
detail::color_type foreground) FMT_NOEXCEPT {
|
||||||
return ansi_color_escape<Char>(foreground, internal::data::foreground_color);
|
return ansi_color_escape<Char>(foreground, "\x1b[38;2;");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
FMT_CONSTEXPR ansi_color_escape<Char> make_background_color(
|
FMT_CONSTEXPR ansi_color_escape<Char> make_background_color(
|
||||||
internal::color_type background) FMT_NOEXCEPT {
|
detail::color_type background) FMT_NOEXCEPT {
|
||||||
return ansi_color_escape<Char>(background, internal::data::background_color);
|
return ansi_color_escape<Char>(background, "\x1b[48;2;");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
@@ -455,110 +474,97 @@ inline void fputs<wchar_t>(const wchar_t* chars, FILE* stream) FMT_NOEXCEPT {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char> inline void reset_color(FILE* stream) FMT_NOEXCEPT {
|
template <typename Char> inline void reset_color(FILE* stream) FMT_NOEXCEPT {
|
||||||
fputs(internal::data::reset_color, stream);
|
fputs("\x1b[0m", stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <> inline void reset_color<wchar_t>(FILE* stream) FMT_NOEXCEPT {
|
template <> inline void reset_color<wchar_t>(FILE* stream) FMT_NOEXCEPT {
|
||||||
fputs(internal::data::wreset_color, stream);
|
fputs(L"\x1b[0m", stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
inline void reset_color(basic_memory_buffer<Char>& buffer) FMT_NOEXCEPT {
|
inline void reset_color(buffer<Char>& buffer) FMT_NOEXCEPT {
|
||||||
const char* begin = data::reset_color;
|
auto reset_color = string_view("\x1b[0m");
|
||||||
const char* end = begin + sizeof(data::reset_color) - 1;
|
buffer.append(reset_color.begin(), reset_color.end());
|
||||||
buffer.append(begin, end);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
std::basic_string<Char> vformat(const text_style& ts,
|
void vformat_to(buffer<Char>& buf, const text_style& ts,
|
||||||
basic_string_view<Char> format_str,
|
basic_string_view<Char> format_str,
|
||||||
basic_format_args<buffer_context<Char> > args) {
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
|
||||||
basic_memory_buffer<Char> buffer;
|
|
||||||
bool has_style = false;
|
bool has_style = false;
|
||||||
if (ts.has_emphasis()) {
|
if (ts.has_emphasis()) {
|
||||||
has_style = true;
|
has_style = true;
|
||||||
ansi_color_escape<Char> escape = make_emphasis<Char>(ts.get_emphasis());
|
auto emphasis = detail::make_emphasis<Char>(ts.get_emphasis());
|
||||||
buffer.append(escape.begin(), escape.end());
|
buf.append(emphasis.begin(), emphasis.end());
|
||||||
}
|
}
|
||||||
if (ts.has_foreground()) {
|
if (ts.has_foreground()) {
|
||||||
has_style = true;
|
has_style = true;
|
||||||
ansi_color_escape<Char> escape =
|
auto foreground = detail::make_foreground_color<Char>(ts.get_foreground());
|
||||||
make_foreground_color<Char>(ts.get_foreground());
|
buf.append(foreground.begin(), foreground.end());
|
||||||
buffer.append(escape.begin(), escape.end());
|
|
||||||
}
|
}
|
||||||
if (ts.has_background()) {
|
if (ts.has_background()) {
|
||||||
has_style = true;
|
has_style = true;
|
||||||
ansi_color_escape<Char> escape =
|
auto background = detail::make_background_color<Char>(ts.get_background());
|
||||||
make_background_color<Char>(ts.get_background());
|
buf.append(background.begin(), background.end());
|
||||||
buffer.append(escape.begin(), escape.end());
|
|
||||||
}
|
}
|
||||||
internal::vformat_to(buffer, format_str, args);
|
detail::vformat_to(buf, format_str, args, {});
|
||||||
if (has_style) {
|
if (has_style) detail::reset_color<Char>(buf);
|
||||||
reset_color<Char>(buffer);
|
|
||||||
}
|
|
||||||
return fmt::to_string(buffer);
|
|
||||||
}
|
}
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S> >
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
template <typename S, typename Char = char_t<S>>
|
||||||
void vprint(std::FILE* f, const text_style& ts, const S& format,
|
void vprint(std::FILE* f, const text_style& ts, const S& format,
|
||||||
basic_format_args<buffer_context<Char> > args) {
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
|
||||||
bool has_style = false;
|
basic_memory_buffer<Char> buf;
|
||||||
if (ts.has_emphasis()) {
|
detail::vformat_to(buf, ts, to_string_view(format), args);
|
||||||
has_style = true;
|
buf.push_back(Char(0));
|
||||||
internal::fputs<Char>(internal::make_emphasis<Char>(ts.get_emphasis()), f);
|
detail::fputs(buf.data(), f);
|
||||||
}
|
|
||||||
if (ts.has_foreground()) {
|
|
||||||
has_style = true;
|
|
||||||
internal::fputs<Char>(
|
|
||||||
internal::make_foreground_color<Char>(ts.get_foreground()), f);
|
|
||||||
}
|
|
||||||
if (ts.has_background()) {
|
|
||||||
has_style = true;
|
|
||||||
internal::fputs<Char>(
|
|
||||||
internal::make_background_color<Char>(ts.get_background()), f);
|
|
||||||
}
|
|
||||||
vprint(f, format, args);
|
|
||||||
if (has_style) {
|
|
||||||
internal::reset_color<Char>(f);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
\rst
|
||||||
Formats a string and prints it to the specified file stream using ANSI
|
Formats a string and prints it to the specified file stream using ANSI
|
||||||
escape sequences to specify text formatting.
|
escape sequences to specify text formatting.
|
||||||
Example:
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
|
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
|
||||||
"Elapsed time: {0:.2f} seconds", 1.23);
|
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(internal::is_string<S>::value)>
|
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
void print(std::FILE* f, const text_style& ts, const S& format_str,
|
void print(std::FILE* f, const text_style& ts, const S& format_str,
|
||||||
const Args&... args) {
|
const Args&... args) {
|
||||||
internal::check_format_string<Args...>(format_str);
|
vprint(f, ts, format_str,
|
||||||
using context = buffer_context<char_t<S> >;
|
fmt::make_args_checked<Args...>(format_str, args...));
|
||||||
format_arg_store<context, Args...> as{args...};
|
|
||||||
vprint(f, ts, format_str, basic_format_args<context>(as));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
\rst
|
||||||
Formats a string and prints it to stdout using ANSI escape sequences to
|
Formats a string and prints it to stdout using ANSI escape sequences to
|
||||||
specify text formatting.
|
specify text formatting.
|
||||||
Example:
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
|
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
|
||||||
"Elapsed time: {0:.2f} seconds", 1.23);
|
"Elapsed time: {0:.2f} seconds", 1.23);
|
||||||
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
FMT_ENABLE_IF(internal::is_string<S>::value)>
|
FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
void print(const text_style& ts, const S& format_str, const Args&... args) {
|
void print(const text_style& ts, const S& format_str, const Args&... args) {
|
||||||
return print(stdout, ts, format_str, args...);
|
return print(stdout, ts, format_str, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S> >
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline std::basic_string<Char> vformat(
|
inline std::basic_string<Char> vformat(
|
||||||
const text_style& ts, const S& format_str,
|
const text_style& ts, const S& format_str,
|
||||||
basic_format_args<buffer_context<Char> > args) {
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
|
||||||
return internal::vformat(ts, to_string_view(format_str), args);
|
basic_memory_buffer<Char> buf;
|
||||||
|
detail::vformat_to(buf, ts, to_string_view(format_str), args);
|
||||||
|
return fmt::to_string(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -573,13 +579,49 @@ inline std::basic_string<Char> vformat(
|
|||||||
"The answer is {}", 42);
|
"The answer is {}", 42);
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args, typename Char = char_t<S> >
|
template <typename S, typename... Args, typename Char = char_t<S>>
|
||||||
inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
|
inline std::basic_string<Char> format(const text_style& ts, const S& format_str,
|
||||||
const Args&... args) {
|
const Args&... args) {
|
||||||
return internal::vformat(ts, to_string_view(format_str),
|
return fmt::vformat(ts, to_string_view(format_str),
|
||||||
{internal::make_args_checked(format_str, args...)});
|
fmt::make_args_checked<Args...>(format_str, args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Formats a string with the given text_style and writes the output to ``out``.
|
||||||
|
*/
|
||||||
|
template <typename OutputIt, typename Char,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value)>
|
||||||
|
OutputIt vformat_to(
|
||||||
|
OutputIt out, const text_style& ts, basic_string_view<Char> format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
|
||||||
|
auto&& buf = detail::get_buffer<Char>(out);
|
||||||
|
detail::vformat_to(buf, ts, format_str, args);
|
||||||
|
return detail::get_iterator(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Formats arguments with the given text_style, writes the result to the output
|
||||||
|
iterator ``out`` and returns the iterator past the end of the output range.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
std::vector<char> out;
|
||||||
|
fmt::format_to(std::back_inserter(out),
|
||||||
|
fmt::emphasis::bold | fg(fmt::color::red), "{}", 42);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
|
bool enable = detail::is_output_iterator<OutputIt, char_t<S>>::value&&
|
||||||
|
detail::is_string<S>::value>
|
||||||
|
inline auto format_to(OutputIt out, const text_style& ts, const S& format_str,
|
||||||
|
Args&&... args) ->
|
||||||
|
typename std::enable_if<enable, OutputIt>::type {
|
||||||
|
return vformat_to(out, ts, to_string_view(format_str),
|
||||||
|
fmt::make_args_checked<Args...>(format_str, args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_MODULE_EXPORT_END
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // FMT_COLOR_H_
|
#endif // FMT_COLOR_H_
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,77 +1,2 @@
|
|||||||
// Formatting library for C++ - std::locale support
|
#include "xchar.h"
|
||||||
//
|
#warning fmt/locale.h is deprecated, include fmt/format.h or fmt/xchar.h instead
|
||||||
// Copyright (c) 2012 - present, Victor Zverovich
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// For the license information refer to format.h.
|
|
||||||
|
|
||||||
#ifndef FMT_LOCALE_H_
|
|
||||||
#define FMT_LOCALE_H_
|
|
||||||
|
|
||||||
#include <locale>
|
|
||||||
#include "format.h"
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
|
||||||
|
|
||||||
namespace internal {
|
|
||||||
template <typename Char>
|
|
||||||
typename buffer_context<Char>::iterator vformat_to(
|
|
||||||
const std::locale& loc, buffer<Char>& buf,
|
|
||||||
basic_string_view<Char> format_str,
|
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
|
||||||
using range = buffer_range<Char>;
|
|
||||||
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str), args,
|
|
||||||
internal::locale_ref(loc));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Char>
|
|
||||||
std::basic_string<Char> vformat(const std::locale& loc,
|
|
||||||
basic_string_view<Char> format_str,
|
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
|
||||||
basic_memory_buffer<Char> buffer;
|
|
||||||
internal::vformat_to(loc, buffer, format_str, args);
|
|
||||||
return fmt::to_string(buffer);
|
|
||||||
}
|
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
|
||||||
inline std::basic_string<Char> vformat(
|
|
||||||
const std::locale& loc, const S& format_str,
|
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
|
||||||
return internal::vformat(loc, to_string_view(format_str), args);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename S, typename... Args, typename Char = char_t<S>>
|
|
||||||
inline std::basic_string<Char> format(const std::locale& loc,
|
|
||||||
const S& format_str, Args&&... args) {
|
|
||||||
return internal::vformat(
|
|
||||||
loc, to_string_view(format_str),
|
|
||||||
{internal::make_args_checked<Args...>(format_str, args...)});
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename S, typename OutputIt, typename... Args,
|
|
||||||
typename Char = enable_if_t<
|
|
||||||
internal::is_output_iterator<OutputIt>::value, char_t<S>>>
|
|
||||||
inline OutputIt vformat_to(OutputIt out, const std::locale& loc,
|
|
||||||
const S& format_str,
|
|
||||||
format_args_t<OutputIt, Char> args) {
|
|
||||||
using range = internal::output_range<OutputIt, Char>;
|
|
||||||
return vformat_to<arg_formatter<range>>(
|
|
||||||
range(out), to_string_view(format_str), args, internal::locale_ref(loc));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename OutputIt, typename S, typename... Args,
|
|
||||||
FMT_ENABLE_IF(internal::is_output_iterator<OutputIt>::value&&
|
|
||||||
internal::is_string<S>::value)>
|
|
||||||
inline OutputIt format_to(OutputIt out, const std::locale& loc,
|
|
||||||
const S& format_str, Args&&... args) {
|
|
||||||
internal::check_format_string<Args...>(format_str);
|
|
||||||
using context = format_context_t<OutputIt, char_t<S>>;
|
|
||||||
format_arg_store<context, Args...> as{args...};
|
|
||||||
return vformat_to(out, loc, to_string_view(format_str),
|
|
||||||
basic_format_args<context>(as));
|
|
||||||
}
|
|
||||||
|
|
||||||
FMT_END_NAMESPACE
|
|
||||||
|
|
||||||
#endif // FMT_LOCALE_H_
|
|
||||||
|
|||||||
515
include/spdlog/fmt/bundled/os.h
Normal file
515
include/spdlog/fmt/bundled/os.h
Normal file
@@ -0,0 +1,515 @@
|
|||||||
|
// Formatting library for C++ - optional OS-specific functionality
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012 - present, Victor Zverovich
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// For the license information refer to format.h.
|
||||||
|
|
||||||
|
#ifndef FMT_OS_H_
|
||||||
|
#define FMT_OS_H_
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <clocale> // locale_t
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib> // strtod_l
|
||||||
|
#include <system_error> // std::system_error
|
||||||
|
|
||||||
|
#if defined __APPLE__ || defined(__FreeBSD__)
|
||||||
|
# include <xlocale.h> // for LC_NUMERIC_MASK on OS X
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "format.h"
|
||||||
|
|
||||||
|
// UWP doesn't provide _pipe.
|
||||||
|
#if FMT_HAS_INCLUDE("winapifamily.h")
|
||||||
|
# include <winapifamily.h>
|
||||||
|
#endif
|
||||||
|
#if (FMT_HAS_INCLUDE(<fcntl.h>) || defined(__APPLE__) || \
|
||||||
|
defined(__linux__)) && \
|
||||||
|
(!defined(WINAPI_FAMILY) || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP))
|
||||||
|
# include <fcntl.h> // for O_RDONLY
|
||||||
|
# define FMT_USE_FCNTL 1
|
||||||
|
#else
|
||||||
|
# define FMT_USE_FCNTL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FMT_POSIX
|
||||||
|
# if defined(_WIN32) && !defined(__MINGW32__)
|
||||||
|
// Fix warnings about deprecated symbols.
|
||||||
|
# define FMT_POSIX(call) _##call
|
||||||
|
# else
|
||||||
|
# define FMT_POSIX(call) call
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Calls to system functions are wrapped in FMT_SYSTEM for testability.
|
||||||
|
#ifdef FMT_SYSTEM
|
||||||
|
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
|
||||||
|
#else
|
||||||
|
# define FMT_SYSTEM(call) ::call
|
||||||
|
# ifdef _WIN32
|
||||||
|
// Fix warnings about deprecated symbols.
|
||||||
|
# define FMT_POSIX_CALL(call) ::_##call
|
||||||
|
# else
|
||||||
|
# define FMT_POSIX_CALL(call) ::call
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Retries the expression while it evaluates to error_result and errno
|
||||||
|
// equals to EINTR.
|
||||||
|
#ifndef _WIN32
|
||||||
|
# define FMT_RETRY_VAL(result, expression, error_result) \
|
||||||
|
do { \
|
||||||
|
(result) = (expression); \
|
||||||
|
} while ((result) == (error_result) && errno == EINTR)
|
||||||
|
#else
|
||||||
|
# define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
FMT_MODULE_EXPORT_BEGIN
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
A reference to a null-terminated string. It can be constructed from a C
|
||||||
|
string or ``std::string``.
|
||||||
|
|
||||||
|
You can use one of the following type aliases for common character types:
|
||||||
|
|
||||||
|
+---------------+-----------------------------+
|
||||||
|
| Type | Definition |
|
||||||
|
+===============+=============================+
|
||||||
|
| cstring_view | basic_cstring_view<char> |
|
||||||
|
+---------------+-----------------------------+
|
||||||
|
| wcstring_view | basic_cstring_view<wchar_t> |
|
||||||
|
+---------------+-----------------------------+
|
||||||
|
|
||||||
|
This class is most useful as a parameter type to allow passing
|
||||||
|
different types of strings to a function, for example::
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
std::string format(cstring_view format_str, const Args & ... args);
|
||||||
|
|
||||||
|
format("{}", 42);
|
||||||
|
format(std::string("{}"), 42);
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename Char> class basic_cstring_view {
|
||||||
|
private:
|
||||||
|
const Char* data_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/** Constructs a string reference object from a C string. */
|
||||||
|
basic_cstring_view(const Char* s) : data_(s) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Constructs a string reference from an ``std::string`` object.
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
basic_cstring_view(const std::basic_string<Char>& s) : data_(s.c_str()) {}
|
||||||
|
|
||||||
|
/** Returns the pointer to a C string. */
|
||||||
|
const Char* c_str() const { return data_; }
|
||||||
|
};
|
||||||
|
|
||||||
|
using cstring_view = basic_cstring_view<char>;
|
||||||
|
using wcstring_view = basic_cstring_view<wchar_t>;
|
||||||
|
|
||||||
|
template <typename Char> struct formatter<std::error_code, Char> {
|
||||||
|
template <typename ParseContext>
|
||||||
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const
|
||||||
|
-> decltype(ctx.out()) {
|
||||||
|
auto out = ctx.out();
|
||||||
|
out = detail::write_bytes(out, ec.category().name(),
|
||||||
|
basic_format_specs<Char>());
|
||||||
|
out = detail::write<Char>(out, Char(':'));
|
||||||
|
out = detail::write<Char>(out, ec.value());
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
FMT_API const std::error_category& system_category() FMT_NOEXCEPT;
|
||||||
|
|
||||||
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
// A converter from UTF-16 to UTF-8.
|
||||||
|
// It is only provided for Windows since other systems support UTF-8 natively.
|
||||||
|
class utf16_to_utf8 {
|
||||||
|
private:
|
||||||
|
memory_buffer buffer_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
utf16_to_utf8() {}
|
||||||
|
FMT_API explicit utf16_to_utf8(basic_string_view<wchar_t> s);
|
||||||
|
operator string_view() const { return string_view(&buffer_[0], size()); }
|
||||||
|
size_t size() const { return buffer_.size() - 1; }
|
||||||
|
const char* c_str() const { return &buffer_[0]; }
|
||||||
|
std::string str() const { return std::string(&buffer_[0], size()); }
|
||||||
|
|
||||||
|
// Performs conversion returning a system error code instead of
|
||||||
|
// throwing exception on conversion error. This method may still throw
|
||||||
|
// in case of memory allocation error.
|
||||||
|
FMT_API int convert(basic_string_view<wchar_t> s);
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_API void format_windows_error(buffer<char>& out, int error_code,
|
||||||
|
const char* message) FMT_NOEXCEPT;
|
||||||
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
FMT_API std::system_error vwindows_error(int error_code, string_view format_str,
|
||||||
|
format_args args);
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Constructs a :class:`std::system_error` object with the description
|
||||||
|
of the form
|
||||||
|
|
||||||
|
.. parsed-literal::
|
||||||
|
*<message>*: *<system-message>*
|
||||||
|
|
||||||
|
where *<message>* is the formatted message and *<system-message>* is the
|
||||||
|
system message corresponding to the error code.
|
||||||
|
*error_code* is a Windows error code as given by ``GetLastError``.
|
||||||
|
If *error_code* is not a valid error code such as -1, the system message
|
||||||
|
will look like "error -1".
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
// This throws a system_error with the description
|
||||||
|
// cannot open file 'madeup': The system cannot find the file specified.
|
||||||
|
// or similar (system message may vary).
|
||||||
|
const char *filename = "madeup";
|
||||||
|
LPOFSTRUCT of = LPOFSTRUCT();
|
||||||
|
HFILE file = OpenFile(filename, &of, OF_READ);
|
||||||
|
if (file == HFILE_ERROR) {
|
||||||
|
throw fmt::windows_error(GetLastError(),
|
||||||
|
"cannot open file '{}'", filename);
|
||||||
|
}
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename... Args>
|
||||||
|
std::system_error windows_error(int error_code, string_view message,
|
||||||
|
const Args&... args) {
|
||||||
|
return vwindows_error(error_code, message, fmt::make_format_args(args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reports a Windows error without throwing an exception.
|
||||||
|
// Can be used to report errors from destructors.
|
||||||
|
FMT_API void report_windows_error(int error_code,
|
||||||
|
const char* message) FMT_NOEXCEPT;
|
||||||
|
#else
|
||||||
|
inline const std::error_category& system_category() FMT_NOEXCEPT {
|
||||||
|
return std::system_category();
|
||||||
|
}
|
||||||
|
#endif // _WIN32
|
||||||
|
|
||||||
|
// std::system is not available on some platforms such as iOS (#2248).
|
||||||
|
#ifdef __OSX__
|
||||||
|
template <typename S, typename... Args, typename Char = char_t<S>>
|
||||||
|
void say(const S& format_str, Args&&... args) {
|
||||||
|
std::system(format("say \"{}\"", format(format_str, args...)).c_str());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// A buffered file.
|
||||||
|
class buffered_file {
|
||||||
|
private:
|
||||||
|
FILE* file_;
|
||||||
|
|
||||||
|
friend class file;
|
||||||
|
|
||||||
|
explicit buffered_file(FILE* f) : file_(f) {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
buffered_file(const buffered_file&) = delete;
|
||||||
|
void operator=(const buffered_file&) = delete;
|
||||||
|
|
||||||
|
// Constructs a buffered_file object which doesn't represent any file.
|
||||||
|
buffered_file() FMT_NOEXCEPT : file_(nullptr) {}
|
||||||
|
|
||||||
|
// Destroys the object closing the file it represents if any.
|
||||||
|
FMT_API ~buffered_file() FMT_NOEXCEPT;
|
||||||
|
|
||||||
|
public:
|
||||||
|
buffered_file(buffered_file&& other) FMT_NOEXCEPT : file_(other.file_) {
|
||||||
|
other.file_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffered_file& operator=(buffered_file&& other) {
|
||||||
|
close();
|
||||||
|
file_ = other.file_;
|
||||||
|
other.file_ = nullptr;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Opens a file.
|
||||||
|
FMT_API buffered_file(cstring_view filename, cstring_view mode);
|
||||||
|
|
||||||
|
// Closes the file.
|
||||||
|
FMT_API void close();
|
||||||
|
|
||||||
|
// Returns the pointer to a FILE object representing this file.
|
||||||
|
FILE* get() const FMT_NOEXCEPT { return file_; }
|
||||||
|
|
||||||
|
// We place parentheses around fileno to workaround a bug in some versions
|
||||||
|
// of MinGW that define fileno as a macro.
|
||||||
|
FMT_API int(fileno)() const;
|
||||||
|
|
||||||
|
void vprint(string_view format_str, format_args args) {
|
||||||
|
fmt::vprint(file_, format_str, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
inline void print(string_view format_str, const Args&... args) {
|
||||||
|
vprint(format_str, fmt::make_format_args(args...));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#if FMT_USE_FCNTL
|
||||||
|
// A file. Closed file is represented by a file object with descriptor -1.
|
||||||
|
// Methods that are not declared with FMT_NOEXCEPT may throw
|
||||||
|
// fmt::system_error in case of failure. Note that some errors such as
|
||||||
|
// closing the file multiple times will cause a crash on Windows rather
|
||||||
|
// than an exception. You can get standard behavior by overriding the
|
||||||
|
// invalid parameter handler with _set_invalid_parameter_handler.
|
||||||
|
class file {
|
||||||
|
private:
|
||||||
|
int fd_; // File descriptor.
|
||||||
|
|
||||||
|
// Constructs a file object with a given descriptor.
|
||||||
|
explicit file(int fd) : fd_(fd) {}
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Possible values for the oflag argument to the constructor.
|
||||||
|
enum {
|
||||||
|
RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
|
||||||
|
WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
|
||||||
|
RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing.
|
||||||
|
CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist.
|
||||||
|
APPEND = FMT_POSIX(O_APPEND), // Open in append mode.
|
||||||
|
TRUNC = FMT_POSIX(O_TRUNC) // Truncate the content of the file.
|
||||||
|
};
|
||||||
|
|
||||||
|
// Constructs a file object which doesn't represent any file.
|
||||||
|
file() FMT_NOEXCEPT : fd_(-1) {}
|
||||||
|
|
||||||
|
// Opens a file and constructs a file object representing this file.
|
||||||
|
FMT_API file(cstring_view path, int oflag);
|
||||||
|
|
||||||
|
public:
|
||||||
|
file(const file&) = delete;
|
||||||
|
void operator=(const file&) = delete;
|
||||||
|
|
||||||
|
file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; }
|
||||||
|
|
||||||
|
// Move assignment is not noexcept because close may throw.
|
||||||
|
file& operator=(file&& other) {
|
||||||
|
close();
|
||||||
|
fd_ = other.fd_;
|
||||||
|
other.fd_ = -1;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroys the object closing the file it represents if any.
|
||||||
|
FMT_API ~file() FMT_NOEXCEPT;
|
||||||
|
|
||||||
|
// Returns the file descriptor.
|
||||||
|
int descriptor() const FMT_NOEXCEPT { return fd_; }
|
||||||
|
|
||||||
|
// Closes the file.
|
||||||
|
FMT_API void close();
|
||||||
|
|
||||||
|
// Returns the file size. The size has signed type for consistency with
|
||||||
|
// stat::st_size.
|
||||||
|
FMT_API long long size() const;
|
||||||
|
|
||||||
|
// Attempts to read count bytes from the file into the specified buffer.
|
||||||
|
FMT_API size_t read(void* buffer, size_t count);
|
||||||
|
|
||||||
|
// Attempts to write count bytes from the specified buffer to the file.
|
||||||
|
FMT_API size_t write(const void* buffer, size_t count);
|
||||||
|
|
||||||
|
// Duplicates a file descriptor with the dup function and returns
|
||||||
|
// the duplicate as a file object.
|
||||||
|
FMT_API static file dup(int fd);
|
||||||
|
|
||||||
|
// Makes fd be the copy of this file descriptor, closing fd first if
|
||||||
|
// necessary.
|
||||||
|
FMT_API void dup2(int fd);
|
||||||
|
|
||||||
|
// Makes fd be the copy of this file descriptor, closing fd first if
|
||||||
|
// necessary.
|
||||||
|
FMT_API void dup2(int fd, std::error_code& ec) FMT_NOEXCEPT;
|
||||||
|
|
||||||
|
// Creates a pipe setting up read_end and write_end file objects for reading
|
||||||
|
// and writing respectively.
|
||||||
|
FMT_API static void pipe(file& read_end, file& write_end);
|
||||||
|
|
||||||
|
// Creates a buffered_file object associated with this file and detaches
|
||||||
|
// this file object from the file.
|
||||||
|
FMT_API buffered_file fdopen(const char* mode);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns the memory page size.
|
||||||
|
long getpagesize();
|
||||||
|
|
||||||
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
struct buffer_size {
|
||||||
|
buffer_size() = default;
|
||||||
|
size_t value = 0;
|
||||||
|
buffer_size operator=(size_t val) const {
|
||||||
|
auto bs = buffer_size();
|
||||||
|
bs.value = val;
|
||||||
|
return bs;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ostream_params {
|
||||||
|
int oflag = file::WRONLY | file::CREATE | file::TRUNC;
|
||||||
|
size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768;
|
||||||
|
|
||||||
|
ostream_params() {}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
ostream_params(T... params, int new_oflag) : ostream_params(params...) {
|
||||||
|
oflag = new_oflag;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
ostream_params(T... params, detail::buffer_size bs)
|
||||||
|
: ostream_params(params...) {
|
||||||
|
this->buffer_size = bs.value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
|
constexpr detail::buffer_size buffer_size;
|
||||||
|
|
||||||
|
/** A fast output stream which is not thread-safe. */
|
||||||
|
class FMT_API ostream final : private detail::buffer<char> {
|
||||||
|
private:
|
||||||
|
file file_;
|
||||||
|
|
||||||
|
void flush() {
|
||||||
|
if (size() == 0) return;
|
||||||
|
file_.write(data(), size());
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void grow(size_t) override;
|
||||||
|
|
||||||
|
ostream(cstring_view path, const detail::ostream_params& params)
|
||||||
|
: file_(path, params.oflag) {
|
||||||
|
set(new char[params.buffer_size], params.buffer_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
ostream(ostream&& other)
|
||||||
|
: detail::buffer<char>(other.data(), other.size(), other.capacity()),
|
||||||
|
file_(std::move(other.file_)) {
|
||||||
|
other.clear();
|
||||||
|
other.set(nullptr, 0);
|
||||||
|
}
|
||||||
|
~ostream() {
|
||||||
|
flush();
|
||||||
|
delete[] data();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
friend ostream output_file(cstring_view path, T... params);
|
||||||
|
|
||||||
|
void close() {
|
||||||
|
flush();
|
||||||
|
file_.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Formats ``args`` according to specifications in ``fmt`` and writes the
|
||||||
|
output to the file.
|
||||||
|
*/
|
||||||
|
template <typename... T> void print(format_string<T...> fmt, T&&... args) {
|
||||||
|
vformat_to(detail::buffer_appender<char>(*this), fmt,
|
||||||
|
fmt::make_format_args(args...));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Opens a file for writing. Supported parameters passed in *params*:
|
||||||
|
|
||||||
|
* ``<integer>``: Flags passed to `open
|
||||||
|
<https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html>`_
|
||||||
|
(``file::WRONLY | file::CREATE`` by default)
|
||||||
|
* ``buffer_size=<integer>``: Output buffer size
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
auto out = fmt::output_file("guide.txt");
|
||||||
|
out.print("Don't {}", "Panic");
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename... T>
|
||||||
|
inline ostream output_file(cstring_view path, T... params) {
|
||||||
|
return {path, detail::ostream_params(params...)};
|
||||||
|
}
|
||||||
|
#endif // FMT_USE_FCNTL
|
||||||
|
|
||||||
|
#ifdef FMT_LOCALE
|
||||||
|
// A "C" numeric locale.
|
||||||
|
class locale {
|
||||||
|
private:
|
||||||
|
# ifdef _WIN32
|
||||||
|
using locale_t = _locale_t;
|
||||||
|
|
||||||
|
static void freelocale(locale_t loc) { _free_locale(loc); }
|
||||||
|
|
||||||
|
static double strtod_l(const char* nptr, char** endptr, _locale_t loc) {
|
||||||
|
return _strtod_l(nptr, endptr, loc);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
|
||||||
|
locale_t locale_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using type = locale_t;
|
||||||
|
locale(const locale&) = delete;
|
||||||
|
void operator=(const locale&) = delete;
|
||||||
|
|
||||||
|
locale() {
|
||||||
|
# ifndef _WIN32
|
||||||
|
locale_ = FMT_SYSTEM(newlocale(LC_NUMERIC_MASK, "C", nullptr));
|
||||||
|
# else
|
||||||
|
locale_ = _create_locale(LC_NUMERIC, "C");
|
||||||
|
# endif
|
||||||
|
if (!locale_) FMT_THROW(system_error(errno, "cannot create locale"));
|
||||||
|
}
|
||||||
|
~locale() { freelocale(locale_); }
|
||||||
|
|
||||||
|
type get() const { return locale_; }
|
||||||
|
|
||||||
|
// Converts string to floating-point number and advances str past the end
|
||||||
|
// of the parsed input.
|
||||||
|
double strtod(const char*& str) const {
|
||||||
|
char* end = nullptr;
|
||||||
|
double result = strtod_l(str, &end, locale_);
|
||||||
|
str = end;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
using Locale FMT_DEPRECATED_ALIAS = locale;
|
||||||
|
#endif // FMT_LOCALE
|
||||||
|
FMT_MODULE_EXPORT_END
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // FMT_OS_H_
|
||||||
@@ -9,10 +9,15 @@
|
|||||||
#define FMT_OSTREAM_H_
|
#define FMT_OSTREAM_H_
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
#include "format.h"
|
#include "format.h"
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace internal {
|
|
||||||
|
template <typename Char> class basic_printf_parse_context;
|
||||||
|
template <typename OutputIt, typename Char> class basic_printf_context;
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
template <class Char> class formatbuf : public std::basic_streambuf<Char> {
|
template <class Char> class formatbuf : public std::basic_streambuf<Char> {
|
||||||
private:
|
private:
|
||||||
@@ -44,21 +49,35 @@ template <class Char> class formatbuf : public std::basic_streambuf<Char> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct converter {
|
||||||
|
template <typename T, FMT_ENABLE_IF(is_integral<T>::value)> converter(T);
|
||||||
|
};
|
||||||
|
|
||||||
template <typename Char> struct test_stream : std::basic_ostream<Char> {
|
template <typename Char> struct test_stream : std::basic_ostream<Char> {
|
||||||
private:
|
private:
|
||||||
struct null;
|
void_t<> operator<<(converter);
|
||||||
// Hide all operator<< from std::basic_ostream<Char>.
|
|
||||||
void operator<<(null);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Hide insertion operators for built-in types.
|
||||||
|
template <typename Char, typename Traits>
|
||||||
|
void_t<> operator<<(std::basic_ostream<Char, Traits>&, Char);
|
||||||
|
template <typename Char, typename Traits>
|
||||||
|
void_t<> operator<<(std::basic_ostream<Char, Traits>&, char);
|
||||||
|
template <typename Traits>
|
||||||
|
void_t<> operator<<(std::basic_ostream<char, Traits>&, char);
|
||||||
|
template <typename Traits>
|
||||||
|
void_t<> operator<<(std::basic_ostream<char, Traits>&, signed char);
|
||||||
|
template <typename Traits>
|
||||||
|
void_t<> operator<<(std::basic_ostream<char, Traits>&, unsigned char);
|
||||||
|
|
||||||
// Checks if T has a user-defined operator<< (e.g. not a member of
|
// Checks if T has a user-defined operator<< (e.g. not a member of
|
||||||
// std::ostream).
|
// std::ostream).
|
||||||
template <typename T, typename Char> class is_streamable {
|
template <typename T, typename Char> class is_streamable {
|
||||||
private:
|
private:
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static decltype((void)(std::declval<test_stream<Char>&>()
|
static bool_constant<!std::is_same<decltype(std::declval<test_stream<Char>&>()
|
||||||
<< std::declval<U>()),
|
<< std::declval<U>()),
|
||||||
std::true_type())
|
void_t<>>::value>
|
||||||
test(int);
|
test(int);
|
||||||
|
|
||||||
template <typename> static std::false_type test(...);
|
template <typename> static std::false_type test(...);
|
||||||
@@ -66,17 +85,18 @@ template <typename T, typename Char> class is_streamable {
|
|||||||
using result = decltype(test<T>(0));
|
using result = decltype(test<T>(0));
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
is_streamable() = default;
|
||||||
|
|
||||||
static const bool value = result::value;
|
static const bool value = result::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Write the content of buf to os.
|
// Write the content of buf to os.
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void write(std::basic_ostream<Char>& os, buffer<Char>& buf) {
|
void write_buffer(std::basic_ostream<Char>& os, buffer<Char>& buf) {
|
||||||
const Char* buf_data = buf.data();
|
const Char* buf_data = buf.data();
|
||||||
using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
|
using unsigned_streamsize = std::make_unsigned<std::streamsize>::type;
|
||||||
unsigned_streamsize size = buf.size();
|
unsigned_streamsize size = buf.size();
|
||||||
unsigned_streamsize max_size =
|
unsigned_streamsize max_size = to_unsigned(max_value<std::streamsize>());
|
||||||
to_unsigned((std::numeric_limits<std::streamsize>::max)());
|
|
||||||
do {
|
do {
|
||||||
unsigned_streamsize n = size <= max_size ? size : max_size;
|
unsigned_streamsize n = size <= max_size ? size : max_size;
|
||||||
os.write(buf_data, static_cast<std::streamsize>(n));
|
os.write(buf_data, static_cast<std::streamsize>(n));
|
||||||
@@ -86,34 +106,58 @@ void write(std::basic_ostream<Char>& os, buffer<Char>& buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
void format_value(buffer<Char>& buf, const T& value) {
|
void format_value(buffer<Char>& buf, const T& value,
|
||||||
|
locale_ref loc = locale_ref()) {
|
||||||
formatbuf<Char> format_buf(buf);
|
formatbuf<Char> format_buf(buf);
|
||||||
std::basic_ostream<Char> output(&format_buf);
|
std::basic_ostream<Char> output(&format_buf);
|
||||||
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR)
|
||||||
|
if (loc) output.imbue(loc.get<std::locale>());
|
||||||
|
#endif
|
||||||
output << value;
|
output << value;
|
||||||
buf.resize(buf.size());
|
output.exceptions(std::ios_base::failbit | std::ios_base::badbit);
|
||||||
|
buf.try_resize(buf.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formats an object of type T that has an overloaded ostream operator<<.
|
// Formats an object of type T that has an overloaded ostream operator<<.
|
||||||
template <typename T, typename Char>
|
template <typename T, typename Char>
|
||||||
struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
struct fallback_formatter<T, Char, enable_if_t<is_streamable<T, Char>::value>>
|
||||||
: formatter<basic_string_view<Char>, Char> {
|
: private formatter<basic_string_view<Char>, Char> {
|
||||||
template <typename Context>
|
FMT_CONSTEXPR auto parse(basic_format_parse_context<Char>& ctx)
|
||||||
auto format(const T& value, Context& ctx) -> decltype(ctx.out()) {
|
-> decltype(ctx.begin()) {
|
||||||
|
return formatter<basic_string_view<Char>, Char>::parse(ctx);
|
||||||
|
}
|
||||||
|
template <typename ParseCtx,
|
||||||
|
FMT_ENABLE_IF(std::is_same<
|
||||||
|
ParseCtx, basic_printf_parse_context<Char>>::value)>
|
||||||
|
auto parse(ParseCtx& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt>
|
||||||
|
auto format(const T& value, basic_format_context<OutputIt, Char>& ctx)
|
||||||
|
-> OutputIt {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
format_value(buffer, value);
|
format_value(buffer, value, ctx.locale());
|
||||||
basic_string_view<Char> str(buffer.data(), buffer.size());
|
basic_string_view<Char> str(buffer.data(), buffer.size());
|
||||||
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
|
||||||
}
|
}
|
||||||
|
template <typename OutputIt>
|
||||||
|
auto format(const T& value, basic_printf_context<OutputIt, Char>& ctx)
|
||||||
|
-> OutputIt {
|
||||||
|
basic_memory_buffer<Char> buffer;
|
||||||
|
format_value(buffer, value, ctx.locale());
|
||||||
|
return std::copy(buffer.begin(), buffer.end(), ctx.out());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
} // namespace internal
|
} // namespace detail
|
||||||
|
|
||||||
|
FMT_MODULE_EXPORT
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
|
void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
internal::vformat_to(buffer, format_str, args);
|
detail::vformat_to(buffer, format_str, args);
|
||||||
internal::write(os, buffer);
|
detail::write_buffer(os, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -125,11 +169,12 @@ void vprint(std::basic_ostream<Char>& os, basic_string_view<Char> format_str,
|
|||||||
fmt::print(cerr, "Don't {}!", "panic");
|
fmt::print(cerr, "Don't {}!", "panic");
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
|
FMT_MODULE_EXPORT
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... Args,
|
||||||
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
|
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
|
||||||
void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) {
|
void print(std::basic_ostream<Char>& os, const S& format_str, Args&&... args) {
|
||||||
vprint(os, to_string_view(format_str),
|
vprint(os, to_string_view(format_str),
|
||||||
{internal::make_args_checked<Args...>(format_str, args...)});
|
fmt::make_args_checked<Args...>(format_str, args...));
|
||||||
}
|
}
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
|||||||
@@ -1,311 +0,0 @@
|
|||||||
// A C++ interface to POSIX functions.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
|
||||||
// All rights reserved.
|
|
||||||
//
|
|
||||||
// For the license information refer to format.h.
|
|
||||||
|
|
||||||
#ifndef FMT_POSIX_H_
|
|
||||||
#define FMT_POSIX_H_
|
|
||||||
|
|
||||||
#if defined(__MINGW32__) || defined(__CYGWIN__)
|
|
||||||
// Workaround MinGW bug https://sourceforge.net/p/mingw/bugs/2024/.
|
|
||||||
# undef __STRICT_ANSI__
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <fcntl.h> // for O_RDONLY
|
|
||||||
#include <locale.h> // for locale_t
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h> // for strtod_l
|
|
||||||
|
|
||||||
#include <cstddef>
|
|
||||||
|
|
||||||
#if defined __APPLE__ || defined(__FreeBSD__)
|
|
||||||
# include <xlocale.h> // for LC_NUMERIC_MASK on OS X
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "format.h"
|
|
||||||
|
|
||||||
#ifndef FMT_POSIX
|
|
||||||
# if defined(_WIN32) && !defined(__MINGW32__)
|
|
||||||
// Fix warnings about deprecated symbols.
|
|
||||||
# define FMT_POSIX(call) _##call
|
|
||||||
# else
|
|
||||||
# define FMT_POSIX(call) call
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Calls to system functions are wrapped in FMT_SYSTEM for testability.
|
|
||||||
#ifdef FMT_SYSTEM
|
|
||||||
# define FMT_POSIX_CALL(call) FMT_SYSTEM(call)
|
|
||||||
#else
|
|
||||||
# define FMT_SYSTEM(call) call
|
|
||||||
# ifdef _WIN32
|
|
||||||
// Fix warnings about deprecated symbols.
|
|
||||||
# define FMT_POSIX_CALL(call) ::_##call
|
|
||||||
# else
|
|
||||||
# define FMT_POSIX_CALL(call) ::call
|
|
||||||
# endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Retries the expression while it evaluates to error_result and errno
|
|
||||||
// equals to EINTR.
|
|
||||||
#ifndef _WIN32
|
|
||||||
# define FMT_RETRY_VAL(result, expression, error_result) \
|
|
||||||
do { \
|
|
||||||
result = (expression); \
|
|
||||||
} while (result == error_result && errno == EINTR)
|
|
||||||
#else
|
|
||||||
# define FMT_RETRY_VAL(result, expression, error_result) result = (expression)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1)
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
|
||||||
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
A reference to a null-terminated string. It can be constructed from a C
|
|
||||||
string or ``std::string``.
|
|
||||||
|
|
||||||
You can use one of the following type aliases for common character types:
|
|
||||||
|
|
||||||
+---------------+-----------------------------+
|
|
||||||
| Type | Definition |
|
|
||||||
+===============+=============================+
|
|
||||||
| cstring_view | basic_cstring_view<char> |
|
|
||||||
+---------------+-----------------------------+
|
|
||||||
| wcstring_view | basic_cstring_view<wchar_t> |
|
|
||||||
+---------------+-----------------------------+
|
|
||||||
|
|
||||||
This class is most useful as a parameter type to allow passing
|
|
||||||
different types of strings to a function, for example::
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
std::string format(cstring_view format_str, const Args & ... args);
|
|
||||||
|
|
||||||
format("{}", 42);
|
|
||||||
format(std::string("{}"), 42);
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
template <typename Char> class basic_cstring_view {
|
|
||||||
private:
|
|
||||||
const Char* data_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/** Constructs a string reference object from a C string. */
|
|
||||||
basic_cstring_view(const Char* s) : data_(s) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
Constructs a string reference from an ``std::string`` object.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
basic_cstring_view(const std::basic_string<Char>& s) : data_(s.c_str()) {}
|
|
||||||
|
|
||||||
/** Returns the pointer to a C string. */
|
|
||||||
const Char* c_str() const { return data_; }
|
|
||||||
};
|
|
||||||
|
|
||||||
using cstring_view = basic_cstring_view<char>;
|
|
||||||
using wcstring_view = basic_cstring_view<wchar_t>;
|
|
||||||
|
|
||||||
// An error code.
|
|
||||||
class error_code {
|
|
||||||
private:
|
|
||||||
int value_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit error_code(int value = 0) FMT_NOEXCEPT : value_(value) {}
|
|
||||||
|
|
||||||
int get() const FMT_NOEXCEPT { return value_; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// A buffered file.
|
|
||||||
class buffered_file {
|
|
||||||
private:
|
|
||||||
FILE* file_;
|
|
||||||
|
|
||||||
friend class file;
|
|
||||||
|
|
||||||
explicit buffered_file(FILE* f) : file_(f) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructs a buffered_file object which doesn't represent any file.
|
|
||||||
buffered_file() FMT_NOEXCEPT : file_(nullptr) {}
|
|
||||||
|
|
||||||
// Destroys the object closing the file it represents if any.
|
|
||||||
FMT_API ~buffered_file() FMT_NOEXCEPT;
|
|
||||||
|
|
||||||
private:
|
|
||||||
buffered_file(const buffered_file&) = delete;
|
|
||||||
void operator=(const buffered_file&) = delete;
|
|
||||||
|
|
||||||
public:
|
|
||||||
buffered_file(buffered_file&& other) FMT_NOEXCEPT : file_(other.file_) {
|
|
||||||
other.file_ = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffered_file& operator=(buffered_file&& other) {
|
|
||||||
close();
|
|
||||||
file_ = other.file_;
|
|
||||||
other.file_ = nullptr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Opens a file.
|
|
||||||
FMT_API buffered_file(cstring_view filename, cstring_view mode);
|
|
||||||
|
|
||||||
// Closes the file.
|
|
||||||
FMT_API void close();
|
|
||||||
|
|
||||||
// Returns the pointer to a FILE object representing this file.
|
|
||||||
FILE* get() const FMT_NOEXCEPT { return file_; }
|
|
||||||
|
|
||||||
// We place parentheses around fileno to workaround a bug in some versions
|
|
||||||
// of MinGW that define fileno as a macro.
|
|
||||||
FMT_API int(fileno)() const;
|
|
||||||
|
|
||||||
void vprint(string_view format_str, format_args args) {
|
|
||||||
fmt::vprint(file_, format_str, args);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
inline void print(string_view format_str, const Args&... args) {
|
|
||||||
vprint(format_str, make_format_args(args...));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// A file. Closed file is represented by a file object with descriptor -1.
|
|
||||||
// Methods that are not declared with FMT_NOEXCEPT may throw
|
|
||||||
// fmt::system_error in case of failure. Note that some errors such as
|
|
||||||
// closing the file multiple times will cause a crash on Windows rather
|
|
||||||
// than an exception. You can get standard behavior by overriding the
|
|
||||||
// invalid parameter handler with _set_invalid_parameter_handler.
|
|
||||||
class file {
|
|
||||||
private:
|
|
||||||
int fd_; // File descriptor.
|
|
||||||
|
|
||||||
// Constructs a file object with a given descriptor.
|
|
||||||
explicit file(int fd) : fd_(fd) {}
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Possible values for the oflag argument to the constructor.
|
|
||||||
enum {
|
|
||||||
RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only.
|
|
||||||
WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only.
|
|
||||||
RDWR = FMT_POSIX(O_RDWR) // Open for reading and writing.
|
|
||||||
};
|
|
||||||
|
|
||||||
// Constructs a file object which doesn't represent any file.
|
|
||||||
file() FMT_NOEXCEPT : fd_(-1) {}
|
|
||||||
|
|
||||||
// Opens a file and constructs a file object representing this file.
|
|
||||||
FMT_API file(cstring_view path, int oflag);
|
|
||||||
|
|
||||||
private:
|
|
||||||
file(const file&) = delete;
|
|
||||||
void operator=(const file&) = delete;
|
|
||||||
|
|
||||||
public:
|
|
||||||
file(file&& other) FMT_NOEXCEPT : fd_(other.fd_) { other.fd_ = -1; }
|
|
||||||
|
|
||||||
file& operator=(file&& other) {
|
|
||||||
close();
|
|
||||||
fd_ = other.fd_;
|
|
||||||
other.fd_ = -1;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destroys the object closing the file it represents if any.
|
|
||||||
FMT_API ~file() FMT_NOEXCEPT;
|
|
||||||
|
|
||||||
// Returns the file descriptor.
|
|
||||||
int descriptor() const FMT_NOEXCEPT { return fd_; }
|
|
||||||
|
|
||||||
// Closes the file.
|
|
||||||
FMT_API void close();
|
|
||||||
|
|
||||||
// Returns the file size. The size has signed type for consistency with
|
|
||||||
// stat::st_size.
|
|
||||||
FMT_API long long size() const;
|
|
||||||
|
|
||||||
// Attempts to read count bytes from the file into the specified buffer.
|
|
||||||
FMT_API std::size_t read(void* buffer, std::size_t count);
|
|
||||||
|
|
||||||
// Attempts to write count bytes from the specified buffer to the file.
|
|
||||||
FMT_API std::size_t write(const void* buffer, std::size_t count);
|
|
||||||
|
|
||||||
// Duplicates a file descriptor with the dup function and returns
|
|
||||||
// the duplicate as a file object.
|
|
||||||
FMT_API static file dup(int fd);
|
|
||||||
|
|
||||||
// Makes fd be the copy of this file descriptor, closing fd first if
|
|
||||||
// necessary.
|
|
||||||
FMT_API void dup2(int fd);
|
|
||||||
|
|
||||||
// Makes fd be the copy of this file descriptor, closing fd first if
|
|
||||||
// necessary.
|
|
||||||
FMT_API void dup2(int fd, error_code& ec) FMT_NOEXCEPT;
|
|
||||||
|
|
||||||
// Creates a pipe setting up read_end and write_end file objects for reading
|
|
||||||
// and writing respectively.
|
|
||||||
FMT_API static void pipe(file& read_end, file& write_end);
|
|
||||||
|
|
||||||
// Creates a buffered_file object associated with this file and detaches
|
|
||||||
// this file object from the file.
|
|
||||||
FMT_API buffered_file fdopen(const char* mode);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Returns the memory page size.
|
|
||||||
long getpagesize();
|
|
||||||
|
|
||||||
#ifdef FMT_LOCALE
|
|
||||||
// A "C" numeric locale.
|
|
||||||
class Locale {
|
|
||||||
private:
|
|
||||||
# ifdef _WIN32
|
|
||||||
using locale_t = _locale_t;
|
|
||||||
|
|
||||||
enum { LC_NUMERIC_MASK = LC_NUMERIC };
|
|
||||||
|
|
||||||
static locale_t newlocale(int category_mask, const char* locale, locale_t) {
|
|
||||||
return _create_locale(category_mask, locale);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void freelocale(locale_t locale) { _free_locale(locale); }
|
|
||||||
|
|
||||||
static double strtod_l(const char* nptr, char** endptr, _locale_t locale) {
|
|
||||||
return _strtod_l(nptr, endptr, locale);
|
|
||||||
}
|
|
||||||
# endif
|
|
||||||
|
|
||||||
locale_t locale_;
|
|
||||||
|
|
||||||
Locale(const Locale&) = delete;
|
|
||||||
void operator=(const Locale&) = delete;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using type = locale_t;
|
|
||||||
|
|
||||||
Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", nullptr)) {
|
|
||||||
if (!locale_) FMT_THROW(system_error(errno, "cannot create locale"));
|
|
||||||
}
|
|
||||||
~Locale() { freelocale(locale_); }
|
|
||||||
|
|
||||||
type get() const { return locale_; }
|
|
||||||
|
|
||||||
// Converts string to floating-point number and advances str past the end
|
|
||||||
// of the parsed input.
|
|
||||||
double strtod(const char*& str) const {
|
|
||||||
char* end = nullptr;
|
|
||||||
double result = strtod_l(str, &end, locale_);
|
|
||||||
str = end;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
#endif // FMT_LOCALE
|
|
||||||
FMT_END_NAMESPACE
|
|
||||||
|
|
||||||
#endif // FMT_POSIX_H_
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// Formatting library for C++
|
// Formatting library for C++ - legacy printf implementation
|
||||||
//
|
//
|
||||||
// Copyright (c) 2012 - 2016, Victor Zverovich
|
// Copyright (c) 2012 - 2016, Victor Zverovich
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
@@ -8,23 +8,62 @@
|
|||||||
#ifndef FMT_PRINTF_H_
|
#ifndef FMT_PRINTF_H_
|
||||||
#define FMT_PRINTF_H_
|
#define FMT_PRINTF_H_
|
||||||
|
|
||||||
#include <algorithm> // std::fill_n
|
#include <algorithm> // std::max
|
||||||
#include <limits> // std::numeric_limits
|
#include <limits> // std::numeric_limits
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
#include "ostream.h"
|
#include "format.h"
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace internal {
|
FMT_MODULE_EXPORT_BEGIN
|
||||||
|
|
||||||
// A helper function to suppress bogus "conditional expression is constant"
|
template <typename T> struct printf_formatter { printf_formatter() = delete; };
|
||||||
// warnings.
|
|
||||||
template <typename T> inline T const_check(T value) { return value; }
|
template <typename Char>
|
||||||
|
class basic_printf_parse_context : public basic_format_parse_context<Char> {
|
||||||
|
using basic_format_parse_context<Char>::basic_format_parse_context;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename OutputIt, typename Char> class basic_printf_context {
|
||||||
|
private:
|
||||||
|
OutputIt out_;
|
||||||
|
basic_format_args<basic_printf_context> args_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
using char_type = Char;
|
||||||
|
using format_arg = basic_format_arg<basic_printf_context>;
|
||||||
|
using parse_context_type = basic_printf_parse_context<Char>;
|
||||||
|
template <typename T> using formatter_type = printf_formatter<T>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Constructs a ``printf_context`` object. References to the arguments are
|
||||||
|
stored in the context object so make sure they have appropriate lifetimes.
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
basic_printf_context(OutputIt out,
|
||||||
|
basic_format_args<basic_printf_context> args)
|
||||||
|
: out_(out), args_(args) {}
|
||||||
|
|
||||||
|
OutputIt out() { return out_; }
|
||||||
|
void advance_to(OutputIt it) { out_ = it; }
|
||||||
|
|
||||||
|
detail::locale_ref locale() { return {}; }
|
||||||
|
|
||||||
|
format_arg arg(int id) const { return args_.get(id); }
|
||||||
|
|
||||||
|
FMT_CONSTEXPR void on_error(const char* message) {
|
||||||
|
detail::error_handler().on_error(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_BEGIN_DETAIL_NAMESPACE
|
||||||
|
|
||||||
// Checks if a value fits in int - used to avoid warnings about comparing
|
// Checks if a value fits in int - used to avoid warnings about comparing
|
||||||
// signed and unsigned integers.
|
// signed and unsigned integers.
|
||||||
template <bool IsSigned> struct int_checker {
|
template <bool IsSigned> struct int_checker {
|
||||||
template <typename T> static bool fits_in_int(T value) {
|
template <typename T> static bool fits_in_int(T value) {
|
||||||
unsigned max = std::numeric_limits<int>::max();
|
unsigned max = max_value<int>();
|
||||||
return value <= max;
|
return value <= max;
|
||||||
}
|
}
|
||||||
static bool fits_in_int(bool) { return true; }
|
static bool fits_in_int(bool) { return true; }
|
||||||
@@ -32,8 +71,8 @@ template <bool IsSigned> struct int_checker {
|
|||||||
|
|
||||||
template <> struct int_checker<true> {
|
template <> struct int_checker<true> {
|
||||||
template <typename T> static bool fits_in_int(T value) {
|
template <typename T> static bool fits_in_int(T value) {
|
||||||
return value >= std::numeric_limits<int>::min() &&
|
return value >= (std::numeric_limits<int>::min)() &&
|
||||||
value <= std::numeric_limits<int>::max();
|
value <= max_value<int>();
|
||||||
}
|
}
|
||||||
static bool fits_in_int(int) { return true; }
|
static bool fits_in_int(int) { return true; }
|
||||||
};
|
};
|
||||||
@@ -94,11 +133,11 @@ template <typename T, typename Context> class arg_converter {
|
|||||||
if (const_check(sizeof(target_type) <= sizeof(int))) {
|
if (const_check(sizeof(target_type) <= sizeof(int))) {
|
||||||
// Extra casts are used to silence warnings.
|
// Extra casts are used to silence warnings.
|
||||||
if (is_signed) {
|
if (is_signed) {
|
||||||
arg_ = internal::make_arg<Context>(
|
arg_ = detail::make_arg<Context>(
|
||||||
static_cast<int>(static_cast<target_type>(value)));
|
static_cast<int>(static_cast<target_type>(value)));
|
||||||
} else {
|
} else {
|
||||||
using unsigned_type = typename make_unsigned_or_bool<target_type>::type;
|
using unsigned_type = typename make_unsigned_or_bool<target_type>::type;
|
||||||
arg_ = internal::make_arg<Context>(
|
arg_ = detail::make_arg<Context>(
|
||||||
static_cast<unsigned>(static_cast<unsigned_type>(value)));
|
static_cast<unsigned>(static_cast<unsigned_type>(value)));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -106,9 +145,9 @@ template <typename T, typename Context> class arg_converter {
|
|||||||
// glibc's printf doesn't sign extend arguments of smaller types:
|
// glibc's printf doesn't sign extend arguments of smaller types:
|
||||||
// std::printf("%lld", -42); // prints "4294967254"
|
// std::printf("%lld", -42); // prints "4294967254"
|
||||||
// but we don't have to do the same because it's a UB.
|
// but we don't have to do the same because it's a UB.
|
||||||
arg_ = internal::make_arg<Context>(static_cast<long long>(value));
|
arg_ = detail::make_arg<Context>(static_cast<long long>(value));
|
||||||
} else {
|
} else {
|
||||||
arg_ = internal::make_arg<Context>(
|
arg_ = detail::make_arg<Context>(
|
||||||
static_cast<typename make_unsigned_or_bool<U>::type>(value));
|
static_cast<typename make_unsigned_or_bool<U>::type>(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,7 +176,7 @@ template <typename Context> class char_converter {
|
|||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
|
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
|
||||||
void operator()(T value) {
|
void operator()(T value) {
|
||||||
arg_ = internal::make_arg<Context>(
|
arg_ = detail::make_arg<Context>(
|
||||||
static_cast<typename Context::char_type>(value));
|
static_cast<typename Context::char_type>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +184,13 @@ template <typename Context> class char_converter {
|
|||||||
void operator()(T) {} // No conversion needed for non-integral types.
|
void operator()(T) {} // No conversion needed for non-integral types.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// An argument visitor that return a pointer to a C string if argument is a
|
||||||
|
// string or null otherwise.
|
||||||
|
template <typename Char> struct get_cstring {
|
||||||
|
template <typename T> const Char* operator()(T) { return nullptr; }
|
||||||
|
const Char* operator()(const Char* s) { return s; }
|
||||||
|
};
|
||||||
|
|
||||||
// Checks if an argument is a valid printf width specifier and sets
|
// Checks if an argument is a valid printf width specifier and sets
|
||||||
// left alignment if it is negative.
|
// left alignment if it is negative.
|
||||||
template <typename Char> class printf_width_handler {
|
template <typename Char> class printf_width_handler {
|
||||||
@@ -158,12 +204,12 @@ template <typename Char> class printf_width_handler {
|
|||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
|
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
|
||||||
unsigned operator()(T value) {
|
unsigned operator()(T value) {
|
||||||
auto width = static_cast<uint32_or_64_t<T>>(value);
|
auto width = static_cast<uint32_or_64_or_128_t<T>>(value);
|
||||||
if (internal::is_negative(value)) {
|
if (detail::is_negative(value)) {
|
||||||
specs_.align = align::left;
|
specs_.align = align::left;
|
||||||
width = 0 - width;
|
width = 0 - width;
|
||||||
}
|
}
|
||||||
unsigned int_max = std::numeric_limits<int>::max();
|
unsigned int_max = max_value<int>();
|
||||||
if (width > int_max) FMT_THROW(format_error("number is too big"));
|
if (width > int_max) FMT_THROW(format_error("number is too big"));
|
||||||
return static_cast<unsigned>(width);
|
return static_cast<unsigned>(width);
|
||||||
}
|
}
|
||||||
@@ -175,208 +221,86 @@ template <typename Char> class printf_width_handler {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char, typename Context>
|
// The ``printf`` argument formatter.
|
||||||
void printf(buffer<Char>& buf, basic_string_view<Char> format,
|
template <typename OutputIt, typename Char>
|
||||||
basic_format_args<Context> args) {
|
class printf_arg_formatter : public arg_formatter<Char> {
|
||||||
Context(std::back_inserter(buf), format, args).format();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename OutputIt, typename Char, typename Context>
|
|
||||||
internal::truncating_iterator<OutputIt> printf(
|
|
||||||
internal::truncating_iterator<OutputIt> it, basic_string_view<Char> format,
|
|
||||||
basic_format_args<Context> args) {
|
|
||||||
return Context(it, format, args).format();
|
|
||||||
}
|
|
||||||
} // namespace internal
|
|
||||||
|
|
||||||
using internal::printf; // For printing into memory_buffer.
|
|
||||||
|
|
||||||
template <typename Range> class printf_arg_formatter;
|
|
||||||
|
|
||||||
template <typename OutputIt, typename Char> class basic_printf_context;
|
|
||||||
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
The ``printf`` argument formatter.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
template <typename Range>
|
|
||||||
class printf_arg_formatter : public internal::arg_formatter_base<Range> {
|
|
||||||
public:
|
|
||||||
using iterator = typename Range::iterator;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using char_type = typename Range::value_type;
|
using base = arg_formatter<Char>;
|
||||||
using base = internal::arg_formatter_base<Range>;
|
using context_type = basic_printf_context<OutputIt, Char>;
|
||||||
using context_type = basic_printf_context<iterator, char_type>;
|
using format_specs = basic_format_specs<Char>;
|
||||||
|
|
||||||
context_type& context_;
|
context_type& context_;
|
||||||
|
|
||||||
void write_null_pointer(char) {
|
OutputIt write_null_pointer(bool is_string = false) {
|
||||||
this->specs()->type = 0;
|
auto s = this->specs;
|
||||||
this->write("(nil)");
|
s.type = 0;
|
||||||
}
|
return write_bytes(this->out, is_string ? "(null)" : "(nil)", s);
|
||||||
|
|
||||||
void write_null_pointer(wchar_t) {
|
|
||||||
this->specs()->type = 0;
|
|
||||||
this->write(L"(nil)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using format_specs = typename base::format_specs;
|
printf_arg_formatter(OutputIt iter, format_specs& s, context_type& ctx)
|
||||||
|
: base{iter, s, locale_ref()}, context_(ctx) {}
|
||||||
|
|
||||||
/**
|
OutputIt operator()(monostate value) { return base::operator()(value); }
|
||||||
\rst
|
|
||||||
Constructs an argument formatter object.
|
|
||||||
*buffer* is a reference to the output buffer and *specs* contains format
|
|
||||||
specifier information for standard argument types.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
printf_arg_formatter(iterator iter, format_specs& specs, context_type& ctx)
|
|
||||||
: base(Range(iter), &specs, internal::locale_ref()), context_(ctx) {}
|
|
||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
|
template <typename T, FMT_ENABLE_IF(detail::is_integral<T>::value)>
|
||||||
iterator operator()(T value) {
|
OutputIt operator()(T value) {
|
||||||
// MSVC2013 fails to compile separate overloads for bool and char_type so
|
// MSVC2013 fails to compile separate overloads for bool and Char so use
|
||||||
// use std::is_same instead.
|
// std::is_same instead.
|
||||||
if (std::is_same<T, bool>::value) {
|
if (std::is_same<T, Char>::value) {
|
||||||
format_specs& fmt_specs = *this->specs();
|
format_specs fmt_specs = this->specs;
|
||||||
if (fmt_specs.type != 's') return base::operator()(value ? 1 : 0);
|
|
||||||
fmt_specs.type = 0;
|
|
||||||
this->write(value != 0);
|
|
||||||
} else if (std::is_same<T, char_type>::value) {
|
|
||||||
format_specs& fmt_specs = *this->specs();
|
|
||||||
if (fmt_specs.type && fmt_specs.type != 'c')
|
if (fmt_specs.type && fmt_specs.type != 'c')
|
||||||
return (*this)(static_cast<int>(value));
|
return (*this)(static_cast<int>(value));
|
||||||
fmt_specs.sign = sign::none;
|
fmt_specs.sign = sign::none;
|
||||||
fmt_specs.alt = false;
|
fmt_specs.alt = false;
|
||||||
fmt_specs.align = align::right;
|
fmt_specs.fill[0] = ' '; // Ignore '0' flag for char types.
|
||||||
return base::operator()(value);
|
// align::numeric needs to be overwritten here since the '0' flag is
|
||||||
} else {
|
// ignored for non-numeric types
|
||||||
return base::operator()(value);
|
if (fmt_specs.align == align::none || fmt_specs.align == align::numeric)
|
||||||
|
fmt_specs.align = align::right;
|
||||||
|
return write<Char>(this->out, static_cast<Char>(value), fmt_specs);
|
||||||
}
|
}
|
||||||
return this->out();
|
return base::operator()(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
|
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
|
||||||
iterator operator()(T value) {
|
OutputIt operator()(T value) {
|
||||||
return base::operator()(value);
|
return base::operator()(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Formats a null-terminated C string. */
|
/** Formats a null-terminated C string. */
|
||||||
iterator operator()(const char* value) {
|
OutputIt operator()(const char* value) {
|
||||||
if (value)
|
if (value) return base::operator()(value);
|
||||||
base::operator()(value);
|
return write_null_pointer(this->specs.type != 'p');
|
||||||
else if (this->specs()->type == 'p')
|
|
||||||
write_null_pointer(char_type());
|
|
||||||
else
|
|
||||||
this->write("(null)");
|
|
||||||
return this->out();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Formats a null-terminated wide C string. */
|
/** Formats a null-terminated wide C string. */
|
||||||
iterator operator()(const wchar_t* value) {
|
OutputIt operator()(const wchar_t* value) {
|
||||||
if (value)
|
if (value) return base::operator()(value);
|
||||||
base::operator()(value);
|
return write_null_pointer(this->specs.type != 'p');
|
||||||
else if (this->specs()->type == 'p')
|
|
||||||
write_null_pointer(char_type());
|
|
||||||
else
|
|
||||||
this->write(L"(null)");
|
|
||||||
return this->out();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator operator()(basic_string_view<char_type> value) {
|
OutputIt operator()(basic_string_view<Char> value) {
|
||||||
return base::operator()(value);
|
return base::operator()(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator operator()(monostate value) { return base::operator()(value); }
|
|
||||||
|
|
||||||
/** Formats a pointer. */
|
/** Formats a pointer. */
|
||||||
iterator operator()(const void* value) {
|
OutputIt operator()(const void* value) {
|
||||||
if (value) return base::operator()(value);
|
return value ? base::operator()(value) : write_null_pointer();
|
||||||
this->specs()->type = 0;
|
|
||||||
write_null_pointer(char_type());
|
|
||||||
return this->out();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Formats an argument of a custom (user-defined) type. */
|
/** Formats an argument of a custom (user-defined) type. */
|
||||||
iterator operator()(typename basic_format_arg<context_type>::handle handle) {
|
OutputIt operator()(typename basic_format_arg<context_type>::handle handle) {
|
||||||
handle.format(context_.parse_context(), context_);
|
auto parse_ctx =
|
||||||
return this->out();
|
basic_printf_parse_context<Char>(basic_string_view<Char>());
|
||||||
|
handle.format(parse_ctx, context_);
|
||||||
|
return this->out;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> struct printf_formatter {
|
template <typename Char>
|
||||||
template <typename ParseContext>
|
void parse_flags(basic_format_specs<Char>& specs, const Char*& it,
|
||||||
auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
const Char* end) {
|
||||||
return ctx.begin();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(const T& value, FormatContext& ctx) -> decltype(ctx.out()) {
|
|
||||||
internal::format_value(internal::get_container(ctx.out()), value);
|
|
||||||
return ctx.out();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** This template formats data and writes the output to a writer. */
|
|
||||||
template <typename OutputIt, typename Char> class basic_printf_context {
|
|
||||||
public:
|
|
||||||
/** The character type for the output. */
|
|
||||||
using char_type = Char;
|
|
||||||
using format_arg = basic_format_arg<basic_printf_context>;
|
|
||||||
template <typename T> using formatter_type = printf_formatter<T>;
|
|
||||||
|
|
||||||
private:
|
|
||||||
using format_specs = basic_format_specs<char_type>;
|
|
||||||
|
|
||||||
OutputIt out_;
|
|
||||||
basic_format_args<basic_printf_context> args_;
|
|
||||||
basic_parse_context<Char> parse_ctx_;
|
|
||||||
|
|
||||||
static void parse_flags(format_specs& specs, const Char*& it,
|
|
||||||
const Char* end);
|
|
||||||
|
|
||||||
// Returns the argument with specified index or, if arg_index is equal
|
|
||||||
// to the maximum unsigned value, the next argument.
|
|
||||||
format_arg get_arg(unsigned arg_index = std::numeric_limits<unsigned>::max());
|
|
||||||
|
|
||||||
// Parses argument index, flags and width and returns the argument index.
|
|
||||||
unsigned parse_header(const Char*& it, const Char* end, format_specs& specs);
|
|
||||||
|
|
||||||
public:
|
|
||||||
/**
|
|
||||||
\rst
|
|
||||||
Constructs a ``printf_context`` object. References to the arguments and
|
|
||||||
the writer are stored in the context object so make sure they have
|
|
||||||
appropriate lifetimes.
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
basic_printf_context(OutputIt out, basic_string_view<char_type> format_str,
|
|
||||||
basic_format_args<basic_printf_context> args)
|
|
||||||
: out_(out), args_(args), parse_ctx_(format_str) {}
|
|
||||||
|
|
||||||
OutputIt out() { return out_; }
|
|
||||||
void advance_to(OutputIt it) { out_ = it; }
|
|
||||||
|
|
||||||
format_arg arg(unsigned id) const { return args_.get(id); }
|
|
||||||
|
|
||||||
basic_parse_context<Char>& parse_context() { return parse_ctx_; }
|
|
||||||
|
|
||||||
FMT_CONSTEXPR void on_error(const char* message) {
|
|
||||||
parse_ctx_.on_error(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Formats stored arguments and writes the output to the range. */
|
|
||||||
template <typename ArgFormatter =
|
|
||||||
printf_arg_formatter<internal::buffer_range<Char>>>
|
|
||||||
OutputIt format();
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename OutputIt, typename Char>
|
|
||||||
void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,
|
|
||||||
const Char*& it,
|
|
||||||
const Char* end) {
|
|
||||||
for (; it != end; ++it) {
|
for (; it != end; ++it) {
|
||||||
switch (*it) {
|
switch (*it) {
|
||||||
case '-':
|
case '-':
|
||||||
@@ -389,7 +313,9 @@ void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,
|
|||||||
specs.fill[0] = '0';
|
specs.fill[0] = '0';
|
||||||
break;
|
break;
|
||||||
case ' ':
|
case ' ':
|
||||||
specs.sign = sign::space;
|
if (specs.sign != sign::plus) {
|
||||||
|
specs.sign = sign::space;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
specs.alt = true;
|
specs.alt = true;
|
||||||
@@ -400,34 +326,24 @@ void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIt, typename Char>
|
template <typename Char, typename GetArg>
|
||||||
typename basic_printf_context<OutputIt, Char>::format_arg
|
int parse_header(const Char*& it, const Char* end,
|
||||||
basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
|
basic_format_specs<Char>& specs, GetArg get_arg) {
|
||||||
if (arg_index == std::numeric_limits<unsigned>::max())
|
int arg_index = -1;
|
||||||
arg_index = parse_ctx_.next_arg_id();
|
Char c = *it;
|
||||||
else
|
|
||||||
parse_ctx_.check_arg_id(--arg_index);
|
|
||||||
return internal::get_arg(*this, arg_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename OutputIt, typename Char>
|
|
||||||
unsigned basic_printf_context<OutputIt, Char>::parse_header(
|
|
||||||
const Char*& it, const Char* end, format_specs& specs) {
|
|
||||||
unsigned arg_index = std::numeric_limits<unsigned>::max();
|
|
||||||
char_type c = *it;
|
|
||||||
if (c >= '0' && c <= '9') {
|
if (c >= '0' && c <= '9') {
|
||||||
// Parse an argument index (if followed by '$') or a width possibly
|
// Parse an argument index (if followed by '$') or a width possibly
|
||||||
// preceded with '0' flag(s).
|
// preceded with '0' flag(s).
|
||||||
internal::error_handler eh;
|
int value = parse_nonnegative_int(it, end, -1);
|
||||||
unsigned value = parse_nonnegative_int(it, end, eh);
|
|
||||||
if (it != end && *it == '$') { // value is an argument index
|
if (it != end && *it == '$') { // value is an argument index
|
||||||
++it;
|
++it;
|
||||||
arg_index = value;
|
arg_index = value != -1 ? value : max_value<int>();
|
||||||
} else {
|
} else {
|
||||||
if (c == '0') specs.fill[0] = '0';
|
if (c == '0') specs.fill[0] = '0';
|
||||||
if (value != 0) {
|
if (value != 0) {
|
||||||
// Nonzero value means that we parsed width and don't need to
|
// Nonzero value means that we parsed width and don't need to
|
||||||
// parse it or flags again, so return now.
|
// parse it or flags again, so return now.
|
||||||
|
if (value == -1) FMT_THROW(format_error("number is too big"));
|
||||||
specs.width = value;
|
specs.width = value;
|
||||||
return arg_index;
|
return arg_index;
|
||||||
}
|
}
|
||||||
@@ -437,70 +353,104 @@ unsigned basic_printf_context<OutputIt, Char>::parse_header(
|
|||||||
// Parse width.
|
// Parse width.
|
||||||
if (it != end) {
|
if (it != end) {
|
||||||
if (*it >= '0' && *it <= '9') {
|
if (*it >= '0' && *it <= '9') {
|
||||||
internal::error_handler eh;
|
specs.width = parse_nonnegative_int(it, end, -1);
|
||||||
specs.width = parse_nonnegative_int(it, end, eh);
|
if (specs.width == -1) FMT_THROW(format_error("number is too big"));
|
||||||
} else if (*it == '*') {
|
} else if (*it == '*') {
|
||||||
++it;
|
++it;
|
||||||
specs.width = visit_format_arg(
|
specs.width = static_cast<int>(visit_format_arg(
|
||||||
internal::printf_width_handler<char_type>(specs), get_arg());
|
detail::printf_width_handler<Char>(specs), get_arg(-1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return arg_index;
|
return arg_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename OutputIt, typename Char>
|
template <typename Char, typename Context>
|
||||||
template <typename ArgFormatter>
|
void vprintf(buffer<Char>& buf, basic_string_view<Char> format,
|
||||||
OutputIt basic_printf_context<OutputIt, Char>::format() {
|
basic_format_args<Context> args) {
|
||||||
auto out = this->out();
|
using OutputIt = buffer_appender<Char>;
|
||||||
const Char* start = parse_ctx_.begin();
|
auto out = OutputIt(buf);
|
||||||
const Char* end = parse_ctx_.end();
|
auto context = basic_printf_context<OutputIt, Char>(out, args);
|
||||||
|
auto parse_ctx = basic_printf_parse_context<Char>(format);
|
||||||
|
|
||||||
|
// Returns the argument with specified index or, if arg_index is -1, the next
|
||||||
|
// argument.
|
||||||
|
auto get_arg = [&](int arg_index) {
|
||||||
|
if (arg_index < 0)
|
||||||
|
arg_index = parse_ctx.next_arg_id();
|
||||||
|
else
|
||||||
|
parse_ctx.check_arg_id(--arg_index);
|
||||||
|
return detail::get_arg(context, arg_index);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Char* start = parse_ctx.begin();
|
||||||
|
const Char* end = parse_ctx.end();
|
||||||
auto it = start;
|
auto it = start;
|
||||||
while (it != end) {
|
while (it != end) {
|
||||||
char_type c = *it++;
|
if (!detail::find<false, Char>(it, end, '%', it)) {
|
||||||
if (c != '%') continue;
|
it = end; // detail::find leaves it == nullptr if it doesn't find '%'
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Char c = *it++;
|
||||||
if (it != end && *it == c) {
|
if (it != end && *it == c) {
|
||||||
out = std::copy(start, it, out);
|
out = detail::write(
|
||||||
|
out, basic_string_view<Char>(start, detail::to_unsigned(it - start)));
|
||||||
start = ++it;
|
start = ++it;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
out = std::copy(start, it - 1, out);
|
out = detail::write(out, basic_string_view<Char>(
|
||||||
|
start, detail::to_unsigned(it - 1 - start)));
|
||||||
|
|
||||||
format_specs specs;
|
basic_format_specs<Char> specs;
|
||||||
specs.align = align::right;
|
specs.align = align::right;
|
||||||
|
|
||||||
// Parse argument index, flags and width.
|
// Parse argument index, flags and width.
|
||||||
unsigned arg_index = parse_header(it, end, specs);
|
int arg_index = parse_header(it, end, specs, get_arg);
|
||||||
|
if (arg_index == 0) parse_ctx.on_error("argument not found");
|
||||||
|
|
||||||
// Parse precision.
|
// Parse precision.
|
||||||
if (it != end && *it == '.') {
|
if (it != end && *it == '.') {
|
||||||
++it;
|
++it;
|
||||||
c = it != end ? *it : 0;
|
c = it != end ? *it : 0;
|
||||||
if ('0' <= c && c <= '9') {
|
if ('0' <= c && c <= '9') {
|
||||||
internal::error_handler eh;
|
specs.precision = parse_nonnegative_int(it, end, 0);
|
||||||
specs.precision = static_cast<int>(parse_nonnegative_int(it, end, eh));
|
|
||||||
} else if (c == '*') {
|
} else if (c == '*') {
|
||||||
++it;
|
++it;
|
||||||
specs.precision =
|
specs.precision = static_cast<int>(
|
||||||
visit_format_arg(internal::printf_precision_handler(), get_arg());
|
visit_format_arg(detail::printf_precision_handler(), get_arg(-1)));
|
||||||
} else {
|
} else {
|
||||||
specs.precision = 0;
|
specs.precision = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
format_arg arg = get_arg(arg_index);
|
auto arg = get_arg(arg_index);
|
||||||
if (specs.alt && visit_format_arg(internal::is_zero_int(), arg))
|
// For d, i, o, u, x, and X conversion specifiers, if a precision is
|
||||||
|
// specified, the '0' flag is ignored
|
||||||
|
if (specs.precision >= 0 && arg.is_integral())
|
||||||
|
specs.fill[0] =
|
||||||
|
' '; // Ignore '0' flag for non-numeric types or if '-' present.
|
||||||
|
if (specs.precision >= 0 && arg.type() == detail::type::cstring_type) {
|
||||||
|
auto str = visit_format_arg(detail::get_cstring<Char>(), arg);
|
||||||
|
auto str_end = str + specs.precision;
|
||||||
|
auto nul = std::find(str, str_end, Char());
|
||||||
|
arg = detail::make_arg<basic_printf_context<OutputIt, Char>>(
|
||||||
|
basic_string_view<Char>(
|
||||||
|
str, detail::to_unsigned(nul != str_end ? nul - str
|
||||||
|
: specs.precision)));
|
||||||
|
}
|
||||||
|
if (specs.alt && visit_format_arg(detail::is_zero_int(), arg))
|
||||||
specs.alt = false;
|
specs.alt = false;
|
||||||
if (specs.fill[0] == '0') {
|
if (specs.fill[0] == '0') {
|
||||||
if (arg.is_arithmetic())
|
if (arg.is_arithmetic() && specs.align != align::left)
|
||||||
specs.align = align::numeric;
|
specs.align = align::numeric;
|
||||||
else
|
else
|
||||||
specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types.
|
specs.fill[0] = ' '; // Ignore '0' flag for non-numeric types or if '-'
|
||||||
|
// flag is also present.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse length and convert the argument to the required type.
|
// Parse length and convert the argument to the required type.
|
||||||
c = it != end ? *it++ : 0;
|
c = it != end ? *it++ : 0;
|
||||||
char_type t = it != end ? *it : 0;
|
Char t = it != end ? *it : 0;
|
||||||
using internal::convert_arg;
|
using detail::convert_arg;
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
if (t == 'h') {
|
if (t == 'h') {
|
||||||
@@ -524,7 +474,7 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
|
|||||||
convert_arg<intmax_t>(arg, t);
|
convert_arg<intmax_t>(arg, t);
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
convert_arg<std::size_t>(arg, t);
|
convert_arg<size_t>(arg, t);
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
convert_arg<std::ptrdiff_t>(arg, t);
|
convert_arg<std::ptrdiff_t>(arg, t);
|
||||||
@@ -549,8 +499,9 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
|
|||||||
specs.type = 'd';
|
specs.type = 'd';
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
visit_format_arg(internal::char_converter<basic_printf_context>(arg),
|
visit_format_arg(
|
||||||
arg);
|
detail::char_converter<basic_printf_context<OutputIt, Char>>(arg),
|
||||||
|
arg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -558,15 +509,16 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
|
|||||||
start = it;
|
start = it;
|
||||||
|
|
||||||
// Format argument.
|
// Format argument.
|
||||||
visit_format_arg(ArgFormatter(out, specs, *this), arg);
|
out = visit_format_arg(
|
||||||
|
detail::printf_arg_formatter<OutputIt, Char>(out, specs, context), arg);
|
||||||
}
|
}
|
||||||
return std::copy(start, it, out);
|
detail::write(out, basic_string_view<Char>(start, to_unsigned(it - start)));
|
||||||
}
|
}
|
||||||
|
FMT_END_DETAIL_NAMESPACE
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
using basic_printf_context_t =
|
using basic_printf_context_t =
|
||||||
basic_printf_context<std::back_insert_iterator<internal::buffer<Char>>,
|
basic_printf_context<detail::buffer_appender<Char>, Char>;
|
||||||
Char>;
|
|
||||||
|
|
||||||
using printf_context = basic_printf_context_t<char>;
|
using printf_context = basic_printf_context_t<char>;
|
||||||
using wprintf_context = basic_printf_context_t<wchar_t>;
|
using wprintf_context = basic_printf_context_t<wchar_t>;
|
||||||
@@ -580,9 +532,9 @@ using wprintf_args = basic_format_args<wprintf_context>;
|
|||||||
arguments and can be implicitly converted to `~fmt::printf_args`.
|
arguments and can be implicitly converted to `~fmt::printf_args`.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... T>
|
||||||
inline format_arg_store<printf_context, Args...> make_printf_args(
|
inline auto make_printf_args(const T&... args)
|
||||||
const Args&... args) {
|
-> format_arg_store<printf_context, T...> {
|
||||||
return {args...};
|
return {args...};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -592,17 +544,19 @@ inline format_arg_store<printf_context, Args...> make_printf_args(
|
|||||||
arguments and can be implicitly converted to `~fmt::wprintf_args`.
|
arguments and can be implicitly converted to `~fmt::wprintf_args`.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... T>
|
||||||
inline format_arg_store<wprintf_context, Args...> make_wprintf_args(
|
inline auto make_wprintf_args(const T&... args)
|
||||||
const Args&... args) {
|
-> format_arg_store<wprintf_context, T...> {
|
||||||
return {args...};
|
return {args...};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline std::basic_string<Char> vsprintf(
|
inline auto vsprintf(
|
||||||
const S& format, basic_format_args<basic_printf_context_t<Char>> args) {
|
const S& fmt,
|
||||||
|
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
|
||||||
|
-> std::basic_string<Char> {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
printf(buffer, to_string_view(format), args);
|
vprintf(buffer, to_string_view(fmt), args);
|
||||||
return to_string(buffer);
|
return to_string(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -615,19 +569,21 @@ inline std::basic_string<Char> vsprintf(
|
|||||||
std::string message = fmt::sprintf("The answer is %d", 42);
|
std::string message = fmt::sprintf("The answer is %d", 42);
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... T,
|
||||||
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
|
typename Char = enable_if_t<detail::is_string<S>::value, char_t<S>>>
|
||||||
inline std::basic_string<Char> sprintf(const S& format, const Args&... args) {
|
inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> {
|
||||||
using context = basic_printf_context_t<Char>;
|
using context = basic_printf_context_t<Char>;
|
||||||
return vsprintf(to_string_view(format), {make_format_args<context>(args...)});
|
return vsprintf(to_string_view(fmt), fmt::make_format_args<context>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline int vfprintf(std::FILE* f, const S& format,
|
inline auto vfprintf(
|
||||||
basic_format_args<basic_printf_context_t<Char>> args) {
|
std::FILE* f, const S& fmt,
|
||||||
|
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
|
||||||
|
-> int {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
printf(buffer, to_string_view(format), args);
|
vprintf(buffer, to_string_view(fmt), args);
|
||||||
std::size_t size = buffer.size();
|
size_t size = buffer.size();
|
||||||
return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
|
return std::fwrite(buffer.data(), sizeof(Char), size, f) < size
|
||||||
? -1
|
? -1
|
||||||
: static_cast<int>(size);
|
: static_cast<int>(size);
|
||||||
@@ -642,18 +598,19 @@ inline int vfprintf(std::FILE* f, const S& format,
|
|||||||
fmt::fprintf(stderr, "Don't %s!", "panic");
|
fmt::fprintf(stderr, "Don't %s!", "panic");
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... T, typename Char = char_t<S>>
|
||||||
typename Char = enable_if_t<internal::is_string<S>::value, char_t<S>>>
|
inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int {
|
||||||
inline int fprintf(std::FILE* f, const S& format, const Args&... args) {
|
|
||||||
using context = basic_printf_context_t<Char>;
|
using context = basic_printf_context_t<Char>;
|
||||||
return vfprintf(f, to_string_view(format),
|
return vfprintf(f, to_string_view(fmt),
|
||||||
{make_format_args<context>(args...)});
|
fmt::make_format_args<context>(args...));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline int vprintf(const S& format,
|
inline auto vprintf(
|
||||||
basic_format_args<basic_printf_context_t<Char>> args) {
|
const S& fmt,
|
||||||
return vfprintf(stdout, to_string_view(format), args);
|
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
|
||||||
|
-> int {
|
||||||
|
return vfprintf(stdout, to_string_view(fmt), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -665,51 +622,31 @@ inline int vprintf(const S& format,
|
|||||||
fmt::printf("Elapsed time: %.2f seconds", 1.23);
|
fmt::printf("Elapsed time: %.2f seconds", 1.23);
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename S, typename... Args,
|
template <typename S, typename... T, FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
FMT_ENABLE_IF(internal::is_string<S>::value)>
|
inline auto printf(const S& fmt, const T&... args) -> int {
|
||||||
inline int printf(const S& format_str, const Args&... args) {
|
return vprintf(
|
||||||
using context = basic_printf_context_t<char_t<S>>;
|
to_string_view(fmt),
|
||||||
return vprintf(to_string_view(format_str),
|
fmt::make_format_args<basic_printf_context_t<char_t<S>>>(args...));
|
||||||
{make_format_args<context>(args...)});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename S, typename Char = char_t<S>>
|
template <typename S, typename Char = char_t<S>>
|
||||||
inline int vfprintf(std::basic_ostream<Char>& os, const S& format,
|
FMT_DEPRECATED auto vfprintf(
|
||||||
basic_format_args<basic_printf_context_t<Char>> args) {
|
std::basic_ostream<Char>& os, const S& fmt,
|
||||||
|
basic_format_args<basic_printf_context_t<type_identity_t<Char>>> args)
|
||||||
|
-> int {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
printf(buffer, to_string_view(format), args);
|
vprintf(buffer, to_string_view(fmt), args);
|
||||||
internal::write(os, buffer);
|
os.write(buffer.data(), static_cast<std::streamsize>(buffer.size()));
|
||||||
return static_cast<int>(buffer.size());
|
return static_cast<int>(buffer.size());
|
||||||
}
|
}
|
||||||
|
template <typename S, typename... T, typename Char = char_t<S>>
|
||||||
/** Formats arguments and writes the output to the range. */
|
FMT_DEPRECATED auto fprintf(std::basic_ostream<Char>& os, const S& fmt,
|
||||||
template <typename ArgFormatter, typename Char,
|
const T&... args) -> int {
|
||||||
typename Context =
|
return vfprintf(os, to_string_view(fmt),
|
||||||
basic_printf_context<typename ArgFormatter::iterator, Char>>
|
fmt::make_format_args<basic_printf_context_t<Char>>(args...));
|
||||||
typename ArgFormatter::iterator vprintf(internal::buffer<Char>& out,
|
|
||||||
basic_string_view<Char> format_str,
|
|
||||||
basic_format_args<Context> args) {
|
|
||||||
typename ArgFormatter::iterator iter(out);
|
|
||||||
Context(iter, format_str, args).template format<ArgFormatter>();
|
|
||||||
return iter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
FMT_MODULE_EXPORT_END
|
||||||
\rst
|
|
||||||
Prints formatted data to the stream *os*.
|
|
||||||
|
|
||||||
**Example**::
|
|
||||||
|
|
||||||
fmt::fprintf(cerr, "Don't %s!", "panic");
|
|
||||||
\endrst
|
|
||||||
*/
|
|
||||||
template <typename S, typename... Args, typename Char = char_t<S>>
|
|
||||||
inline int fprintf(std::basic_ostream<Char>& os, const S& format_str,
|
|
||||||
const Args&... args) {
|
|
||||||
using context = basic_printf_context_t<Char>;
|
|
||||||
return vfprintf(os, to_string_view(format_str),
|
|
||||||
{make_format_args<context>(args...)});
|
|
||||||
}
|
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // FMT_PRINTF_H_
|
#endif // FMT_PRINTF_H_
|
||||||
|
|||||||
@@ -12,47 +12,39 @@
|
|||||||
#ifndef FMT_RANGES_H_
|
#ifndef FMT_RANGES_H_
|
||||||
#define FMT_RANGES_H_
|
#define FMT_RANGES_H_
|
||||||
|
|
||||||
|
#include <initializer_list>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include "format.h"
|
|
||||||
|
|
||||||
// output only up to N items from the range.
|
#include "format.h"
|
||||||
#ifndef FMT_RANGE_OUTPUT_LENGTH_LIMIT
|
|
||||||
# define FMT_RANGE_OUTPUT_LENGTH_LIMIT 256
|
|
||||||
#endif
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template <typename Char> struct formatting_base {
|
template <typename Char, typename Enable = void> struct formatting_range {
|
||||||
|
#ifdef FMT_DEPRECATED_BRACED_RANGES
|
||||||
|
Char prefix = '{';
|
||||||
|
Char postfix = '}';
|
||||||
|
#else
|
||||||
|
Char prefix = '[';
|
||||||
|
Char postfix = ']';
|
||||||
|
#endif
|
||||||
|
|
||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
return ctx.begin();
|
return ctx.begin();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char, typename Enable = void>
|
template <typename Char, typename Enable = void> struct formatting_tuple {
|
||||||
struct formatting_range : formatting_base<Char> {
|
Char prefix = '(';
|
||||||
static FMT_CONSTEXPR_DECL const std::size_t range_length_limit =
|
Char postfix = ')';
|
||||||
FMT_RANGE_OUTPUT_LENGTH_LIMIT; // output only up to N items from the
|
|
||||||
// range.
|
template <typename ParseContext>
|
||||||
Char prefix;
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
Char delimiter;
|
return ctx.begin();
|
||||||
Char postfix;
|
}
|
||||||
formatting_range() : prefix('{'), delimiter(','), postfix('}') {}
|
|
||||||
static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
|
|
||||||
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char, typename Enable = void>
|
namespace detail {
|
||||||
struct formatting_tuple : formatting_base<Char> {
|
|
||||||
Char prefix;
|
|
||||||
Char delimiter;
|
|
||||||
Char postfix;
|
|
||||||
formatting_tuple() : prefix('('), delimiter(','), postfix(')') {}
|
|
||||||
static FMT_CONSTEXPR_DECL const bool add_delimiter_spaces = true;
|
|
||||||
static FMT_CONSTEXPR_DECL const bool add_prepostfix_space = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace internal {
|
|
||||||
|
|
||||||
template <typename RangeT, typename OutputIterator>
|
template <typename RangeT, typename OutputIterator>
|
||||||
OutputIterator copy(const RangeT& range, OutputIterator out) {
|
OutputIterator copy(const RangeT& range, OutputIterator out) {
|
||||||
@@ -73,8 +65,14 @@ OutputIterator copy(char ch, OutputIterator out) {
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename OutputIterator>
|
||||||
|
OutputIterator copy(wchar_t ch, OutputIterator out) {
|
||||||
|
*out++ = ch;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
/// Return true value if T has std::string interface, like std::string_view.
|
/// Return true value if T has std::string interface, like std::string_view.
|
||||||
template <typename T> class is_like_std_string {
|
template <typename T> class is_std_string_like {
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static auto check(U* p)
|
static auto check(U* p)
|
||||||
-> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
|
-> decltype((void)p->find('a'), p->length(), (void)p->data(), int());
|
||||||
@@ -86,28 +84,113 @@ template <typename T> class is_like_std_string {
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
struct is_like_std_string<fmt::basic_string_view<Char>> : std::true_type {};
|
struct is_std_string_like<fmt::basic_string_view<Char>> : std::true_type {};
|
||||||
|
|
||||||
template <typename... Ts> struct conditional_helper {};
|
template <typename... Ts> struct conditional_helper {};
|
||||||
|
|
||||||
template <typename T, typename _ = void> struct is_range_ : std::false_type {};
|
template <typename T, typename _ = void> struct is_range_ : std::false_type {};
|
||||||
|
|
||||||
#if !FMT_MSC_VER || FMT_MSC_VER > 1800
|
#if !FMT_MSC_VER || FMT_MSC_VER > 1800
|
||||||
|
|
||||||
|
# define FMT_DECLTYPE_RETURN(val) \
|
||||||
|
->decltype(val) { return val; } \
|
||||||
|
static_assert( \
|
||||||
|
true, "") // This makes it so that a semicolon is required after the
|
||||||
|
// macro, which helps clang-format handle the formatting.
|
||||||
|
|
||||||
|
// C array overload
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
auto range_begin(const T (&arr)[N]) -> const T* {
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
template <typename T, std::size_t N>
|
||||||
|
auto range_end(const T (&arr)[N]) -> const T* {
|
||||||
|
return arr + N;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Enable = void>
|
||||||
|
struct has_member_fn_begin_end_t : std::false_type {};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct is_range_<
|
struct has_member_fn_begin_end_t<T, void_t<decltype(std::declval<T>().begin()),
|
||||||
T, conditional_t<false,
|
decltype(std::declval<T>().end())>>
|
||||||
conditional_helper<decltype(std::declval<T>().begin()),
|
: std::true_type {};
|
||||||
decltype(std::declval<T>().end())>,
|
|
||||||
void>> : std::true_type {};
|
// Member function overload
|
||||||
|
template <typename T>
|
||||||
|
auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).begin());
|
||||||
|
template <typename T>
|
||||||
|
auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast<T&&>(rng).end());
|
||||||
|
|
||||||
|
// ADL overload. Only participates in overload resolution if member functions
|
||||||
|
// are not found.
|
||||||
|
template <typename T>
|
||||||
|
auto range_begin(T&& rng)
|
||||||
|
-> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
|
||||||
|
decltype(begin(static_cast<T&&>(rng)))> {
|
||||||
|
return begin(static_cast<T&&>(rng));
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
auto range_end(T&& rng) -> enable_if_t<!has_member_fn_begin_end_t<T&&>::value,
|
||||||
|
decltype(end(static_cast<T&&>(rng)))> {
|
||||||
|
return end(static_cast<T&&>(rng));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename Enable = void>
|
||||||
|
struct has_const_begin_end : std::false_type {};
|
||||||
|
template <typename T, typename Enable = void>
|
||||||
|
struct has_mutable_begin_end : std::false_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct has_const_begin_end<
|
||||||
|
T, void_t<decltype(detail::range_begin(
|
||||||
|
std::declval<const remove_cvref_t<T>&>())),
|
||||||
|
decltype(detail::range_begin(
|
||||||
|
std::declval<const remove_cvref_t<T>&>()))>>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct has_mutable_begin_end<
|
||||||
|
T, void_t<decltype(detail::range_begin(std::declval<T>())),
|
||||||
|
decltype(detail::range_begin(std::declval<T>())),
|
||||||
|
enable_if_t<std::is_copy_constructible<T>::value>>>
|
||||||
|
: std::true_type {};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_range_<T, void>
|
||||||
|
: std::integral_constant<bool, (has_const_begin_end<T>::value ||
|
||||||
|
has_mutable_begin_end<T>::value)> {};
|
||||||
|
|
||||||
|
template <typename T, typename Enable = void> struct range_to_view;
|
||||||
|
template <typename T>
|
||||||
|
struct range_to_view<T, enable_if_t<has_const_begin_end<T>::value>> {
|
||||||
|
struct view_t {
|
||||||
|
const T* m_range_ptr;
|
||||||
|
|
||||||
|
auto begin() const FMT_DECLTYPE_RETURN(detail::range_begin(*m_range_ptr));
|
||||||
|
auto end() const FMT_DECLTYPE_RETURN(detail::range_end(*m_range_ptr));
|
||||||
|
};
|
||||||
|
static auto view(const T& range) -> view_t { return {&range}; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct range_to_view<T, enable_if_t<!has_const_begin_end<T>::value &&
|
||||||
|
has_mutable_begin_end<T>::value>> {
|
||||||
|
struct view_t {
|
||||||
|
T m_range_copy;
|
||||||
|
|
||||||
|
auto begin() FMT_DECLTYPE_RETURN(detail::range_begin(m_range_copy));
|
||||||
|
auto end() FMT_DECLTYPE_RETURN(detail::range_end(m_range_copy));
|
||||||
|
};
|
||||||
|
static auto view(const T& range) -> view_t { return {range}; }
|
||||||
|
};
|
||||||
|
# undef FMT_DECLTYPE_RETURN
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// tuple_size and tuple_element check.
|
/// tuple_size and tuple_element check.
|
||||||
template <typename T> class is_tuple_like_ {
|
template <typename T> class is_tuple_like_ {
|
||||||
template <typename U>
|
template <typename U>
|
||||||
static auto check(U* p)
|
static auto check(U* p) -> decltype(std::tuple_size<U>::value, int());
|
||||||
-> decltype(std::tuple_size<U>::value,
|
|
||||||
(void)std::declval<typename std::tuple_element<0, U>::type>(),
|
|
||||||
int());
|
|
||||||
template <typename> static void check(...);
|
template <typename> static void check(...);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -119,26 +202,24 @@ template <typename T> class is_tuple_like_ {
|
|||||||
#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
|
#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VER >= 1900
|
||||||
template <typename T, T... N>
|
template <typename T, T... N>
|
||||||
using integer_sequence = std::integer_sequence<T, N...>;
|
using integer_sequence = std::integer_sequence<T, N...>;
|
||||||
template <std::size_t... N> using index_sequence = std::index_sequence<N...>;
|
template <size_t... N> using index_sequence = std::index_sequence<N...>;
|
||||||
template <std::size_t N>
|
template <size_t N> using make_index_sequence = std::make_index_sequence<N>;
|
||||||
using make_index_sequence = std::make_index_sequence<N>;
|
|
||||||
#else
|
#else
|
||||||
template <typename T, T... N> struct integer_sequence {
|
template <typename T, T... N> struct integer_sequence {
|
||||||
using value_type = T;
|
using value_type = T;
|
||||||
|
|
||||||
static FMT_CONSTEXPR std::size_t size() { return sizeof...(N); }
|
static FMT_CONSTEXPR size_t size() { return sizeof...(N); }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t... N>
|
template <size_t... N> using index_sequence = integer_sequence<size_t, N...>;
|
||||||
using index_sequence = integer_sequence<std::size_t, N...>;
|
|
||||||
|
|
||||||
template <typename T, std::size_t N, T... Ns>
|
template <typename T, size_t N, T... Ns>
|
||||||
struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
|
struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Ns...> {};
|
||||||
template <typename T, T... Ns>
|
template <typename T, T... Ns>
|
||||||
struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
|
struct make_integer_sequence<T, 0, Ns...> : integer_sequence<T, Ns...> {};
|
||||||
|
|
||||||
template <std::size_t N>
|
template <size_t N>
|
||||||
using make_index_sequence = make_integer_sequence<std::size_t, N>;
|
using make_index_sequence = make_integer_sequence<size_t, N>;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <class Tuple, class F, size_t... Is>
|
template <class Tuple, class F, size_t... Is>
|
||||||
@@ -160,37 +241,48 @@ template <class Tuple, class F> void for_each(Tuple&& tup, F&& f) {
|
|||||||
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
|
for_each(indexes, std::forward<Tuple>(tup), std::forward<F>(f));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Arg, FMT_ENABLE_IF(!is_like_std_string<
|
template <typename Range>
|
||||||
typename std::decay<Arg>::type>::value)>
|
using value_type =
|
||||||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) {
|
remove_cvref_t<decltype(*detail::range_begin(std::declval<Range>()))>;
|
||||||
return add_space ? " {}" : "{}";
|
|
||||||
|
template <typename OutputIt> OutputIt write_delimiter(OutputIt out) {
|
||||||
|
*out++ = ',';
|
||||||
|
*out++ = ' ';
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Arg, FMT_ENABLE_IF(is_like_std_string<
|
template <
|
||||||
typename std::decay<Arg>::type>::value)>
|
typename Char, typename OutputIt, typename Arg,
|
||||||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const Arg&) {
|
FMT_ENABLE_IF(is_std_string_like<typename std::decay<Arg>::type>::value)>
|
||||||
return add_space ? " \"{}\"" : "\"{}\"";
|
OutputIt write_range_entry(OutputIt out, const Arg& v) {
|
||||||
|
*out++ = '"';
|
||||||
|
out = write<Char>(out, v);
|
||||||
|
*out++ = '"';
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char*) {
|
template <typename Char, typename OutputIt, typename Arg,
|
||||||
return add_space ? " \"{}\"" : "\"{}\"";
|
FMT_ENABLE_IF(std::is_same<Arg, Char>::value)>
|
||||||
}
|
OutputIt write_range_entry(OutputIt out, const Arg v) {
|
||||||
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t*) {
|
*out++ = '\'';
|
||||||
return add_space ? L" \"{}\"" : L"\"{}\"";
|
*out++ = v;
|
||||||
|
*out++ = '\'';
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_CONSTEXPR const char* format_str_quoted(bool add_space, const char) {
|
template <
|
||||||
return add_space ? " '{}'" : "'{}'";
|
typename Char, typename OutputIt, typename Arg,
|
||||||
}
|
FMT_ENABLE_IF(!is_std_string_like<typename std::decay<Arg>::type>::value &&
|
||||||
FMT_CONSTEXPR const wchar_t* format_str_quoted(bool add_space, const wchar_t) {
|
!std::is_same<Arg, Char>::value)>
|
||||||
return add_space ? L" '{}'" : L"'{}'";
|
OutputIt write_range_entry(OutputIt out, const Arg& v) {
|
||||||
|
return write<Char>(out, v);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace detail
|
||||||
|
|
||||||
template <typename T> struct is_tuple_like {
|
template <typename T> struct is_tuple_like {
|
||||||
static FMT_CONSTEXPR_DECL const bool value =
|
static FMT_CONSTEXPR_DECL const bool value =
|
||||||
internal::is_tuple_like_<T>::value && !internal::is_range_<T>::value;
|
detail::is_tuple_like_<T>::value && !detail::is_range_<T>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TupleT, typename Char>
|
template <typename TupleT, typename Char>
|
||||||
@@ -199,23 +291,14 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
|
|||||||
// C++11 generic lambda for format()
|
// C++11 generic lambda for format()
|
||||||
template <typename FormatContext> struct format_each {
|
template <typename FormatContext> struct format_each {
|
||||||
template <typename T> void operator()(const T& v) {
|
template <typename T> void operator()(const T& v) {
|
||||||
if (i > 0) {
|
if (i > 0) out = detail::write_delimiter(out);
|
||||||
if (formatting.add_prepostfix_space) {
|
out = detail::write_range_entry<Char>(out, v);
|
||||||
*out++ = ' ';
|
|
||||||
}
|
|
||||||
out = internal::copy(formatting.delimiter, out);
|
|
||||||
}
|
|
||||||
out = format_to(out,
|
|
||||||
internal::format_str_quoted(
|
|
||||||
(formatting.add_delimiter_spaces && i > 0), v),
|
|
||||||
v);
|
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
formatting_tuple<Char>& formatting;
|
formatting_tuple<Char>& formatting;
|
||||||
std::size_t& i;
|
size_t& i;
|
||||||
typename std::add_lvalue_reference<decltype(
|
typename std::add_lvalue_reference<
|
||||||
std::declval<FormatContext>().out())>::type out;
|
decltype(std::declval<FormatContext>().out())>::type out;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -229,14 +312,11 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
|
|||||||
template <typename FormatContext = format_context>
|
template <typename FormatContext = format_context>
|
||||||
auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
|
auto format(const TupleT& values, FormatContext& ctx) -> decltype(ctx.out()) {
|
||||||
auto out = ctx.out();
|
auto out = ctx.out();
|
||||||
std::size_t i = 0;
|
size_t i = 0;
|
||||||
internal::copy(formatting.prefix, out);
|
|
||||||
|
|
||||||
internal::for_each(values, format_each<FormatContext>{formatting, i, out});
|
detail::copy(formatting.prefix, out);
|
||||||
if (formatting.add_prepostfix_space) {
|
detail::for_each(values, format_each<FormatContext>{formatting, i, out});
|
||||||
*out++ = ' ';
|
detail::copy(formatting.postfix, out);
|
||||||
}
|
|
||||||
internal::copy(formatting.postfix, out);
|
|
||||||
|
|
||||||
return ctx.out();
|
return ctx.out();
|
||||||
}
|
}
|
||||||
@@ -244,14 +324,22 @@ struct formatter<TupleT, Char, enable_if_t<fmt::is_tuple_like<TupleT>::value>> {
|
|||||||
|
|
||||||
template <typename T, typename Char> struct is_range {
|
template <typename T, typename Char> struct is_range {
|
||||||
static FMT_CONSTEXPR_DECL const bool value =
|
static FMT_CONSTEXPR_DECL const bool value =
|
||||||
internal::is_range_<T>::value &&
|
detail::is_range_<T>::value && !detail::is_std_string_like<T>::value &&
|
||||||
!internal::is_like_std_string<T>::value &&
|
!std::is_convertible<T, std::basic_string<Char>>::value &&
|
||||||
!std::is_convertible<T, std::basic_string<Char>>::value;
|
!std::is_constructible<detail::std_string_view<Char>, T>::value;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename RangeT, typename Char>
|
template <typename T, typename Char>
|
||||||
struct formatter<RangeT, Char,
|
struct formatter<
|
||||||
enable_if_t<fmt::is_range<RangeT, Char>::value>> {
|
T, Char,
|
||||||
|
enable_if_t<
|
||||||
|
fmt::is_range<T, Char>::value
|
||||||
|
// Workaround a bug in MSVC 2017 and earlier.
|
||||||
|
#if !FMT_MSC_VER || FMT_MSC_VER >= 1927
|
||||||
|
&& (has_formatter<detail::value_type<T>, format_context>::value ||
|
||||||
|
detail::has_fallback_formatter<detail::value_type<T>, Char>::value)
|
||||||
|
#endif
|
||||||
|
>> {
|
||||||
formatting_range<Char> formatting;
|
formatting_range<Char> formatting;
|
||||||
|
|
||||||
template <typename ParseContext>
|
template <typename ParseContext>
|
||||||
@@ -260,29 +348,121 @@ struct formatter<RangeT, Char,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
typename FormatContext::iterator format(const RangeT& values,
|
typename FormatContext::iterator format(const T& values, FormatContext& ctx) {
|
||||||
FormatContext& ctx) {
|
auto out = detail::copy(formatting.prefix, ctx.out());
|
||||||
auto out = internal::copy(formatting.prefix, ctx.out());
|
size_t i = 0;
|
||||||
std::size_t i = 0;
|
auto view = detail::range_to_view<T>::view(values);
|
||||||
for (auto it = values.begin(), end = values.end(); it != end; ++it) {
|
auto it = view.begin();
|
||||||
if (i > 0) {
|
auto end = view.end();
|
||||||
if (formatting.add_prepostfix_space) *out++ = ' ';
|
for (; it != end; ++it) {
|
||||||
out = internal::copy(formatting.delimiter, out);
|
if (i > 0) out = detail::write_delimiter(out);
|
||||||
}
|
out = detail::write_range_entry<Char>(out, *it);
|
||||||
out = format_to(out,
|
++i;
|
||||||
internal::format_str_quoted(
|
|
||||||
(formatting.add_delimiter_spaces && i > 0), *it),
|
|
||||||
*it);
|
|
||||||
if (++i > formatting.range_length_limit) {
|
|
||||||
out = format_to(out, " ... <other elements>");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (formatting.add_prepostfix_space) *out++ = ' ';
|
return detail::copy(formatting.postfix, out);
|
||||||
return internal::copy(formatting.postfix, out);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename Char, typename... T> struct tuple_join_view : detail::view {
|
||||||
|
const std::tuple<T...>& tuple;
|
||||||
|
basic_string_view<Char> sep;
|
||||||
|
|
||||||
|
tuple_join_view(const std::tuple<T...>& t, basic_string_view<Char> s)
|
||||||
|
: tuple(t), sep{s} {}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Char, typename... T>
|
||||||
|
using tuple_arg_join = tuple_join_view<Char, T...>;
|
||||||
|
|
||||||
|
template <typename Char, typename... T>
|
||||||
|
struct formatter<tuple_join_view<Char, T...>, Char> {
|
||||||
|
template <typename ParseContext>
|
||||||
|
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
|
||||||
|
return ctx.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx) ->
|
||||||
|
typename FormatContext::iterator {
|
||||||
|
return format(value, ctx, detail::make_index_sequence<sizeof...(T)>{});
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename FormatContext, size_t... N>
|
||||||
|
auto format(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
|
||||||
|
detail::index_sequence<N...>) ->
|
||||||
|
typename FormatContext::iterator {
|
||||||
|
using std::get;
|
||||||
|
return format_args(value, ctx, get<N>(value.tuple)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format_args(const tuple_join_view<Char, T...>&, FormatContext& ctx) ->
|
||||||
|
typename FormatContext::iterator {
|
||||||
|
// NOTE: for compilers that support C++17, this empty function instantiation
|
||||||
|
// can be replaced with a constexpr branch in the variadic overload.
|
||||||
|
return ctx.out();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename FormatContext, typename Arg, typename... Args>
|
||||||
|
auto format_args(const tuple_join_view<Char, T...>& value, FormatContext& ctx,
|
||||||
|
const Arg& arg, const Args&... args) ->
|
||||||
|
typename FormatContext::iterator {
|
||||||
|
using base = formatter<typename std::decay<Arg>::type, Char>;
|
||||||
|
auto out = base().format(arg, ctx);
|
||||||
|
if (sizeof...(Args) > 0) {
|
||||||
|
out = std::copy(value.sep.begin(), value.sep.end(), out);
|
||||||
|
ctx.advance_to(out);
|
||||||
|
return format_args(value, ctx, args...);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
FMT_MODULE_EXPORT_BEGIN
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Returns an object that formats `tuple` with elements separated by `sep`.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
std::tuple<int, char> t = {1, 'a'};
|
||||||
|
fmt::print("{}", fmt::join(t, ", "));
|
||||||
|
// Output: "1, a"
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename... T>
|
||||||
|
FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple, string_view sep)
|
||||||
|
-> tuple_join_view<char, T...> {
|
||||||
|
return {tuple, sep};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
FMT_CONSTEXPR auto join(const std::tuple<T...>& tuple,
|
||||||
|
basic_string_view<wchar_t> sep)
|
||||||
|
-> tuple_join_view<wchar_t, T...> {
|
||||||
|
return {tuple, sep};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
\rst
|
||||||
|
Returns an object that formats `initializer_list` with elements separated by
|
||||||
|
`sep`.
|
||||||
|
|
||||||
|
**Example**::
|
||||||
|
|
||||||
|
fmt::print("{}", fmt::join({1, 2, 3}, ", "));
|
||||||
|
// Output: "1, 2, 3"
|
||||||
|
\endrst
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
auto join(std::initializer_list<T> list, string_view sep)
|
||||||
|
-> join_view<const T*, const T*> {
|
||||||
|
return join(std::begin(list), std::end(list), sep);
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_MODULE_EXPORT_END
|
||||||
FMT_END_NAMESPACE
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
#endif // FMT_RANGES_H_
|
#endif // FMT_RANGES_H_
|
||||||
|
|||||||
@@ -1,293 +0,0 @@
|
|||||||
/*
|
|
||||||
* For conversion between std::chrono::durations without undefined
|
|
||||||
* behaviour or erroneous results.
|
|
||||||
* This is a stripped down version of duration_cast, for inclusion in fmt.
|
|
||||||
* See https://github.com/pauldreik/safe_duration_cast
|
|
||||||
*
|
|
||||||
* Copyright Paul Dreik 2019
|
|
||||||
*
|
|
||||||
* This file is licensed under the fmt license, see format.h
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <cmath>
|
|
||||||
#include <limits>
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include "format.h"
|
|
||||||
|
|
||||||
FMT_BEGIN_NAMESPACE
|
|
||||||
|
|
||||||
namespace safe_duration_cast {
|
|
||||||
|
|
||||||
template <typename To, typename From,
|
|
||||||
FMT_ENABLE_IF(!std::is_same<From, To>::value &&
|
|
||||||
std::numeric_limits<From>::is_signed ==
|
|
||||||
std::numeric_limits<To>::is_signed)>
|
|
||||||
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) {
|
|
||||||
ec = 0;
|
|
||||||
using F = std::numeric_limits<From>;
|
|
||||||
using T = std::numeric_limits<To>;
|
|
||||||
static_assert(F::is_integer, "From must be integral");
|
|
||||||
static_assert(T::is_integer, "To must be integral");
|
|
||||||
|
|
||||||
// A and B are both signed, or both unsigned.
|
|
||||||
if (F::digits <= T::digits) {
|
|
||||||
// From fits in To without any problem.
|
|
||||||
} else {
|
|
||||||
// From does not always fit in To, resort to a dynamic check.
|
|
||||||
if (from < T::min() || from > T::max()) {
|
|
||||||
// outside range.
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return static_cast<To>(from);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* converts From to To, without loss. If the dynamic value of from
|
|
||||||
* can't be converted to To without loss, ec is set.
|
|
||||||
*/
|
|
||||||
template <typename To, typename From,
|
|
||||||
FMT_ENABLE_IF(!std::is_same<From, To>::value &&
|
|
||||||
std::numeric_limits<From>::is_signed !=
|
|
||||||
std::numeric_limits<To>::is_signed)>
|
|
||||||
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) {
|
|
||||||
ec = 0;
|
|
||||||
using F = std::numeric_limits<From>;
|
|
||||||
using T = std::numeric_limits<To>;
|
|
||||||
static_assert(F::is_integer, "From must be integral");
|
|
||||||
static_assert(T::is_integer, "To must be integral");
|
|
||||||
|
|
||||||
if (F::is_signed && !T::is_signed) {
|
|
||||||
// From may be negative, not allowed!
|
|
||||||
if (from < 0) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// From is positive. Can it always fit in To?
|
|
||||||
if (F::digits <= T::digits) {
|
|
||||||
// yes, From always fits in To.
|
|
||||||
} else {
|
|
||||||
// from may not fit in To, we have to do a dynamic check
|
|
||||||
if (from > static_cast<From>(T::max())) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!F::is_signed && T::is_signed) {
|
|
||||||
// can from be held in To?
|
|
||||||
if (F::digits < T::digits) {
|
|
||||||
// yes, From always fits in To.
|
|
||||||
} else {
|
|
||||||
// from may not fit in To, we have to do a dynamic check
|
|
||||||
if (from > static_cast<From>(T::max())) {
|
|
||||||
// outside range.
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// reaching here means all is ok for lossless conversion.
|
|
||||||
return static_cast<To>(from);
|
|
||||||
|
|
||||||
} // function
|
|
||||||
|
|
||||||
template <typename To, typename From,
|
|
||||||
FMT_ENABLE_IF(std::is_same<From, To>::value)>
|
|
||||||
FMT_CONSTEXPR To lossless_integral_conversion(const From from, int& ec) {
|
|
||||||
ec = 0;
|
|
||||||
return from;
|
|
||||||
} // function
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
/**
|
|
||||||
* converts From to To if possible, otherwise ec is set.
|
|
||||||
*
|
|
||||||
* input | output
|
|
||||||
* ---------------------------------|---------------
|
|
||||||
* NaN | NaN
|
|
||||||
* Inf | Inf
|
|
||||||
* normal, fits in output | converted (possibly lossy)
|
|
||||||
* normal, does not fit in output | ec is set
|
|
||||||
* subnormal | best effort
|
|
||||||
* -Inf | -Inf
|
|
||||||
*/
|
|
||||||
// clang-format on
|
|
||||||
template <typename To, typename From,
|
|
||||||
FMT_ENABLE_IF(!std::is_same<From, To>::value)>
|
|
||||||
FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) {
|
|
||||||
ec = 0;
|
|
||||||
using T = std::numeric_limits<To>;
|
|
||||||
static_assert(std::is_floating_point<From>::value, "From must be floating");
|
|
||||||
static_assert(std::is_floating_point<To>::value, "To must be floating");
|
|
||||||
|
|
||||||
// catch the only happy case
|
|
||||||
if (std::isfinite(from)) {
|
|
||||||
if (from >= T::lowest() && from <= T::max()) {
|
|
||||||
return static_cast<To>(from);
|
|
||||||
}
|
|
||||||
// not within range.
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// nan and inf will be preserved
|
|
||||||
return static_cast<To>(from);
|
|
||||||
} // function
|
|
||||||
|
|
||||||
template <typename To, typename From,
|
|
||||||
FMT_ENABLE_IF(std::is_same<From, To>::value)>
|
|
||||||
FMT_CONSTEXPR To safe_float_conversion(const From from, int& ec) {
|
|
||||||
ec = 0;
|
|
||||||
static_assert(std::is_floating_point<From>::value, "From must be floating");
|
|
||||||
return from;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* safe duration cast between integral durations
|
|
||||||
*/
|
|
||||||
template <typename To, typename FromRep, typename FromPeriod,
|
|
||||||
FMT_ENABLE_IF(std::is_integral<FromRep>::value),
|
|
||||||
FMT_ENABLE_IF(std::is_integral<typename To::rep>::value)>
|
|
||||||
To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
|
|
||||||
int& ec) {
|
|
||||||
using From = std::chrono::duration<FromRep, FromPeriod>;
|
|
||||||
ec = 0;
|
|
||||||
// the basic idea is that we need to convert from count() in the from type
|
|
||||||
// to count() in the To type, by multiplying it with this:
|
|
||||||
using Factor = std::ratio_divide<typename From::period, typename To::period>;
|
|
||||||
|
|
||||||
static_assert(Factor::num > 0, "num must be positive");
|
|
||||||
static_assert(Factor::den > 0, "den must be positive");
|
|
||||||
|
|
||||||
// the conversion is like this: multiply from.count() with Factor::num
|
|
||||||
// /Factor::den and convert it to To::rep, all this without
|
|
||||||
// overflow/underflow. let's start by finding a suitable type that can hold
|
|
||||||
// both To, From and Factor::num
|
|
||||||
using IntermediateRep =
|
|
||||||
typename std::common_type<typename From::rep, typename To::rep,
|
|
||||||
decltype(Factor::num)>::type;
|
|
||||||
|
|
||||||
// safe conversion to IntermediateRep
|
|
||||||
IntermediateRep count =
|
|
||||||
lossless_integral_conversion<IntermediateRep>(from.count(), ec);
|
|
||||||
if (ec) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
// multiply with Factor::num without overflow or underflow
|
|
||||||
if (Factor::num != 1) {
|
|
||||||
constexpr auto max1 =
|
|
||||||
std::numeric_limits<IntermediateRep>::max() / Factor::num;
|
|
||||||
if (count > max1) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
constexpr auto min1 =
|
|
||||||
std::numeric_limits<IntermediateRep>::min() / Factor::num;
|
|
||||||
if (count < min1) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
count *= Factor::num;
|
|
||||||
}
|
|
||||||
|
|
||||||
// this can't go wrong, right? den>0 is checked earlier.
|
|
||||||
if (Factor::den != 1) {
|
|
||||||
count /= Factor::den;
|
|
||||||
}
|
|
||||||
// convert to the to type, safely
|
|
||||||
using ToRep = typename To::rep;
|
|
||||||
const ToRep tocount = lossless_integral_conversion<ToRep>(count, ec);
|
|
||||||
if (ec) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
return To{tocount};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* safe duration_cast between floating point durations
|
|
||||||
*/
|
|
||||||
template <typename To, typename FromRep, typename FromPeriod,
|
|
||||||
FMT_ENABLE_IF(std::is_floating_point<FromRep>::value),
|
|
||||||
FMT_ENABLE_IF(std::is_floating_point<typename To::rep>::value)>
|
|
||||||
To safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
|
|
||||||
int& ec) {
|
|
||||||
using From = std::chrono::duration<FromRep, FromPeriod>;
|
|
||||||
ec = 0;
|
|
||||||
if (std::isnan(from.count())) {
|
|
||||||
// nan in, gives nan out. easy.
|
|
||||||
return To{std::numeric_limits<typename To::rep>::quiet_NaN()};
|
|
||||||
}
|
|
||||||
// maybe we should also check if from is denormal, and decide what to do about
|
|
||||||
// it.
|
|
||||||
|
|
||||||
// +-inf should be preserved.
|
|
||||||
if (std::isinf(from.count())) {
|
|
||||||
return To{from.count()};
|
|
||||||
}
|
|
||||||
|
|
||||||
// the basic idea is that we need to convert from count() in the from type
|
|
||||||
// to count() in the To type, by multiplying it with this:
|
|
||||||
using Factor = std::ratio_divide<typename From::period, typename To::period>;
|
|
||||||
|
|
||||||
static_assert(Factor::num > 0, "num must be positive");
|
|
||||||
static_assert(Factor::den > 0, "den must be positive");
|
|
||||||
|
|
||||||
// the conversion is like this: multiply from.count() with Factor::num
|
|
||||||
// /Factor::den and convert it to To::rep, all this without
|
|
||||||
// overflow/underflow. let's start by finding a suitable type that can hold
|
|
||||||
// both To, From and Factor::num
|
|
||||||
using IntermediateRep =
|
|
||||||
typename std::common_type<typename From::rep, typename To::rep,
|
|
||||||
decltype(Factor::num)>::type;
|
|
||||||
|
|
||||||
// force conversion of From::rep -> IntermediateRep to be safe,
|
|
||||||
// even if it will never happen be narrowing in this context.
|
|
||||||
IntermediateRep count =
|
|
||||||
safe_float_conversion<IntermediateRep>(from.count(), ec);
|
|
||||||
if (ec) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
// multiply with Factor::num without overflow or underflow
|
|
||||||
if (Factor::num != 1) {
|
|
||||||
constexpr auto max1 = std::numeric_limits<IntermediateRep>::max() /
|
|
||||||
static_cast<IntermediateRep>(Factor::num);
|
|
||||||
if (count > max1) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
constexpr auto min1 = std::numeric_limits<IntermediateRep>::lowest() /
|
|
||||||
static_cast<IntermediateRep>(Factor::num);
|
|
||||||
if (count < min1) {
|
|
||||||
ec = 1;
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
count *= static_cast<IntermediateRep>(Factor::num);
|
|
||||||
}
|
|
||||||
|
|
||||||
// this can't go wrong, right? den>0 is checked earlier.
|
|
||||||
if (Factor::den != 1) {
|
|
||||||
using common_t = typename std::common_type<IntermediateRep, intmax_t>::type;
|
|
||||||
count /= static_cast<common_t>(Factor::den);
|
|
||||||
}
|
|
||||||
|
|
||||||
// convert to the to type, safely
|
|
||||||
using ToRep = typename To::rep;
|
|
||||||
|
|
||||||
const ToRep tocount = safe_float_conversion<ToRep>(count, ec);
|
|
||||||
if (ec) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
return To{tocount};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace safe_duration_cast
|
|
||||||
|
|
||||||
FMT_END_NAMESPACE
|
|
||||||
236
include/spdlog/fmt/bundled/xchar.h
Normal file
236
include/spdlog/fmt/bundled/xchar.h
Normal file
@@ -0,0 +1,236 @@
|
|||||||
|
// Formatting library for C++ - optional wchar_t and exotic character support
|
||||||
|
//
|
||||||
|
// Copyright (c) 2012 - present, Victor Zverovich
|
||||||
|
// All rights reserved.
|
||||||
|
//
|
||||||
|
// For the license information refer to format.h.
|
||||||
|
|
||||||
|
#ifndef FMT_WCHAR_H_
|
||||||
|
#define FMT_WCHAR_H_
|
||||||
|
|
||||||
|
#include <cwchar>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
|
#include "format.h"
|
||||||
|
|
||||||
|
FMT_BEGIN_NAMESPACE
|
||||||
|
namespace detail {
|
||||||
|
template <typename T>
|
||||||
|
using is_exotic_char = bool_constant<!std::is_same<T, char>::value>;
|
||||||
|
}
|
||||||
|
|
||||||
|
FMT_MODULE_EXPORT_BEGIN
|
||||||
|
|
||||||
|
using wstring_view = basic_string_view<wchar_t>;
|
||||||
|
using wformat_parse_context = basic_format_parse_context<wchar_t>;
|
||||||
|
using wformat_context = buffer_context<wchar_t>;
|
||||||
|
using wformat_args = basic_format_args<wformat_context>;
|
||||||
|
using wmemory_buffer = basic_memory_buffer<wchar_t>;
|
||||||
|
|
||||||
|
#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409
|
||||||
|
// Workaround broken conversion on older gcc.
|
||||||
|
template <typename... Args> using wformat_string = wstring_view;
|
||||||
|
#else
|
||||||
|
template <typename... Args>
|
||||||
|
using wformat_string = basic_format_string<wchar_t, type_identity_t<Args>...>;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <> struct is_char<wchar_t> : std::true_type {};
|
||||||
|
template <> struct is_char<detail::char8_type> : std::true_type {};
|
||||||
|
template <> struct is_char<char16_t> : std::true_type {};
|
||||||
|
template <> struct is_char<char32_t> : std::true_type {};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
constexpr format_arg_store<wformat_context, Args...> make_wformat_args(
|
||||||
|
const Args&... args) {
|
||||||
|
return {args...};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline namespace literals {
|
||||||
|
constexpr auto operator"" _format(const wchar_t* s, size_t n)
|
||||||
|
-> detail::udl_formatter<wchar_t> {
|
||||||
|
return {{s, n}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
|
||||||
|
constexpr detail::udl_arg<wchar_t> operator"" _a(const wchar_t* s, size_t) {
|
||||||
|
return {s};
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
} // namespace literals
|
||||||
|
|
||||||
|
template <typename It, typename Sentinel>
|
||||||
|
auto join(It begin, Sentinel end, wstring_view sep)
|
||||||
|
-> join_view<It, Sentinel, wchar_t> {
|
||||||
|
return {begin, end, sep};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Range>
|
||||||
|
auto join(Range&& range, wstring_view sep)
|
||||||
|
-> join_view<detail::iterator_t<Range>, detail::sentinel_t<Range>,
|
||||||
|
wchar_t> {
|
||||||
|
return join(std::begin(range), std::end(range), sep);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
auto join(std::initializer_list<T> list, wstring_view sep)
|
||||||
|
-> join_view<const T*, const T*, wchar_t> {
|
||||||
|
return join(std::begin(list), std::end(list), sep);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Char, FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
|
||||||
|
auto vformat(basic_string_view<Char> format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args)
|
||||||
|
-> std::basic_string<Char> {
|
||||||
|
basic_memory_buffer<Char> buffer;
|
||||||
|
detail::vformat_to(buffer, format_str, args);
|
||||||
|
return to_string(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass char_t as a default template parameter instead of using
|
||||||
|
// std::basic_string<char_t<S>> to reduce the symbol size.
|
||||||
|
template <typename S, typename... Args, typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(!std::is_same<Char, char>::value)>
|
||||||
|
auto format(const S& format_str, Args&&... args) -> std::basic_string<Char> {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
||||||
|
return vformat(to_string_view(format_str), vargs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Locale, typename S, typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto vformat(
|
||||||
|
const Locale& loc, const S& format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args)
|
||||||
|
-> std::basic_string<Char> {
|
||||||
|
return detail::vformat(loc, to_string_view(format_str), args);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Locale, typename S, typename... Args,
|
||||||
|
typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_locale<Locale>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto format(const Locale& loc, const S& format_str, Args&&... args)
|
||||||
|
-> std::basic_string<Char> {
|
||||||
|
return detail::vformat(loc, to_string_view(format_str),
|
||||||
|
fmt::make_args_checked<Args...>(format_str, args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt, typename S, typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
auto vformat_to(OutputIt out, const S& format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args)
|
||||||
|
-> OutputIt {
|
||||||
|
auto&& buf = detail::get_buffer<Char>(out);
|
||||||
|
detail::vformat_to(buf, to_string_view(format_str), args);
|
||||||
|
return detail::get_iterator(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
|
typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto format_to(OutputIt out, const S& fmt, Args&&... args) -> OutputIt {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
|
||||||
|
return vformat_to(out, to_string_view(fmt), vargs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S, typename... Args, typename Char, size_t SIZE,
|
||||||
|
typename Allocator, FMT_ENABLE_IF(detail::is_string<S>::value)>
|
||||||
|
FMT_DEPRECATED auto format_to(basic_memory_buffer<Char, SIZE, Allocator>& buf,
|
||||||
|
const S& format_str, Args&&... args) ->
|
||||||
|
typename buffer_context<Char>::iterator {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
||||||
|
detail::vformat_to(buf, to_string_view(format_str), vargs, {});
|
||||||
|
return detail::buffer_appender<Char>(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Locale, typename S, typename OutputIt, typename... Args,
|
||||||
|
typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_locale<Locale>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto vformat_to(
|
||||||
|
OutputIt out, const Locale& loc, const S& format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args) -> OutputIt {
|
||||||
|
auto&& buf = detail::get_buffer<Char>(out);
|
||||||
|
vformat_to(buf, to_string_view(format_str), args, detail::locale_ref(loc));
|
||||||
|
return detail::get_iterator(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename OutputIt, typename Locale, typename S, typename... Args,
|
||||||
|
typename Char = char_t<S>,
|
||||||
|
bool enable = detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_locale<Locale>::value&& detail::is_exotic_char<Char>::value>
|
||||||
|
inline auto format_to(OutputIt out, const Locale& loc, const S& format_str,
|
||||||
|
Args&&... args) ->
|
||||||
|
typename std::enable_if<enable, OutputIt>::type {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(format_str, args...);
|
||||||
|
return vformat_to(out, loc, to_string_view(format_str), vargs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt, typename Char, typename... Args,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto vformat_to_n(
|
||||||
|
OutputIt out, size_t n, basic_string_view<Char> format_str,
|
||||||
|
basic_format_args<buffer_context<type_identity_t<Char>>> args)
|
||||||
|
-> format_to_n_result<OutputIt> {
|
||||||
|
detail::iterator_buffer<OutputIt, Char, detail::fixed_buffer_traits> buf(out,
|
||||||
|
n);
|
||||||
|
detail::vformat_to(buf, format_str, args);
|
||||||
|
return {buf.out(), buf.count()};
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutputIt, typename S, typename... Args,
|
||||||
|
typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_output_iterator<OutputIt, Char>::value&&
|
||||||
|
detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto format_to_n(OutputIt out, size_t n, const S& fmt,
|
||||||
|
const Args&... args) -> format_to_n_result<OutputIt> {
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
|
||||||
|
return vformat_to_n(out, n, to_string_view(fmt), vargs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename S, typename... Args, typename Char = char_t<S>,
|
||||||
|
FMT_ENABLE_IF(detail::is_exotic_char<Char>::value)>
|
||||||
|
inline auto formatted_size(const S& fmt, Args&&... args) -> size_t {
|
||||||
|
detail::counting_buffer<Char> buf;
|
||||||
|
const auto& vargs = fmt::make_args_checked<Args...>(fmt, args...);
|
||||||
|
detail::vformat_to(buf, to_string_view(fmt), vargs);
|
||||||
|
return buf.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) {
|
||||||
|
wmemory_buffer buffer;
|
||||||
|
detail::vformat_to(buffer, fmt, args);
|
||||||
|
buffer.push_back(L'\0');
|
||||||
|
if (std::fputws(buffer.data(), f) == -1)
|
||||||
|
FMT_THROW(system_error(errno, FMT_STRING("cannot write to file")));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void vprint(wstring_view fmt, wformat_args args) {
|
||||||
|
vprint(stdout, fmt, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... T>
|
||||||
|
void print(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
|
||||||
|
return vprint(f, wstring_view(fmt), make_wformat_args(args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
|
||||||
|
return vprint(wstring_view(fmt), make_wformat_args(args...));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Converts *value* to ``std::wstring`` using the default format for type *T*.
|
||||||
|
*/
|
||||||
|
template <typename T> inline auto to_wstring(const T& value) -> std::wstring {
|
||||||
|
return format(FMT_STRING(L"{}"), value);
|
||||||
|
}
|
||||||
|
FMT_MODULE_EXPORT_END
|
||||||
|
FMT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // FMT_WCHAR_H_
|
||||||
20
include/spdlog/fmt/chrono.h
Normal file
20
include/spdlog/fmt/chrono.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Copyright(c) 2016 Gabi Melman.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
//
|
||||||
|
// include bundled or external copy of fmtlib's chrono support
|
||||||
|
//
|
||||||
|
|
||||||
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
|
# ifdef SPDLOG_HEADER_ONLY
|
||||||
|
# ifndef FMT_HEADER_ONLY
|
||||||
|
# define FMT_HEADER_ONLY
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# include <spdlog/fmt/bundled/chrono.h>
|
||||||
|
#else
|
||||||
|
# include <fmt/chrono.h>
|
||||||
|
#endif
|
||||||
20
include/spdlog/fmt/compile.h
Normal file
20
include/spdlog/fmt/compile.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Copyright(c) 2016 Gabi Melman.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
//
|
||||||
|
// include bundled or external copy of fmtlib's ostream support
|
||||||
|
//
|
||||||
|
|
||||||
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
|
# ifdef SPDLOG_HEADER_ONLY
|
||||||
|
# ifndef FMT_HEADER_ONLY
|
||||||
|
# define FMT_HEADER_ONLY
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# include <spdlog/fmt/bundled/compile.h>
|
||||||
|
#else
|
||||||
|
# include <fmt/compile.h>
|
||||||
|
#endif
|
||||||
@@ -11,17 +11,17 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#if !defined(SPDLOG_FMT_EXTERNAL)
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
# if !defined(SPDLOG_COMPILED_LIB) && !defined(FMT_HEADER_ONLY)
|
||||||
#ifndef FMT_HEADER_ONLY
|
# define FMT_HEADER_ONLY
|
||||||
#define FMT_HEADER_ONLY
|
# endif
|
||||||
#endif
|
# ifndef FMT_USE_WINDOWS_H
|
||||||
#endif
|
# define FMT_USE_WINDOWS_H 0
|
||||||
#ifndef FMT_USE_WINDOWS_H
|
# endif
|
||||||
#define FMT_USE_WINDOWS_H 0
|
// enable the 'n' flag in for backward compatibility with fmt 6.x
|
||||||
#endif
|
# define FMT_DEPRECATED_N_SPECIFIER
|
||||||
#include "bundled/core.h"
|
# include <spdlog/fmt/bundled/core.h>
|
||||||
#include "bundled/format.h"
|
# include <spdlog/fmt/bundled/format.h>
|
||||||
#else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib
|
#else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib
|
||||||
#include "fmt/core.h"
|
# include <fmt/core.h>
|
||||||
#include "fmt/format.h"
|
# include <fmt/format.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -7,12 +7,14 @@
|
|||||||
//
|
//
|
||||||
// include bundled or external copy of fmtlib's ostream support
|
// include bundled or external copy of fmtlib's ostream support
|
||||||
//
|
//
|
||||||
|
|
||||||
#if !defined(SPDLOG_FMT_EXTERNAL)
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
#ifndef FMT_HEADER_ONLY
|
# ifdef SPDLOG_HEADER_ONLY
|
||||||
#define FMT_HEADER_ONLY
|
# ifndef FMT_HEADER_ONLY
|
||||||
#endif
|
# define FMT_HEADER_ONLY
|
||||||
#include "bundled/ostream.h"
|
# endif
|
||||||
#include "fmt.h"
|
# endif
|
||||||
|
# include <spdlog/fmt/bundled/ostream.h>
|
||||||
#else
|
#else
|
||||||
#include <fmt/ostream.h>
|
# include <fmt/ostream.h>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
20
include/spdlog/fmt/xchar.h
Normal file
20
include/spdlog/fmt/xchar.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//
|
||||||
|
// Copyright(c) 2016 Gabi Melman.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
//
|
||||||
|
// include bundled or external copy of fmtlib's ostream support
|
||||||
|
//
|
||||||
|
|
||||||
|
#if !defined(SPDLOG_FMT_EXTERNAL)
|
||||||
|
# ifdef SPDLOG_HEADER_ONLY
|
||||||
|
# ifndef FMT_HEADER_ONLY
|
||||||
|
# define FMT_HEADER_ONLY
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
# include <spdlog/fmt/bundled/xchar.h>
|
||||||
|
#else
|
||||||
|
# include <fmt/xchar.h>
|
||||||
|
#endif
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "fmt/fmt.h"
|
#include <spdlog/fmt/fmt.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
|
|||||||
14
include/spdlog/fwd.h
Normal file
14
include/spdlog/fwd.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
class logger;
|
||||||
|
class formatter;
|
||||||
|
|
||||||
|
namespace sinks {
|
||||||
|
class sink;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace spdlog
|
||||||
@@ -4,12 +4,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/logger.h"
|
# include <spdlog/logger.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/sinks/sink.h"
|
#include <spdlog/sinks/sink.h>
|
||||||
#include "spdlog/details/backtracer.h"
|
#include <spdlog/details/backtracer.h>
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include <spdlog/pattern_formatter.h>
|
||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
@@ -64,11 +64,6 @@ SPDLOG_INLINE void swap(logger &a, logger &b)
|
|||||||
a.swap(b);
|
a.swap(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE bool logger::should_log(level::level_enum msg_level) const
|
|
||||||
{
|
|
||||||
return msg_level >= level_.load(std::memory_order_relaxed);
|
|
||||||
}
|
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
SPDLOG_INLINE void logger::set_level(level::level_enum log_level)
|
||||||
{
|
{
|
||||||
level_.store(log_level);
|
level_.store(log_level);
|
||||||
@@ -85,7 +80,7 @@ SPDLOG_INLINE const std::string &logger::name() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// set formatting for the sinks in this logger.
|
// set formatting for the sinks in this logger.
|
||||||
// each sink will get a seperate instance of the formatter object.
|
// each sink will get a separate instance of the formatter object.
|
||||||
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
||||||
{
|
{
|
||||||
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
for (auto it = sinks_.begin(); it != sinks_.end(); ++it)
|
||||||
@@ -94,6 +89,7 @@ SPDLOG_INLINE void logger::set_formatter(std::unique_ptr<formatter> f)
|
|||||||
{
|
{
|
||||||
// last element - we can be move it.
|
// last element - we can be move it.
|
||||||
(*it)->set_formatter(std::move(f));
|
(*it)->set_formatter(std::move(f));
|
||||||
|
break; // to prevent clang-tidy warning
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -155,7 +151,7 @@ SPDLOG_INLINE std::vector<sink_ptr> &logger::sinks()
|
|||||||
// error handler
|
// error handler
|
||||||
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
SPDLOG_INLINE void logger::set_error_handler(err_handler handler)
|
||||||
{
|
{
|
||||||
custom_err_handler_ = handler;
|
custom_err_handler_ = std::move(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new logger with same sinks and configuration.
|
// create new logger with same sinks and configuration.
|
||||||
@@ -167,6 +163,18 @@ SPDLOG_INLINE std::shared_ptr<logger> logger::clone(std::string logger_name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// protected methods
|
// protected methods
|
||||||
|
SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, bool log_enabled, bool traceback_enabled)
|
||||||
|
{
|
||||||
|
if (log_enabled)
|
||||||
|
{
|
||||||
|
sink_it_(log_msg);
|
||||||
|
}
|
||||||
|
if (traceback_enabled)
|
||||||
|
{
|
||||||
|
tracer_.push_back(log_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
|
SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
for (auto &sink : sinks_)
|
for (auto &sink : sinks_)
|
||||||
@@ -202,10 +210,10 @@ SPDLOG_INLINE void logger::flush_()
|
|||||||
SPDLOG_INLINE void logger::dump_backtrace_()
|
SPDLOG_INLINE void logger::dump_backtrace_()
|
||||||
{
|
{
|
||||||
using details::log_msg;
|
using details::log_msg;
|
||||||
if (tracer_)
|
if (tracer_.enabled())
|
||||||
{
|
{
|
||||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
|
sink_it_(log_msg{name(), level::info, "****************** Backtrace Start ******************"});
|
||||||
tracer_.foreach_pop([this](const details::log_msg &msg) { this->sink_it_(msg); });
|
tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); });
|
||||||
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
|
sink_it_(log_msg{name(), level::info, "****************** Backtrace End ********************"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -218,7 +226,6 @@ SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg)
|
|||||||
|
|
||||||
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (custom_err_handler_)
|
if (custom_err_handler_)
|
||||||
{
|
{
|
||||||
custom_err_handler_(msg);
|
custom_err_handler_(msg);
|
||||||
@@ -240,7 +247,11 @@ SPDLOG_INLINE void logger::err_handler_(const std::string &msg)
|
|||||||
auto tm_time = details::os::localtime(system_clock::to_time_t(now));
|
auto tm_time = details::os::localtime(system_clock::to_time_t(now));
|
||||||
char date_buf[64];
|
char date_buf[64];
|
||||||
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time);
|
||||||
fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
#if defined(USING_R) && defined(R_R_H) // if in R environment
|
||||||
|
REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
||||||
|
#else
|
||||||
|
std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] {%s}\n", err_counter, date_buf, name().c_str(), msg.c_str());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// Thread safe logger (except for set_pattern(..), set_formatter(..) and
|
// Thread safe logger (except for set_error_handler())
|
||||||
// set_error_handler())
|
|
||||||
// Has name, log level, vector of std::shared sink pointers and formatter
|
// Has name, log level, vector of std::shared sink pointers and formatter
|
||||||
// Upon each log write the logger:
|
// Upon each log write the logger:
|
||||||
// 1. Checks if its log level is enough to log the message and if yes:
|
// 1. Checks if its log level is enough to log the message and if yes:
|
||||||
@@ -15,32 +14,37 @@
|
|||||||
// The use of private formatter per sink provides the opportunity to cache some
|
// The use of private formatter per sink provides the opportunity to cache some
|
||||||
// formatted data, and support for different format per sink.
|
// formatted data, and support for different format per sink.
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
#include "spdlog/details/backtracer.h"
|
#include <spdlog/details/backtracer.h>
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
#include "spdlog/details/os.h"
|
# ifndef _WIN32
|
||||||
|
# error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
|
||||||
|
# endif
|
||||||
|
# include <spdlog/details/os.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_EXCEPTIONS
|
#ifndef SPDLOG_NO_EXCEPTIONS
|
||||||
#define SPDLOG_LOGGER_CATCH() \
|
# define SPDLOG_LOGGER_CATCH() \
|
||||||
catch (const std::exception &ex) \
|
catch (const std::exception &ex) \
|
||||||
{ \
|
{ \
|
||||||
err_handler_(ex.what()); \
|
err_handler_(ex.what()); \
|
||||||
} \
|
} \
|
||||||
catch (...) \
|
catch (...) \
|
||||||
{ \
|
{ \
|
||||||
err_handler_("Unknown exception in logger"); \
|
err_handler_("Rethrowing unknown exception in logger"); \
|
||||||
}
|
throw; \
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_LOGGER_CATCH()
|
# define SPDLOG_LOGGER_CATCH()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
|
|
||||||
class logger
|
class SPDLOG_API logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// Empty logger
|
// Empty logger
|
||||||
@@ -71,74 +75,18 @@ public:
|
|||||||
logger(const logger &other);
|
logger(const logger &other);
|
||||||
logger(logger &&other) SPDLOG_NOEXCEPT;
|
logger(logger &&other) SPDLOG_NOEXCEPT;
|
||||||
logger &operator=(logger other) SPDLOG_NOEXCEPT;
|
logger &operator=(logger other) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
|
void swap(spdlog::logger &other) SPDLOG_NOEXCEPT;
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void log(source_loc loc, level::level_enum lvl, string_view_t fmt, const Args &... args)
|
void log(source_loc loc, level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
{
|
{
|
||||||
auto level_enabled = should_log(lvl);
|
log_(loc, lvl, fmt, std::forward<Args>(args)...);
|
||||||
if (!level_enabled && !tracer_)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
SPDLOG_TRY
|
|
||||||
{
|
|
||||||
memory_buf_t buf;
|
|
||||||
fmt::format_to(buf, fmt, args...);
|
|
||||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
|
||||||
if (level_enabled)
|
|
||||||
{
|
|
||||||
sink_it_(log_msg);
|
|
||||||
}
|
|
||||||
if (tracer_)
|
|
||||||
{
|
|
||||||
tracer_.push_back(log_msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SPDLOG_LOGGER_CATCH()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
void log(level::level_enum lvl, string_view_t fmt, const Args &... args)
|
void log(level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
{
|
{
|
||||||
log(source_loc{}, lvl, fmt, args...);
|
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void trace(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::trace, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void debug(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::debug, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void info(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::info, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void warn(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::warn, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void error(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::err, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void critical(string_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::critical, fmt, args...);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -148,27 +96,43 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// T can be statically converted to string_view
|
// T can be statically converted to string_view
|
||||||
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, T>::type * = nullptr>
|
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::string_view_t>::value, int>::type = 0>
|
||||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||||
{
|
{
|
||||||
auto level_enabled = should_log(lvl);
|
log(loc, lvl, string_view_t{msg});
|
||||||
if (!level_enabled && !tracer_)
|
}
|
||||||
|
|
||||||
|
// T cannot be statically converted to format string (including string_view)
|
||||||
|
template<class T, typename std::enable_if<!is_convertible_to_any_format_string<const T &>::value, int>::type = 0>
|
||||||
|
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
||||||
|
{
|
||||||
|
log(loc, lvl, "{}", msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void log(log_clock::time_point log_time, source_loc loc, level::level_enum lvl, string_view_t msg)
|
||||||
|
{
|
||||||
|
bool log_enabled = should_log(lvl);
|
||||||
|
bool traceback_enabled = tracer_.enabled();
|
||||||
|
if (!log_enabled && !traceback_enabled)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SPDLOG_TRY
|
|
||||||
|
details::log_msg log_msg(log_time, loc, name_, lvl, msg);
|
||||||
|
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
void log(source_loc loc, level::level_enum lvl, string_view_t msg)
|
||||||
|
{
|
||||||
|
bool log_enabled = should_log(lvl);
|
||||||
|
bool traceback_enabled = tracer_.enabled();
|
||||||
|
if (!log_enabled && !traceback_enabled)
|
||||||
{
|
{
|
||||||
details::log_msg log_msg(loc, name_, lvl, msg);
|
return;
|
||||||
if (level_enabled)
|
|
||||||
{
|
|
||||||
sink_it_(log_msg);
|
|
||||||
}
|
|
||||||
if (tracer_)
|
|
||||||
{
|
|
||||||
tracer_.push_back(log_msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
SPDLOG_LOGGER_CATCH()
|
|
||||||
|
details::log_msg log_msg(loc, name_, lvl, msg);
|
||||||
|
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
void log(level::level_enum lvl, string_view_t msg)
|
void log(level::level_enum lvl, string_view_t msg)
|
||||||
@@ -176,15 +140,92 @@ public:
|
|||||||
log(source_loc{}, lvl, msg);
|
log(source_loc{}, lvl, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// T cannot be statically converted to string_view or wstring_view
|
template<typename... Args>
|
||||||
template<class T, typename std::enable_if<!std::is_convertible<const T &, spdlog::string_view_t>::value &&
|
void trace(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
!is_convertible_to_wstring_view<const T &>::value,
|
|
||||||
T>::type * = nullptr>
|
|
||||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
|
||||||
{
|
{
|
||||||
log(loc, lvl, "{}", msg);
|
log(level::trace, fmt, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void debug(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::debug, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void info(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::info, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void warn(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::warn, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void error(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::err, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void critical(fmt::format_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::critical, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
template<typename... Args>
|
||||||
|
void log(level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(source_loc{}, lvl, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void log(source_loc loc, level::level_enum lvl, fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log_(loc, lvl, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void trace(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::trace, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void debug(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::debug, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void info(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::info, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void warn(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::warn, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void error(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::err, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void critical(fmt::wformat_string<Args...> fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
log(level::critical, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void trace(const T &msg)
|
void trace(const T &msg)
|
||||||
{
|
{
|
||||||
@@ -221,107 +262,18 @@ public:
|
|||||||
log(level::critical, msg);
|
log(level::critical, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
// return true logging is enabled for the given level.
|
||||||
#ifndef _WIN32
|
bool should_log(level::level_enum msg_level) const
|
||||||
#error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows
|
|
||||||
#else
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void log(source_loc loc, level::level_enum lvl, wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
{
|
||||||
auto level_enabled = should_log(lvl);
|
return msg_level >= level_.load(std::memory_order_relaxed);
|
||||||
if (!level_enabled && !tracer_)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
SPDLOG_TRY
|
|
||||||
{
|
|
||||||
// format to wmemory_buffer and convert to utf8
|
|
||||||
fmt::wmemory_buffer wbuf;
|
|
||||||
fmt::format_to(wbuf, fmt, args...);
|
|
||||||
|
|
||||||
memory_buf_t buf;
|
|
||||||
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
|
|
||||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
|
||||||
|
|
||||||
if (level_enabled)
|
|
||||||
{
|
|
||||||
sink_it_(log_msg);
|
|
||||||
}
|
|
||||||
if (tracer_)
|
|
||||||
{
|
|
||||||
tracer_.push_back(log_msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
SPDLOG_LOGGER_CATCH()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
// return true if backtrace logging is enabled.
|
||||||
void log(level::level_enum lvl, wstring_view_t fmt, const Args &... args)
|
bool should_backtrace() const
|
||||||
{
|
{
|
||||||
log(source_loc{}, lvl, fmt, args...);
|
return tracer_.enabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void trace(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::trace, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void debug(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::debug, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void info(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::info, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void warn(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::warn, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void error(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::err, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename... Args>
|
|
||||||
void critical(wstring_view_t fmt, const Args &... args)
|
|
||||||
{
|
|
||||||
log(level::critical, fmt, args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
// T can be statically converted to wstring_view
|
|
||||||
template<class T, typename std::enable_if<is_convertible_to_wstring_view<const T &>::value, T>::type * = nullptr>
|
|
||||||
void log(source_loc loc, level::level_enum lvl, const T &msg)
|
|
||||||
{
|
|
||||||
if (!should_log(lvl))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
memory_buf_t buf;
|
|
||||||
details::os::wstr_to_utf8buf(msg, buf);
|
|
||||||
|
|
||||||
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
|
||||||
sink_it_(log_msg);
|
|
||||||
}
|
|
||||||
SPDLOG_LOGGER_CATCH()
|
|
||||||
}
|
|
||||||
#endif // _WIN32
|
|
||||||
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
|
||||||
|
|
||||||
bool should_log(level::level_enum msg_level) const;
|
|
||||||
|
|
||||||
void set_level(level::level_enum log_level);
|
void set_level(level::level_enum log_level);
|
||||||
|
|
||||||
level::level_enum level() const;
|
level::level_enum level() const;
|
||||||
@@ -329,7 +281,7 @@ public:
|
|||||||
const std::string &name() const;
|
const std::string &name() const;
|
||||||
|
|
||||||
// set formatting for the sinks in this logger.
|
// set formatting for the sinks in this logger.
|
||||||
// each sink will get a seperate instance of the formatter object.
|
// each sink will get a separate instance of the formatter object.
|
||||||
void set_formatter(std::unique_ptr<formatter> f);
|
void set_formatter(std::unique_ptr<formatter> f);
|
||||||
|
|
||||||
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local);
|
||||||
@@ -364,6 +316,74 @@ protected:
|
|||||||
err_handler custom_err_handler_{nullptr};
|
err_handler custom_err_handler_{nullptr};
|
||||||
details::backtracer tracer_;
|
details::backtracer tracer_;
|
||||||
|
|
||||||
|
// common implementation for after templated public api has been resolved
|
||||||
|
template<typename... Args>
|
||||||
|
void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
bool log_enabled = should_log(lvl);
|
||||||
|
bool traceback_enabled = tracer_.enabled();
|
||||||
|
if (!log_enabled && !traceback_enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SPDLOG_TRY
|
||||||
|
{
|
||||||
|
memory_buf_t buf;
|
||||||
|
fmt::detail::vformat_to(buf, fmt, fmt::make_format_args(args...));
|
||||||
|
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||||
|
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||||
|
}
|
||||||
|
SPDLOG_LOGGER_CATCH()
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
template<typename... Args>
|
||||||
|
void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args)
|
||||||
|
{
|
||||||
|
bool log_enabled = should_log(lvl);
|
||||||
|
bool traceback_enabled = tracer_.enabled();
|
||||||
|
if (!log_enabled && !traceback_enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SPDLOG_TRY
|
||||||
|
{
|
||||||
|
// format to wmemory_buffer and convert to utf8
|
||||||
|
fmt::wmemory_buffer wbuf;
|
||||||
|
fmt::detail::vformat_to(wbuf, fmt, fmt::make_format_args<fmt::wformat_context>(args...));
|
||||||
|
memory_buf_t buf;
|
||||||
|
details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf);
|
||||||
|
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||||
|
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||||
|
}
|
||||||
|
SPDLOG_LOGGER_CATCH()
|
||||||
|
}
|
||||||
|
|
||||||
|
// T can be statically converted to wstring_view, and no formatting needed.
|
||||||
|
template<class T, typename std::enable_if<std::is_convertible<const T &, spdlog::wstring_view_t>::value, int>::type = 0>
|
||||||
|
void log_(source_loc loc, level::level_enum lvl, const T &msg)
|
||||||
|
{
|
||||||
|
bool log_enabled = should_log(lvl);
|
||||||
|
bool traceback_enabled = tracer_.enabled();
|
||||||
|
if (!log_enabled && !traceback_enabled)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SPDLOG_TRY
|
||||||
|
{
|
||||||
|
memory_buf_t buf;
|
||||||
|
details::os::wstr_to_utf8buf(msg, buf);
|
||||||
|
details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size()));
|
||||||
|
log_it_(log_msg, log_enabled, traceback_enabled);
|
||||||
|
}
|
||||||
|
SPDLOG_LOGGER_CATCH()
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
|
||||||
|
|
||||||
|
// log the given message (if the given log level is high enough),
|
||||||
|
// and save backtrace (if backtrace is enabled).
|
||||||
|
void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled);
|
||||||
virtual void sink_it_(const details::log_msg &msg);
|
virtual void sink_it_(const details::log_msg &msg);
|
||||||
virtual void flush_();
|
virtual void flush_();
|
||||||
void dump_backtrace_();
|
void dump_backtrace_();
|
||||||
@@ -379,5 +399,5 @@ void swap(logger &a, logger &b);
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "logger-inl.h"
|
# include "logger-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,20 +4,22 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
# include <spdlog/pattern_formatter.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/details/fmt_helper.h"
|
#include <spdlog/details/fmt_helper.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
#include "spdlog/fmt/fmt.h"
|
#include <spdlog/fmt/fmt.h>
|
||||||
#include "spdlog/formatter.h"
|
#include <spdlog/formatter.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iterator>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -39,57 +41,70 @@ public:
|
|||||||
: padinfo_(padinfo)
|
: padinfo_(padinfo)
|
||||||
, dest_(dest)
|
, dest_(dest)
|
||||||
{
|
{
|
||||||
|
remaining_pad_ = static_cast<long>(padinfo.width_) - static_cast<long>(wrapped_size);
|
||||||
if (padinfo_.width_ <= wrapped_size)
|
if (remaining_pad_ <= 0)
|
||||||
{
|
{
|
||||||
total_pad_ = 0;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
total_pad_ = padinfo.width_ - wrapped_size;
|
if (padinfo_.side_ == padding_info::pad_side::left)
|
||||||
if (padinfo_.side_ == padding_info::left)
|
|
||||||
{
|
{
|
||||||
pad_it(total_pad_);
|
pad_it(remaining_pad_);
|
||||||
total_pad_ = 0;
|
remaining_pad_ = 0;
|
||||||
}
|
}
|
||||||
else if (padinfo_.side_ == padding_info::center)
|
else if (padinfo_.side_ == padding_info::pad_side::center)
|
||||||
{
|
{
|
||||||
auto half_pad = total_pad_ / 2;
|
auto half_pad = remaining_pad_ / 2;
|
||||||
auto reminder = total_pad_ & 1;
|
auto reminder = remaining_pad_ & 1;
|
||||||
pad_it(half_pad);
|
pad_it(half_pad);
|
||||||
total_pad_ = half_pad + reminder; // for the right side
|
remaining_pad_ = half_pad + reminder; // for the right side
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static unsigned int count_digits(T n)
|
||||||
|
{
|
||||||
|
return fmt_helper::count_digits(n);
|
||||||
|
}
|
||||||
|
|
||||||
~scoped_padder()
|
~scoped_padder()
|
||||||
{
|
{
|
||||||
if (total_pad_)
|
if (remaining_pad_ >= 0)
|
||||||
{
|
{
|
||||||
pad_it(total_pad_);
|
pad_it(remaining_pad_);
|
||||||
|
}
|
||||||
|
else if (padinfo_.truncate_)
|
||||||
|
{
|
||||||
|
long new_size = static_cast<long>(dest_.size()) + remaining_pad_;
|
||||||
|
dest_.resize(static_cast<size_t>(new_size));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void pad_it(size_t count)
|
void pad_it(long count)
|
||||||
{
|
{
|
||||||
// count = std::min(count, spaces_.size());
|
fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast<size_t>(count)), dest_);
|
||||||
assert(count <= spaces_.size());
|
|
||||||
fmt_helper::append_string_view(string_view_t(spaces_.data(), count), dest_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const padding_info &padinfo_;
|
const padding_info &padinfo_;
|
||||||
memory_buf_t &dest_;
|
memory_buf_t &dest_;
|
||||||
size_t total_pad_;
|
long remaining_pad_;
|
||||||
string_view_t spaces_{" ", 64};
|
string_view_t spaces_{" ", 64};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct null_scoped_padder
|
struct null_scoped_padder
|
||||||
{
|
{
|
||||||
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {}
|
null_scoped_padder(size_t /*wrapped_size*/, const padding_info & /*padinfo*/, memory_buf_t & /*dest*/) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static unsigned int count_digits(T /* number */)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class name_formatter : public flag_formatter
|
class name_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit name_formatter(padding_info padinfo)
|
explicit name_formatter(padding_info padinfo)
|
||||||
@@ -105,7 +120,7 @@ public:
|
|||||||
|
|
||||||
// log level appender
|
// log level appender
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class level_formatter : public flag_formatter
|
class level_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit level_formatter(padding_info padinfo)
|
explicit level_formatter(padding_info padinfo)
|
||||||
@@ -114,7 +129,7 @@ public:
|
|||||||
|
|
||||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||||
{
|
{
|
||||||
string_view_t &level_name = level::to_string_view(msg.level);
|
const string_view_t &level_name = level::to_string_view(msg.level);
|
||||||
ScopedPadder p(level_name.size(), padinfo_, dest);
|
ScopedPadder p(level_name.size(), padinfo_, dest);
|
||||||
fmt_helper::append_string_view(level_name, dest);
|
fmt_helper::append_string_view(level_name, dest);
|
||||||
}
|
}
|
||||||
@@ -122,7 +137,7 @@ public:
|
|||||||
|
|
||||||
// short log level appender
|
// short log level appender
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class short_level_formatter : public flag_formatter
|
class short_level_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit short_level_formatter(padding_info padinfo)
|
explicit short_level_formatter(padding_info padinfo)
|
||||||
@@ -155,7 +170,7 @@ static int to12h(const tm &t)
|
|||||||
static std::array<const char *, 7> days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}};
|
static std::array<const char *, 7> days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class a_formatter : public flag_formatter
|
class a_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit a_formatter(padding_info padinfo)
|
explicit a_formatter(padding_info padinfo)
|
||||||
@@ -193,7 +208,7 @@ public:
|
|||||||
static const std::array<const char *, 12> months{{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}};
|
static const std::array<const char *, 12> months{{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"}};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class b_formatter : public flag_formatter
|
class b_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit b_formatter(padding_info padinfo)
|
explicit b_formatter(padding_info padinfo)
|
||||||
@@ -209,11 +224,11 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Full month name
|
// Full month name
|
||||||
static const std::array<const char *, 12> full_months{{
|
static const std::array<const char *, 12> full_months{
|
||||||
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}};
|
{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}};
|
||||||
|
|
||||||
template<typename ScopedPadder>
|
template<typename ScopedPadder>
|
||||||
class B_formatter : public flag_formatter
|
class B_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit B_formatter(padding_info padinfo)
|
explicit B_formatter(padding_info padinfo)
|
||||||
@@ -593,14 +608,7 @@ public:
|
|||||||
const size_t field_size = 6;
|
const size_t field_size = 6;
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
|
|
||||||
#ifdef _WIN32
|
auto total_minutes = get_cached_offset(msg, tm_time);
|
||||||
int total_minutes = get_cached_offset(msg, tm_time);
|
|
||||||
#else
|
|
||||||
// No need to chache under gcc,
|
|
||||||
// it is very fast (already stored in tm.tm_gmtoff)
|
|
||||||
(void)(msg);
|
|
||||||
int total_minutes = os::utc_minutes_offset(tm_time);
|
|
||||||
#endif
|
|
||||||
bool is_negative = total_minutes < 0;
|
bool is_negative = total_minutes < 0;
|
||||||
if (is_negative)
|
if (is_negative)
|
||||||
{
|
{
|
||||||
@@ -619,7 +627,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
log_clock::time_point last_update_{std::chrono::seconds(0)};
|
log_clock::time_point last_update_{std::chrono::seconds(0)};
|
||||||
#ifdef _WIN32
|
|
||||||
int offset_minutes_{0};
|
int offset_minutes_{0};
|
||||||
|
|
||||||
int get_cached_offset(const log_msg &msg, const std::tm &tm_time)
|
int get_cached_offset(const log_msg &msg, const std::tm &tm_time)
|
||||||
@@ -632,7 +639,6 @@ private:
|
|||||||
}
|
}
|
||||||
return offset_minutes_;
|
return offset_minutes_;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Thread id
|
// Thread id
|
||||||
@@ -646,7 +652,7 @@ public:
|
|||||||
|
|
||||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||||
{
|
{
|
||||||
const auto field_size = fmt_helper::count_digits(msg.thread_id);
|
const auto field_size = ScopedPadder::count_digits(msg.thread_id);
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
fmt_helper::append_int(msg.thread_id, dest);
|
fmt_helper::append_int(msg.thread_id, dest);
|
||||||
}
|
}
|
||||||
@@ -664,7 +670,7 @@ public:
|
|||||||
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override
|
void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override
|
||||||
{
|
{
|
||||||
const auto pid = static_cast<uint32_t>(details::os::pid());
|
const auto pid = static_cast<uint32_t>(details::os::pid());
|
||||||
auto field_size = fmt_helper::count_digits(pid);
|
auto field_size = ScopedPadder::count_digits(pid);
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
fmt_helper::append_int(pid, dest);
|
fmt_helper::append_int(pid, dest);
|
||||||
}
|
}
|
||||||
@@ -763,8 +769,16 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t text_size =
|
size_t text_size;
|
||||||
padinfo_.enabled() ? std::char_traits<char>::length(msg.source.filename) + fmt_helper::count_digits(msg.source.line) + 1 : 0;
|
if (padinfo_.enabled())
|
||||||
|
{
|
||||||
|
// calc text size for padding based on "filename:line"
|
||||||
|
text_size = std::char_traits<char>::length(msg.source.filename) + ScopedPadder::count_digits(msg.source.line) + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
text_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
ScopedPadder p(text_size, padinfo_, dest);
|
ScopedPadder p(text_size, padinfo_, dest);
|
||||||
fmt_helper::append_string_view(msg.source.filename, dest);
|
fmt_helper::append_string_view(msg.source.filename, dest);
|
||||||
@@ -802,11 +816,31 @@ public:
|
|||||||
: flag_formatter(padinfo)
|
: flag_formatter(padinfo)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# pragma warning(push)
|
||||||
|
# pragma warning(disable : 4127) // consider using 'if constexpr' instead
|
||||||
|
#endif // _MSC_VER
|
||||||
static const char *basename(const char *filename)
|
static const char *basename(const char *filename)
|
||||||
{
|
{
|
||||||
const char *rv = std::strrchr(filename, os::folder_sep);
|
// if the size is 2 (1 character + null terminator) we can use the more efficient strrchr
|
||||||
return rv != nullptr ? rv + 1 : filename;
|
// the branch will be elided by optimizations
|
||||||
|
if (sizeof(os::folder_seps) == 2)
|
||||||
|
{
|
||||||
|
const char *rv = std::strrchr(filename, os::folder_seps[0]);
|
||||||
|
return rv != nullptr ? rv + 1 : filename;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const std::reverse_iterator<const char *> begin(filename + std::strlen(filename));
|
||||||
|
const std::reverse_iterator<const char *> end(filename);
|
||||||
|
|
||||||
|
const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), std::end(os::folder_seps) - 1);
|
||||||
|
return it != end ? it.base() : filename;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
# pragma warning(pop)
|
||||||
|
#endif // _MSC_VER
|
||||||
|
|
||||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||||
{
|
{
|
||||||
@@ -836,7 +870,7 @@ public:
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto field_size = fmt_helper::count_digits(msg.source.line);
|
auto field_size = ScopedPadder::count_digits(msg.source.line);
|
||||||
ScopedPadder p(field_size, padinfo_, dest);
|
ScopedPadder p(field_size, padinfo_, dest);
|
||||||
fmt_helper::append_int(msg.source.line, dest);
|
fmt_helper::append_int(msg.source.line, dest);
|
||||||
}
|
}
|
||||||
@@ -865,7 +899,6 @@ public:
|
|||||||
|
|
||||||
// print elapsed time since last message
|
// print elapsed time since last message
|
||||||
template<typename ScopedPadder, typename Units>
|
template<typename ScopedPadder, typename Units>
|
||||||
|
|
||||||
class elapsed_formatter final : public flag_formatter
|
class elapsed_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -878,19 +911,21 @@ public:
|
|||||||
|
|
||||||
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override
|
||||||
{
|
{
|
||||||
auto delta = std::max(msg.time - last_message_time_, log_clock::duration::zero());
|
auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero());
|
||||||
auto delta_units = std::chrono::duration_cast<DurationUnits>(delta);
|
auto delta_units = std::chrono::duration_cast<DurationUnits>(delta);
|
||||||
last_message_time_ = msg.time;
|
last_message_time_ = msg.time;
|
||||||
ScopedPadder p(6, padinfo_, dest);
|
auto delta_count = static_cast<size_t>(delta_units.count());
|
||||||
fmt_helper::pad6(static_cast<size_t>(delta_units.count()), dest);
|
auto n_digits = static_cast<size_t>(ScopedPadder::count_digits(delta_count));
|
||||||
|
ScopedPadder p(n_digits, padinfo_, dest);
|
||||||
|
fmt_helper::append_int(delta_count, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
log_clock::time_point last_message_time_;
|
log_clock::time_point last_message_time_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Full info formatter
|
// Full info formatter
|
||||||
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v
|
// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v
|
||||||
class full_formatter final : public flag_formatter
|
class full_formatter final : public flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -904,8 +939,6 @@ public:
|
|||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
using std::chrono::seconds;
|
using std::chrono::seconds;
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_DATETIME
|
|
||||||
|
|
||||||
// cache the date/time part for the next second.
|
// cache the date/time part for the next second.
|
||||||
auto duration = msg.time.time_since_epoch();
|
auto duration = msg.time.time_since_epoch();
|
||||||
auto secs = duration_cast<seconds>(duration);
|
auto secs = duration_cast<seconds>(duration);
|
||||||
@@ -941,20 +974,15 @@ public:
|
|||||||
dest.push_back(']');
|
dest.push_back(']');
|
||||||
dest.push_back(' ');
|
dest.push_back(' ');
|
||||||
|
|
||||||
#else // no datetime needed
|
// append logger name if exists
|
||||||
(void)tm_time;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef SPDLOG_NO_NAME
|
|
||||||
if (msg.logger_name.size() > 0)
|
if (msg.logger_name.size() > 0)
|
||||||
{
|
{
|
||||||
dest.push_back('[');
|
dest.push_back('[');
|
||||||
// fmt_helper::append_str(*msg.logger_name, dest);
|
|
||||||
fmt_helper::append_string_view(msg.logger_name, dest);
|
fmt_helper::append_string_view(msg.logger_name, dest);
|
||||||
dest.push_back(']');
|
dest.push_back(']');
|
||||||
dest.push_back(' ');
|
dest.push_back(' ');
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
dest.push_back('[');
|
dest.push_back('[');
|
||||||
// wrap the level name with color
|
// wrap the level name with color
|
||||||
msg.color_range_start = dest.size();
|
msg.color_range_start = dest.size();
|
||||||
@@ -986,11 +1014,13 @@ private:
|
|||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern, pattern_time_type time_type, std::string eol)
|
SPDLOG_INLINE pattern_formatter::pattern_formatter(
|
||||||
|
std::string pattern, pattern_time_type time_type, std::string eol, custom_flags custom_user_flags)
|
||||||
: pattern_(std::move(pattern))
|
: pattern_(std::move(pattern))
|
||||||
, eol_(std::move(eol))
|
, eol_(std::move(eol))
|
||||||
, pattern_time_type_(time_type)
|
, pattern_time_type_(time_type)
|
||||||
, last_log_secs_(0)
|
, last_log_secs_(0)
|
||||||
|
, custom_handlers_(std::move(custom_user_flags))
|
||||||
{
|
{
|
||||||
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
std::memset(&cached_tm_, 0, sizeof(cached_tm_));
|
||||||
compile_pattern_(pattern_);
|
compile_pattern_(pattern_);
|
||||||
@@ -1009,19 +1039,23 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type,
|
|||||||
|
|
||||||
SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
|
SPDLOG_INLINE std::unique_ptr<formatter> pattern_formatter::clone() const
|
||||||
{
|
{
|
||||||
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_);
|
custom_flags cloned_custom_formatters;
|
||||||
|
for (auto &it : custom_handlers_)
|
||||||
|
{
|
||||||
|
cloned_custom_formatters[it.first] = it.second->clone();
|
||||||
|
}
|
||||||
|
return details::make_unique<pattern_formatter>(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters));
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest)
|
SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest)
|
||||||
{
|
{
|
||||||
#ifndef SPDLOG_NO_DATETIME
|
|
||||||
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
auto secs = std::chrono::duration_cast<std::chrono::seconds>(msg.time.time_since_epoch());
|
||||||
if (secs != last_log_secs_)
|
if (secs != last_log_secs_)
|
||||||
{
|
{
|
||||||
cached_tm_ = get_time_(msg);
|
cached_tm_ = get_time_(msg);
|
||||||
last_log_secs_ = secs;
|
last_log_secs_ = secs;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
for (auto &f : formatters_)
|
for (auto &f : formatters_)
|
||||||
{
|
{
|
||||||
f->format(msg, cached_tm_, dest);
|
f->format(msg, cached_tm_, dest);
|
||||||
@@ -1030,6 +1064,12 @@ SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory
|
|||||||
details::fmt_helper::append_string_view(eol_, dest);
|
details::fmt_helper::append_string_view(eol_, dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern)
|
||||||
|
{
|
||||||
|
pattern_ = std::move(pattern);
|
||||||
|
compile_pattern_(pattern_);
|
||||||
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
|
SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
|
||||||
{
|
{
|
||||||
if (pattern_time_type_ == pattern_time_type::local)
|
if (pattern_time_type_ == pattern_time_type::local)
|
||||||
@@ -1042,9 +1082,19 @@ SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg)
|
|||||||
template<typename Padder>
|
template<typename Padder>
|
||||||
SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding)
|
SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding)
|
||||||
{
|
{
|
||||||
|
// process custom flags
|
||||||
|
auto it = custom_handlers_.find(flag);
|
||||||
|
if (it != custom_handlers_.end())
|
||||||
|
{
|
||||||
|
auto custom_handler = it->second->clone();
|
||||||
|
custom_handler->set_padding_info(padding);
|
||||||
|
formatters_.push_back(std::move(custom_handler));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// process built-in flags
|
||||||
switch (flag)
|
switch (flag)
|
||||||
{
|
{
|
||||||
|
|
||||||
case ('+'): // default formatter
|
case ('+'): // default formatter
|
||||||
formatters_.push_back(details::make_unique<details::full_formatter>(padding));
|
formatters_.push_back(details::make_unique<details::full_formatter>(padding));
|
||||||
break;
|
break;
|
||||||
@@ -1218,14 +1268,29 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i
|
|||||||
|
|
||||||
default: // Unknown flag appears as is
|
default: // Unknown flag appears as is
|
||||||
auto unknown_flag = details::make_unique<details::aggregate_formatter>();
|
auto unknown_flag = details::make_unique<details::aggregate_formatter>();
|
||||||
unknown_flag->add_ch('%');
|
|
||||||
unknown_flag->add_ch(flag);
|
if (!padding.truncate_)
|
||||||
formatters_.push_back((std::move(unknown_flag)));
|
{
|
||||||
|
unknown_flag->add_ch('%');
|
||||||
|
unknown_flag->add_ch(flag);
|
||||||
|
formatters_.push_back((std::move(unknown_flag)));
|
||||||
|
}
|
||||||
|
// fix issue #1617 (prev char was '!' and should have been treated as funcname flag instead of truncating flag)
|
||||||
|
// spdlog::set_pattern("[%10!] %v") => "[ main] some message"
|
||||||
|
// spdlog::set_pattern("[%3!!] %v") => "[mai] some message"
|
||||||
|
else
|
||||||
|
{
|
||||||
|
padding.truncate_ = false;
|
||||||
|
formatters_.push_back(details::make_unique<details::source_funcname_formatter<Padder>>(padding));
|
||||||
|
unknown_flag->add_ch(flag);
|
||||||
|
formatters_.push_back((std::move(unknown_flag)));
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract given pad spec (e.g. %8X)
|
// Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X)
|
||||||
// Advance the given it pass the end of the padding spec found (if any)
|
// Advance the given it pass the end of the padding spec found (if any)
|
||||||
// Return padding.
|
// Return padding.
|
||||||
SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end)
|
SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end)
|
||||||
@@ -1242,21 +1307,21 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri
|
|||||||
switch (*it)
|
switch (*it)
|
||||||
{
|
{
|
||||||
case '-':
|
case '-':
|
||||||
side = padding_info::right;
|
side = padding_info::pad_side::right;
|
||||||
++it;
|
++it;
|
||||||
break;
|
break;
|
||||||
case '=':
|
case '=':
|
||||||
side = padding_info::center;
|
side = padding_info::pad_side::center;
|
||||||
++it;
|
++it;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
side = details::padding_info::left;
|
side = details::padding_info::pad_side::left;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it == end || !std::isdigit(static_cast<unsigned char>(*it)))
|
if (it == end || !std::isdigit(static_cast<unsigned char>(*it)))
|
||||||
{
|
{
|
||||||
return padding_info{0, side};
|
return padding_info{}; // no padding if no digit found here
|
||||||
}
|
}
|
||||||
|
|
||||||
auto width = static_cast<size_t>(*it) - '0';
|
auto width = static_cast<size_t>(*it) - '0';
|
||||||
@@ -1265,7 +1330,20 @@ SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_(std::stri
|
|||||||
auto digit = static_cast<size_t>(*it) - '0';
|
auto digit = static_cast<size_t>(*it) - '0';
|
||||||
width = width * 10 + digit;
|
width = width * 10 + digit;
|
||||||
}
|
}
|
||||||
return details::padding_info{std::min<size_t>(width, max_width), side};
|
|
||||||
|
// search for the optional truncate marker '!'
|
||||||
|
bool truncate;
|
||||||
|
if (it != end && *it == '!')
|
||||||
|
{
|
||||||
|
truncate = true;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
truncate = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return details::padding_info{std::min<size_t>(width, max_width), side, truncate};
|
||||||
}
|
}
|
||||||
|
|
||||||
SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &pattern)
|
SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &pattern)
|
||||||
@@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
#include "spdlog/formatter.h"
|
#include <spdlog/formatter.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
@@ -21,7 +22,7 @@ namespace details {
|
|||||||
// padding information.
|
// padding information.
|
||||||
struct padding_info
|
struct padding_info
|
||||||
{
|
{
|
||||||
enum pad_side
|
enum class pad_side
|
||||||
{
|
{
|
||||||
left,
|
left,
|
||||||
right,
|
right,
|
||||||
@@ -29,20 +30,24 @@ struct padding_info
|
|||||||
};
|
};
|
||||||
|
|
||||||
padding_info() = default;
|
padding_info() = default;
|
||||||
padding_info(size_t width, padding_info::pad_side side)
|
padding_info(size_t width, padding_info::pad_side side, bool truncate)
|
||||||
: width_(width)
|
: width_(width)
|
||||||
, side_(side)
|
, side_(side)
|
||||||
|
, truncate_(truncate)
|
||||||
|
, enabled_(true)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool enabled() const
|
bool enabled() const
|
||||||
{
|
{
|
||||||
return width_ != 0;
|
return enabled_;
|
||||||
}
|
}
|
||||||
const size_t width_ = 0;
|
size_t width_ = 0;
|
||||||
const pad_side side_ = left;
|
pad_side side_ = pad_side::left;
|
||||||
|
bool truncate_ = false;
|
||||||
|
bool enabled_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
class flag_formatter
|
class SPDLOG_API flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit flag_formatter(padding_info padinfo)
|
explicit flag_formatter(padding_info padinfo)
|
||||||
@@ -58,11 +63,24 @@ protected:
|
|||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
class pattern_formatter final : public formatter
|
class SPDLOG_API custom_flag_formatter : public details::flag_formatter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit pattern_formatter(
|
virtual std::unique_ptr<custom_flag_formatter> clone() const = 0;
|
||||||
std::string pattern, pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
|
||||||
|
void set_padding_info(details::padding_info padding)
|
||||||
|
{
|
||||||
|
flag_formatter::padinfo_ = padding;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SPDLOG_API pattern_formatter final : public formatter
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using custom_flags = std::unordered_map<char, std::unique_ptr<custom_flag_formatter>>;
|
||||||
|
|
||||||
|
explicit pattern_formatter(std::string pattern, pattern_time_type time_type = pattern_time_type::local,
|
||||||
|
std::string eol = spdlog::details::os::default_eol, custom_flags custom_user_flags = custom_flags());
|
||||||
|
|
||||||
// use default pattern is not given
|
// use default pattern is not given
|
||||||
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, std::string eol = spdlog::details::os::default_eol);
|
||||||
@@ -73,6 +91,14 @@ public:
|
|||||||
std::unique_ptr<formatter> clone() const override;
|
std::unique_ptr<formatter> clone() const override;
|
||||||
void format(const details::log_msg &msg, memory_buf_t &dest) override;
|
void format(const details::log_msg &msg, memory_buf_t &dest) override;
|
||||||
|
|
||||||
|
template<typename T, typename... Args>
|
||||||
|
pattern_formatter &add_flag(char flag, Args &&...args)
|
||||||
|
{
|
||||||
|
custom_handlers_[flag] = details::make_unique<T>(std::forward<Args>(args)...);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
void set_pattern(std::string pattern);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string pattern_;
|
std::string pattern_;
|
||||||
std::string eol_;
|
std::string eol_;
|
||||||
@@ -80,6 +106,7 @@ private:
|
|||||||
std::tm cached_tm_;
|
std::tm cached_tm_;
|
||||||
std::chrono::seconds last_log_secs_;
|
std::chrono::seconds last_log_secs_;
|
||||||
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
std::vector<std::unique_ptr<details::flag_formatter>> formatters_;
|
||||||
|
custom_flags custom_handlers_;
|
||||||
|
|
||||||
std::tm get_time_(const details::log_msg &msg);
|
std::tm get_time_(const details::log_msg &msg);
|
||||||
template<typename Padder>
|
template<typename Padder>
|
||||||
@@ -88,12 +115,12 @@ private:
|
|||||||
// Extract given pad spec (e.g. %8X)
|
// Extract given pad spec (e.g. %8X)
|
||||||
// Advance the given it pass the end of the padding spec found (if any)
|
// Advance the given it pass the end of the padding spec found (if any)
|
||||||
// Return padding.
|
// Return padding.
|
||||||
details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
|
static details::padding_info handle_padspec_(std::string::const_iterator &it, std::string::const_iterator end);
|
||||||
|
|
||||||
void compile_pattern_(const std::string &pattern);
|
void compile_pattern_(const std::string &pattern);
|
||||||
};
|
};
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "pattern_formatter-inl.h"
|
# include "pattern_formatter-inl.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -5,21 +5,21 @@
|
|||||||
|
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
|
|
||||||
#include "spdlog/details/fmt_helper.h"
|
# include <spdlog/details/fmt_helper.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
# include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/details/os.h"
|
# include <spdlog/details/os.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
# include <spdlog/sinks/base_sink.h>
|
||||||
#include "spdlog/details/synchronous_factory.h"
|
# include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
#include <android/log.h>
|
# include <android/log.h>
|
||||||
#include <chrono>
|
# include <chrono>
|
||||||
#include <mutex>
|
# include <mutex>
|
||||||
#include <string>
|
# include <string>
|
||||||
#include <thread>
|
# include <thread>
|
||||||
|
|
||||||
#if !defined(SPDLOG_ANDROID_RETRIES)
|
# if !defined(SPDLOG_ANDROID_RETRIES)
|
||||||
#define SPDLOG_ANDROID_RETRIES 2
|
# define SPDLOG_ANDROID_RETRIES 2
|
||||||
#endif
|
# endif
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -64,7 +64,7 @@ protected:
|
|||||||
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("__android_log_write() failed", ret));
|
throw_spdlog_ex("__android_log_write() failed", ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/sinks/ansicolor_sink.h"
|
# include <spdlog/sinks/ansicolor_sink.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include <spdlog/pattern_formatter.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -21,20 +21,20 @@ SPDLOG_INLINE ansicolor_sink<ConsoleMutex>::ansicolor_sink(FILE *target_file, co
|
|||||||
|
|
||||||
{
|
{
|
||||||
set_color_mode(mode);
|
set_color_mode(mode);
|
||||||
colors_[level::trace] = white;
|
colors_[level::trace] = to_string_(white);
|
||||||
colors_[level::debug] = cyan;
|
colors_[level::debug] = to_string_(cyan);
|
||||||
colors_[level::info] = green;
|
colors_[level::info] = to_string_(green);
|
||||||
colors_[level::warn] = yellow_bold;
|
colors_[level::warn] = to_string_(yellow_bold);
|
||||||
colors_[level::err] = red_bold;
|
colors_[level::err] = to_string_(red_bold);
|
||||||
colors_[level::critical] = bold_on_red;
|
colors_[level::critical] = to_string_(bold_on_red);
|
||||||
colors_[level::off] = reset;
|
colors_[level::off] = to_string_(reset);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color)
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color(level::level_enum color_level, string_view_t color)
|
||||||
{
|
{
|
||||||
std::lock_guard<mutex_t> lock(mutex_);
|
std::lock_guard<mutex_t> lock(mutex_);
|
||||||
colors_[color_level] = color;
|
colors_[color_level] = to_string_(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
@@ -43,7 +43,8 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::log(const details::log_msg &msg
|
|||||||
// Wrap the originally formatted message in color codes.
|
// Wrap the originally formatted message in color codes.
|
||||||
// If color is not supported in the terminal, log as is instead.
|
// If color is not supported in the terminal, log as is instead.
|
||||||
std::lock_guard<mutex_t> lock(mutex_);
|
std::lock_guard<mutex_t> lock(mutex_);
|
||||||
|
msg.color_range_start = 0;
|
||||||
|
msg.color_range_end = 0;
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
formatter_->format(msg, formatted);
|
formatter_->format(msg, formatted);
|
||||||
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
if (should_do_colors_ && msg.color_range_end > msg.color_range_start)
|
||||||
@@ -105,13 +106,15 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::set_color_mode(color_mode mode)
|
|||||||
case color_mode::never:
|
case color_mode::never:
|
||||||
should_do_colors_ = false;
|
should_do_colors_ = false;
|
||||||
return;
|
return;
|
||||||
|
default:
|
||||||
|
should_do_colors_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
|
SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_ccode_(const string_view_t &color_code)
|
||||||
{
|
{
|
||||||
fwrite(color_code.data(), sizeof(string_view_t::char_type), color_code.size(), target_file_);
|
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
@@ -120,6 +123,12 @@ SPDLOG_INLINE void ansicolor_sink<ConsoleMutex>::print_range_(const memory_buf_t
|
|||||||
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
fwrite(formatted.data() + start, sizeof(char), end - start, target_file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ConsoleMutex>
|
||||||
|
SPDLOG_INLINE std::string ansicolor_sink<ConsoleMutex>::to_string_(const string_view_t &sv)
|
||||||
|
{
|
||||||
|
return std::string(sv.data(), sv.size());
|
||||||
|
}
|
||||||
|
|
||||||
// ansicolor_stdout_sink
|
// ansicolor_stdout_sink
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
|
SPDLOG_INLINE ansicolor_stdout_sink<ConsoleMutex>::ansicolor_stdout_sink(color_mode mode)
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/console_globals.h"
|
#include <spdlog/details/console_globals.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/sinks/sink.h"
|
#include <spdlog/sinks/sink.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <array>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -30,7 +30,11 @@ public:
|
|||||||
~ansicolor_sink() override = default;
|
~ansicolor_sink() override = default;
|
||||||
|
|
||||||
ansicolor_sink(const ansicolor_sink &other) = delete;
|
ansicolor_sink(const ansicolor_sink &other) = delete;
|
||||||
|
ansicolor_sink(ansicolor_sink &&other) = delete;
|
||||||
|
|
||||||
ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
|
ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
|
||||||
|
ansicolor_sink &operator=(ansicolor_sink &&other) = delete;
|
||||||
|
|
||||||
void set_color(level::level_enum color_level, string_view_t color);
|
void set_color(level::level_enum color_level, string_view_t color);
|
||||||
void set_color_mode(color_mode mode);
|
void set_color_mode(color_mode mode);
|
||||||
bool should_color();
|
bool should_color();
|
||||||
@@ -80,9 +84,10 @@ private:
|
|||||||
mutex_t &mutex_;
|
mutex_t &mutex_;
|
||||||
bool should_do_colors_;
|
bool should_do_colors_;
|
||||||
std::unique_ptr<spdlog::formatter> formatter_;
|
std::unique_ptr<spdlog::formatter> formatter_;
|
||||||
std::unordered_map<level::level_enum, string_view_t, level::level_hasher> colors_;
|
std::array<std::string, level::n_levels> colors_;
|
||||||
void print_ccode_(const string_view_t &color_code);
|
void print_ccode_(const string_view_t &color_code);
|
||||||
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
void print_range_(const memory_buf_t &formatted, size_t start, size_t end);
|
||||||
|
static std::string to_string_(const string_view_t &sv);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename ConsoleMutex>
|
template<typename ConsoleMutex>
|
||||||
@@ -109,5 +114,5 @@ using ansicolor_stderr_sink_st = ansicolor_stderr_sink<details::console_nullmute
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "ansicolor_sink-inl.h"
|
# include "ansicolor_sink-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/sinks/base_sink.h"
|
# include <spdlog/sinks/base_sink.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include <spdlog/pattern_formatter.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
// implementers..
|
// implementers..
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
#include "spdlog/sinks/sink.h"
|
#include <spdlog/sinks/sink.h>
|
||||||
|
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -22,8 +21,14 @@ class base_sink : public sink
|
|||||||
public:
|
public:
|
||||||
base_sink();
|
base_sink();
|
||||||
explicit base_sink(std::unique_ptr<spdlog::formatter> formatter);
|
explicit base_sink(std::unique_ptr<spdlog::formatter> formatter);
|
||||||
|
~base_sink() override = default;
|
||||||
|
|
||||||
base_sink(const base_sink &) = delete;
|
base_sink(const base_sink &) = delete;
|
||||||
|
base_sink(base_sink &&) = delete;
|
||||||
|
|
||||||
base_sink &operator=(const base_sink &) = delete;
|
base_sink &operator=(const base_sink &) = delete;
|
||||||
|
base_sink &operator=(base_sink &&) = delete;
|
||||||
|
|
||||||
void log(const details::log_msg &msg) final;
|
void log(const details::log_msg &msg) final;
|
||||||
void flush() final;
|
void flush() final;
|
||||||
void set_pattern(const std::string &pattern) final;
|
void set_pattern(const std::string &pattern) final;
|
||||||
@@ -32,7 +37,7 @@ public:
|
|||||||
protected:
|
protected:
|
||||||
// sink formatter
|
// sink formatter
|
||||||
std::unique_ptr<spdlog::formatter> formatter_;
|
std::unique_ptr<spdlog::formatter> formatter_;
|
||||||
Mutex mutex_;
|
mutable Mutex mutex_;
|
||||||
|
|
||||||
virtual void sink_it_(const details::log_msg &msg) = 0;
|
virtual void sink_it_(const details::log_msg &msg) = 0;
|
||||||
virtual void flush_() = 0;
|
virtual void flush_() = 0;
|
||||||
@@ -43,5 +48,5 @@ protected:
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "base_sink-inl.h"
|
# include "base_sink-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,11 +4,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef SPDLOG_HEADER_ONLY
|
#ifndef SPDLOG_HEADER_ONLY
|
||||||
#include "spdlog/sinks/basic_file_sink.h"
|
# include <spdlog/sinks/basic_file_sink.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/details/os.h>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/file_helper.h"
|
#include <spdlog/details/file_helper.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
#include <spdlog/sinks/base_sink.h>
|
||||||
#include "spdlog/details/synchronous_factory.h"
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -54,5 +54,5 @@ inline std::shared_ptr<logger> basic_logger_st(const std::string &logger_name, c
|
|||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|
||||||
#ifdef SPDLOG_HEADER_ONLY
|
#ifdef SPDLOG_HEADER_ONLY
|
||||||
#include "basic_file_sink-inl.h"
|
# include "basic_file_sink-inl.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -3,13 +3,15 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/common.h"
|
#include <spdlog/common.h>
|
||||||
#include "spdlog/details/file_helper.h"
|
#include <spdlog/details/file_helper.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/fmt/fmt.h"
|
#include <spdlog/fmt/fmt.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
#include <spdlog/fmt/chrono.h>
|
||||||
#include "spdlog/details/os.h"
|
#include <spdlog/sinks/base_sink.h>
|
||||||
#include "spdlog/details/synchronous_factory.h"
|
#include <spdlog/details/os.h>
|
||||||
|
#include <spdlog/details/circular_q.h>
|
||||||
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
@@ -35,6 +37,27 @@ struct daily_filename_calculator
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generator of daily log file names with strftime format.
|
||||||
|
* Usages:
|
||||||
|
* auto sink = std::make_shared<spdlog::sinks::daily_file_format_sink_mt>("myapp-%Y-%m-%d:%H:%M:%S.log", hour, minute);"
|
||||||
|
* auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", hour, minute)"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
struct daily_filename_format_calculator
|
||||||
|
{
|
||||||
|
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
|
||||||
|
{
|
||||||
|
// generate fmt datetime format string, e.g. {:%Y-%m-%d}.
|
||||||
|
filename_t fmt_filename = fmt::format(SPDLOG_FILENAME_T("{{:{}}}"), filename);
|
||||||
|
#if defined(_MSC_VER) && defined(SPDLOG_WCHAR_FILENAMES) // for some reason msvc doesnt allow fmt::runtime(..) with wchar here
|
||||||
|
return fmt::format(fmt_filename, now_tm);
|
||||||
|
#else
|
||||||
|
return fmt::format(SPDLOG_FMT_RUNTIME(fmt_filename), now_tm);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Rotating file sink based on date.
|
* Rotating file sink based on date.
|
||||||
* If truncate != false , the created file will be truncated.
|
* If truncate != false , the created file will be truncated.
|
||||||
@@ -55,7 +78,7 @@ public:
|
|||||||
{
|
{
|
||||||
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || rotation_minute > 59)
|
||||||
{
|
{
|
||||||
SPDLOG_THROW(spdlog_ex("daily_file_sink: Invalid rotation time in ctor"));
|
throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto now = log_clock::now();
|
auto now = log_clock::now();
|
||||||
@@ -65,25 +88,20 @@ public:
|
|||||||
|
|
||||||
if (max_files_ > 0)
|
if (max_files_ > 0)
|
||||||
{
|
{
|
||||||
filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
|
init_filenames_q_();
|
||||||
filenames_q_.push_back(std::move(filename));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename_t &filename() const
|
filename_t filename()
|
||||||
{
|
{
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
return file_helper_.filename();
|
return file_helper_.filename();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override
|
void sink_it_(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
#ifdef SPDLOG_NO_DATETIME
|
|
||||||
auto time = log_clock::now();
|
|
||||||
#else
|
|
||||||
auto time = msg.time;
|
auto time = msg.time;
|
||||||
#endif
|
|
||||||
|
|
||||||
bool should_rotate = time >= rotation_tp_;
|
bool should_rotate = time >= rotation_tp_;
|
||||||
if (should_rotate)
|
if (should_rotate)
|
||||||
{
|
{
|
||||||
@@ -95,7 +113,7 @@ protected:
|
|||||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
file_helper_.write(formatted);
|
file_helper_.write(formatted);
|
||||||
|
|
||||||
// Do the cleaning ony at the end because it might throw on failure.
|
// Do the cleaning only at the end because it might throw on failure.
|
||||||
if (should_rotate && max_files_ > 0)
|
if (should_rotate && max_files_ > 0)
|
||||||
{
|
{
|
||||||
delete_old_();
|
delete_old_();
|
||||||
@@ -108,6 +126,29 @@ protected:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void init_filenames_q_()
|
||||||
|
{
|
||||||
|
using details::os::path_exists;
|
||||||
|
|
||||||
|
filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
|
||||||
|
std::vector<filename_t> filenames;
|
||||||
|
auto now = log_clock::now();
|
||||||
|
while (filenames.size() < max_files_)
|
||||||
|
{
|
||||||
|
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||||
|
if (!path_exists(filename))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
filenames.emplace_back(filename);
|
||||||
|
now -= std::chrono::hours(24);
|
||||||
|
}
|
||||||
|
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter)
|
||||||
|
{
|
||||||
|
filenames_q_.push_back(std::move(*iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
tm now_tm(log_clock::time_point tp)
|
tm now_tm(log_clock::time_point tp)
|
||||||
{
|
{
|
||||||
time_t tnow = log_clock::to_time_t(tp);
|
time_t tnow = log_clock::to_time_t(tp);
|
||||||
@@ -136,7 +177,7 @@ private:
|
|||||||
using details::os::filename_to_str;
|
using details::os::filename_to_str;
|
||||||
using details::os::remove_if_exists;
|
using details::os::remove_if_exists;
|
||||||
|
|
||||||
filename_t current_file = filename();
|
filename_t current_file = file_helper_.filename();
|
||||||
if (filenames_q_.full())
|
if (filenames_q_.full())
|
||||||
{
|
{
|
||||||
auto old_filename = std::move(filenames_q_.front());
|
auto old_filename = std::move(filenames_q_.front());
|
||||||
@@ -145,7 +186,7 @@ private:
|
|||||||
if (!ok)
|
if (!ok)
|
||||||
{
|
{
|
||||||
filenames_q_.push_back(std::move(current_file));
|
filenames_q_.push_back(std::move(current_file));
|
||||||
SPDLOG_THROW(spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno));
|
throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
filenames_q_.push_back(std::move(current_file));
|
filenames_q_.push_back(std::move(current_file));
|
||||||
@@ -163,6 +204,8 @@ private:
|
|||||||
|
|
||||||
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
using daily_file_sink_mt = daily_file_sink<std::mutex>;
|
||||||
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
||||||
|
using daily_file_format_sink_mt = daily_file_sink<std::mutex, daily_filename_format_calculator>;
|
||||||
|
using daily_file_format_sink_st = daily_file_sink<details::null_mutex, daily_filename_format_calculator>;
|
||||||
|
|
||||||
} // namespace sinks
|
} // namespace sinks
|
||||||
|
|
||||||
@@ -171,15 +214,29 @@ using daily_file_sink_st = daily_file_sink<details::null_mutex>;
|
|||||||
//
|
//
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> daily_logger_mt(
|
inline std::shared_ptr<logger> daily_logger_mt(
|
||||||
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
|
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate);
|
return Factory::template create<sinks::daily_file_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> daily_logger_format_mt(
|
||||||
|
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::daily_file_format_sink_mt>(logger_name, filename, hour, minute, truncate, max_files);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Factory = spdlog::synchronous_factory>
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
inline std::shared_ptr<logger> daily_logger_st(
|
inline std::shared_ptr<logger> daily_logger_st(
|
||||||
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false)
|
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
|
||||||
{
|
{
|
||||||
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate);
|
return Factory::template create<sinks::daily_file_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> daily_logger_format_st(
|
||||||
|
const std::string &logger_name, const filename_t &filename, int hour = 0, int minute = 0, bool truncate = false, uint16_t max_files = 0)
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::daily_file_format_sink_st>(logger_name, filename, hour, minute, truncate, max_files);
|
||||||
}
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "base_sink.h"
|
#include "base_sink.h"
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/details/pattern_formatter.h"
|
#include <spdlog/pattern_formatter.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -24,6 +24,10 @@ class dist_sink : public base_sink<Mutex>
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
dist_sink() = default;
|
dist_sink() = default;
|
||||||
|
explicit dist_sink(std::vector<std::shared_ptr<sink>> sinks)
|
||||||
|
: sinks_(sinks)
|
||||||
|
{}
|
||||||
|
|
||||||
dist_sink(const dist_sink &) = delete;
|
dist_sink(const dist_sink &) = delete;
|
||||||
dist_sink &operator=(const dist_sink &) = delete;
|
dist_sink &operator=(const dist_sink &) = delete;
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "dist_sink.h"
|
#include "dist_sink.h"
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/details/log_msg.h"
|
#include <spdlog/details/log_msg.h>
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -16,7 +17,7 @@
|
|||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// #include "spdlog/sinks/dup_filter_sink.h"
|
// #include <spdlog/sinks/dup_filter_sink.h>
|
||||||
//
|
//
|
||||||
// int main() {
|
// int main() {
|
||||||
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
|
// auto dup_filter = std::make_shared<dup_filter_sink_st>(std::chrono::seconds(5));
|
||||||
@@ -33,10 +34,6 @@
|
|||||||
// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages..
|
// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages..
|
||||||
// [2019-06-25 17:50:56.512] [logger] [info] Different Hello
|
// [2019-06-25 17:50:56.512] [logger] [info] Different Hello
|
||||||
|
|
||||||
#ifdef SPDLOG_NO_DATETIME
|
|
||||||
#error "spdlog::sinks::dup_filter_sink: cannot work when SPDLOG_NO_DATETIME is defined"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
template<typename Mutex>
|
template<typename Mutex>
|
||||||
@@ -66,10 +63,13 @@ protected:
|
|||||||
// log the "skipped.." message
|
// log the "skipped.." message
|
||||||
if (skip_counter_ > 0)
|
if (skip_counter_ > 0)
|
||||||
{
|
{
|
||||||
memory_buf_t buf;
|
char buf[64];
|
||||||
fmt::format_to(buf, "Skipped {} duplicate messages..", skip_counter_);
|
auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", static_cast<unsigned>(skip_counter_));
|
||||||
details::log_msg skipped_msg{msg.logger_name, msg.level, string_view_t{buf.data(), buf.size()}};
|
if (msg_size > 0 && static_cast<size_t>(msg_size) < sizeof(buf))
|
||||||
dist_sink<Mutex>::sink_it_(skipped_msg);
|
{
|
||||||
|
details::log_msg skipped_msg{msg.logger_name, level::info, string_view_t{buf, static_cast<size_t>(msg_size)}};
|
||||||
|
dist_sink<Mutex>::sink_it_(skipped_msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// log current message
|
// log current message
|
||||||
|
|||||||
194
include/spdlog/sinks/hourly_file_sink.h
Normal file
194
include/spdlog/sinks/hourly_file_sink.h
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
#include <spdlog/details/file_helper.h>
|
||||||
|
#include <spdlog/details/null_mutex.h>
|
||||||
|
#include <spdlog/fmt/fmt.h>
|
||||||
|
#include <spdlog/sinks/base_sink.h>
|
||||||
|
#include <spdlog/details/os.h>
|
||||||
|
#include <spdlog/details/circular_q.h>
|
||||||
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <ctime>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace sinks {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generator of Hourly log file names in format basename.YYYY-MM-DD-HH.ext
|
||||||
|
*/
|
||||||
|
struct hourly_filename_calculator
|
||||||
|
{
|
||||||
|
// Create filename for the form basename.YYYY-MM-DD-H
|
||||||
|
static filename_t calc_filename(const filename_t &filename, const tm &now_tm)
|
||||||
|
{
|
||||||
|
filename_t basename, ext;
|
||||||
|
std::tie(basename, ext) = details::file_helper::split_by_extension(filename);
|
||||||
|
return fmt::format(SPDLOG_FILENAME_T("{}_{:04d}{:02d}{:02d}_{:02d}{}"), basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1,
|
||||||
|
now_tm.tm_mday, now_tm.tm_hour, ext);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Rotating file sink based on time.
|
||||||
|
* If truncate != false , the created file will be truncated.
|
||||||
|
* If max_files > 0, retain only the last max_files and delete previous.
|
||||||
|
*/
|
||||||
|
template<typename Mutex, typename FileNameCalc = hourly_filename_calculator>
|
||||||
|
class hourly_file_sink final : public base_sink<Mutex>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
// create hourly file sink which rotates on given time
|
||||||
|
hourly_file_sink(filename_t base_filename, bool truncate = false, uint16_t max_files = 0)
|
||||||
|
: base_filename_(std::move(base_filename))
|
||||||
|
, truncate_(truncate)
|
||||||
|
, max_files_(max_files)
|
||||||
|
, filenames_q_()
|
||||||
|
{
|
||||||
|
auto now = log_clock::now();
|
||||||
|
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||||
|
file_helper_.open(filename, truncate_);
|
||||||
|
rotation_tp_ = next_rotation_tp_();
|
||||||
|
|
||||||
|
if (max_files_ > 0)
|
||||||
|
{
|
||||||
|
init_filenames_q_();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
filename_t filename()
|
||||||
|
{
|
||||||
|
std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
|
||||||
|
return file_helper_.filename();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void sink_it_(const details::log_msg &msg) override
|
||||||
|
{
|
||||||
|
auto time = msg.time;
|
||||||
|
bool should_rotate = time >= rotation_tp_;
|
||||||
|
if (should_rotate)
|
||||||
|
{
|
||||||
|
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time));
|
||||||
|
file_helper_.open(filename, truncate_);
|
||||||
|
rotation_tp_ = next_rotation_tp_();
|
||||||
|
}
|
||||||
|
memory_buf_t formatted;
|
||||||
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
|
file_helper_.write(formatted);
|
||||||
|
|
||||||
|
// Do the cleaning only at the end because it might throw on failure.
|
||||||
|
if (should_rotate && max_files_ > 0)
|
||||||
|
{
|
||||||
|
delete_old_();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush_() override
|
||||||
|
{
|
||||||
|
file_helper_.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void init_filenames_q_()
|
||||||
|
{
|
||||||
|
using details::os::path_exists;
|
||||||
|
|
||||||
|
filenames_q_ = details::circular_q<filename_t>(static_cast<size_t>(max_files_));
|
||||||
|
std::vector<filename_t> filenames;
|
||||||
|
auto now = log_clock::now();
|
||||||
|
while (filenames.size() < max_files_)
|
||||||
|
{
|
||||||
|
auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now));
|
||||||
|
if (!path_exists(filename))
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
filenames.emplace_back(filename);
|
||||||
|
now -= std::chrono::hours(1);
|
||||||
|
}
|
||||||
|
for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter)
|
||||||
|
{
|
||||||
|
filenames_q_.push_back(std::move(*iter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tm now_tm(log_clock::time_point tp)
|
||||||
|
{
|
||||||
|
time_t tnow = log_clock::to_time_t(tp);
|
||||||
|
return spdlog::details::os::localtime(tnow);
|
||||||
|
}
|
||||||
|
|
||||||
|
log_clock::time_point next_rotation_tp_()
|
||||||
|
{
|
||||||
|
auto now = log_clock::now();
|
||||||
|
tm date = now_tm(now);
|
||||||
|
date.tm_min = 0;
|
||||||
|
date.tm_sec = 0;
|
||||||
|
auto rotation_time = log_clock::from_time_t(std::mktime(&date));
|
||||||
|
if (rotation_time > now)
|
||||||
|
{
|
||||||
|
return rotation_time;
|
||||||
|
}
|
||||||
|
return {rotation_time + std::chrono::hours(1)};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the file N rotations ago.
|
||||||
|
// Throw spdlog_ex on failure to delete the old file.
|
||||||
|
void delete_old_()
|
||||||
|
{
|
||||||
|
using details::os::filename_to_str;
|
||||||
|
using details::os::remove_if_exists;
|
||||||
|
|
||||||
|
filename_t current_file = file_helper_.filename();
|
||||||
|
if (filenames_q_.full())
|
||||||
|
{
|
||||||
|
auto old_filename = std::move(filenames_q_.front());
|
||||||
|
filenames_q_.pop_front();
|
||||||
|
bool ok = remove_if_exists(old_filename) == 0;
|
||||||
|
if (!ok)
|
||||||
|
{
|
||||||
|
filenames_q_.push_back(std::move(current_file));
|
||||||
|
SPDLOG_THROW(spdlog_ex("Failed removing hourly file " + filename_to_str(old_filename), errno));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
filenames_q_.push_back(std::move(current_file));
|
||||||
|
}
|
||||||
|
|
||||||
|
filename_t base_filename_;
|
||||||
|
log_clock::time_point rotation_tp_;
|
||||||
|
details::file_helper file_helper_;
|
||||||
|
bool truncate_;
|
||||||
|
uint16_t max_files_;
|
||||||
|
details::circular_q<filename_t> filenames_q_;
|
||||||
|
};
|
||||||
|
|
||||||
|
using hourly_file_sink_mt = hourly_file_sink<std::mutex>;
|
||||||
|
using hourly_file_sink_st = hourly_file_sink<details::null_mutex>;
|
||||||
|
|
||||||
|
} // namespace sinks
|
||||||
|
|
||||||
|
//
|
||||||
|
// factory functions
|
||||||
|
//
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> hourly_logger_mt(
|
||||||
|
const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0)
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::hourly_file_sink_mt>(logger_name, filename, truncate, max_files);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> hourly_logger_st(
|
||||||
|
const std::string &logger_name, const filename_t &filename, bool truncate = false, uint16_t max_files = 0)
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::hourly_file_sink_st>(logger_name, filename, truncate, max_files);
|
||||||
|
}
|
||||||
|
} // namespace spdlog
|
||||||
98
include/spdlog/sinks/mongo_sink.h
Normal file
98
include/spdlog/sinks/mongo_sink.h
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//
|
||||||
|
// Custom sink for mongodb
|
||||||
|
// Building and using requires mongocxx library.
|
||||||
|
// For building mongocxx library check the url below
|
||||||
|
// http://mongocxx.org/mongocxx-v3/installation/
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/details/log_msg.h"
|
||||||
|
#include "spdlog/sinks/base_sink.h"
|
||||||
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
|
#include <bsoncxx/builder/stream/document.hpp>
|
||||||
|
#include <bsoncxx/types.hpp>
|
||||||
|
#include <bsoncxx/view_or_value.hpp>
|
||||||
|
|
||||||
|
#include <mongocxx/client.hpp>
|
||||||
|
#include <mongocxx/instance.hpp>
|
||||||
|
#include <mongocxx/uri.hpp>
|
||||||
|
|
||||||
|
namespace spdlog {
|
||||||
|
namespace sinks {
|
||||||
|
template<typename Mutex>
|
||||||
|
class mongo_sink : public base_sink<Mutex>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
mongo_sink(const std::string &db_name, const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client_ = spdlog::details::make_unique<mongocxx::client>(mongocxx::uri{uri});
|
||||||
|
db_name_ = db_name;
|
||||||
|
coll_name_ = collection_name;
|
||||||
|
}
|
||||||
|
catch (const std::exception)
|
||||||
|
{
|
||||||
|
throw spdlog_ex("Error opening database");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~mongo_sink()
|
||||||
|
{
|
||||||
|
flush_();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void sink_it_(const details::log_msg &msg) override
|
||||||
|
{
|
||||||
|
using bsoncxx::builder::stream::document;
|
||||||
|
using bsoncxx::builder::stream::finalize;
|
||||||
|
|
||||||
|
if (client_ != nullptr)
|
||||||
|
{
|
||||||
|
auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" << level::to_string_view(msg.level).data()
|
||||||
|
<< "message" << std::string(msg.payload.begin(), msg.payload.end()) << "logger_name"
|
||||||
|
<< std::string(msg.logger_name.begin(), msg.logger_name.end()) << "thread_id"
|
||||||
|
<< static_cast<int>(msg.thread_id) << finalize;
|
||||||
|
client_->database(db_name_).collection(coll_name_).insert_one(doc.view());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush_() override {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static mongocxx::instance instance_;
|
||||||
|
std::string db_name_;
|
||||||
|
std::string coll_name_;
|
||||||
|
std::unique_ptr<mongocxx::client> client_ = nullptr;
|
||||||
|
};
|
||||||
|
mongocxx::instance mongo_sink<std::mutex>::instance_{};
|
||||||
|
|
||||||
|
#include "spdlog/details/null_mutex.h"
|
||||||
|
#include <mutex>
|
||||||
|
using mongo_sink_mt = mongo_sink<std::mutex>;
|
||||||
|
using mongo_sink_st = mongo_sink<spdlog::details::null_mutex>;
|
||||||
|
|
||||||
|
} // namespace sinks
|
||||||
|
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> mongo_logger_mt(const std::string &logger_name, const std::string &db_name,
|
||||||
|
const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::mongo_sink_mt>(logger_name, db_name, collection_name, uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger> mongo_logger_st(const std::string &logger_name, const std::string &db_name,
|
||||||
|
const std::string &collection_name, const std::string &uri = "mongodb://localhost:27017")
|
||||||
|
{
|
||||||
|
return Factory::template create<sinks::mongo_sink_st>(logger_name, db_name, collection_name, uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace spdlog
|
||||||
@@ -5,13 +5,14 @@
|
|||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
# include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
# include <spdlog/sinks/base_sink.h>
|
||||||
|
|
||||||
#include <winbase.h>
|
# include <mutex>
|
||||||
|
# include <string>
|
||||||
|
|
||||||
#include <mutex>
|
// Avoid including windows.h (https://stackoverflow.com/a/30741042)
|
||||||
#include <string>
|
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString);
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
@@ -22,12 +23,11 @@ template<typename Mutex>
|
|||||||
class msvc_sink : public base_sink<Mutex>
|
class msvc_sink : public base_sink<Mutex>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit msvc_sink() {}
|
msvc_sink() = default;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void sink_it_(const details::log_msg &msg) override
|
void sink_it_(const details::log_msg &msg) override
|
||||||
{
|
{
|
||||||
|
|
||||||
memory_buf_t formatted;
|
memory_buf_t formatted;
|
||||||
base_sink<Mutex>::formatter_->format(msg, formatted);
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
OutputDebugStringA(fmt::to_string(formatted).c_str());
|
OutputDebugStringA(fmt::to_string(formatted).c_str());
|
||||||
|
|||||||
@@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
#include <spdlog/sinks/base_sink.h>
|
||||||
#include "spdlog/details/synchronous_factory.h"
|
#include <spdlog/details/synchronous_factory.h>
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "spdlog/details/null_mutex.h"
|
#include <spdlog/details/null_mutex.h>
|
||||||
#include "spdlog/sinks/base_sink.h"
|
#include <spdlog/sinks/base_sink.h>
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|||||||
93
include/spdlog/sinks/qt_sinks.h
Normal file
93
include/spdlog/sinks/qt_sinks.h
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
// Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors.
|
||||||
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
//
|
||||||
|
// Custom sink for QPlainTextEdit or QTextEdit and its childs(QTextBrowser...
|
||||||
|
// etc) Building and using requires Qt library.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "spdlog/common.h"
|
||||||
|
#include "spdlog/details/log_msg.h"
|
||||||
|
#include "spdlog/details/synchronous_factory.h"
|
||||||
|
#include "spdlog/sinks/base_sink.h"
|
||||||
|
|
||||||
|
#include <QTextEdit>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
|
//
|
||||||
|
// qt_sink class
|
||||||
|
//
|
||||||
|
namespace spdlog {
|
||||||
|
namespace sinks {
|
||||||
|
template <typename Mutex> class qt_sink : public base_sink<Mutex> {
|
||||||
|
public:
|
||||||
|
qt_sink(QObject *qt_object = nullptr, const std::string &meta_method = "") {
|
||||||
|
qt_object_ = qt_object;
|
||||||
|
meta_method_ = meta_method;
|
||||||
|
}
|
||||||
|
|
||||||
|
~qt_sink() { flush_(); }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void sink_it_(const details::log_msg &msg) override {
|
||||||
|
memory_buf_t formatted;
|
||||||
|
base_sink<Mutex>::formatter_->format(msg, formatted);
|
||||||
|
string_view_t str = string_view_t(formatted.data(), formatted.size());
|
||||||
|
QMetaObject::invokeMethod(qt_object_, meta_method_.c_str(), Qt::AutoConnection,
|
||||||
|
Q_ARG(QString, QString::fromUtf8(str.data(), static_cast<int>(str.size())).trimmed()));
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush_() override {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
QObject *qt_object_ = nullptr;
|
||||||
|
std::string meta_method_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#include "spdlog/details/null_mutex.h"
|
||||||
|
#include <mutex>
|
||||||
|
using qt_sink_mt = qt_sink<std::mutex>;
|
||||||
|
using qt_sink_st = qt_sink<spdlog::details::null_mutex>;
|
||||||
|
} // namespace sinks
|
||||||
|
|
||||||
|
//
|
||||||
|
// Factory functions
|
||||||
|
//
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_mt(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") {
|
||||||
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_st(const std::string &logger_name, QTextEdit* qt_object, const std::string &meta_method = "append") {
|
||||||
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_mt(const std::string &logger_name, QPlainTextEdit* qt_object , const std::string &meta_method = "appendPlainText") {
|
||||||
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_st(const std::string &logger_name, QPlainTextEdit* qt_object, const std::string &meta_method = "appendPlainText") {
|
||||||
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_mt(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) {
|
||||||
|
return Factory::template create<sinks::qt_sink_mt>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Factory = spdlog::synchronous_factory>
|
||||||
|
inline std::shared_ptr<logger>
|
||||||
|
qt_logger_st(const std::string &logger_name, QObject* qt_object, const std::string &meta_method) {
|
||||||
|
return Factory::template create<sinks::qt_sink_st>(logger_name, qt_object, meta_method);
|
||||||
|
}
|
||||||
|
} // namespace spdlog
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user