Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "log" |
| 24 | "os" |
| 25 | "testing" |
| 26 | ) |
| 27 | |
| 28 | // Logger is a simple wrapper of a logging function. |
| 29 | // |
| 30 | // In reality the users might actually use different logging libraries, and they |
| 31 | // are not always compatible with each other. |
| 32 | // |
| 33 | // Logger is meant to be a simple common ground that it's easy to wrap whatever |
| 34 | // logging library they use into. |
| 35 | // |
| 36 | // See https://issues.apache.org/jira/browse/THRIFT-4985 for the design |
| 37 | // discussion behind it. |
Yuxuan 'fishy' Wang | 875178c | 2023-11-17 12:36:31 -0800 | [diff] [blame] | 38 | // |
| 39 | // Deprecated: This is no longer used by any thrift go library code, |
| 40 | // will be removed in the future version. |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 41 | type Logger func(msg string) |
| 42 | |
| 43 | // NopLogger is a Logger implementation that does nothing. |
Yuxuan 'fishy' Wang | 875178c | 2023-11-17 12:36:31 -0800 | [diff] [blame] | 44 | // |
| 45 | // Deprecated: This is no longer used by any thrift go library code, |
| 46 | // will be removed in the future version. |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 47 | func NopLogger(msg string) {} |
| 48 | |
| 49 | // StdLogger wraps stdlib log package into a Logger. |
| 50 | // |
| 51 | // If logger passed in is nil, it will fallback to use stderr and default flags. |
Yuxuan 'fishy' Wang | 875178c | 2023-11-17 12:36:31 -0800 | [diff] [blame] | 52 | // |
| 53 | // Deprecated: This is no longer used by any thrift go library code, |
| 54 | // will be removed in the future version. |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 55 | func StdLogger(logger *log.Logger) Logger { |
| 56 | if logger == nil { |
| 57 | logger = log.New(os.Stderr, "", log.LstdFlags) |
| 58 | } |
| 59 | return func(msg string) { |
| 60 | logger.Print(msg) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // TestLogger is a Logger implementation can be used in test codes. |
| 65 | // |
| 66 | // It fails the test when being called. |
Yuxuan 'fishy' Wang | 875178c | 2023-11-17 12:36:31 -0800 | [diff] [blame] | 67 | // |
| 68 | // Deprecated: This is no longer used by any thrift go library code, |
| 69 | // will be removed in the future version. |
Yuxuan 'fishy' Wang | e4870a3 | 2019-10-24 13:23:30 -0700 | [diff] [blame] | 70 | func TestLogger(tb testing.TB) Logger { |
| 71 | return func(msg string) { |
| 72 | tb.Errorf("logger called with msg: %q", msg) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func fallbackLogger(logger Logger) Logger { |
| 77 | if logger == nil { |
| 78 | return StdLogger(nil) |
| 79 | } |
| 80 | return logger |
| 81 | } |