5b16c6d6749ed4a91057cd2cf689f203c3b3e5c5
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / global / GenBuildLogger.java
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13 GenBuildLogger.java
14
15 Abstract:
16
17 --*/
18
19 package org.tianocore.build.global;
20
21 import java.io.File;
22 import java.util.List;
23 import java.util.Vector;
24
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.Task;
27
28 import org.tianocore.common.logger.EdkLog;
29 import org.tianocore.common.logger.LogMethod;
30
31 public class GenBuildLogger implements LogMethod {
32 private Project project = null;
33
34 ///
35 /// flag to present whether cache all msg or not
36 /// true means not to cache.
37 ///
38 private boolean flag = true;
39
40 private List<String> v = null;
41
42 public GenBuildLogger (Project project) {
43 this.project = project;
44 }
45
46 public GenBuildLogger (Project project, boolean flag) {
47 this.project = project;
48 this.flag = flag;
49
50 //
51 // Only flag is false, v will be initialized and used.
52 //
53 if (!flag) {
54 v = new Vector<String>(2048);
55 }
56 }
57
58 /**
59 Rules: flag = true: means no cache Action: Print it to console
60
61 flag = false: mean cache all msg exception some special Action: loglevel
62 is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and
63 cache the msg others -- No print and cache the msg
64 **/
65 public synchronized void putMessage(Object msgSource, int msgLevel,
66 String msg) {
67 if (this.project == null) {
68 return;
69 }
70
71 //
72 // If msgLevel is always print, then print it
73 //
74 switch (msgLevel) {
75 case EdkLog.EDK_ALWAYS:
76 log(msgSource, msg, Project.MSG_INFO);
77 break;
78 case EdkLog.EDK_ERROR:
79 if (flag) {
80 log(msgSource, msg, Project.MSG_ERR);
81 } else {
82 log(msgSource, msg, Project.MSG_ERR);
83 v.add(msg);
84 }
85 break;
86 case EdkLog.EDK_WARNING:
87 if (flag) {
88 log(msgSource, msg, Project.MSG_WARN);
89 } else {
90 v.add(msg);
91 }
92 break;
93 case EdkLog.EDK_INFO:
94 if (flag) {
95 log(msgSource, msg, Project.MSG_INFO);
96 } else {
97 v.add(msg);
98 }
99 break;
100 case EdkLog.EDK_VERBOSE:
101 if (flag) {
102 log(msgSource, msg, Project.MSG_VERBOSE);
103 } else {
104 v.add(msg);
105 }
106 break;
107 case EdkLog.EDK_DEBUG:
108 if (flag) {
109 log(msgSource, msg, Project.MSG_DEBUG);
110 } else {
111 v.add(msg);
112 }
113 break;
114 }
115 }
116
117 public void flushToFile(File file) {
118 //
119 // Sort msg and store to the file (TBD)
120 //
121
122 }
123
124 private void log(Object msgSource, String msg, int level) {
125 if (msgSource instanceof Task) {
126 this.project.log((Task)msgSource, msg, level);
127 } else if (msgSource instanceof String){
128
129 //
130 // Pad 12 space to keep message
131 //
132 msg = msg.replaceAll("\n", "\n ");
133 this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);
134 } else {
135 this.project.log(msg, level);
136 }
137 }
138 }