summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/GenBuild/org/tianocore/build/global/GenBuildLogger.java
blob: b1e119a501a0193f92a5cfef44d1272e02472503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*++

 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php

 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

 Module Name:
 GenBuildLogger.java

 Abstract:

 --*/

package org.tianocore.build.global;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.tools.ant.BuildEvent;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.util.StringUtils;

import org.tianocore.build.id.FpdModuleIdentification;
import org.tianocore.common.logger.EdkLog;
import org.tianocore.common.logger.LogMethod;

public class GenBuildLogger extends DefaultLogger implements LogMethod {
    
    Project project = null;
    
    ///
    /// flag to present whether cache all msg or not
    /// true means to cache.
    ///
    private static boolean flag = false;

    private static Map<FpdModuleIdentification, List<String>> map = new LinkedHashMap<FpdModuleIdentification, List<String> >(256);
    
    private FpdModuleIdentification id = null;
    //
	//  semaroph for multi thread
	// 
    public static Object semaphore = new Object();
    
    public GenBuildLogger () {
        
    }

    public GenBuildLogger (Project project) {
        this.project = project;
    }

    /**
      Rules: flag = false: means no cache Action: Print it to console
      
      flag = true: mean cache all msg exception some special Action: loglevel
      is EDK_ALWAYS -- Print but no cache loglevel is EDK_ERROR -- Print and
      cache the msg others -- No print and cache the msg
    **/
    public synchronized void putMessage(Object msgSource, int msgLevel, String msg) {
        if (this.project == null) {
            return;
        }

        //
        // If msgLevel is always print, then print it
        //
        switch (msgLevel) {
        case EdkLog.EDK_ALWAYS:
            //
            // Do some special
            //
            log(msgSource, msg, Project.MSG_ERR);
            break;
        case EdkLog.EDK_ERROR:
            log(msgSource, msg, Project.MSG_ERR);
            break;
        case EdkLog.EDK_WARNING:
            log(msgSource, msg, Project.MSG_WARN);
            break;
        case EdkLog.EDK_INFO:
            log(msgSource, msg, Project.MSG_INFO);
            break;
        case EdkLog.EDK_VERBOSE:
            log(msgSource, msg, Project.MSG_VERBOSE);
            break;
        case EdkLog.EDK_DEBUG:
            log(msgSource, msg, Project.MSG_DEBUG);
            break;
        }
    }
    
    public static void flushErrorModuleLog(FpdModuleIdentification errorModuleId) {
        List<String> errorLogs = map.get(errorModuleId);
        if (errorLogs != null) {
            EdkLog.log("ErrorLog", EdkLog.EDK_ERROR, errorModuleId + " error logs: ");
            for(int i = 0; i < errorLogs.size(); i++) {
                EdkLog.log(EdkLog.EDK_ERROR, errorLogs.get(i));
            }
        }
    }

    public void flushToFile(File file) {
        //
        // Put all messages in map to file
        //
        String msg = "Writing log to file [" + file.getPath() + "]";
        log("Logging", msg, Project.MSG_INFO);
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            Iterator<FpdModuleIdentification> iter = map.keySet().iterator();
            List<String> mainLogs = null;
            while (iter.hasNext()) {
                FpdModuleIdentification item = iter.next();
                if(item == null) {
                    mainLogs = map.get(item);
                    continue ;
                }
                bw.write(">>>>>>>>>>>>>");
                bw.write(" " + item + " Build Log ");
                bw.write(">>>>>>>>>>>>>");
                bw.newLine();
                List<String> allMessages = map.get(item);
                for(int i = 0; i < allMessages.size(); i++) {
                    bw.write(allMessages.get(i));
                    bw.newLine();
                }
            }
            if (mainLogs != null) {
                bw.write(">>>>>>>>>>>>>");
                bw.write(" Main Logs (already print to command) ");
                bw.write(">>>>>>>>>>>>>");
                bw.newLine();
                for(int i = 0; i < mainLogs.size(); i++) {
                    bw.write(mainLogs.get(i));
                    bw.newLine();
                }
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            new BuildException("Writing log error. " + e.getMessage());
        }
        
    }
    
    private void log(Object msgSource, String msg, int level) {
        if (msgSource instanceof Task) {
            ((Task)msgSource).getProject().log((Task)msgSource, msg, level);
        } else if (msgSource instanceof String){
            //
            // Pad 12 space to keep message in unify format
            //
            msg = msg.replaceAll("\n", "\n            ");
            this.project.log(String.format("%12s", "[" + msgSource + "] ") + msg, level);
        } else {
            this.project.log(msg, level);
        }
    }
    public void targetStarted(BuildEvent event) {
        if (!flag) {
            super.targetStarted(event);
        }
    }
    
    public void messageLogged(BuildEvent event) {
        
        int currentLevel = event.getPriority();
        //
        // If current level is upper than Ant Level, skip it
        //
        if (currentLevel <= this.msgOutputLevel) {
            String originalMessage = event.getMessage();

            StringBuffer message = new StringBuffer();
            if (!emacsMode && event.getTask() != null) {
                String label = String.format("%12s", "[" + event.getTask().getTaskName() + "] ");
                //
                // Append label first
                //
                message.append(label);
                
                //
                // Format all output message's line separator
                //
                try {
                    BufferedReader r = new BufferedReader(new StringReader(originalMessage));
                    boolean ifFirstLine = true;
                    String line = null;
                    while ((line = r.readLine()) != null) {
                        if (!ifFirstLine) {
                            message.append(StringUtils.LINE_SEP);
                        }
                        ifFirstLine = false;
                        message.append(line);
                    }
                } catch (IOException e) {
                    message.append(originalMessage);
                }
            } else {
                message.append(originalMessage);
            }

            String msg = message.toString();
            if (currentLevel == Project.MSG_ERR) {
                printMessage(msg, err, currentLevel);
            } else if(currentLevel == Project.MSG_WARN) {
                printMessage(msg, out, currentLevel);
            } else if(!flag) {
                printMessage(msg, out, currentLevel);
            } 
            log(msg);
        }
    }
    
    public static void setCacheEnable(boolean enable) {
        flag = enable;
    }
    
    protected synchronized void log(String message) {
        //
        // cache log
        //
        if (map.containsKey(this.id)) {
            map.get(this.id).add(message);
        } else {
            List<String> list = new Vector<String>(1024);
            list.add(message);
            map.put(this.id, list);
        }
    }
    
    public Object clone() {
        GenBuildLogger newLogger = new GenBuildLogger();
        //
        // Transfer emacs mode, out, err, level to new Logger
        //
        newLogger.setEmacsMode(this.emacsMode);
        newLogger.setOutputPrintStream(this.out);
        newLogger.setErrorPrintStream(this.err);
        newLogger.setMessageOutputLevel(this.msgOutputLevel);
        
        //
        // Transfer project
        //
        newLogger.project = this.project;
        return newLogger;
    }

    public void setId(FpdModuleIdentification id) {
        this.id = id;
    }

    public void buildFinished(BuildEvent event) {
		if (this.msgOutputLevel >= Project.MSG_VERBOSE) {
			int level = this.msgOutputLevel;
			synchronized(semaphore){
			    this.msgOutputLevel = this.msgOutputLevel - 1;
			    super.buildFinished(event);
			    this.msgOutputLevel = level;
			}
		} else {
			super.buildFinished(event);
		}
    }
}