package com.intel.realsense.camera;

import android.util.Log;

import com.intel.realsense.librealsense.Device;
import com.intel.realsense.librealsense.DeviceList;
import com.intel.realsense.librealsense.Extension;
import com.intel.realsense.librealsense.FwLogMsg;
import com.intel.realsense.librealsense.FwLogParsedMsg;
import com.intel.realsense.librealsense.FwLogger;
import com.intel.realsense.librealsense.RsContext;

public class FwLogsThread extends Thread{
    private static final String TAG = "librs_fwLogsThread";
    private FwLogger mFwLoggerDevice;
    private volatile boolean mAreFwLogsRequested;
    private String mFwLogsParsingFilePath;
    private boolean mIsParsingFileInitialized = false;
    private boolean mAllFlashLogsAlreadyPulled = false;

    @Override
    public void run() {
        mAreFwLogsRequested = true;
        try(RsContext ctx = new RsContext()) {
            try (DeviceList devices = ctx.queryDevices()) {
                if (devices.getDeviceCount() > 0) {
                    // only device 0 is taken care of, as the camera app is supposed to work only with one device
                    try (Device device = devices.createDevice(0)) {
                        if (device != null) {
                            try (final FwLogger fwLoggerDevice = device.as(Extension.FW_LOGGER)) {
                                mFwLoggerDevice = fwLoggerDevice;
                                if (mIsParsingFileInitialized)
                                    mFwLoggerDevice.initParser(mFwLogsParsingFilePath);
                                while (mAreFwLogsRequested) {
                                    String logReceived = "";
                                    try (FwLogMsg logMsg = mFwLoggerDevice.getFwLog()) {
                                        if (logMsg.getSize() > 0) {
                                            if (mIsParsingFileInitialized) {
                                                try (FwLogParsedMsg parsedMsg = mFwLoggerDevice.parseFwLog(logMsg)) {
                                                    logReceived = parsedMsg.getTimestamp() + " - " +
                                                            parsedMsg.getSeverity() + " " +
                                                            parsedMsg.getFileName() + " " +
                                                            parsedMsg.getLine() + " " +
                                                            parsedMsg.getThreadName() + " " +
                                                            parsedMsg.getMessage();
                                                    Log.d(TAG, logReceived);
                                                }
                                            } else {
                                                logReceived = logMsg.getTimestamp() + " " +
                                                        logMsg.getSeverityStr() + " ";
                                                byte[] buffer = new byte[512];
                                                buffer = logMsg.getData(buffer);
                                                for (byte b : buffer) {
                                                    logReceived += String.format("%02X", b) + " ";
                                                }
                                                Log.d(TAG, logReceived);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public void init(String fw_logging_file_path)
    {
        mFwLogsParsingFilePath = fw_logging_file_path;
        mIsParsingFileInitialized = true;
    }


    public void stopLogging()
    {
        mAreFwLogsRequested = false;
    }

    public void getFwLogsFromFlash()
    {
        if(!mAllFlashLogsAlreadyPulled){
            try(RsContext ctx = new RsContext()){
                try(DeviceList devices = ctx.queryDevices()) {
                    if (devices.getDeviceCount() > 0) {
                        try (Device device = devices.createDevice(0)) {
                            if(device != null) {
                                try(final FwLogger fwLoggerDevice = device.as(Extension.FW_LOGGER)){
                                    mFwLoggerDevice = fwLoggerDevice;
                                    if (mIsParsingFileInitialized)
                                        mFwLoggerDevice.initParser(mFwLogsParsingFilePath);
                                    Log.d(TAG, "-------------------flash logs retrieval--------------------");
                                    while (true) {
                                        try{
                                            String logReceived = "";
                                            try (FwLogMsg logMsg = mFwLoggerDevice.getFwLogsFromFlash()) {
                                                if (logMsg.getSize() > 0) {
                                                    if (mIsParsingFileInitialized) {
                                                        try (FwLogParsedMsg parsedMsg = mFwLoggerDevice.parseFwLog(logMsg)) {
                                                            logReceived = parsedMsg.getTimestamp() + " - " +
                                                                    parsedMsg.getSeverity() + " " +
                                                                    parsedMsg.getFileName() + " " +
                                                                    parsedMsg.getLine() + " " +
                                                                    parsedMsg.getThreadName() + " " +
                                                                    parsedMsg.getMessage();
                                                            Log.d(TAG, logReceived);
                                                        }
                                                    } else {
                                                        logReceived = logMsg.getTimestamp() + " " +
                                                                logMsg.getSeverityStr() + " ";
                                                        byte[] buffer = new byte[512];
                                                        buffer = logMsg.getData(buffer);
                                                        for (byte b : buffer) {
                                                            logReceived += String.format("%02X", b) + " ";
                                                        }
                                                        Log.d(TAG, logReceived);
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Log.d(TAG, "Interrupted: " + e.getMessage());
                                            mAllFlashLogsAlreadyPulled = true;
                                            break;
                                        }
                                    }
                                    Log.d(TAG, "-------------------flash logs finished--------------------");
                                }
                            }
                        }
                    }
                }
            }
        }
        else {
            Log.d(TAG, "Flash logs already pulled");
        }
    }
}

@@ -4149,32 +4149,150 @@
 eCount() %3E 0) %7B%0A
+                        // only device 0 is taken care of, as the camera app is supposed to work only with one device%0A
                 

comment has been added in FwLogsThread for unique device for flash logs