package com.ea.nimble; import android.content.pm.PackageInfo; import android.os.Environment; import android.support.v4.media.session.PlaybackStateCompat; import androidx.core.app.NotificationCompat; import com.ea.nimble.ILog; import com.ironsource.u3; 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; /* loaded from: classes2.dex */ 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; public void disconnectFromCore() { this.m_core = null; } @Override // com.ea.nimble.Component public String getComponentId() { return Log.COMPONENT_ID; } @Override // com.ea.nimble.ILog public int getThresholdLevel() { return this.m_level; } @Override // com.ea.nimble.ILog public void setLogCallback(ILog.LogCallback logCallback) { this.m_callback = logCallback; } @Override // com.ea.nimble.ILog public void setThresholdLevel(int i) { this.m_level = i; } public 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 * PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID) { return; } LogImpl.this.clearLog(); } } public static class LogRecord { public int level; public String message; private LogRecord() { } } @Override // com.ea.nimble.Component public void setup() { configure(); } @Override // com.ea.nimble.Component public void suspend() { Timer timer = this.m_guardTimer; if (timer != null) { timer.pause(); } } @Override // com.ea.nimble.Component public void resume() { Timer timer = this.m_guardTimer; if (timer != null) { timer.fire(); this.m_guardTimer.resume(); } } @Override // com.ea.nimble.Component public void teardown() { Timer timer = this.m_guardTimer; if (timer != null) { timer.cancel(); this.m_guardTimer = null; } FileOutputStream fileOutputStream = this.m_logFileStream; if (fileOutputStream != null) { try { fileOutputStream.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 String getLogFilePath() { File file = this.m_filePath; if (file != null) { return file.toString(); } return null; } @Override // com.ea.nimble.ILog public void writeWithSource(int i, Object obj, String str, Object... objArr) { String name; if (obj instanceof LogSource) { name = ((LogSource) obj).getLogSourceTitle(); } else { name = obj != null ? obj.getClass().getName() : ""; } writeWithTitle(i, name, 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); ILog.LogCallback logCallback = this.m_callback; if (logCallback != null) { logCallback.callback(i, str, str2); } } public void connectToCore(BaseCore baseCore) { this.m_core = baseCore; configure(); flushCache(); } 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; String.format("LOG: Default Log level(%d) without log configuration file", Integer.valueOf(parseLevel)); return; } return; } int parseLevel2 = parseLevel(settings.get("Level")); if (parseLevel2 != this.m_level) { this.m_level = parseLevel2; String.format("LOG: Log level(%d)", Integer.valueOf(parseLevel2)); } if (this.m_level <= 100) { this.m_messageLengthLimit = 0; } else { String str = settings.get("MessageLengthLimit"); if (str == null) { this.m_messageLengthLimit = 1000; } else { try { int parseInt = Integer.parseInt(str); this.m_messageLengthLimit = parseInt; if (parseInt < 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; return; } return; } String str3 = settings.get("Location"); StringBuilder sb = new StringBuilder(); sb.append(ApplicationEnvironment.getComponent().getCachePath()); String str4 = File.separator; sb.append(str4); sb.append(str2); String sb2 = sb.toString(); if (Utility.validString(str3) && str3.equalsIgnoreCase(u3.e) && Environment.getExternalStorageState().equals("mounted")) { String name = ApplicationEnvironment.getCurrentActivity().getClass().getPackage().getName(); PackageInfo packageInfo = Utility.getPackageInfo(0); if (packageInfo != null) { name = packageInfo.packageName; } File file = new File(Environment.getExternalStorageDirectory(), name); boolean exists = file.exists(); if (!exists) { exists = file.mkdir(); } if (exists) { sb2 = file + str4 + str2; } } File file2 = new File(sb2); if (file2 != this.m_filePath) { this.m_filePath = file2; try { this.m_logFileStream = new FileOutputStream(this.m_filePath, true); StringBuilder sb3 = new StringBuilder(); sb3.append("LOG: File path: "); sb3.append(this.m_filePath.toString()); } catch (FileNotFoundException unused2) { android.util.Log.e(Global.NIMBLE_ID, "LOG: Can't create log file at " + sb2); 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 { int parseInt2 = Integer.parseInt(settings.get("FileCheckInterval")); this.m_interval = parseInt2; if (parseInt2 <= 0) { this.m_interval = 3600; } } catch (NumberFormatException unused3) { this.m_interval = 3600; } try { int parseInt3 = Integer.parseInt(settings.get("MaxFileSize")); this.m_sizeLimit = parseInt3; if (parseInt3 <= 0) { this.m_sizeLimit = 1024; } } catch (NumberFormatException unused4) { this.m_sizeLimit = 1024; } GuardTask guardTask = new GuardTask(); guardTask.run(); Timer timer = new Timer(guardTask); this.m_guardTimer = timer; timer.schedule(this.m_interval, true); } private void flushCache() { Iterator it = this.m_cache.iterator(); while (it.hasNext()) { LogRecord next = it.next(); int i = next.level; if (i >= this.m_level) { writeLine(i, 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 600; } if (str.equalsIgnoreCase(NotificationCompat.GROUP_KEY_SILENT)) { return 700; } } 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) { outputMessageToFile(formatLine[i2]); i2++; } } else if (i == 100) { String[] formatLine2 = formatLine("NIM_VERBOSE", str); int length2 = formatLine2.length; while (i2 < length2) { outputMessageToFile(formatLine2[i2]); i2++; } } else if (i == 200) { String[] formatLine3 = formatLine("NIM_DEBUG", str); int length3 = formatLine3.length; while (i2 < length3) { outputMessageToFile(formatLine3[i2]); i2++; } } else if (i == 300) { String[] formatLine4 = formatLine("NIM_INFO", str); int length4 = formatLine4.length; while (i2 < length4) { outputMessageToFile(formatLine4[i2]); i2++; } } else if (i == 400) { String[] formatLine5 = formatLine("NIM_WARN", str); int length5 = formatLine5.length; while (i2 < length5) { String str2 = formatLine5[i2]; android.util.Log.w(Global.NIMBLE_ID, str2); outputMessageToFile(str2); i2++; } } else if (i == 500) { String[] formatLine6 = formatLine("NIM_ERROR", str); int length6 = formatLine6.length; while (i2 < length6) { String str3 = formatLine6[i2]; android.util.Log.e(Global.NIMBLE_ID, str3); outputMessageToFile(str3); i2++; } } else if (i == 600) { String[] formatLine7 = formatLine("NIM_FATAL", str); String str4 = formatLine7[0]; while (i2 < formatLine7.length - 1) { android.util.Log.e(Global.NIMBLE_ID, str4); outputMessageToFile(str4); i2++; str4 = formatLine7[i2]; } android.util.Log.wtf(Global.NIMBLE_ID, str4); outputMessageToFile(str4); } else { String[] formatLine8 = formatLine(String.format("NIM(%d)", Integer.valueOf(i)), str); String str5 = formatLine8[0]; while (i2 < formatLine8.length - 1) { android.util.Log.e(Global.NIMBLE_ID, str5); outputMessageToFile(str5); i2++; str5 = formatLine8[i2]; } android.util.Log.wtf(Global.NIMBLE_ID, str5); outputMessageToFile(str5); } 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 = this.m_messageLengthLimit; int i2 = 0; if (length > i && i != 0) { str3 = str3.substring(0, this.m_messageLengthLimit) + String.format("... and %d chars more", Integer.valueOf(length - i)); length = str3.length(); } String[] strArr = new String[(int) Math.ceil(length / 4000.0d)]; while (i2 < length) { int i3 = i2 + DEFAULT_CONSOLE_OUTPUT_LIMIT; if (i3 < length) { strArr[i2 / DEFAULT_CONSOLE_OUTPUT_LIMIT] = str3.substring(i2, i3); } else { strArr[i2 / DEFAULT_CONSOLE_OUTPUT_LIMIT] = str3.substring(i2); } i2 = i3; } 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(Charset.forName("UTF-8"))); this.m_logFileStream.flush(); } catch (IOException e) { android.util.Log.e(Global.NIMBLE_ID, "Error writing to log file: " + e.toString()); } } } }