天天看點

Flex compiler API指南(第二章)

第二章 日志和報告

    Adobe Flex Complier API為您生成報告(Reports) 并提供在編譯過程中的進度和日志等資訊。

   使用自定義日志

    您可以使用編譯器擷取錯誤資訊。您可以自定義一個logger并為這個application指派logger,告訴這個application您正在編譯。

   使用自定義日志的具體步驟如下:

    1、建立一個JAVA類,實作flex2.tools.oem.Logger接口;例如:

// java/SimpleLogger.java

import flex2.tools.oem.Message;

import flex2.tools.oem.Logger;

import java.util.*;

  

public class SimpleLogger implements Logger {

    SimpleLogger() {

        System.out.println("----------------------------------");

    }

    public void log(Message msg, int errorCode, String source) {

        System.out.println(msg);

        System.out.println("----------------------------------");

    }

}
           

    這個類必須實作log方法,它有3個參數:message,errorCode和source。

    2、在您的JAVA應用裡,調用接口的setLogger()方法為這個Applocation指派logger;例如:

    application.setLogger(new SimpleLogger());

    如果您沒有調用setLogger()方法,則編譯器記錄log資訊到标準輸出。

    3、編譯并運作例子

    4、測試這個logger,在您的例子中加入一些錯誤的文法;例如:

<?xml version="1.0"?>

<!-- apps/ErrorTestApp.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>

        // Generates a warning because there is no return type.

        public function doSomethingWrong() {

            // Generates an error because x lacks a type.

            var x;

        }

    </mx:Script>

    <mx:Label text="Hello World"/>

</mx:Application>      

    例子可能輸入如下資訊:

---------------------------------- ERROR: Loading configuration file C:\home\dev\depot\flex\sdk\frameworks\flex-config.xml -------------------------------------------------------------------- ERROR: return value for function 'doSomethingWrong' has no type declaration. ---------------------------------- ERROR: variable 'x' has no type declaration. ----------------------------------

    使用消息類中的getLevel()、getPath(),getLine()和getColumn(),您可以獲得其他資訊,比如錯誤級别(error level)和錯誤地點(location of error),下面的例子比上面的例子輸出了更多的資訊:

// java/ComplexLogger.java

import flex2.tools.oem.Message;

import flex2.tools.oem.Logger;

import java.util.*;

import java.io.*;

  

public class ComplexLogger implements Logger {

    ComplexLogger() {

        String LOGFILENAME = "output.txt";

        try {

            System.setOut(new PrintStream(new 

                FileOutputStream(LOGFILENAME)));

        } catch (Exception e) { 

            System.out.println("There was an error creating 

                the log file.");

        }

        System.out.println("Ran at : " + new Date());

        System.out.println("----------------------------------");

    }

    public void log(Message msg, int errorCode, String source) {

        if (msg.getLevel() == "info") {

            // Suppress info messages.

        } else {

            System.out.println("ERROR  : " + errorCode);

            System.out.println("MESSAGE: " + msg);

            System.out.println("SOURCE : " + source);

            System.out.println("LEVEL  : " + msg.getLevel());

            System.out.println("PATH   : " + msg.getPath());

            System.out.println("LINE   : " + msg.getLine());

            System.out.println("COLUMN : " + msg.getColumn());

            System.out.println("----------------------------------");

        }

    }

}
           

    不是所有的編譯錯誤都有明确的解釋。例如,在您的MXML代碼中發生錯誤,就沒有錯誤碼,是以Logger傳回 -1 。 而AS代碼,無論何時,都會傳回錯誤代碼。

    您也可以使用Report Class在編譯正在執行的時候檢視Message對象。

   建立報告

    Flex compiler API包括了對application、library或正在編譯的項目建立報告的能力。在報告中,您可以包含應用的詳細資訊,資訊包括連結、資源、依賴、庫和資源綁定(resource bundle)等,也包括背景顔色,高度,寬度等應用程式的增量資訊。

    您可以用Application和Library對象的getReport()方法來獲得報告。這個方法傳回一個Report類執行個體,使用它就可以輸出關于編譯目标的資訊。

    在您使用Report類生成報告之前,您必須建構應用程式或庫,這意味着您必須在調用任何報告t方法之前調用build()方法。

    下面的例子将輸出報告資訊到标準輸出:

// java/MyReportCompiler.java

import flex2.tools.oem.Application;

import flex2.tools.oem.Report;

import flex2.tools.oem.Configuration;

import flex2.tools.oem.Logger;

import java.io.*;

public class MyReportCompiler {

