自制谷歌分析

时间:2012-10-12 11:11:55

标签: android event-log

由于Google分析可能引发许多隐私问题,因此我实施了一个事件记录器。

我的第一个想法是将用户生成的事件跟踪到日志文件中,然后将它们发送回服务器,该服务器将为系统管理员和应用工程师执行数据分析。

目前的想法是将Logger实例化为ApplicationService类,并使用这些元素onCreateonDestroy来安全地处理LogFile。 / p>

解决方案非常简单:

  1. 打开文件
  2. 每次生成事件时都附加到
  3. 一旦达到MAX_NUM_LINES,将日志发送到服务器(可能我会压缩我正在生成的文本文件)
  4. 我想知道在野外已经有什么东西已经烘烤了我不知道你可能知道(类似ACRA)。 每个贡献都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

这是我的实施。 然而,任何更好的版本都非常感谢。 TSG objet只是一个静态类,我用它作为时间管理器。

只要您重新发布/编辑修改,就可以使用代码并对其进行改进。

public class Logger {
    private BufferedWriter logFile;
    private String nameFile;
    public int fileLines;
    private File fTemp;
    private timeStampGenerator TSG;
    private int LOG_LINES_LIMIT = 100;
    private Object mutex;

    public enum EventType {
        BUTTON_PRESSED, 
        PAGE_VIEWED, 
        LOADED_ACTIVITY,  
        GENERIC_EVENT
    }

    public Logger (String fileName) throws IOException {
        nameFile = fileName;

        createLogFile();

        fileLines = countLines();

        TSG = new timeStampGenerator();

        // This is our mutex to access to the file
        mutex = new Object();
    }


    public void createLogFile() throws IOException{
        fTemp = new File (nameFile);

        if (!fTemp.exists()) {
            fTemp.createNewFile();
        }

        logFile = new BufferedWriter(new FileWriter(nameFile, true));

    }

    public void LogEvent(EventType event, String comment, String value) {

        String line = "";
        line += TSG.getTimestampMillis();
        line += ",";
        line += event.name();
        line += ",";

        if (comment != "") {
            line += comment.replaceAll(",", ";");
        } else {
            line += " ";
        }

        line += ",";

        if (value != "") {
            line += value.replaceAll(",", ";");
        } else {
            line += " ";
        }

        line += "\n";

        synchronized (mutex) {
            try {
                logFile.append(line);
            } catch (IOException e) {
                // Do wathever you want here
            }
            fileLines++;
        }
    }


    public int countLines() //throws IOException 
    {
        InputStream is;
        try {
            is = new BufferedInputStream(new FileInputStream(nameFile));
        } catch (FileNotFoundException e1) {
            //let's consider it an empty file
            return 0;
        }


        int count = 0;
        boolean empty = true;


        try {
            int readChars = 0;

            byte[] c = new byte[1024];

            while ((readChars = is.read(c)) != -1) {
                empty = false;
                for (int i = 0; i < readChars; ++i) {
                    if (c[i] == '\n')
                        ++count;
                }
            }
        } catch(IOException e) {
            // Do wathever you want here
        }


        try {
            is.close();
        } catch (IOException e) {
            // Do wathever you want here
        }

        return (count == 0 && !empty) ? 1 : count;
    }


    public boolean isLimitReached() {

        return (fileLines >= LOG_LINES_LIMIT);

    }


    public void close () {
        flush();
        try {
            logFile.close();
        } catch (IOException e) {
            // Do wathever you want here
        }
    }



    /** 
     * clear the content of the file
     */
    public void clearFile() {
        synchronized (mutex) {
            if ( fTemp.delete() ) {
                try {
                    createLogFile();
                } catch (IOException e1) {
                    // Do wathever you want here
                }
            }

        }
    }


    /**
     *  Get the full content of the file
     * @return the content
     */
    public String getContent() {

        StringBuffer fileData = new StringBuffer();

        synchronized (mutex) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader( nameFile ));
                char[] buf = new char[1024];
                int numRead = 0;
                while ((numRead = reader.read(buf)) != -1) {
                    String readData = String.valueOf(buf, 0, numRead);
                    fileData.append(readData);
                }
                reader.close();
            } catch (IOException e) {
                // Do wathever you want here
            }
        }

        return fileData.toString();
    }

    public void flush() {
        try {
            logFile.flush();
        } catch (IOException e) {
            // Do wathever you want here
    }
    }
}