001package com.gigya.android.sdk; 002 003import android.Manifest; 004import android.content.Context; 005import android.content.pm.PackageManager; 006import android.os.Environment; 007import android.support.v4.content.ContextCompat; 008import android.util.Log; 009 010import java.io.BufferedWriter; 011import java.io.File; 012import java.io.FileInputStream; 013import java.io.FileOutputStream; 014import java.io.FileWriter; 015import java.io.IOException; 016import java.nio.channels.FileChannel; 017 018/** 019 * Gigya custom Android logger class. 020 * Logs are issued only when DEBUG is set to TRUE. 021 */ 022public class GigyaLogger { 023 024 private static final String LOG_TAG = "GigyaSDK"; 025 private static boolean DEBUG = false; 026 public static boolean IOC = false; 027 028 public static void setDebugMode(boolean debugModeEnabled) { 029 DEBUG = debugModeEnabled; 030 } 031 032 public static boolean isDebug() { 033 return DEBUG; 034 } 035 036 public static void debug(String tag, String message) { 037 if (DEBUG) { 038 final String logMessage = logMessage(tag, message); 039 Log.d(LOG_TAG, logMessage); 040 if (smartLogEnabled()) { 041 appendLog(logMessage); 042 } 043 } 044 } 045 046 public static void error(String tag, String message) { 047 if (DEBUG) { 048 final String logMessage = logMessage(tag, message); 049 Log.e(LOG_TAG, logMessage); 050 if (smartLogEnabled()) { 051 appendLog(logMessage); 052 } 053 } 054 } 055 056 public static void info(String tag, String message) { 057 if (DEBUG) { 058 final String logMessage = logMessage(tag, message); 059 Log.i(LOG_TAG, logMessage); 060 if (smartLogEnabled()) { 061 appendLog(logMessage); 062 } 063 } 064 } 065 066 public static void ioc(String tag, String message) { 067 if (DEBUG && IOC) { 068 Log.d(LOG_TAG, logMessage(tag, message)); 069 } 070 } 071 072 private static String logMessage(String classTAG, String message) { 073 return "<< " + classTAG + " *** " + message + " >>"; 074 } 075 076 077 private static String _smartLoggerPath; 078 079 public static void enableSmartLog(Context appContext) { 080 _smartLoggerPath = appContext.getCacheDir().getAbsolutePath() + "/gsLog.file"; 081 } 082 083 private static boolean smartLogEnabled() { 084 return _smartLoggerPath != null; 085 } 086 087 @SuppressWarnings("ResultOfMethodCallIgnored") 088 private static void appendLog(String text) { 089 File logFile = new File(_smartLoggerPath); 090 091 try { 092 if (!logFile.exists()) { 093 logFile.createNewFile(); 094 } 095 int fileSize = Integer.parseInt(String.valueOf(logFile.length() / 1024)); 096 if (fileSize > 35) { 097 logFile.delete(); 098 logFile.createNewFile(); 099 } 100 } catch (IOException e) { 101 e.printStackTrace(); 102 Log.e("GigyaLogger", "Failed to create new Gigya log file"); 103 } 104 105 try { 106 //BufferedWriter for performance, true to set append to file flag 107 BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 108 buf.append(text); 109 buf.newLine(); 110 buf.close(); 111 } catch (IOException e) { 112 Log.e("GigyaLogger", "Failed to write to Gigya log file"); 113 e.printStackTrace(); 114 } 115 } 116 117 public static void exportSmartLog(Context context) { 118 final int externalStoragePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE); 119 if (externalStoragePermission == PackageManager.PERMISSION_GRANTED && _smartLoggerPath != null) { 120 File to = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/gsLog.txt"); 121 File from = new File(_smartLoggerPath); 122 try { 123 FileInputStream inStream = new FileInputStream(from); 124 FileOutputStream outStream = new FileOutputStream(to); 125 FileChannel inChannel = inStream.getChannel(); 126 FileChannel outChannel = outStream.getChannel(); 127 inChannel.transferTo(0, inChannel.size(), outChannel); 128 inStream.close(); 129 outStream.close(); 130 } catch (Exception ex) { 131 ex.printStackTrace(); 132 } 133 } 134 } 135 136}