    public static void main(String[] args) {

        String assetRoot = "../assets/";

        String outputRoot = "../apps/";

        File[] themeFile = new File[] 

            {new File(assetRoot, "myTheme.css")};

        File[] libFile = new File[] 

            {new File(assetRoot, "MyComponents.swc")};

        try {

            Application application = new 

                Application(new File(outputRoot, 

                "TestAppWithAllAssets.mxml"));

            application.setOutput(new File(outputRoot, 

                "TestAppWithAllAssets.swf"));

            

            application.setLogger(new ComplexLogger());

            application.setProgressMeter(new MyProgressMeter());

            Configuration config = application.getDefaultConfiguration();

            config.setTheme(themeFile); 

            config.addLibraryPath(libFile);

            application.setConfiguration(config);

            application.build(true);              

            Report report = application.getReport();

            // Lists the image files that are embedded.

            System.out.println("\n\nEMBEDDED ASSETS: ");

            String[] cnames = report.getAssetNames(Report.COMPILER);

            for (int i=0; i<cnames.length; i++) {

                System.out.println(cnames[i]);         

            }

            // Lists the libraries that are used.

            System.out.println("\nLIBRARIES: ");

            String[] libs = report.getLibraryNames(Report.COMPILER);

            for (int i=0; i<libs.length; i++) {

                System.out.println(libs[i]);         

            }

                        // Lists source files, their definition names, and dependencies.

            System.out.println("\nSOURCE NAMES: ");

            String[] list = report.getSourceNames(Report.COMPILER);

            for (int i=0; i<list.length; i++) {

                System.out.println(list[i]);         

                String[] defs = report.getDefinitionNames(list[i]);

                System.out.println("DEFINITIONS: ");

                for (int j=0; j<defs.length; j++) {

                    System.out.println(defs[j]);         

                    

                    System.out.println("   DEPENDENCIES: ");

                    String[] deps = report.getDependencies(defs[j]);

                    for (int k=0; k<deps.length; k++) {

                        System.out.println("      " + deps[k]);         

                    }

                    System.out.println("   PREREQS: ");

                    String[] prereqs = report.getPrerequisites(defs[j]);

                    for (int k=0; k<prereqs.length; k++) {

                        System.out.println("      " + prereqs[k]);         

                    }

                }

            }   

            // Get application info.

            System.out.println("\nAPPLICATION INFO: ");

            System.out.println("   Background Color: " + 

                "0x" + Integer.toHexString(report.getBackgroundColor()).

                    toUpperCase());

            System.out.println("   Height          : " + 

                report.getDefaultHeight() + 

                " (" + 

                Math.round(100 * report.getHeightPercent()) + 

                "%)");

            System.out.println("   Width           : " + 

                report.getDefaultWidth() + 

                " (" + 

                Math.round(100 * report.getWidthPercent()) + 

                "%)");

            System.out.println("   Page Title      : " + 

                report.getPageTitle());

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

}
           

    輸出資訊大體如下:

EMBEDDED ASSETS: C:\home\depot\EN\Docs\Flex\Flex2next\oem_kit\code\assets\bird-gray.gifC:\home\depot\EN\Docs\Flex\Flex2next\oem_kit\code\assets\bird-silly.gif C:\home\depot\EN\Docs\Flex\Flex2next\oem_kit\code\assets\bird.gif LIBRARIES: C:\home\depot\EN\Docs\Flex\Flex2next\oem_kit\code\assets\MyComponents.swc C:\home\dev\depot\flex\sdk\bundles\en_US\charts_rb.swc C:\home\dev\depot\flex\sdk\bundles\en_US\framework_rb.swc C:\home\dev\depot\flex\sdk\frameworks\libs\flex.swc C:\home\dev\depot\flex\sdk\frameworks\libs\framework.swc C:\home\dev\depot\flex\sdk\frameworks\libs\playerglobal.swc SOURCE NAMES: C:\home\depot\EN\Docs\Flex\Flex2next\oem_kit\code\apps\TestAppWithAllAssets .mxml DEFINITIONS: TestAppWithAllAssets    DEPENDENCIES:       AS3       MyButton       MyLabel       mx.controls:Image       mx.controls:Label       mx.core:UIComponentDescriptor       mx.core:mx_internal       mx.events:PropertyChangeEvent       mx.styles:CSSStyleDeclaration       mx.styles:StyleManager    PREREQS:       mx.core:Application APPLICATION INFO:    Background Color: 0x869CA7    Height          : 375 (75%)    Width           : 500 (100%)    Page Title      : Test App With All Assets

   使用報告檢視消息(Message)

    您也可以在編譯期間獲得消息資訊,消息級别包括資訊(info),警告(warning)和錯誤(error),您可以使用Reoprt類的getMessages()方法通路Message對象數組:

// java/MySimpleReportCompiler.java

import flex2.tools.oem.Application;

import flex2.tools.oem.Report;

import flex2.tools.oem.Message;

import java.io.*;

public class MySimpleReportCompiler {

    public static void main(String[] args) {

        try {

            Application application = new Application(new 

                File("../apps/ErrorTestApp.mxml"));

            application.setOutput(new File("../apps/ErrorTestApp.swf"));

            application.build(true);              

            Report report = application.getReport();           

            Message[] m = report.getMessages();            

            for (int i=0; i<m.length; i++) {

                System.out.println(m[i].getLevel().toUpperCase() + 

                    " MESSAGE " + i + ": " + m[i]);

            }            

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

}
           

   觀察進度

    Flex Complier API 包含一個進度尺,它可以讓您輕松觀察進度。

    1、使用進度尺:

// java/MyProgressMeter.java

import flex2.tools.oem.ProgressMeter;

public class MyProgressMeter implements ProgressMeter {

    long before, after;

    MyProgressMeter() {

    }

    public void start() {

        before = System.currentTimeMillis();

        System.out.print("begin...");

    }

    public void end() {

        after = System.currentTimeMillis();

        System.out.println("done");

        System.out.println("Elapsed Time: " + (after - before) + "ms");

    }

    public void percentDone(int n) {

        System.out.print(n + "...");

    }

}
           

    這個類必須實作start(), end() 和 persentDone()方法,這些方法加入了在編譯過程中的計算時間的邏輯

    2、在編譯Flex應用程式的JAVA應用中,調用setProgressMeter()方法為應用程式指派一個進度尺;例如:

        application.setProgressMeter(new MyProgressMeter());

    3、編譯這個應用程式,進度尺大體輸出如下:

begin...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...7 2...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 ...done