├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── LICENSE-MPL2 ├── Makefile ├── README.md ├── examples ├── common │ └── mod.rs └── compact-color.rs └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | /tags 4 | /.cargo 5 | /rusty-tags.vi 6 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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 | ## 4.3.0 - 2019-10-25 8 | 9 | * Add `GlobalLoggerGuard::is_canceled` 10 | 11 | ## 4.2.0 - 2019-10-16 12 | 13 | * Update the arc-swap dependency to 0.4. 14 | 15 | ## 4.1.2 - 2019-07-18 16 | 17 | * Call `slog` macros via `$crate::` prefix to prevent the users of this crate from having to manually import `slog_trace`, `slog_debug`, etc. 18 | 19 | ## 4.1.1 - 2018-12-20 20 | 21 | * Fix dependency reqs for building with older `slog` versions 22 | 23 | ## 4.1.0 - 2018-12-16 24 | ### Changed 25 | 26 | * Update `crossbeam` and `lazy_static` 27 | 28 | ## 4.0.1 - 2017-12-14 29 | ### Changed 30 | 31 | * Fix `nightly` compilation 32 | 33 | ## 4.0.0 - 2017-05-28 34 | ### Changed 35 | 36 | * Default to panicking instead of discarding messages 37 | * Relicensed under MPL-2.0/MIT/Apache-2.0 38 | 39 | ## 3.0.0 - 2017-04-11 40 | ### Changed 41 | 42 | * Switch to reference-based API, which avoids allocations 43 | 44 | ## 2.0.0 - 2017-04-11 45 | ### Changed 46 | 47 | * Update dependencies 48 | 49 | ## 2.0.0-3.1 - 2017-04-06 50 | ### Fixed 51 | 52 | * Example in documentation was not saving the guard 53 | 54 | ## 2.0.0-3.0 - 2017-03-27 55 | ### Changed 56 | 57 | * Add `GlobalLoggerGuard` 58 | * Bump dependencies 59 | 60 | ## 2.0.0-2.0 - 2017-03-11 61 | 62 | * Fix examples 63 | * Bump slog dependency version 64 | 65 | ## 2.0.0-0.1 - 2017-03-05 66 | 67 | * Move to use slog v2 developement release 68 | 69 | ## 0.2.2 - 2016-11-30 70 | ### Changed 71 | 72 | * Moved to own repository 73 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "slog-scope" 3 | version = "4.3.0" 4 | authors = ["Dawid Ciężarkiewicz "] 5 | description = "Logging scopes for slog-rs" 6 | keywords = ["slog", "logging", "slog", "log"] 7 | license = "MPL-2.0 OR MIT OR Apache-2.0" 8 | documentation = "https://docs.rs/slog-scope" 9 | homepage = "https://github.com/slog-rs/slog" 10 | repository = "https://github.com/slog-rs/scope" 11 | readme = "README.md" 12 | 13 | [lib] 14 | path = "lib.rs" 15 | 16 | [dependencies] 17 | slog = "2.4" 18 | lazy_static = "1.2" 19 | arc-swap = "1.1" 20 | 21 | [dev-dependencies] 22 | slog-term = "2" 23 | slog-async = "2" 24 | -------------------------------------------------------------------------------- /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 |

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

