• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://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,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos;
17 
18 import java.util.logging.ConsoleHandler;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21 
22 /**
23  * Class used to print logs and will remove if the official log module is ready.
24  *
25  */
26 public class Log {
27     private static final Logger log = Logger.getLogger("PackingTool");
28 
29     static {
30         log.setUseParentHandlers(false);
31         PackFormatter formatter = new PackFormatter();
32         ConsoleHandler handler = new ConsoleHandler();
33         handler.setFormatter(formatter);
34         log.addHandler(handler);
35         log.setLevel(Level.WARNING);
36     }
37 
38     /**
39      * A constructor used to create a Log.
40      *
41      * @param className class name
42      */
Log(String className)43     public Log(String className) {}
44 
45     /**
46      * print log in DEBUG level.
47      *
48      * @param msg log's content
49      */
debug(String msg)50     public void debug(String msg) {
51         log.info(LogType.buildTag(LogType.DEBUG) + msg);
52     }
53 
54     /**
55      * print log in INFO level.
56      *
57      * @param msg log's content
58      */
info(String msg)59     public void info(String msg) {
60         log.info(LogType.buildTag(LogType.INFO) + msg);
61     }
62 
63     /**
64      * print log in ERROR level.
65      *
66      * @param msg log's content
67      */
error(String msg)68     public void error(String msg) {
69         log.warning(LogType.buildTag(LogType.ERROR) + msg);
70     }
71 
72     /**
73      * print log in WARNING level.
74      *
75      * @param msg log's content
76      */
warning(String msg)77     public void warning(String msg) {
78         log.warning(LogType.buildTag(LogType.WARNING) + msg);
79     }
80 }
81 
82 enum LogType {
83     DEBUG("Debug"),
84     INFO("Info"),
85     WARNING("Warning"),
86     ERROR("Error");
87 
88     private String value;
89 
LogType(String value)90     LogType(String value) {
91         this.value = value;
92     }
93 
94     /**
95      * get log tag
96      *
97      * @param logType log type
98      * @return format log tag
99      */
buildTag(LogType logType)100     public static String buildTag(LogType logType) {
101         return "Ohos BundleTool [" + logType.value + "]: ";
102     }
103 }
104 
105