• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 package dev.pigweed.pw_log;
16 
17 import android.util.Log;
18 import java.util.logging.Level;
19 
20 /**
21  * Partial implementation of the com.google.common.flogger.FluentLogger API that
22  * logs to android.util.Log.
23  */
24 public final class Logger {
25   private final String tag;
26 
27   public final class AndroidLogApi {
28     private final int level;
29 
30     private Throwable cause = null;
31 
AndroidLogApi(Level level)32     private AndroidLogApi(Level level) {
33       if (level == Level.FINEST || level == Level.FINER) {
34         this.level = Log.VERBOSE;
35       } else if (level == Level.FINE || level == Level.CONFIG) {
36         this.level = Log.DEBUG;
37       } else if (level == Level.WARNING) {
38         this.level = Log.WARN;
39       } else if (level == Level.SEVERE) {
40         this.level = Log.ERROR;
41       } else {
42         this.level = Log.INFO;
43       }
44     }
45 
withCause(Throwable cause)46     public AndroidLogApi withCause(Throwable cause) {
47       this.cause = cause;
48       return this;
49     }
50 
log(String message)51     public void log(String message) {
52       if (cause != null) {
53         message = String.format("%s: %s", cause, message);
54       }
55 
56       Log.println(level, tag, message);
57     }
58 
log(String message, Object... args)59     public void log(String message, Object... args) {
60       log(String.format(message, args));
61     }
62   }
63 
forClass(Class<?> enclosingClass)64   public static Logger forClass(Class<?> enclosingClass) {
65     return new Logger(enclosingClass.getSimpleName());
66   }
67 
Logger(String tag)68   private Logger(String tag) {
69     this.tag = tag;
70   }
71 
at(Level level)72   public AndroidLogApi at(Level level) {
73     return new AndroidLogApi(level);
74   }
75 
atSevere()76   public AndroidLogApi atSevere() {
77     return at(Level.SEVERE);
78   }
79 
atWarning()80   public AndroidLogApi atWarning() {
81     return at(Level.WARNING);
82   }
83 
atInfo()84   public AndroidLogApi atInfo() {
85     return at(Level.INFO);
86   }
87 
atConfig()88   public AndroidLogApi atConfig() {
89     return at(Level.CONFIG);
90   }
91 
atFine()92   public AndroidLogApi atFine() {
93     return at(Level.FINE);
94   }
95 
atFiner()96   public AndroidLogApi atFiner() {
97     return at(Level.FINER);
98   }
99 
atFinest()100   public AndroidLogApi atFinest() {
101     return at(Level.FINEST);
102   }
103 }
104