20 | 21 | # slog-scope - Logging scopes for [slog-rs] 22 | 23 | `slog-scope` allows logging without manually handling `Logger` objects. 24 | 25 | It is generally advised **NOT** to use `slog_scope` in libraries. Read more in 26 | [slog-rs 27 | FAQ](https://github.com/slog-rs/slog/wiki/FAQ#do-i-have-to-pass-logger-around) 28 | 29 | For more information, help, to report issues etc. see [slog-rs][slog-rs]. 30 | 31 | 32 | [slog-rs]: //github.com/slog-rs/slog 33 | 34 | 35 | 36 | ## Verification Recommendation 37 | 38 | To help with the maintaince, the ownership of this crate is potentially shared between multiple developers. 39 | It is recommended to always use [cargo-crev](https://github.com/crev-dev/cargo-crev) 40 | to verify the trustworthiness of each of your dependencies, including this one. 41 | -------------------------------------------------------------------------------- /examples/common/mod.rs: -------------------------------------------------------------------------------- 1 | use slog_scope; 2 | 3 | pub fn simulate_server() { 4 | 5 | let log = slog_scope::logger(); 6 | 7 | let server = log.new(o!("host" => "localhost", "port" => "8080")); 8 | let peer1 = server.new(o!("peer_addr" => "8.8.8.8", "port" => "18230")); 9 | let peer2 = server.new(o!("peer_addr" => "82.9.9.9", "port" => "42381")); 10 | 11 | info!(server, "starting"); 12 | info!(server, "listening"); 13 | debug!(peer2, "connected"); 14 | debug!(peer2, "message received"; "length" => 2); 15 | debug!(peer1, "connected"); 16 | warn!(peer2, "weak encryption requested"; "algo" => "xor"); 17 | debug!(peer2, "response sent"; "length" => 8); 18 | debug!(peer2, "disconnected"); 19 | debug!(peer1, "message received"; "length" => 2); 20 | debug!(peer1, "response sent"; "length" => 8); 21 | debug!(peer1, "disconnected"); 22 | crit!(server, "internal error"); 23 | info!(server, "exit"); 24 | 25 | } 26 | -------------------------------------------------------------------------------- /examples/compact-color.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate slog; 3 | extern crate slog_term; 4 | extern crate slog_async; 5 | extern crate slog_scope; 6 | 7 | use slog::Drain; 8 | use std::sync::Arc; 9 | 10 | mod common; 11 | 12 | fn main() { 13 | let decorator = slog_term::TermDecorator::new().build(); 14 | let drain = slog_term::CompactFormat::new(decorator).build().fuse(); 15 | let drain = slog_async::Async::new(drain).build().fuse(); 16 | 17 | let log = slog::Logger::root(Arc::new(drain), o!("version" => "0.5")); 18 | 19 | let _guard = slog_scope::set_global_logger(log); 20 | common::simulate_server(); 21 | } 22 | -------------------------------------------------------------------------------- /lib.rs: -------------------------------------------------------------------------------- 1 | //! Logging scopes for slog-rs 2 | //! 3 | //! Logging scopes are convenience functionality for slog-rs to free user from manually passing 4 | //! `Logger` objects around. 5 | //! 6 | //! Set of macros is also provided as an alternative to original `slog` crate macros, for logging 7 | //! directly to `Logger` of the current logging scope. 8 | //! 9 | //! # Set global logger upfront 10 | //! 11 | //! **Warning**: Since `slog-scope` version 4.0.0, `slog-scope` defaults to 12 | //! panicking on logging if no scope or global logger was set. Because of it, it 13 | //! is advised to always set a global logger upfront with `set_global_logger`. 14 | //! 15 | //! # Using `slog-scope` as a part of API is not advised 16 | //! 17 | //! Part of a `slog` logging philosophy is ability to freely express logging contexts 18 | //! according to logical structure, rather than callstack structure. By using 19 | //! logging scopes the logging context is tied to code flow again, which is less 20 | //! expressive. 21 | //! 22 | //! It is generally advised **NOT** to use `slog_scope` in libraries. Read more in 23 | //! [slog-rs FAQ](https://github.com/slog-rs/slog/wiki/FAQ#do-i-have-to-pass-logger-around) 24 | //! 25 | //! ``` 26 | //! #[macro_use(slog_o, slog_info, slog_log, slog_record, slog_record_static, slog_b, slog_kv)] 27 | //! extern crate slog; 28 | //! #[macro_use] 29 | //! extern crate slog_scope; 30 | //! extern crate slog_term; 31 | //! 32 | //! use slog::Drain; 33 | //! 34 | //! fn foo() { 35 | //! slog_info!(slog_scope::logger(), "foo"); 36 | //! info!("foo"); // Same as above, but more ergonomic and a bit faster 37 | //! // since it uses `with_logger` 38 | //! } 39 | //! 40 | //! fn main() { 41 | //! let plain = slog_term::PlainSyncDecorator::new(std::io::stdout()); 42 | //! let log = slog::Logger::root( 43 | //! slog_term::FullFormat::new(plain) 44 | //! .build().fuse(), slog_o!() 45 | //! ); 46 | //! 47 | //! // Make sure to save the guard, see documentation for more information 48 | //! let _guard = slog_scope::set_global_logger(log); 49 | //! slog_scope::scope(&slog_scope::logger().new(slog_o!("scope" => "1")), 50 | //! || foo() 51 | //! ); 52 | //! } 53 | 54 | #![warn(missing_docs)] 55 | 56 | #[macro_use(o)] 57 | extern crate slog; 58 | #[macro_use] 59 | extern crate lazy_static; 60 | extern crate arc_swap; 61 | 62 | use slog::{Logger, Record, OwnedKVList}; 63 | 64 | use std::sync::Arc; 65 | use std::cell::RefCell; 66 | use arc_swap::ArcSwap; 67 | 68 | use std::result; 69 | 70 | pub use slog::{slog_crit, slog_debug, slog_error, slog_info, slog_trace, slog_warn}; 71 | 72 | /// Log a critical level message using current scope logger 73 | #[macro_export] 74 | macro_rules! crit( ($($args:tt)+) => { 75 | $crate::with_logger(|logger| $crate::slog_crit![logger, $($args)+]) 76 | };); 77 | /// Log a error level message using current scope logger 78 | #[macro_export] 79 | macro_rules! error( ($($args:tt)+) => { 80 | $crate::with_logger(|logger| $crate::slog_error![logger, $($args)+]) 81 | };); 82 | /// Log a warning level message using current scope logger 83 | #[macro_export] 84 | macro_rules! warn( ($($args:tt)+) => { 85 | $crate::with_logger(|logger| $crate::slog_warn![logger, $($args)+]) 86 | };); 87 | /// Log a info level message using current scope logger 88 | #[macro_export] 89 | macro_rules! info( ($($args:tt)+) => { 90 | $crate::with_logger(|logger| $crate::slog_info![logger, $($args)+]) 91 | };); 92 | /// Log a debug level message using current scope logger 93 | #[macro_export] 94 | macro_rules! debug( ($($args:tt)+) => { 95 | $crate::with_logger(|logger| $crate::slog_debug![logger, $($args)+]) 96 | };); 97 | /// Log a trace level message using current scope logger 98 | #[macro_export] 99 | macro_rules! trace( ($($args:tt)+) => { 100 | $crate::with_logger(|logger| $crate::slog_trace![logger, $($args)+]) 101 | };); 102 | 103 | thread_local! { 104 | static TL_SCOPES: RefCell> = RefCell::new(Vec::with_capacity(8)) 105 | } 106 | 107 | lazy_static! { 108 | static ref GLOBAL_LOGGER : ArcSwap = ArcSwap::from( 109 | Arc::new( 110 | slog::Logger::root(slog::Discard, o!()) 111 | ) 112 | ); 113 | } 114 | 115 | struct NoGlobalLoggerSet; 116 | 117 | impl slog::Drain for NoGlobalLoggerSet { 118 | type Ok = (); 119 | type Err = slog::Never; 120 | 121 | fn log(&self, 122 | _record: &Record, 123 | _values: &OwnedKVList) 124 | -> result::Result { 125 | panic!( 126 | "slog-scope: No logger set. Use `slog_scope::set_global_logger` or `slog_scope::scope`." 127 | ) 128 | } 129 | } 130 | 131 | 132 | /// Guard resetting global logger 133 | /// 134 | /// On drop it will reset global logger to an `slog::Drain` that panics upon receiving a log record, 135 | /// so use outside of main is discouraged. 136 | /// This will `drop` any existing global logger. 137 | #[must_use] 138 | pub struct GlobalLoggerGuard { 139 | canceled : bool, 140 | } 141 | 142 | impl GlobalLoggerGuard { 143 | /// Getter for canceled to check status 144 | pub fn is_canceled(&self) -> bool { 145 | self.canceled 146 | } 147 | 148 | fn new() -> Self { 149 | GlobalLoggerGuard { 150 | canceled: false, 151 | } 152 | } 153 | 154 | /// Cancel resetting global logger 155 | pub fn cancel_reset(mut self) { 156 | self.canceled = true; 157 | } 158 | } 159 | 160 | impl Drop for GlobalLoggerGuard { 161 | fn drop(&mut self) { 162 | if !self.canceled { 163 | GLOBAL_LOGGER.store( 164 | Arc::new( 165 | slog::Logger::root(NoGlobalLoggerSet, o!()) 166 | ) 167 | ); 168 | } 169 | } 170 | } 171 | 172 | 173 | /// Set global `Logger` that is returned by calls like `logger()` outside of any logging scope. 174 | pub fn set_global_logger(l: slog::Logger) -> GlobalLoggerGuard { 175 | GLOBAL_LOGGER.store(Arc::new(l)); 176 | 177 | GlobalLoggerGuard::new() 178 | } 179 | 180 | struct ScopeGuard; 181 | 182 | 183 | impl ScopeGuard { 184 | fn new(logger: &slog::Logger) -> Self { 185 | TL_SCOPES.with(|s| { s.borrow_mut().push(logger as *const Logger); }); 186 | 187 | ScopeGuard 188 | } 189 | } 190 | 191 | impl Drop for ScopeGuard { 192 | fn drop(&mut self) { 193 | TL_SCOPES.with(|s| { s.borrow_mut().pop().expect("TL_SCOPES should contain a logger"); }) 194 | } 195 | } 196 | 197 | /// Access the `Logger` for the current logging scope 198 | /// 199 | /// This function needs to clone an underlying scoped 200 | /// `Logger`. If performance is of vital importance, 201 | /// use `with_logger`. 202 | pub fn logger() -> Logger { 203 | TL_SCOPES.with(|s| { 204 | let s = s.borrow(); 205 | match s.last() { 206 | Some(logger) => (unsafe {&**logger}).clone(), 207 | None => Logger::clone(&GLOBAL_LOGGER.load()) 208 | } 209 | }) 210 | } 211 | 212 | /// Access the `Logger` for the current logging scope 213 | /// 214 | /// This function doesn't have to clone the Logger 215 | /// so it might be a bit faster. 216 | pub fn with_logger(f : F) -> R 217 | where F : FnOnce(&Logger) -> R { 218 | TL_SCOPES.with(|s| { 219 | let s = s.borrow(); 220 | match s.last() { 221 | Some(logger) => f(unsafe {&**logger}), 222 | None => f(&GLOBAL_LOGGER.load()), 223 | } 224 | }) 225 | } 226 | 227 | /// Execute code in a logging scope 228 | /// 229 | /// Logging scopes allow using a `slog::Logger` without explicitly 230 | /// passing it in the code. 231 | /// 232 | /// At any time current active `Logger` for a given thread can be retrived 233 | /// with `logger()` call. 234 | /// 235 | /// Logging scopes can be nested and are panic safe. 236 | /// 237 | /// `logger` is the `Logger` to use during the duration of `f`. 238 | /// `with_current_logger` can be used to build it as a child of currently active 239 | /// logger. 240 | /// 241 | /// `f` is a code to be executed in the logging scope. 242 | /// 243 | /// Note: Thread scopes are thread-local. Each newly spawned thread starts 244 | /// with a global logger, as a current logger. 245 | pub fn scope(logger: &slog::Logger, f: SF) -> R 246 | where SF: FnOnce() -> R 247 | { 248 | let _guard = ScopeGuard::new(&logger); 249 | f() 250 | } 251 | --------------------------------------------------------------------------------