├── .editorconfig ├── .github └── workflows │ └── test.yml ├── .gitignore ├── .rustfmt.toml ├── .travis.yml ├── .vimrc ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── LICENSE-MPL2 ├── Makefile ├── README.md ├── build.rs └── lib.rs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.rs] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 4 7 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | # We use `actions-rs` for most of our actions 2 | # 3 | # This file is for the main tests, it is based on test.yml in the main slog repo. 4 | # If we want to add clippy & rustfmt, they should be separate workflows. 5 | on: [push, pull_request] 6 | name: Cargo Test 7 | 8 | env: 9 | CARGO_TERM_COLOR: always 10 | # has a history of occasional bugs (especially on old versions) 11 | # 12 | # the ci is free so we might as well use it ;) 13 | CARGO_INCREMENTAL: 0 14 | 15 | 16 | # Tested versions: 17 | # 1. stable 18 | # 2. nightly 19 | # 3. Minimum Supported Rust Version (MSRV) 20 | 21 | jobs: 22 | test: 23 | # Only run on PRs if the source branch is on someone else's repo 24 | if: ${{ github.event_name != 'pull_request' || github.repository != github.event.pull_request.head.repo.full_name }} 25 | 26 | runs-on: ubuntu-latest 27 | strategy: 28 | fail-fast: false # Even if one job fails we still want to see the other ones 29 | matrix: 30 | # 1.59 is MSRV 31 | rust: [1.59, stable, nightly] 32 | # NOTE: Features to test must be specified manually. They are applied to all versions separately. 33 | # 34 | # This has the advantage of being more flexibile and thorough 35 | # This has the disadvantage of being more vebrose 36 | # 37 | # Specific feature combos can be overridden per-version with 'include' and 'ecclude' 38 | features: ["", "nested-values", "dynamic-keys", "nested-values dynamic-keys"] 39 | 40 | steps: 41 | - uses: actions/checkout@v2 42 | - uses: actions-rs/toolchain@v1 43 | with: 44 | toolchain: ${{ matrix.rust }} 45 | override: true 46 | - name: Check 47 | # A failing `cargo check` always ends the build 48 | run: | 49 | cargo check --verbose --features "${{ matrix.features }}" 50 | # A failing `cargo check` always fails the build 51 | continue-on-error: false 52 | - name: Test 53 | run: | 54 | cargo test --verbose --features "${{ matrix.features }}" 55 | 56 | # NOTE: Upstream slog has a hack here for continue-on-error 57 | # 58 | # We don't appear to need it. 59 | # continue-on-error: ${{ matrix.features == '' }} 60 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | /tags 4 | /.cargo 5 | /rusty-tags.vi 6 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | max_width = 80 2 | reorder_imports = true 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | - beta 5 | - nightly 6 | 7 | script: 8 | - make all 9 | - make travistest 10 | - if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then make bench ; fi 11 | 12 | env: 13 | global: 14 | - RUST_BACKTRACE=1 15 | matrix: 16 | - 17 | - RELEASE=true 18 | 19 | notifications: 20 | webhooks: 21 | urls: 22 | - https://webhooks.gitter.im/e/6d8e17dd2fa83b143168 23 | on_success: change # options: [always|never|change] default: always 24 | on_failure: change # options: [always|never|change] default: always 25 | on_start: false # default: false 26 | -------------------------------------------------------------------------------- /.vimrc: -------------------------------------------------------------------------------- 1 | let g:rustfmt_autosave = 1 2 | 3 | autocmd BufEnter *.rs setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab textwidth=80 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/). 6 | 7 | ## [Unreleased] 8 | 9 | * Define minimum supported rust version. 10 | * Setup Github Actions. 11 | * Fixup some minor typos in CHANGELOG.md, including an incorrect release date for 2.8.0. 12 | 13 | ## 2.8.0 - 2023-08-26 14 | 15 | * Call of deprecated `err.description()` replaced with `err.to_string()`. 16 | * Avoided all catchable panics in async drain thread. 17 | 18 | ## 2.7.0 - 2021-07-29 19 | 20 | * Fix license field to be a valid SPDX expression 21 | * Support u128/i128 22 | 23 | ## 2.6.0 - 2021-01-12 24 | 25 | * Update crossbeam-channel to 0.5 26 | * Expose the serialization capabilities 27 | 28 | ## 2.5.0 - 2020-01-29 29 | 30 | * Fix compilation warnings 31 | * Upgrade `crossbeam-channel` 32 | 33 | ## 2.4.0 - 2020-01-29 34 | 35 | * Do not join() when dropping AsyncCore/AsyncGuard from worker thread 36 | * Upgrade to thread_local v1 37 | * Replace the std mpsc channel with a crossbeam channel 38 | * Replace the std mpsc channels with a crossbeam channel 39 | * add missing LICENSE files 40 | 41 | ## 2.3.0 - 2018-04-04 42 | 43 | * Configurable overflow strategy (can now block or drop the messages silently). 44 | * Configurable name of the background thread. 45 | 46 | ## 2.2.0 - 2017-07-23 47 | 48 | * Experimental support for `nested-values` and `dynamic-keys`. **Note**: 49 | consider unstable. 50 | 51 | ## 2.1.0 - 2017-07-23 52 | 53 | * Relicense under MPL/Apache/MIT 54 | * Added `AsyncGuard` 55 | * `build` to be deprecated in the future 56 | 57 | 58 | ## 2.0.1 - 2017-04-11 59 | ### Fixed 60 | 61 | * Don't reverse the order of key-value pairs 62 | 63 | ## 2.0.0 - 2017-04-11 64 | ### Changed 65 | * Update to slog to stable release 66 | * Minor improvements around overflow documentation 67 | 68 | ## 2.0.0-3.0 - 2017-03-25 69 | 70 | * Bump slog version to 2.0.0-3.0 71 | 72 | ## 2.0.0-2.0 - 2017-03-11 73 | 74 | * Bump slog version to 2.0.0-2.0 75 | 76 | ## 0.2.0-alpha2 - 2017-02-23 77 | 78 | * Update to latest `slog` version 79 | * Misc changes 80 | 81 | ## 0.2.0-alpha1 - 2017-02-19 82 | ### Changed 83 | 84 | * Fork from `slog-extra` to become a part of `slog v2` 85 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "slog-async" 3 | version = "2.8.0" 4 | authors = ["Dawid Ciężarkiewicz "] 5 | description = "Asynchronous drain for slog-rs" 6 | keywords = ["slog", "logging", "log", "asynchronous"] 7 | categories = ["development-tools::debugging"] 8 | license = "MPL-2.0 OR MIT OR Apache-2.0" 9 | documentation = "https://docs.rs/slog-async" 10 | homepage = "https://github.com/slog-rs/slog" 11 | repository = "https://github.com/slog-rs/async" 12 | readme = "README.md" 13 | rust-version = "1.59.0" 14 | 15 | [features] 16 | nested-values = ["slog/nested-values"] 17 | dynamic-keys = ["slog/dynamic-keys"] 18 | default = [] 19 | 20 | [lib] 21 | path = "lib.rs" 22 | 23 | [dependencies] 24 | slog = "2.1" 25 | thread_local = "1" 26 | take_mut = "0.2.0" 27 | crossbeam-channel = "0.5" 28 | 29 | [package.metadata.docs.rs] 30 | features = ["nested-values", "dynamic-keys"] 31 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 The Rust Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /LICENSE-MPL2: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PKG_NAME=$(shell grep name Cargo.toml | head -n 1 | awk -F \" '{print $$2}') 2 | DOCS_DEFAULT_MODULE=$(subst -,_,$(PKG_NAME)) 3 | ifeq (, $(shell which cargo-check 2> /dev/null)) 4 | DEFAULT_TARGET=build 5 | else 6 | DEFAULT_TARGET=build 7 | endif 8 | 9 | default: $(DEFAULT_TARGET) 10 | 11 | ALL_TARGETS += build $(EXAMPLES) test doc 12 | ifneq ($(RELEASE),) 13 | $(info RELEASE BUILD: $(PKG_NAME)) 14 | CARGO_FLAGS += --release 15 | else 16 | $(info DEBUG BUILD: $(PKG_NAME); use `RELEASE=true make [args]` for release build) 17 | endif 18 | 19 | EXAMPLES = $(shell cd examples 2>/dev/null && ls *.rs 2>/dev/null | sed -e 's/.rs$$//g' ) 20 | 21 | all: $(ALL_TARGETS) 22 | 23 | .PHONY: run test build doc clean clippy 24 | run test build clean: 25 | cargo $@ $(CARGO_FLAGS) 26 | 27 | check: 28 | $(info Running check; use `make build` to actually build) 29 | cargo $@ $(CARGO_FLAGS) 30 | 31 | clippy: 32 | cargo build --features clippy 33 | 34 | .PHONY: bench 35 | bench: 36 | cargo $@ $(filter-out --release,$(CARGO_FLAGS)) 37 | 38 | .PHONY: travistest 39 | travistest: test 40 | 41 | .PHONY: longtest 42 | longtest: 43 | @echo "Running longtest. Press Ctrl+C to stop at any time" 44 | @sleep 2 45 | @i=0; while i=$$((i + 1)) && echo "Iteration $$i" && make test ; do :; done 46 | 47 | .PHONY: $(EXAMPLES) 48 | $(EXAMPLES): 49 | cargo build --example $@ $(CARGO_FLAGS) 50 | 51 | .PHONY: doc 52 | doc: FORCE 53 | cargo doc 54 | 55 | .PHONY: publishdoc 56 | publishdoc: 57 | rm -rf target/doc 58 | make doc 59 | echo '' > target/doc/index.html 60 | ghp-import -n target/doc 61 | git push -f origin gh-pages 62 | 63 | .PHONY: docview 64 | docview: doc 65 | xdg-open target/doc/$(PKG_NAME)/index.html 66 | 67 | .PHONY: FORCE 68 | FORCE: 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # slog-async - Asynchronous drain for [slog-rs][slog-rs] 2 | 3 |

