All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.util.MyLogUtil Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
/*
 * Copyright 2018 jw362j.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hadoopz.MyDroidLib.util;

import android.os.Environment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.util.Log;
import com.mycomm.MyConveyor.core.MyConveyor;
import com.mycomm.MyConveyor.core.TaskRequest;
import com.mycomm.MyConveyor.core.TaskType;
import com.mycomm.MyConveyor.core.Tasker;

/**
 * @author BaoHang
 * @version 1.0
 * @data 2012-2-20
 */
class MyLogUtil {

    private static final String log_flag_runtime = "d33600715c5e79c155e268fbf99556tt.txt";
    private static int log_flag = -100;
    private static boolean log_runtime_switcher;
    private static final Boolean MYLOG_SWITCH = true; // 日志文件总开关
    private static final Boolean MYLOG_WRITE_TO_FILE = true;// 日志写入文件开关
    private static final char MYLOG_TYPE = 'v';// 输入日志类型,w代表只输出告警信息等,v代表输出所有信息
    private static final String MYLOG_PATH_SDCARD_DIR = "/sdcard/mylog/";// 日志文件在sdcard中的路径
    private static final int SDCARD_LOG_FILE_SAVE_DAYS = 0;// sd卡中日志文件的最多保存天数
    private static final String MYLOGFILEName = "-Log.log";// 本类输出的日志文件名称
    private static final SimpleDateFormat myLogSdf = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");// 日志的输出格式
    private static final SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式

    public static void w(String tag, Object msg) {
        log(tag, msg.toString(), 'w');
    }

    public static void e(String tag, Object msg) {
        log(tag, msg.toString(), 'e');
    }

    public static void d(String tag, Object msg) {
        log(tag, msg.toString(), 'd');
    }

    public static void i(String tag, Object msg) {
        log(tag, msg.toString(), 'i');
    }

    public static void v(String tag, Object msg) {
        log(tag, msg.toString(), 'v');
    }

    public static void w(String tag, String text) {
        log(tag, text, 'w');
    }

    public static void e(String tag, String text) {
        log(tag, text, 'e');
    }

    public static void d(String tag, String text) {
        log(tag, text, 'd');
    }

    public static void i(String tag, String text) {
        log(tag, text, 'i');
    }

    public static void v(String tag, String text) {
        log(tag, text, 'v');
    }

    /**
     * 根据tag, msg和等级,输出日志
     *
     * @param tag
     * @param msg
     * @param level
     * @return void
     * @since v 1.0
     */
    private static void log(final String tag, final String msg, final char level) {
        if (MYLOG_SWITCH) {
            if ('e' == level && ('e' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) { // 输出错误信息
                Log.e(tag, msg);
            } else if ('w' == level && ('w' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.w(tag, msg);
            } else if ('d' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.d(tag, msg);
            } else if ('i' == level && ('d' == MYLOG_TYPE || 'v' == MYLOG_TYPE)) {
                Log.i(tag, msg);
            } else {
                Log.v(tag, msg);
            }
            if (MYLOG_WRITE_TO_FILE) {

                if (log_flag == -100) {
                    log_flag = 1;
                    if (isFlagExist()) {
                        log_runtime_switcher = true;
                    } else {
                        return;
                    }
                }
                if (!log_runtime_switcher) {
                    return;
                }
                if (msg == null) {
                    return;
                }
                executeLogTask(new Tasker() {
                    public void onTask() {
                        writeLogtoFile(String.valueOf(level), tag, msg);
                    }
                });

            }
        }
    }

    private static boolean isFlagExist() {
        boolean isCardExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        if (!isCardExist) {
            return false;
        }
        File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + log_flag_runtime);
        if (f.exists()) {
            
            File file = new File(MYLOG_PATH_SDCARD_DIR);
            if (!file.exists()) {
                file.mkdirs();
            }
            
            return true;
        }
        return false;
    }

    /**
     * 打开日志文件并写入日志
     *
     * @return *
     */
    private static void writeLogtoFile(String mylogtype, String tag, String text) {// 新建或打开日志文件
        Date nowtime = new Date();
        String needWriteFiel = logfile.format(nowtime);
        String needWriteMessage = myLogSdf.format(nowtime) + "    " + mylogtype
                + "    " + tag + "    " + text;
        File file = new File(MYLOG_PATH_SDCARD_DIR, needWriteFiel + MYLOGFILEName);

        try {
            FileWriter filerWriter = new FileWriter(file, true);//后面这个参数代表是不是要接上文件中原来的数据,不进行覆盖
            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
            bufWriter.write(needWriteMessage);
            bufWriter.newLine();
            bufWriter.close();
            filerWriter.close();
        } catch (IOException e) {
//            e.printStackTrace();
            Log.e(tag, e.getMessage());
        }
    }

    /**
     * 删除制定的日志文件
     *
     */
    public static void delFile() {// 删除日志文件
        String needDelFiel = logfile.format(getDateBefore());
        File file = new File(MYLOG_PATH_SDCARD_DIR, needDelFiel + MYLOGFILEName);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 得到现在时间前的几天日期,用来得到需要删除的日志文件名
     *
     */
    private static Date getDateBefore() {
        Date nowtime = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(nowtime);
        now.set(Calendar.DATE, now.get(Calendar.DATE)
                - SDCARD_LOG_FILE_SAVE_DAYS);
        return now.getTime();
    }

    private static void executeLogTask(final Tasker tasker) {
        MyConveyor.getInstance().execute(new TaskRequest() {
            public TaskType getTaskType() {
                return TaskType.TASK_LOG;
            }

            public Tasker getTask() {
                return tasker;
            }
        });
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy