summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Log.java
blob: 49a7a3615bc8e14b0804935d41286825176f0ea3 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/** @file
 
 The file is used to provides static interfaces to save log and error information 
 
 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.
 
 **/

package org.tianocore.frameworkwizard.common;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;

import org.tianocore.frameworkwizard.FrameworkWizardUI;
import org.tianocore.frameworkwizard.workspace.Workspace;

/**
 The class is used to provides static interfaces to save log and error information
 
 **/
public class Log {
    //
    //Log file directory path
    //
    private static String strLogDir = Workspace.getCurrentWorkspace() + DataType.FILE_SEPARATOR + "Tools"
                                      + DataType.FILE_SEPARATOR + "Logs";

    //
    //Log file
    //
    private static File fleLogFile = null;

    //
    //Wrn file
    //
    private static File fleWrnFile = null;

    //
    //Err file
    //
    private static File fleErrFile = null;

    //
    //Log file name
    //
    private static String strLogFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.log";

    //
    //Wrn file name
    //
    private static String strWrnFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.wrn";

    //
    //Err file name
    //
    private static String strErrFileName = strLogDir + DataType.FILE_SEPARATOR + "frameworkwizard.err";

    //
    //Flag for create log or not
    //
    private static boolean isSaveLog = false;

    /**
     Main class, used for test
     
     @param args
     
     **/
    public static void main(String[] args) {
        try {
            //Log.log("Test", "test");
            //Log.err("Test1", "test1");
            Log
               .wrn("aaa bbbbbb cccccccccccc ddddddddddd eeeeeeeeee fffffffffff gggggggggggggggggg hhhhhhhhhhhhhhhhhhhhhhhhhhhhh iiiii jjjj kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk lll mmm nn poooooooooooooooooooooooooooooooooooooooooooop");
            Log.wrn("Incorrect data type for ModuleEntryPoint");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToLogFile to save log item and log information to log file
     
     @param strItem The log item
     @param strLog The log information
     
     **/
    public static void log(String strItem, String strLog) {
        try {
            writeToLogFile(strItem + ":" + strLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToLogFile to save log information to log file
     
     @param strLog The log information
     
     **/
    public static void log(String strLog) {
        try {
            writeToLogFile(strLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToWrnFile to save wrn item and wrn information to wrn file
     
     @param strItem The wrn item
     @param strLog The wrn information
     
     **/
    public static void wrn(String strItem, String strWrn) {
        try {
            writeToWrnFile("Warning when " + strItem + "::" + strWrn);
            showWrnMessage(strWrn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToWrnFile to save wrn information to wrn file
     
     @param strLog The wrn information
     
     **/
    public static void wrn(String strWrn) {
        try {
            writeToWrnFile("Warning::" + strWrn);
            showWrnMessage("Warning::" + strWrn);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToErrFile to save err item and err information to err file
     
     @param strItem The err item
     @param strLog The err information
     
     **/
    public static void err(String strItem, String strErr) {
        try {
            writeToErrFile("Error when " + strItem + "::" + strErr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Call writeToErrFile to save err information to err file
     
     @param strLog The err information
     
     **/
    public static void err(String strErr) {
        try {
            writeToErrFile("Error::" + strErr);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     Brings up a dialog to show err message
     When the message's length > defined max length, wrap the text to the next line.
     
     @param strErr The input data of err message
     
     **/
    private static void showWrnMessage(String strErr) {
        String strReturn = Tools.wrapStringByWord(strErr);
        JOptionPane.showConfirmDialog(FrameworkWizardUI.getInstance(), strReturn, "Warning",
                                      JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
    }

    /**
     Open log file and write log information
     
     @param strLog The log information
     @throws IOException
     
     **/
    private static void writeToLogFile(String strLog) throws Exception {
        if (isSaveLog) {
            try {
                createLogDir();
                if (fleLogFile == null) {
                    fleLogFile = new File(strLogFileName);
                    fleLogFile.delete();
                    fleLogFile.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(fleLogFile, true);
                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     Open wrn file and write wrn information
     
     @param strLog The log information
     * @throws Exception 
     
     **/
    private static void writeToWrnFile(String strLog) throws Exception {
        if (isSaveLog) {
            try {
                createLogDir();
                if (fleWrnFile == null) {
                    fleWrnFile = new File(strWrnFileName);
                    fleWrnFile.delete();
                    fleWrnFile.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(fleWrnFile, true);
                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     Open err file and write err information
     
     @param strLog The log information
     @throws IOException
     
     **/
    private static void writeToErrFile(String strLog) throws Exception {
        if (isSaveLog) {
            try {
                createLogDir();
                if (fleErrFile == null) {
                    fleErrFile = new File(strErrFileName);
                    fleErrFile.delete();
                    fleErrFile.createNewFile();
                }
                FileOutputStream fos = new FileOutputStream(fleErrFile, true);
                fos.write((Tools.getCurrentDateTime() + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.write((strLog + DataType.DOS_LINE_SEPARATOR).getBytes());
                fos.flush();
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     Check if directory for Logs exists or not
     Create the directory if it doesn't exist  
     * @throws Exception 
     
     **/
    private static void createLogDir() throws Exception {
        File f = new File(strLogDir);
        if (!f.exists()) {
            FileOperation.newFolder(strLogDir);
        }
    }

    public static boolean isSaveLog() {
        return isSaveLog;
    }

    public static void setSaveLog(boolean isSaveLog) {
        Log.isSaveLog = isSaveLog;
    }
}