4 | 5 | Travis CI Build Status 6 | 7 | 8 | 9 | slog-async on crates.io 10 | 11 | 12 | 13 | slog-rs Gitter Chat 14 | 15 | 16 | 17 | slog-rs Dependency Status 18 | 19 |

20 | 21 | For more information, help, to report issues etc. see [slog-rs][slog-rs]. 22 | 23 | Note: Unlike other logging solutions `slog-rs` does not have a hardcoded async 24 | logging implementation. This crate is just a reasonable reference 25 | implementation. It should have good performance and work well in most use 26 | cases. See documentation and implementation for more details. 27 | 28 | It's relatively easy to implement custom `slog-rs` async logging. Feel free to 29 | use this one as a starting point. 30 | 31 | [slog-rs]: //github.com/slog-rs/slog 32 | -------------------------------------------------------------------------------- /build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::process::Command; 3 | use std::str::{self, FromStr}; 4 | 5 | // This build script is adapted from serde. 6 | // See https://github.com/serde-rs/serde/blob/9c39115f827170f7adbdfa4115f5916c5979393c/serde/build.rs 7 | fn main() { 8 | let minor = match rustc_minor_version() { 9 | Some(minor) => minor, 10 | None => return, 11 | }; 12 | 13 | let target = env::var("TARGET").unwrap(); 14 | let emscripten = target == "asmjs-unknown-emscripten" || target == "wasm32-unknown-emscripten"; 15 | 16 | // 128-bit integers stabilized in Rust 1.26: 17 | // https://blog.rust-lang.org/2018/05/10/Rust-1.26.html 18 | // 19 | // Disabled on Emscripten targets as Emscripten doesn't 20 | // currently support integers larger than 64 bits. 21 | if minor >= 26 && !emscripten { 22 | println!("cargo:rustc-cfg=integer128"); 23 | } 24 | } 25 | 26 | fn rustc_minor_version() -> Option { 27 | let rustc = match env::var_os("RUSTC") { 28 | Some(rustc) => rustc, 29 | None => return None, 30 | }; 31 | 32 | let output = match Command::new(rustc).arg("--version").output() { 33 | Ok(output) => output, 34 | Err(_) => return None, 35 | }; 36 | 37 | let version = match str::from_utf8(&output.stdout) { 38 | Ok(version) => version, 39 | Err(_) => return None, 40 | }; 41 | 42 | let mut pieces = version.split('.'); 43 | if pieces.next() != Some("rustc 1") { 44 | return None; 45 | } 46 | 47 | let next = match pieces.next() { 48 | Some(next) => next, 49 | None => return None, 50 | }; 51 | 52 | u32::from_str(next).ok() 53 | } 54 | -------------------------------------------------------------------------------- /lib.rs: -------------------------------------------------------------------------------- 1 | // {{{ Crate docs 2 | //! # Async drain for slog-rs 3 | //! 4 | //! `slog-rs` is an ecosystem of reusable components for structured, extensible, 5 | //! composable logging for Rust. 6 | //! 7 | //! `slog-async` allows building `Drain`s that offload processing to another 8 | //! thread. Typically, serialization and IO operations are slow enough that 9 | //! they make logging hinder the performance of the main code. Sending log 10 | //! records to another thread is much faster (ballpark of 100ns). 11 | //! 12 | //! Note: Unlike other logging solutions, `slog-rs` does not have a hardcoded 13 | //! async logging implementation. This crate is just a reasonable reference 14 | //! implementation. It should have good performance and work well in most use 15 | //! cases. See the documentation and implementation for more details. 16 | //! 17 | //! It's relatively easy to implement your own `slog-rs` async logging. Feel 18 | //! free to use this one as a starting point. 19 | //! 20 | //! ## Beware of `std::process::exit` 21 | //! 22 | //! When using `std::process::exit` to terminate a process with an exit code, 23 | //! it is important to notice that destructors will not be called. This matters 24 | //! for `slog_async` as it **prevents flushing** of the async drain and 25 | //! **discards messages** that are not yet written. 26 | //! 27 | //! A way around this issue is encapsulate the construction of the logger into 28 | //! its own function that returns before `std::process::exit` is called. 29 | //! 30 | //! ``` 31 | //! // ... 32 | //! fn main() { 33 | //! let exit_code = run(); // logger gets flushed as `run()` returns. 34 | //! # if false { 35 | //! # // this must not run or it'll end the doctest 36 | //! std::process::exit(exit_code) 37 | //! # } 38 | //! } 39 | //! 40 | //! fn run() -> i32 { 41 | //! // initialize the logger 42 | //! 43 | //! // ... do your thing ... 44 | //! 45 | //! 1 // exit code to return 46 | //! } 47 | //! ``` 48 | // }}} 49 | 50 | // {{{ Imports & meta 51 | #![warn(missing_docs)] 52 | 53 | #[macro_use] 54 | extern crate slog; 55 | extern crate crossbeam_channel; 56 | extern crate take_mut; 57 | extern crate thread_local; 58 | 59 | use crossbeam_channel::Sender; 60 | 61 | use slog::{BorrowedKV, Level, Record, RecordStatic, SingleKV, KV}; 62 | use slog::{Key, OwnedKVList, Serializer}; 63 | 64 | use slog::Drain; 65 | use std::fmt; 66 | use std::sync; 67 | use std::{io, thread}; 68 | 69 | use std::sync::atomic::AtomicUsize; 70 | use std::sync::atomic::Ordering; 71 | use std::sync::Mutex; 72 | use take_mut::take; 73 | 74 | use std::panic::{catch_unwind, AssertUnwindSafe}; 75 | // }}} 76 | 77 | // {{{ Serializer 78 | struct ToSendSerializer { 79 | kv: Box, 80 | } 81 | 82 | impl ToSendSerializer { 83 | fn new() -> Self { 84 | ToSendSerializer { kv: Box::new(()) } 85 | } 86 | 87 | fn finish(self) -> Box { 88 | self.kv 89 | } 90 | } 91 | 92 | impl Serializer for ToSendSerializer { 93 | fn emit_bool(&mut self, key: Key, val: bool) -> slog::Result { 94 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 95 | Ok(()) 96 | } 97 | fn emit_unit(&mut self, key: Key) -> slog::Result { 98 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, ())))); 99 | Ok(()) 100 | } 101 | fn emit_none(&mut self, key: Key) -> slog::Result { 102 | let val: Option<()> = None; 103 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 104 | Ok(()) 105 | } 106 | fn emit_char(&mut self, key: Key, val: char) -> slog::Result { 107 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 108 | Ok(()) 109 | } 110 | fn emit_u8(&mut self, key: Key, val: u8) -> slog::Result { 111 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 112 | Ok(()) 113 | } 114 | fn emit_i8(&mut self, key: Key, val: i8) -> slog::Result { 115 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 116 | Ok(()) 117 | } 118 | fn emit_u16(&mut self, key: Key, val: u16) -> slog::Result { 119 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 120 | Ok(()) 121 | } 122 | fn emit_i16(&mut self, key: Key, val: i16) -> slog::Result { 123 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 124 | Ok(()) 125 | } 126 | fn emit_u32(&mut self, key: Key, val: u32) -> slog::Result { 127 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 128 | Ok(()) 129 | } 130 | fn emit_i32(&mut self, key: Key, val: i32) -> slog::Result { 131 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 132 | Ok(()) 133 | } 134 | fn emit_f32(&mut self, key: Key, val: f32) -> slog::Result { 135 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 136 | Ok(()) 137 | } 138 | fn emit_u64(&mut self, key: Key, val: u64) -> slog::Result { 139 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 140 | Ok(()) 141 | } 142 | fn emit_i64(&mut self, key: Key, val: i64) -> slog::Result { 143 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 144 | Ok(()) 145 | } 146 | fn emit_f64(&mut self, key: Key, val: f64) -> slog::Result { 147 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 148 | Ok(()) 149 | } 150 | #[cfg(integer128)] 151 | fn emit_u128(&mut self, key: Key, val: u128) -> slog::Result { 152 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 153 | Ok(()) 154 | } 155 | #[cfg(integer128)] 156 | fn emit_i128(&mut self, key: Key, val: i128) -> slog::Result { 157 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 158 | Ok(()) 159 | } 160 | fn emit_usize(&mut self, key: Key, val: usize) -> slog::Result { 161 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 162 | Ok(()) 163 | } 164 | fn emit_isize(&mut self, key: Key, val: isize) -> slog::Result { 165 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 166 | Ok(()) 167 | } 168 | fn emit_str(&mut self, key: Key, val: &str) -> slog::Result { 169 | let val = val.to_owned(); 170 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 171 | Ok(()) 172 | } 173 | fn emit_arguments( 174 | &mut self, 175 | key: Key, 176 | val: &fmt::Arguments, 177 | ) -> slog::Result { 178 | let val = fmt::format(*val); 179 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 180 | Ok(()) 181 | } 182 | 183 | #[cfg(feature = "nested-values")] 184 | fn emit_serde( 185 | &mut self, 186 | key: Key, 187 | value: &slog::SerdeValue, 188 | ) -> slog::Result { 189 | let val = value.to_sendable(); 190 | take(&mut self.kv, |kv| Box::new((kv, SingleKV(key, val)))); 191 | Ok(()) 192 | } 193 | } 194 | // }}} 195 | 196 | // {{{ Async 197 | // {{{ AsyncError 198 | /// Errors reported by `Async` 199 | #[derive(Debug)] 200 | pub enum AsyncError { 201 | /// Could not send record to worker thread due to full queue 202 | Full, 203 | /// Fatal problem - mutex or channel poisoning issue 204 | Fatal(Box), 205 | } 206 | 207 | impl From> for AsyncError { 208 | fn from(_: crossbeam_channel::TrySendError) -> AsyncError { 209 | AsyncError::Full 210 | } 211 | } 212 | 213 | impl From> for AsyncError { 214 | fn from(_: crossbeam_channel::SendError) -> AsyncError { 215 | AsyncError::Fatal(Box::new(io::Error::new( 216 | io::ErrorKind::BrokenPipe, 217 | "The logger thread terminated", 218 | ))) 219 | } 220 | } 221 | 222 | impl From> for AsyncError { 223 | fn from(err: std::sync::PoisonError) -> AsyncError { 224 | AsyncError::Fatal(Box::new(io::Error::new( 225 | io::ErrorKind::BrokenPipe, 226 | err.to_string(), 227 | ))) 228 | } 229 | } 230 | 231 | /// `AsyncResult` alias 232 | pub type AsyncResult = std::result::Result; 233 | 234 | // }}} 235 | 236 | // {{{ AsyncCore 237 | /// `AsyncCore` builder 238 | pub struct AsyncCoreBuilder 239 | where 240 | D: slog::Drain + Send + 'static, 241 | { 242 | chan_size: usize, 243 | blocking: bool, 244 | drain: D, 245 | thread_name: Option, 246 | } 247 | 248 | impl AsyncCoreBuilder 249 | where 250 | D: slog::Drain + Send + 'static, 251 | { 252 | fn new(drain: D) -> Self { 253 | AsyncCoreBuilder { 254 | chan_size: 128, 255 | blocking: false, 256 | drain, 257 | thread_name: None, 258 | } 259 | } 260 | 261 | /// Configure a name to be used for the background thread. 262 | /// 263 | /// The name must not contain '\0'. 264 | /// 265 | /// # Panics 266 | /// 267 | /// If a name with '\0' is passed. 268 | pub fn thread_name(mut self, name: String) -> Self { 269 | assert!(name.find('\0').is_none(), "Name with \\'0\\' in it passed"); 270 | self.thread_name = Some(name); 271 | self 272 | } 273 | 274 | /// Set channel size used to send logging records to worker thread. When 275 | /// buffer is full `AsyncCore` will start returning `AsyncError::Full` or block, depending on 276 | /// the `blocking` configuration. 277 | pub fn chan_size(mut self, s: usize) -> Self { 278 | self.chan_size = s; 279 | self 280 | } 281 | 282 | /// Should the logging call be blocking if the channel is full? 283 | /// 284 | /// Default is false, in which case it'll return `AsyncError::Full`. 285 | pub fn blocking(mut self, blocking: bool) -> Self { 286 | self.blocking = blocking; 287 | self 288 | } 289 | 290 | fn spawn_thread(self) -> (thread::JoinHandle<()>, Sender) { 291 | let (tx, rx) = crossbeam_channel::bounded(self.chan_size); 292 | let mut builder = thread::Builder::new(); 293 | if let Some(thread_name) = self.thread_name { 294 | builder = builder.name(thread_name); 295 | } 296 | let drain = self.drain; 297 | let join = builder 298 | .spawn(move || { 299 | let drain = AssertUnwindSafe(&drain); 300 | // catching possible unwinding panics which can occur in used inner Drain implementation 301 | if let Err(panic_cause) = catch_unwind(move || loop { 302 | match rx.recv() { 303 | Ok(AsyncMsg::Record(r)) => { 304 | if r.log_to(&*drain).is_err() { 305 | eprintln!("slog-async failed while writing"); 306 | return; 307 | } 308 | } 309 | Ok(AsyncMsg::Finish) => return, 310 | Err(recv_error) => { 311 | eprintln!("slog-async failed while receiving: {recv_error}"); 312 | return; 313 | } 314 | } 315 | }) { 316 | eprintln!("slog-async failed with panic: {panic_cause:?}") 317 | } 318 | }) 319 | .unwrap(); 320 | 321 | (join, tx) 322 | } 323 | 324 | /// Build `AsyncCore` 325 | pub fn build(self) -> AsyncCore { 326 | self.build_no_guard() 327 | } 328 | 329 | /// Build `AsyncCore` 330 | pub fn build_no_guard(self) -> AsyncCore { 331 | let blocking = self.blocking; 332 | let (join, tx) = self.spawn_thread(); 333 | 334 | AsyncCore { 335 | ref_sender: tx, 336 | tl_sender: thread_local::ThreadLocal::new(), 337 | join: Mutex::new(Some(join)), 338 | blocking, 339 | } 340 | } 341 | 342 | /// Build `AsyncCore` with `AsyncGuard` 343 | /// 344 | /// See `AsyncGuard` for more information. 345 | pub fn build_with_guard(self) -> (AsyncCore, AsyncGuard) { 346 | let blocking = self.blocking; 347 | let (join, tx) = self.spawn_thread(); 348 | 349 | ( 350 | AsyncCore { 351 | ref_sender: tx.clone(), 352 | tl_sender: thread_local::ThreadLocal::new(), 353 | join: Mutex::new(None), 354 | blocking, 355 | }, 356 | AsyncGuard { 357 | join: Some(join), 358 | tx, 359 | }, 360 | ) 361 | } 362 | } 363 | 364 | /// Async guard 365 | /// 366 | /// All `Drain`s are reference-counted by every `Logger` that uses them. 367 | /// `Async` drain runs a worker thread and sends a termination (and flushing) 368 | /// message only when being `drop`ed. Because of that it's actually 369 | /// quite easy to have a left-over reference to a `Async` drain, when 370 | /// terminating: especially on `panic`s or similar unwinding event. Typically 371 | /// it's caused be a leftover reference like `Logger` in thread-local variable, 372 | /// global variable, or a thread that is not being joined on. It might be a 373 | /// program bug, but logging should work reliably especially in case of bugs. 374 | /// 375 | /// `AsyncGuard` is a remedy: it will send a flush and termination message to 376 | /// a `Async` worker thread, and wait for it to finish on it's own `drop`. Using it 377 | /// is a simplest way to guarantee log flushing when using `slog_async`. 378 | pub struct AsyncGuard { 379 | // Should always be `Some`. `None` only 380 | // after `drop` 381 | join: Option>, 382 | tx: Sender, 383 | } 384 | 385 | impl Drop for AsyncGuard { 386 | fn drop(&mut self) { 387 | let _err: Result<(), Box> = { 388 | || { 389 | let _ = self.tx.send(AsyncMsg::Finish); 390 | let join = self.join.take().unwrap(); 391 | if join.thread().id() != thread::current().id() { 392 | // See AsyncCore::drop for rationale of this branch. 393 | join.join().map_err(|_| { 394 | io::Error::new( 395 | io::ErrorKind::BrokenPipe, 396 | "Logging thread worker join error", 397 | ) 398 | })?; 399 | } 400 | Ok(()) 401 | } 402 | }(); 403 | } 404 | } 405 | 406 | /// Core of `Async` drain 407 | /// 408 | /// See `Async` for documentation. 409 | /// 410 | /// Wrapping `AsyncCore` allows implementing custom overflow (and other errors) 411 | /// handling strategy. 412 | /// 413 | /// Note: On drop `AsyncCore` waits for it's worker-thread to finish (after 414 | /// handling all previous `Record`s sent to it). If you can't tolerate the 415 | /// delay, make sure you drop it eg. in another thread. 416 | pub struct AsyncCore { 417 | ref_sender: Sender, 418 | tl_sender: thread_local::ThreadLocal>, 419 | join: Mutex>>, 420 | blocking: bool, 421 | } 422 | 423 | impl AsyncCore { 424 | /// New `AsyncCore` with default parameters 425 | pub fn new(drain: D) -> Self 426 | where 427 | D: slog::Drain + Send + 'static, 428 | D: std::panic::RefUnwindSafe, 429 | { 430 | AsyncCoreBuilder::new(drain).build() 431 | } 432 | 433 | /// Build `AsyncCore` drain with custom parameters 434 | pub fn custom< 435 | D: slog::Drain + Send + 'static, 436 | >( 437 | drain: D, 438 | ) -> AsyncCoreBuilder { 439 | AsyncCoreBuilder::new(drain) 440 | } 441 | fn get_sender( 442 | &self, 443 | ) -> Result< 444 | &crossbeam_channel::Sender, 445 | std::sync::PoisonError< 446 | sync::MutexGuard>, 447 | >, 448 | > { 449 | self.tl_sender.get_or_try(|| Ok(self.ref_sender.clone())) 450 | } 451 | 452 | /// Send `AsyncRecord` to a worker thread. 453 | fn send(&self, r: AsyncRecord) -> AsyncResult<()> { 454 | let sender = self.get_sender()?; 455 | 456 | if self.blocking { 457 | sender.send(AsyncMsg::Record(r))?; 458 | } else { 459 | sender.try_send(AsyncMsg::Record(r))?; 460 | } 461 | 462 | Ok(()) 463 | } 464 | } 465 | 466 | impl Drain for AsyncCore { 467 | type Ok = (); 468 | type Err = AsyncError; 469 | 470 | fn log( 471 | &self, 472 | record: &Record, 473 | logger_values: &OwnedKVList, 474 | ) -> AsyncResult<()> { 475 | self.send(AsyncRecord::from(record, logger_values)) 476 | } 477 | } 478 | 479 | /// Serialized record. 480 | pub struct AsyncRecord { 481 | msg: String, 482 | level: Level, 483 | location: Box, 484 | tag: String, 485 | logger_values: OwnedKVList, 486 | kv: Box, 487 | } 488 | 489 | impl AsyncRecord { 490 | /// Serializes a `Record` and an `OwnedKVList`. 491 | pub fn from(record: &Record, logger_values: &OwnedKVList) -> Self { 492 | let mut ser = ToSendSerializer::new(); 493 | record 494 | .kv() 495 | .serialize(record, &mut ser) 496 | .expect("`ToSendSerializer` can't fail"); 497 | 498 | AsyncRecord { 499 | msg: fmt::format(*record.msg()), 500 | level: record.level(), 501 | location: Box::new(*record.location()), 502 | tag: String::from(record.tag()), 503 | logger_values: logger_values.clone(), 504 | kv: ser.finish(), 505 | } 506 | } 507 | 508 | /// Writes the record to a `Drain`. 509 | pub fn log_to(self, drain: &D) -> Result { 510 | let rs = RecordStatic { 511 | location: &*self.location, 512 | level: self.level, 513 | tag: &self.tag, 514 | }; 515 | 516 | drain.log( 517 | &Record::new( 518 | &rs, 519 | &format_args!("{}", self.msg), 520 | BorrowedKV(&self.kv), 521 | ), 522 | &self.logger_values, 523 | ) 524 | } 525 | 526 | /// Deconstruct this `AsyncRecord` into a record and `OwnedKVList`. 527 | pub fn as_record_values(&self, mut f: impl FnMut(&Record, &OwnedKVList)) { 528 | let rs = RecordStatic { 529 | location: &*self.location, 530 | level: self.level, 531 | tag: &self.tag, 532 | }; 533 | 534 | f( 535 | &Record::new( 536 | &rs, 537 | &format_args!("{}", self.msg), 538 | BorrowedKV(&self.kv), 539 | ), 540 | &self.logger_values, 541 | ) 542 | } 543 | } 544 | 545 | enum AsyncMsg { 546 | Record(AsyncRecord), 547 | Finish, 548 | } 549 | 550 | impl Drop for AsyncCore { 551 | fn drop(&mut self) { 552 | let _err: Result<(), Box> = { 553 | || { 554 | if let Some(join) = self.join.lock()?.take() { 555 | let _ = self.get_sender()?.send(AsyncMsg::Finish); 556 | if join.thread().id() != thread::current().id() { 557 | // A custom Drain::log implementation could dynamically 558 | // swap out the logger which eventually invokes 559 | // AsyncCore::drop in the worker thread. 560 | // If we drop the AsyncCore inside the logger thread, 561 | // this join() either panic or dead-lock. 562 | // TODO: Figure out whether skipping join() instead of 563 | // panicking is desirable. 564 | join.join().map_err(|_| { 565 | io::Error::new( 566 | io::ErrorKind::BrokenPipe, 567 | "Logging thread worker join error", 568 | ) 569 | })?; 570 | } 571 | } 572 | Ok(()) 573 | } 574 | }(); 575 | } 576 | } 577 | // }}} 578 | 579 | /// Behavior used when the channel is full. 580 | /// 581 | /// # Note 582 | /// 583 | /// More variants may be added in the future, without considering it a breaking change. 584 | #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] 585 | pub enum OverflowStrategy { 586 | /// The message gets dropped and a message with number of dropped is produced once there's 587 | /// space. 588 | /// 589 | /// This is the default. 590 | /// 591 | /// Note that the message with number of dropped messages takes one slot in the channel as 592 | /// well. 593 | DropAndReport, 594 | /// The message gets dropped silently. 595 | Drop, 596 | /// The caller is blocked until there's enough space. 597 | Block, 598 | #[doc(hidden)] 599 | DoNotMatchAgainstThisAndReadTheDocs, 600 | } 601 | 602 | /// `Async` builder 603 | pub struct AsyncBuilder 604 | where 605 | D: slog::Drain + Send + 'static, 606 | { 607 | core: AsyncCoreBuilder, 608 | // Increment a counter whenever a message is dropped due to not fitting inside the channel. 609 | inc_dropped: bool, 610 | } 611 | 612 | impl AsyncBuilder 613 | where 614 | D: slog::Drain + Send + 'static, 615 | { 616 | fn new(drain: D) -> AsyncBuilder { 617 | AsyncBuilder { 618 | core: AsyncCoreBuilder::new(drain), 619 | inc_dropped: true, 620 | } 621 | } 622 | 623 | /// Set channel size used to send logging records to worker thread. When 624 | /// buffer is full `AsyncCore` will start returning `AsyncError::Full`. 625 | pub fn chan_size(self, s: usize) -> Self { 626 | AsyncBuilder { 627 | core: self.core.chan_size(s), 628 | ..self 629 | } 630 | } 631 | 632 | /// Sets what will happen if the channel is full. 633 | pub fn overflow_strategy( 634 | self, 635 | overflow_strategy: OverflowStrategy, 636 | ) -> Self { 637 | let (block, inc) = match overflow_strategy { 638 | OverflowStrategy::Block => (true, false), 639 | OverflowStrategy::Drop => (false, false), 640 | OverflowStrategy::DropAndReport => (false, true), 641 | OverflowStrategy::DoNotMatchAgainstThisAndReadTheDocs => { 642 | panic!("Invalid variant") 643 | } 644 | }; 645 | AsyncBuilder { 646 | core: self.core.blocking(block), 647 | inc_dropped: inc, 648 | } 649 | } 650 | 651 | /// Configure a name to be used for the background thread. 652 | /// 653 | /// The name must not contain '\0'. 654 | /// 655 | /// # Panics 656 | /// 657 | /// If a name with '\0' is passed. 658 | pub fn thread_name(self, name: String) -> Self { 659 | AsyncBuilder { 660 | core: self.core.thread_name(name), 661 | ..self 662 | } 663 | } 664 | 665 | /// Complete building `Async` 666 | pub fn build(self) -> Async { 667 | Async { 668 | core: self.core.build_no_guard(), 669 | dropped: AtomicUsize::new(0), 670 | inc_dropped: self.inc_dropped, 671 | } 672 | } 673 | 674 | /// Complete building `Async` 675 | pub fn build_no_guard(self) -> Async { 676 | Async { 677 | core: self.core.build_no_guard(), 678 | dropped: AtomicUsize::new(0), 679 | inc_dropped: self.inc_dropped, 680 | } 681 | } 682 | 683 | /// Complete building `Async` with `AsyncGuard` 684 | /// 685 | /// See `AsyncGuard` for more information. 686 | pub fn build_with_guard(self) -> (Async, AsyncGuard) { 687 | let (core, guard) = self.core.build_with_guard(); 688 | ( 689 | Async { 690 | core, 691 | dropped: AtomicUsize::new(0), 692 | inc_dropped: self.inc_dropped, 693 | }, 694 | guard, 695 | ) 696 | } 697 | } 698 | 699 | /// Async drain 700 | /// 701 | /// `Async` will send all the logging records to a wrapped drain running in 702 | /// another thread. 703 | /// 704 | /// `Async` never returns `AsyncError::Full`. 705 | /// 706 | /// `Record`s are passed to the worker thread through a channel with a bounded 707 | /// size (see `AsyncBuilder::chan_size`). On channel overflow `Async` will 708 | /// start dropping `Record`s and log a message informing about it after 709 | /// sending more `Record`s is possible again. The exact details of handling 710 | /// overflow is implementation defined, might change and should not be relied 711 | /// on, other than message won't be dropped as long as channel does not 712 | /// overflow. 713 | /// 714 | /// Any messages reported by `Async` will contain `slog-async` logging `Record` 715 | /// tag to allow easy custom handling. 716 | /// 717 | /// Note: On drop `Async` waits for it's worker-thread to finish (after handling 718 | /// all previous `Record`s sent to it). If you can't tolerate the delay, make 719 | /// sure you drop it eg. in another thread. 720 | pub struct Async { 721 | core: AsyncCore, 722 | dropped: AtomicUsize, 723 | // Increment the dropped counter if dropped? 724 | inc_dropped: bool, 725 | } 726 | 727 | impl Async { 728 | /// New `AsyncCore` with default parameters 729 | pub fn default< 730 | D: slog::Drain + Send + 'static, 731 | >( 732 | drain: D, 733 | ) -> Self { 734 | AsyncBuilder::new(drain).build() 735 | } 736 | 737 | /// Build `Async` drain with custom parameters 738 | /// 739 | /// The wrapped drain must handle all results (`Drain`) 740 | /// since there's no way to return it back. See `slog::DrainExt::fuse()` and 741 | /// `slog::DrainExt::ignore_res()` for typical error handling strategies. 742 | pub fn new + Send + 'static>( 743 | drain: D, 744 | ) -> AsyncBuilder { 745 | AsyncBuilder::new(drain) 746 | } 747 | 748 | fn push_dropped(&self, logger_values: &OwnedKVList) -> AsyncResult<()> { 749 | let dropped = self.dropped.swap(0, Ordering::Relaxed); 750 | if dropped > 0 { 751 | match self.core.log( 752 | &record!( 753 | slog::Level::Error, 754 | "slog-async", 755 | &format_args!( 756 | "slog-async: logger dropped messages \ 757 | due to channel \ 758 | overflow" 759 | ), 760 | b!("count" => dropped) 761 | ), 762 | logger_values, 763 | ) { 764 | Ok(()) => {} 765 | Err(AsyncError::Full) => { 766 | self.dropped.fetch_add(dropped + 1, Ordering::Relaxed); 767 | return Ok(()); 768 | } 769 | Err(e) => return Err(e), 770 | } 771 | } 772 | Ok(()) 773 | } 774 | } 775 | 776 | impl Drain for Async { 777 | type Ok = (); 778 | type Err = AsyncError; 779 | 780 | // TODO: Review `Ordering::Relaxed` 781 | fn log( 782 | &self, 783 | record: &Record, 784 | logger_values: &OwnedKVList, 785 | ) -> AsyncResult<()> { 786 | self.push_dropped(logger_values)?; 787 | 788 | match self.core.log(record, logger_values) { 789 | Ok(()) => {} 790 | Err(AsyncError::Full) if self.inc_dropped => { 791 | self.dropped.fetch_add(1, Ordering::Relaxed); 792 | } 793 | Err(AsyncError::Full) => {} 794 | Err(e) => return Err(e), 795 | } 796 | 797 | Ok(()) 798 | } 799 | } 800 | 801 | impl Drop for Async { 802 | fn drop(&mut self) { 803 | let _ = self.push_dropped(&o!().into()); 804 | } 805 | } 806 | 807 | // }}} 808 | 809 | 810 | #[cfg(test)] 811 | mod test { 812 | use super::*; 813 | use std::sync::mpsc; 814 | 815 | #[test] 816 | fn integration_test() { 817 | let (mock_drain, mock_drain_rx) = MockDrain::new(); 818 | let async_drain = AsyncBuilder::new(mock_drain) 819 | .build(); 820 | let slog = slog::Logger::root(async_drain.fuse(), o!("field1" => "value1")); 821 | 822 | info!(slog, "Message 1"; "field2" => "value2"); 823 | warn!(slog, "Message 2"; "field3" => "value3"); 824 | assert_eq!(mock_drain_rx.recv().unwrap(), r#"INFO Message 1: [("field1", "value1"), ("field2", "value2")]"#); 825 | assert_eq!(mock_drain_rx.recv().unwrap(), r#"WARN Message 2: [("field1", "value1"), ("field3", "value3")]"#); 826 | } 827 | 828 | 829 | /// Test-helper drain 830 | #[derive(Debug)] 831 | struct MockDrain { 832 | tx: mpsc::Sender, 833 | } 834 | 835 | impl MockDrain { 836 | fn new() -> (Self, mpsc::Receiver) { 837 | let (tx, rx) = mpsc::channel(); 838 | (Self { tx }, rx) 839 | } 840 | } 841 | 842 | impl slog::Drain for MockDrain { 843 | type Ok = (); 844 | type Err = slog::Never; 845 | 846 | fn log(&self, record: &Record, logger_kv: &OwnedKVList) -> Result { 847 | let mut serializer = MockSerializer::default(); 848 | logger_kv.serialize(record, &mut serializer).unwrap(); 849 | record.kv().serialize(record, &mut serializer).unwrap(); 850 | let level = record.level().as_short_str(); 851 | let msg = record.msg().to_string(); 852 | let entry = format!("{} {}: {:?}", level, msg, serializer.kvs); 853 | self.tx.send(entry).unwrap(); 854 | Ok(()) 855 | } 856 | } 857 | 858 | #[derive(Default)] 859 | struct MockSerializer { 860 | kvs: Vec<(String, String)>, 861 | } 862 | 863 | impl slog::Serializer for MockSerializer { 864 | fn emit_arguments(&mut self, key: Key, val: &fmt::Arguments) -> Result<(), slog::Error> { 865 | self.kvs.push((key.to_string(), val.to_string())); 866 | Ok(()) 867 | } 868 | } 869 | } 870 | 871 | // vim: foldmethod=marker foldmarker={{{,}}} 872 | --------------------------------------------------------------------------------