package com.ea.nimble; import android.content.pm.PackageManager; import android.os.Environment; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Locale; import java.util.Map; import org.apache.http.HttpHeaders; import kotlin.text.Charsets; public class LogImpl extends Component implements ILog { private static final int DEFAULT_CHECK_INTERVAL = 3600; private static final int DEFAULT_CONSOLE_OUTPUT_LIMIT = 4000; private static final int DEFAULT_MESSAGE_LENGTH_LIMIT = 1000; private static final int DEFAULT_SIZE_LIMIT = 1024; private int m_messageLengthLimit; private int m_sizeLimit; private BaseCore m_core = null; private int m_level = 0; private File m_filePath = null; private FileOutputStream m_logFileStream = null; private DateFormat m_format = null; private Timer m_guardTimer = null; private int m_interval = 0; private ArrayList m_cache = new ArrayList<>(); private ILog.LogCallback m_callback = null; @Override // com.ea.nimble.Component public String getComponentId() { return Log.COMPONENT_ID; } private class GuardTask implements Runnable { private GuardTask() { } @Override // java.lang.Runnable public void run() { if (LogImpl.this.m_filePath == null || LogImpl.this.m_filePath.length() <= LogImpl.this.m_sizeLimit) { return; } LogImpl.this.clearLog(); } } private static class LogRecord { public int level; public String message; private LogRecord() { } } LogImpl() { } @Override // com.ea.nimble.Component public void setup() { configure(); } @Override // com.ea.nimble.Component public void suspend() { if (this.m_guardTimer != null) { this.m_guardTimer.pause(); } } @Override // com.ea.nimble.Component public void resume() { if (this.m_guardTimer != null) { this.m_guardTimer.fire(); this.m_guardTimer.resume(); } } @Override // com.ea.nimble.Component public void teardown() { if (this.m_guardTimer != null) { this.m_guardTimer.cancel(); this.m_guardTimer = null; } if (this.m_logFileStream != null) { try { this.m_logFileStream.close(); } catch (IOException unused) { android.util.Log.e(Global.NIMBLE_ID, "LOG: Can't close log file"); } this.m_logFileStream = null; } } @Override // com.ea.nimble.ILog public int getThresholdLevel() { return this.m_level; } @Override // com.ea.nimble.ILog public void setThresholdLevel(int i) { this.m_level = i; } @Override // com.ea.nimble.ILog public String getLogFilePath() { if (this.m_filePath != null) { return this.m_filePath.toString(); } return null; } @Override // com.ea.nimble.ILog public void setLogCallback(ILog.LogCallback logCallback) { this.m_callback = logCallback; } @Override // com.ea.nimble.ILog public void writeWithSource(int i, Object obj, String str, Object... objArr) { String str2 = ""; if (obj instanceof LogSource) { str2 = ((LogSource) obj).getLogSourceTitle(); } else if (obj != null) { str2 = obj.getClass().getName(); } writeWithTitle(i, str2, str, objArr); } @Override // com.ea.nimble.ILog public void writeWithTitle(int i, String str, String str2, Object... objArr) { if (i < this.m_level || !Utility.validString(str2)) { return; } if (objArr.length > 0) { str2 = String.format(str2, objArr); } write(i, str, str2); if (this.m_callback != null) { this.m_callback.callback(i, str, str2); } } protected void connectToCore(BaseCore baseCore) { this.m_core = baseCore; configure(); flushCache(); } protected void disconnectFromCore() { this.m_core = null; } private void configure() { boolean z = this.m_level == 0; Map settings = this.m_core.getSettings(BaseCore.NIMBLE_LOG_SETTING); if (settings == null) { int parseLevel = parseLevel(null); if (parseLevel != this.m_level) { this.m_level = parseLevel; android.util.Log.i(Global.NIMBLE_ID, String.format("LOG: Default Log level(%d) without log configuration file", Integer.valueOf(this.m_level))); return; } return; } int parseLevel2 = parseLevel(settings.get("Level")); if (parseLevel2 != this.m_level) { this.m_level = parseLevel2; android.util.Log.i(Global.NIMBLE_ID, String.format("LOG: Log level(%d)", Integer.valueOf(this.m_level))); } if (this.m_level <= 100) { this.m_messageLengthLimit = 0; } else { String str = settings.get("MessageLengthLimit"); if (str == null) { this.m_messageLengthLimit = 1000; } else { try { this.m_messageLengthLimit = Integer.parseInt(str); if (this.m_messageLengthLimit < 0) { this.m_messageLengthLimit = 1000; } } catch (NumberFormatException unused) { this.m_messageLengthLimit = 1000; } } } String str2 = settings.get("File"); if (!Utility.validString(str2)) { if (z || this.m_filePath != null) { this.m_filePath = null; this.m_logFileStream = null; this.m_interval = 0; android.util.Log.i(Global.NIMBLE_ID, "LOG: Disable log to file since no filename provided"); return; } return; } String str3 = settings.get(HttpHeaders.LOCATION); String str4 = ApplicationEnvironment.getComponent().getCachePath() + File.separator + str2; if (Utility.validString(str3) && str3.equalsIgnoreCase("external") && Environment.getExternalStorageState().equals("mounted")) { String name = ApplicationEnvironment.getCurrentActivity().getClass().getPackage().getName(); try { PackageManager packageManager = ApplicationEnvironment.getCurrentActivity().getPackageManager(); if (packageManager != null) { name = packageManager.getPackageInfo(ApplicationEnvironment.getCurrentActivity().getPackageName(), 0).packageName; } } catch (Exception unused2) { } File file = new File(Environment.getExternalStorageDirectory(), name); boolean exists = file.exists(); if (!exists) { exists = file.mkdir(); } if (exists) { str4 = file + File.separator + str2; } } File file2 = new File(str4); if (file2 != this.m_filePath) { this.m_filePath = file2; try { this.m_logFileStream = new FileOutputStream(this.m_filePath, true); android.util.Log.d(Global.NIMBLE_ID, "LOG: File path: " + this.m_filePath.toString()); } catch (FileNotFoundException unused3) { android.util.Log.e(Global.NIMBLE_ID, "LOG: Can't create log file at " + str4); this.m_filePath = null; return; } } String str5 = settings.get("DateFormat"); if (str5 != null && str5.length() > 0) { this.m_format = new SimpleDateFormat(str5, Locale.getDefault()); } else { this.m_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()); } try { this.m_interval = Integer.parseInt(settings.get("FileCheckInterval")); if (this.m_interval <= 0) { this.m_interval = DEFAULT_CHECK_INTERVAL; } } catch (NumberFormatException unused4) { this.m_interval = DEFAULT_CHECK_INTERVAL; } try { this.m_sizeLimit = Integer.parseInt(settings.get("MaxFileSize")); if (this.m_sizeLimit <= 0) { this.m_sizeLimit = 1024; } } catch (NumberFormatException unused5) { this.m_sizeLimit = 1024; } GuardTask guardTask = new GuardTask(); guardTask.run(); this.m_guardTimer = new Timer(guardTask); this.m_guardTimer.schedule(this.m_interval, true); } private void flushCache() { Iterator it = this.m_cache.iterator(); while (it.hasNext()) { LogRecord next = it.next(); writeLine(next.level, next.message); } this.m_cache = null; } private int parseLevel(String str) { try { if (Utility.validString(str)) { int parseInt = Integer.parseInt(str); if (parseInt != 0) { return parseInt; } } } catch (NumberFormatException unused) { if (str.equalsIgnoreCase("all")) { return 0; } if (str.equalsIgnoreCase("verbose")) { return 100; } if (str.equalsIgnoreCase("debug")) { return 200; } if (str.equalsIgnoreCase("info")) { return 300; } if (str.equalsIgnoreCase("warn")) { return 400; } if (str.equalsIgnoreCase("error")) { return 500; } if (str.equalsIgnoreCase("fatal")) { return Log.LEVEL_FATAL; } if (str.equalsIgnoreCase("silent")) { return Log.LEVEL_SILENT; } } return (this.m_core.getConfiguration() == NimbleConfiguration.INTEGRATION || this.m_core.getConfiguration() == NimbleConfiguration.STAGE) ? 100 : 500; } /* JADX INFO: Access modifiers changed from: private */ public void clearLog() { try { this.m_logFileStream.close(); this.m_logFileStream = new FileOutputStream(this.m_filePath, false); } catch (IOException unused) { android.util.Log.e(Global.NIMBLE_ID, "LOG: Can't clear log file"); } } private void writeLine(int i, String str) { int i2 = 0; if (i == 0) { String[] formatLine = formatLine("NIM_ALL", str); int length = formatLine.length; while (i2 < length) { String str2 = formatLine[i2]; android.util.Log.v(Global.NIMBLE_ID, str2); outputMessageToFile(str2); i2++; } } else if (i == 100) { String[] formatLine2 = formatLine("NIM_VERBOSE", str); int length2 = formatLine2.length; while (i2 < length2) { String str3 = formatLine2[i2]; android.util.Log.v(Global.NIMBLE_ID, str3); outputMessageToFile(str3); i2++; } } else if (i == 200) { String[] formatLine3 = formatLine("NIM_DEBUG", str); int length3 = formatLine3.length; while (i2 < length3) { String str4 = formatLine3[i2]; android.util.Log.d(Global.NIMBLE_ID, str4); outputMessageToFile(str4); i2++; } } else if (i == 300) { String[] formatLine4 = formatLine("NIM_INFO", str); int length4 = formatLine4.length; while (i2 < length4) { String str5 = formatLine4[i2]; android.util.Log.i(Global.NIMBLE_ID, str5); outputMessageToFile(str5); i2++; } } else if (i == 400) { String[] formatLine5 = formatLine("NIM_WARN", str); int length5 = formatLine5.length; while (i2 < length5) { String str6 = formatLine5[i2]; android.util.Log.w(Global.NIMBLE_ID, str6); outputMessageToFile(str6); i2++; } } else if (i == 500) { String[] formatLine6 = formatLine("NIM_ERROR", str); int length6 = formatLine6.length; while (i2 < length6) { String str7 = formatLine6[i2]; android.util.Log.e(Global.NIMBLE_ID, str7); outputMessageToFile(str7); i2++; } } else if (i == 600) { String[] formatLine7 = formatLine("NIM_FATAL", str); String str8 = formatLine7[0]; while (i2 < formatLine7.length - 1) { android.util.Log.e(Global.NIMBLE_ID, str8); outputMessageToFile(str8); i2++; str8 = formatLine7[i2]; } android.util.Log.wtf(Global.NIMBLE_ID, str8); outputMessageToFile(str8); } else { String[] formatLine8 = formatLine(String.format("NIM(%d)", Integer.valueOf(i)), str); String str9 = formatLine8[0]; while (i2 < formatLine8.length - 1) { android.util.Log.e(Global.NIMBLE_ID, str9); outputMessageToFile(str9); i2++; str9 = formatLine8[i2]; } android.util.Log.wtf(Global.NIMBLE_ID, str9); outputMessageToFile(str9); } if (i >= 600) { if (this.m_core.getConfiguration() == NimbleConfiguration.INTEGRATION || this.m_core.getConfiguration() == NimbleConfiguration.STAGE) { throw new AssertionError(str); } } } private void write(int i, String str, String str2) { String str3; if (Utility.validString(str)) { str3 = str + "> " + str2; } else { str3 = " " + str2; } if (this.m_cache != null) { LogRecord logRecord = new LogRecord(); logRecord.level = i; logRecord.message = str3; this.m_cache.add(logRecord); return; } writeLine(i, str3); } private String[] formatLine(String str, String str2) { String str3 = str + ">" + str2; int length = str3.length(); int i = 0; if (length > this.m_messageLengthLimit && this.m_messageLengthLimit != 0) { str3 = str3.substring(0, this.m_messageLengthLimit) + String.format("... and %d chars more", Integer.valueOf(length - this.m_messageLengthLimit)); length = str3.length(); } double d = length; Double.isNaN(d); String[] strArr = new String[(int) Math.ceil(d / 4000.0d)]; while (i < length) { int i2 = i + DEFAULT_CONSOLE_OUTPUT_LIMIT; if (i2 < length) { strArr[i / DEFAULT_CONSOLE_OUTPUT_LIMIT] = str3.substring(i, i2); } else { strArr[i / DEFAULT_CONSOLE_OUTPUT_LIMIT] = str3.substring(i); } i = i2; } return strArr; } private void outputMessageToFile(String str) { String property = System.getProperty("line.separator"); if (this.m_logFileStream != null) { try { this.m_logFileStream.write((this.m_format.format(new Date()) + " " + str + property).getBytes()); this.m_logFileStream.flush(); } catch (IOException e) { android.util.Log.e(Global.NIMBLE_ID, "Error writing to log file: " + e); } } } }